From d91ba00cb7478f349e1cb767c39b0a0f7943ef2c Mon Sep 17 00:00:00 2001 From: Yiyi Sun Date: Sat, 30 Sep 2023 11:37:35 -0400 Subject: [PATCH] rename app.query to app.runAsync --- apprun.d.ts | 6 ++++-- src/app.ts | 6 +++++- src/component.ts | 11 ++++++++--- tests/app.spec.ts | 12 ++++++------ 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/apprun.d.ts b/apprun.d.ts index c105cd4..c32af15 100644 --- a/apprun.d.ts +++ b/apprun.d.ts @@ -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; + query(name: string, ...args): Promise; // obsolete + runAsync(name: string, ...args): Promise; h(tag: string | Function, ...children: any[]): VNode | VNode[]; createElement(tag: string | Function, ...children: any[]): VNode | VNode[]; render(element: Element | string, node: VDOM): void; @@ -67,7 +68,8 @@ declare module 'apprun' { start(element?: Element | string, options?: MountOptions): Component; on(name: E, fn: (...args: any[]) => void, options?: any): void; run(name: E, ...args: any[]): number; - query(name: string, ...args): Promise; + query(name: string, ...args): Promise; // obsolete + runAsync(name: string, ...args): Promise; rendered: (state: T) => void; mounted: (props: any, children: any[], state: T) => T | void; unmount: () => void; diff --git a/src/app.ts b/src/app.ts index 8b9d8be..18e67e7 100644 --- a/src/app.ts +++ b/src/app.ts @@ -58,7 +58,7 @@ export class App { }, options.delay); } - query(name: string, ...args): Promise { + runAsync(name: string, ...args): Promise { const subscribers = this.getSubscribers(name, this._events); console.assert(subscribers && subscribers.length > 0, 'No subscriber for event: ' + name); const promises = subscribers.map(sub => { @@ -68,6 +68,10 @@ export class App { return Promise.all(promises); } + query(name: string, ...args): Promise { + return this.query(name, ...args); + } + private getSubscribers(name: string, events) { const subscribers = events[name] || []; diff --git a/src/component.ts b/src/component.ts index 54a75c8..93c2529 100644 --- a/src/component.ts +++ b/src/component.ts @@ -261,11 +261,16 @@ export class Component { 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() { diff --git a/tests/app.spec.ts b/tests/app.spec.ts index 12aa195..2407b7b 100644 --- a/tests/app.spec.ts +++ b/tests/app.spec.ts @@ -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(); @@ -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); @@ -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(); }); @@ -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(); }); @@ -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(); });