From 7ae2eb977358a88c3f6953eac1a87d16844c7c76 Mon Sep 17 00:00:00 2001 From: Mike Christensen Date: Thu, 19 Oct 2023 17:17:54 +0100 Subject: [PATCH] model: use statePromise --- src/Model.discontinuity.test.ts | 3 ++- src/Model.test.ts | 7 ++++--- src/Model.ts | 8 ++++---- src/stream/Middleware.test.ts | 2 +- src/stream/Stream.test.ts | 2 +- src/utilities/promises.ts | 11 +++++++++++ src/utilities/test/promises.ts | 12 ------------ 7 files changed, 23 insertions(+), 22 deletions(-) create mode 100644 src/utilities/promises.ts diff --git a/src/Model.discontinuity.test.ts b/src/Model.discontinuity.test.ts index a8ebdd0f..b563b687 100644 --- a/src/Model.discontinuity.test.ts +++ b/src/Model.discontinuity.test.ts @@ -5,7 +5,8 @@ import { it, describe, expect, vi, beforeEach } from 'vitest'; import Model from './Model.js'; import { ModelOptions } from './types/model.js'; -import { getEventPromises, statePromise } from './utilities/test/promises.js'; +import { statePromise } from './utilities/promises.js'; +import { getEventPromises } from './utilities/test/promises.js'; vi.mock('ably/promises'); diff --git a/src/Model.test.ts b/src/Model.test.ts index 8a7aa09f..e0caaf99 100644 --- a/src/Model.test.ts +++ b/src/Model.test.ts @@ -7,8 +7,9 @@ import Model from './Model.js'; import { StreamOptions, IStream, StreamState } from './stream/Stream.js'; import { IStreamFactory } from './stream/StreamFactory.js'; import type { ModelState, ModelStateChange, ModelOptions } from './types/model.d.ts'; +import { statePromise, timeout } from './utilities/promises.js'; import { createMessage, customMessage } from './utilities/test/messages.js'; -import { getNthEventPromise, getEventPromises, statePromise, timeout } from './utilities/test/promises.js'; +import { getNthEventPromise, getEventPromises } from './utilities/test/promises.js'; vi.mock('ably/promises'); @@ -128,8 +129,8 @@ describe('Model', () => { await statePromise(model, 'preparing'); completeSync(); - await statePromise(model, 'ready'); - await expect(ready).resolves.toEqual({ current: 'ready', previous: 'preparing', reason: undefined }); + const registerResult = await ready; + expect([undefined, { current: 'ready', previous: 'preparing', reason: undefined }]).toContain(registerResult); expect(sync).toHaveBeenCalledOnce(); expect(model.data.optimistic).toEqual(simpleTestData); diff --git a/src/Model.ts b/src/Model.ts index afce5b5b..41bbfdc0 100644 --- a/src/Model.ts +++ b/src/Model.ts @@ -22,6 +22,7 @@ import type { } from './types/model.js'; import type { OptimisticInvocationParams } from './types/optimistic.js'; import EventEmitter from './utilities/EventEmitter.js'; +import { statePromise } from './utilities/promises.js'; /** * A Model encapsulates an observable, collaborative data model backed by a transactional database in your backend. @@ -130,7 +131,7 @@ export default class Model extends EventEmitter this.whenState('ready', this.state, resolve)); + return statePromise(this, 'ready'); } /** @@ -172,7 +173,7 @@ export default class Model extends EventEmitter this.whenState('disposed', this.state, resolve)); + return statePromise(this, 'disposed'); } /** @@ -197,9 +198,8 @@ export default class Model extends EventEmitter this.whenState('ready', this.state, resolve)); await this.init(); - return result; + return statePromise(this, 'ready'); } /** diff --git a/src/stream/Middleware.test.ts b/src/stream/Middleware.test.ts index 706f1b5f..d5be6d71 100644 --- a/src/stream/Middleware.test.ts +++ b/src/stream/Middleware.test.ts @@ -3,8 +3,8 @@ import shuffle from 'lodash/shuffle.js'; import { it, describe, expect, vi } from 'vitest'; import { SlidingWindow, OrderedHistoryResumer, lexicographicOrderer } from './Middleware.js'; +import { timeout } from '../utilities/promises.js'; import { createMessage } from '../utilities/test/messages.js'; -import { timeout } from '../utilities/test/promises.js'; describe('SlidingWindow', () => { it('emits events immediately with no timeout', async () => { diff --git a/src/stream/Stream.test.ts b/src/stream/Stream.test.ts index dd200126..09b71186 100644 --- a/src/stream/Stream.test.ts +++ b/src/stream/Stream.test.ts @@ -4,8 +4,8 @@ import { Subject } from 'rxjs'; import { it, describe, expect, beforeEach, afterEach, vi } from 'vitest'; import Stream, { HISTORY_PAGE_SIZE, StreamOptions, StreamState } from './Stream.js'; +import { statePromise } from '../utilities/promises.js'; import { createMessage } from '../utilities/test/messages.js'; -import { statePromise } from '../utilities/test/promises.js'; vi.mock('ably/promises'); diff --git a/src/utilities/promises.ts b/src/utilities/promises.ts new file mode 100644 index 00000000..b95859a6 --- /dev/null +++ b/src/utilities/promises.ts @@ -0,0 +1,11 @@ +import { EventListener } from './EventEmitter.js'; + +interface StateListener { + state: S; + whenState(targetState: S, currentState: S, listener: EventListener, ...listenerArgs: unknown[]): void; +} + +export const statePromise = (object: StateListener, state: S) => + new Promise((resolve) => object.whenState(state, object.state, resolve)); + +export const timeout = (ms: number = 0) => new Promise((resolve) => setTimeout(resolve, ms)); diff --git a/src/utilities/test/promises.ts b/src/utilities/test/promises.ts index 98d33012..9b24e097 100644 --- a/src/utilities/test/promises.ts +++ b/src/utilities/test/promises.ts @@ -1,7 +1,5 @@ import { Subject, lastValueFrom, take } from 'rxjs'; -import { EventListener } from '../EventEmitter.js'; - export const getNthEventPromise = (subject: Subject, n: number) => lastValueFrom(subject.pipe(take(n))); export const getEventPromises = (subject: Subject, n: number) => { @@ -11,13 +9,3 @@ export const getEventPromises = (subject: Subject, n: number) => { } return promises; }; - -interface StateListener { - state: S; - whenState(targetState: S, currentState: S, listener: EventListener, ...listenerArgs: unknown[]): void; -} - -export const statePromise = (object: StateListener, state: S) => - new Promise((resolve) => object.whenState(state, object.state, resolve)); - -export const timeout = (ms: number = 0) => new Promise((resolve) => setTimeout(resolve, ms));