Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace update functions with a merge function #51

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions src/Errors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import toString from 'lodash/toString';

import type { UpdateTargets } from './UpdatesRegistry.js';

export function toError(err: any) {
return err instanceof Error || err instanceof AggregateError ? err : new Error(toString(err));
}
Expand All @@ -19,25 +17,3 @@ export class RegistrationError extends Error {
this.name = 'RegistrationError';
}
}

/**
* Represents an error that occurs when a given update function specified by
* the target is not registered on the model.
*
* @internal
* @extends {RegistrationError}
*/
export class UpdateRegistrationError extends RegistrationError {
constructor(targets: Partial<UpdateTargets>) {
let message = 'update not registered';
if (targets.channel && !targets.event) {
message = `update for channel '${targets.channel}' not registered`;
} else if (!targets.channel && targets.event) {
message = `update for event '${targets.event}' not registered`;
} else if (targets.channel && targets.event) {
message = `update for event '${targets.event}' on channel '${targets.channel}' not registered`;
}
super(message);
this.name = 'UpdateRegistrationError';
}
}
9 changes: 5 additions & 4 deletions src/Model.discontinuity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ describe('Model', () => {
const logger = pino({ level: 'silent' });
context.ably = ably;
context.logger = logger;
context.channelName = 'models:myModel:events';
});
it<ModelTestContext>('handles discontinuity with resync', async ({ ably, logger }) => {
it<ModelTestContext>('handles discontinuity with resync', async ({ channelName, ably, logger }) => {
const channel = ably.channels.get('foo');
let suspendChannel: (...args: any[]) => void = () => {
throw new Error('suspended not defined');
Expand All @@ -58,11 +59,11 @@ describe('Model', () => {
let counter = 0;

const sync = vi.fn(async () => `${counter++}`);
const model = new Model<string, {}>('test', { ably, logger });
const update1 = vi.fn(async (state, event) => {
const model = new Model<string, {}>('test', { ably, channelName, logger });
const mergeFn = vi.fn(async (state, event) => {
return event.data;
});
await model.$register({ $sync: sync, $update: { s1: { testEvent: update1 } } });
await model.$register({ $sync: sync, $merge: mergeFn });

expect(sync).toHaveBeenCalledOnce();

Expand Down
Loading
Loading