Skip to content

Commit

Permalink
rename app.query to app.runAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
yysun committed Sep 30, 2023
1 parent 16f2a7a commit d91ba00
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
6 changes: 4 additions & 2 deletions apprun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ declare module 'apprun' {
off(name: string, fn: (...args: any[]) => void): void;
find(name: string): any;
run(name: string, ...args: any[]): number;
query(name: string, ...args): Promise<any[]>;
query(name: string, ...args): Promise<any[]>; // obsolete
runAsync(name: string, ...args): Promise<any[]>;
h(tag: string | Function, ...children: any[]): VNode | VNode[];
createElement(tag: string | Function, ...children: any[]): VNode | VNode[];
render(element: Element | string, node: VDOM): void;
Expand All @@ -67,7 +68,8 @@ declare module 'apprun' {
start(element?: Element | string, options?: MountOptions): Component<T, E>;
on(name: E, fn: (...args: any[]) => void, options?: any): void;
run(name: E, ...args: any[]): number;
query(name: string, ...args): Promise<any[]>;
query(name: string, ...args): Promise<any[]>; // obsolete
runAsync(name: string, ...args): Promise<any[]>;
rendered: (state: T) => void;
mounted: (props: any, children: any[], state: T) => T | void;
unmount: () => void;
Expand Down
6 changes: 5 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class App {
}, options.delay);
}

query(name: string, ...args): Promise<any[]> {
runAsync(name: string, ...args): Promise<any[]> {
const subscribers = this.getSubscribers(name, this._events);
console.assert(subscribers && subscribers.length > 0, 'No subscriber for event: ' + name);
const promises = subscribers.map(sub => {
Expand All @@ -68,6 +68,10 @@ export class App {
return Promise.all(promises);
}

query(name: string, ...args): Promise<any[]> {
return this.query(name, ...args);
}

private getSubscribers(name: string, events) {
const subscribers = events[name] || [];

Expand Down
11 changes: 8 additions & 3 deletions src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,16 @@ export class Component<T = any, E = any> {
this._app.on(name, fn, options);
}

public query(event: E, ...args) {
public runAsync(event: E, ...args) {
const name = event.toString();
return this.is_global_event(name) ?
app.query(name, ...args) :
this._app.query(name, ...args);
app.runAsync(name, ...args) :
this._app.runAsync(name, ...args);
}

// obsolete
public query(event: E, ...args) {
return this.runAsync(event, ...args);
}

public unmount() {
Expand Down
12 changes: 6 additions & 6 deletions tests/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ describe('app events', () => {
expect(has).toBeUndefined();
});

it('.query should return promise', (done) => {
it('.runAsync should return promise', (done) => {
const app = new App();
app.on('0', _ => 10);
app.on('0', n => n * 10);

app.query('0', 5).then(a => {
app.runAsync('0', 5).then(a => {
expect(a[0]).toBe(10);
expect(a[1]).toBe(50);
done();
Expand All @@ -204,7 +204,7 @@ describe('app events', () => {
app.on('12*', _ => 20);
app.on('12', _ => 30);

app.query('12').then(a => {
app.runAsync('12').then(a => {
expect(a[0]).toBe(30);
expect(a[1]).toBe(20);
expect(a[2]).toBe(10);
Expand All @@ -216,7 +216,7 @@ describe('app events', () => {
it('should pass event options as context', (done) => {
const app = new App();
app.on('0', (m, { n }) => n * m, { n: 10 });
app.query('0', 5).then(a => {
app.runAsync('0', 5).then(a => {
expect(a[0]).toBe(50);
done();
});
Expand All @@ -225,7 +225,7 @@ describe('app events', () => {
it('should pass event name in the context', (done) => {
const app = new App();
app.on('0*', (_, { event }) => event);
app.query('000', 5).then(a => {
app.runAsync('000', 5).then(a => {
expect(a[0]).toBe('000');
done();
});
Expand All @@ -234,7 +234,7 @@ describe('app events', () => {
it('should not create extra handlers', (done) => {
const app = new App();
app.on('0*', (_, { event }) => event);
app.query('000', 5).then(a => {
app.runAsync('000', 5).then(a => {
expect(a[0]).toBe('000');
done();
});
Expand Down

0 comments on commit d91ba00

Please sign in to comment.