Skip to content

Commit

Permalink
Doc strings and expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
zknill committed Oct 16, 2023
1 parent 51c7870 commit c9a56b1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/Model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ let simpleTestData: TestData = {

interface ModelTestContext extends ModelOptions {
streams: IStreamFactory;
channelName: string;
}

const modelStatePromise = <T, M extends MutationMethods>(model: Model<T, M>, state: ModelState) =>
Expand Down Expand Up @@ -142,8 +141,11 @@ describe('Model', () => {
const ready = model.$register({ $sync: sync });
await modelStatePromise(model, 'preparing');
completeSync();

await ready;
await modelStatePromise(model, 'ready');
await expect(ready).resolves.toEqual({ current: 'ready', previous: 'preparing', reason: undefined });

expect(sync).toHaveBeenCalledOnce();
expect(model.optimistic).toEqual(simpleTestData);
expect(model.confirmed).toEqual(simpleTestData);
Expand All @@ -155,10 +157,10 @@ describe('Model', () => {
completeSync = resolve;
});

const resyned = model.$sync();
const resynced = model.$sync();
await modelStatePromise(model, 'preparing');
completeSync();
await resyned;
await resynced;
await modelStatePromise(model, 'ready');
expect(sync).toHaveBeenCalledTimes(2);

Expand Down
8 changes: 4 additions & 4 deletions src/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class Model<T, M extends MutationMethods> extends EventEmitter<Re
private optimisticData!: T;
private confirmedData!: T;

private _sync: SyncFunc<T> = async () => {
private syncFunc: SyncFunc<T> = async () => {
throw new Error('sync func not registered');
};
private merge?: MergeFunc<T>;
Expand Down Expand Up @@ -119,7 +119,7 @@ export default class Model<T, M extends MutationMethods> extends EventEmitter<Re

/**
* The sync function that allows the model to be manually resynced
* @returns A promise that resolves when the model has completed the registrtion and is ready to start emitting updates.
* @returns A promise that resolves when the model has successfully re-synchronised its state and is ready to start emitting updates.
*/
public get $sync() {
return () => {
Expand Down Expand Up @@ -186,7 +186,7 @@ export default class Model<T, M extends MutationMethods> extends EventEmitter<Re
);
}
this.wasRegistered = true;
this._sync = registration.$sync;
this.syncFunc = registration.$sync;

if (registration.$merge) {
this.merge = registration.$merge;
Expand Down Expand Up @@ -300,7 +300,7 @@ export default class Model<T, M extends MutationMethods> extends EventEmitter<Re
this.removeStream();
this.addStream(this.stream.channel);

const data = await this._sync();
const data = await this.syncFunc();
this.setOptimisticData(data);
this.setConfirmedData(data);
this.setState('ready');
Expand Down

0 comments on commit c9a56b1

Please sign in to comment.