Skip to content

Commit

Permalink
model: use statePromise
Browse files Browse the repository at this point in the history
  • Loading branch information
mschristensen committed Oct 19, 2023
1 parent 819876c commit 7ae2eb9
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/Model.discontinuity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
7 changes: 4 additions & 3 deletions src/Model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -130,7 +131,7 @@ export default class Model<T> extends EventEmitter<Record<ModelState, ModelState
*/
public async $sync() {
await this.init();
return new Promise((resolve) => this.whenState('ready', this.state, resolve));
return statePromise(this, 'ready');
}

/**
Expand Down Expand Up @@ -172,7 +173,7 @@ export default class Model<T> extends EventEmitter<Record<ModelState, ModelState
await this.pendingConfirmationRegistry.finalise(reason);
this.subscriptionMap = new WeakMap();
this.streamSubscriptionsMap = new WeakMap();
return new Promise((resolve) => this.whenState('disposed', this.state, resolve));
return statePromise(this, 'disposed');
}

/**
Expand All @@ -197,9 +198,8 @@ export default class Model<T> extends EventEmitter<Record<ModelState, ModelState
this.merge = registration.$merge;
}

const result = new Promise((resolve) => this.whenState('ready', this.state, resolve));
await this.init();
return result;
return statePromise(this, 'ready');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/stream/Middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion src/stream/Stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
11 changes: 11 additions & 0 deletions src/utilities/promises.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { EventListener } from './EventEmitter.js';

interface StateListener<T, S> {
state: S;
whenState(targetState: S, currentState: S, listener: EventListener<T>, ...listenerArgs: unknown[]): void;
}

export const statePromise = <T, S>(object: StateListener<T, S>, state: S) =>
new Promise((resolve) => object.whenState(state, object.state, resolve));

export const timeout = (ms: number = 0) => new Promise((resolve) => setTimeout(resolve, ms));
12 changes: 0 additions & 12 deletions src/utilities/test/promises.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Subject, lastValueFrom, take } from 'rxjs';

import { EventListener } from '../EventEmitter.js';

export const getNthEventPromise = <T>(subject: Subject<T>, n: number) => lastValueFrom(subject.pipe(take(n)));

export const getEventPromises = <T>(subject: Subject<T>, n: number) => {
Expand All @@ -11,13 +9,3 @@ export const getEventPromises = <T>(subject: Subject<T>, n: number) => {
}
return promises;
};

interface StateListener<T, S> {
state: S;
whenState(targetState: S, currentState: S, listener: EventListener<T>, ...listenerArgs: unknown[]): void;
}

export const statePromise = <T, S>(object: StateListener<T, S>, state: S) =>
new Promise((resolve) => object.whenState(state, object.state, resolve));

export const timeout = (ms: number = 0) => new Promise((resolve) => setTimeout(resolve, ms));

0 comments on commit 7ae2eb9

Please sign in to comment.