diff --git a/src/Model.test.ts b/src/Model.test.ts index b9acdc7a..ca5e2f96 100644 --- a/src/Model.test.ts +++ b/src/Model.test.ts @@ -32,7 +32,7 @@ vi.mock('./stream/StreamFactory', () => { subscribe(): void {} unsubscribe(): void {} async dispose() {} - async reset() {} + async sync() {} } const streams: { [key: string]: IStream } = {}; @@ -128,7 +128,6 @@ describe('Model', () => { await statePromise(model, 'preparing'); completeSync(); - await ready; await statePromise(model, 'ready'); await expect(ready).resolves.toEqual({ current: 'ready', previous: 'preparing', reason: undefined }); @@ -297,11 +296,11 @@ describe('Model', () => { model.$register({ $sync: sync, }); - expect(() => + await expect( model.$register({ $sync: sync, }), - ).toThrow('$register was already called'); + ).rejects.toThrow('$register was already called'); }); it('fails to register after initialization', async ({ channelName, ably, logger, streams }) => { @@ -323,7 +322,7 @@ describe('Model', () => { model.setState('ready'); - expect(() => model.$register({ $sync: sync })).toThrow( + await expect(model.$register({ $sync: sync })).rejects.toThrow( `$register can only be called when the model is in the initialized state`, ); }); diff --git a/src/Model.ts b/src/Model.ts index 79fca61d..46b97ff0 100644 --- a/src/Model.ts +++ b/src/Model.ts @@ -128,11 +128,9 @@ export default class Model extends EventEmitter { - this.init(); - return new Promise((resolve) => this.whenState('ready', this.state, resolve)); - }; + public async $sync() { + await this.init(); + return new Promise((resolve) => this.whenState('ready', this.state, resolve)); } /** @@ -183,7 +181,7 @@ export default class Model extends EventEmitter} registration - The set of methods to register. * @returns A promise that resolves when the model has completed the registrtion and is ready to start emitting updates. */ - public $register(registration: Registration) { + public async $register(registration: Registration) { if (this.wasRegistered) { throw new Error('$register was already called'); } @@ -199,8 +197,9 @@ export default class Model extends EventEmitter this.whenState('ready', this.state, resolve)); + const result = new Promise((resolve) => this.whenState('ready', this.state, resolve)); + await this.init(); + return result; } /** @@ -296,7 +295,7 @@ export default class Model extends EventEmitter extends EventEmitter = async (err: Error | null, event?: AblyTypes.Message) => { try { if (err) { @@ -330,9 +328,10 @@ export default class Model extends EventEmitter