From 99e69148de77d07380228bcb057b50222c7ad941 Mon Sep 17 00:00:00 2001 From: zak Date: Fri, 13 Oct 2023 12:19:42 +0100 Subject: [PATCH] Rename StreamRegistry to StreamFactory The StreamFactory is now only responsible for creating streams given the channel name and the set of default options. It doesn't hold on to the references to stream after creating them. --- src/Model.test.ts | 18 +++++++++--------- src/Model.ts | 8 +++----- ...mRegistry.test.ts => StreamFactory.test.ts} | 9 ++++----- src/{StreamRegistry.ts => StreamFactory.ts} | 11 +++++------ 4 files changed, 21 insertions(+), 25 deletions(-) rename src/{StreamRegistry.test.ts => StreamFactory.test.ts} (96%) rename src/{StreamRegistry.ts => StreamFactory.ts} (70%) diff --git a/src/Model.test.ts b/src/Model.test.ts index 53ff4547..5c877d3a 100644 --- a/src/Model.test.ts +++ b/src/Model.test.ts @@ -6,20 +6,20 @@ import { it, describe, expect, afterEach, vi, beforeEach } from 'vitest'; import Model from './Model.js'; import { StreamOptions, IStream, StreamState } from './Stream.js'; -import { IStreamRegistry } from './StreamRegistry.js'; +import { IStreamFactory } from './StreamFactory.js'; import type { ModelState, ModelStateChange, ModelOptions, Event } from './types/model.d.ts'; import type { MutationMethods, EventComparator, MutationContext } from './types/mutations.d.ts'; import { createMessage, customMessage } from './utilities/test/messages.js'; vi.mock('ably/promises'); -// Mocks the StreamRegistry import so that we can modify the Stream instances +// Mocks the StreamFactory import so that we can modify the Stream instances // used by the model to spy on their methods. -// This implementation ensures that all instances of StreamRegistry use the -// same cache of Stream instances so that the StreamRegistry instantiated in the -// model returns the same Stream instances as the StreamRegistry instantiated +// This implementation ensures that all instances of StreamFactory use the +// same cache of Stream instances so that the StreamFactory instantiated in the +// model returns the same Stream instances as the StreamFactory instantiated // in these tests. -vi.mock('./StreamRegistry', () => { +vi.mock('./StreamFactory', () => { class MockStream implements IStream { constructor(readonly options: Pick) {} get state() { @@ -39,7 +39,7 @@ vi.mock('./StreamRegistry', () => { const streams: { [key: string]: IStream } = {}; return { - default: class implements IStreamRegistry { + default: class implements IStreamFactory { newStream(options: Pick) { if (!streams[options.channel]) { streams[options.channel] = new MockStream(options); @@ -65,7 +65,7 @@ const simpleTestData: TestData = { }; interface ModelTestContext extends ModelOptions { - streams: IStreamRegistry; + streams: IStreamFactory; channelName: string; } @@ -88,7 +88,7 @@ describe('Model', () => { const logger = pino({ level: 'silent' }); context.ably = ably; context.logger = logger; - const { default: provider } = await import('./StreamRegistry.js'); + const { default: provider } = await import('./StreamFactory.js'); context.streams = new provider({ ably, logger }); context.channelName = 'models:myModelTest:events'; }); diff --git a/src/Model.ts b/src/Model.ts index 605da4b6..c93a8cfd 100644 --- a/src/Model.ts +++ b/src/Model.ts @@ -6,7 +6,7 @@ import { toError } from './Errors.js'; import MutationsRegistry from './MutationsRegistry.js'; import PendingConfirmationRegistry from './PendingConfirmationRegistry.js'; import { IStream } from './Stream.js'; -import StreamRegistry, { IStreamRegistry } from './StreamRegistry.js'; +import StreamFactory, { IStreamFactory as IStreamFactory } from './StreamFactory.js'; import type { StandardCallback } from './types/callbacks'; import { MergeFunc } from './types/merge.js'; import type { @@ -51,9 +51,7 @@ export default class Model extends EventEmitter; private readonly stream: IStream; - - // TODO: make rename to stream factory - private readonly streamFactory: IStreamRegistry; + private readonly streamFactory: IStreamFactory; private readonly mutationsRegistry: MutationsRegistry; private optimisticEvents: OptimisticEventWithParams[] = []; @@ -76,7 +74,7 @@ export default class Model extends EventEmitter { }); it('succeeds with gte zero event buffer ms', async ({ ably, logger }) => { - new StreamRegistry({ eventBufferOptions: { bufferMs: 0 }, ably, logger }); - new StreamRegistry({ eventBufferOptions: { bufferMs: 1 }, ably, logger }); + new StreamFactory({ eventBufferOptions: { bufferMs: 0 }, ably, logger }); + new StreamFactory({ eventBufferOptions: { bufferMs: 1 }, ably, logger }); }); it('fails with lt zero event buffer ms', async ({ ably, logger }) => { try { - new StreamRegistry({ eventBufferOptions: { bufferMs: -1 }, ably, logger }); + new StreamFactory({ eventBufferOptions: { bufferMs: -1 }, ably, logger }); expect(true).toBe(false); } catch (err) { expect(err.toString(), 'Stream registry should have thrown an error').not.toContain('AssertionError'); } }); - // TODO discontinuity // TODO reauth https://ably.com/docs/realtime/channels?lang=nodejs#fatal-errors }); diff --git a/src/StreamRegistry.ts b/src/StreamFactory.ts similarity index 70% rename from src/StreamRegistry.ts rename to src/StreamFactory.ts index 9b2ee5cb..8352c355 100644 --- a/src/StreamRegistry.ts +++ b/src/StreamFactory.ts @@ -1,15 +1,14 @@ import Stream, { IStream, StreamOptions } from './Stream.js'; -export interface IStreamRegistry { +export interface IStreamFactory { newStream(options: Pick): IStream; - //get streams(): { [key: string]: IStream }; } /** - * The StreamRegistry class encapsulates a set of names stream instances that are + * The StreamFactory class creates Stream instances that are * used to deliver change events to a model. */ -export default class StreamRegistry implements IStreamRegistry { +export default class StreamFactory implements IStreamFactory { /** * @param {Pick} options - The default options used when instantiating a stream. */ @@ -23,9 +22,9 @@ export default class StreamRegistry implements IStreamRegistry { } /** - * Retrieve an existing stream instance for the given channel or create a new one if it doesn't yet exist. + * Create a new Stream instance for the given channel. * @param {Pick} options - The options used in conjunction with the default options when instantiating a stream - * @returns {IStream} The pre-existing or newly created stream instance. + * @returns {IStream} The newly created stream instance. */ // TODO: should this cache the streams? newStream(options: Pick) {