-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
models: add Model and EventStream classes
- Loading branch information
1 parent
4a7afc1
commit a90688f
Showing
8 changed files
with
226 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { it, describe, expect, expectTypeOf, beforeEach, afterEach } from 'vitest'; | ||
import { Realtime, Types } from 'ably/promises'; | ||
import { WebSocket } from 'mock-socket'; | ||
|
||
import EventStream from './EventStream'; | ||
|
||
import Server from './utilities/test/mock-server.js'; | ||
import defaultClientConfig from './utilities/test/default-client-config.js'; | ||
|
||
interface EventStreamTestContext { | ||
client: Types.RealtimePromise; | ||
server: Server; | ||
} | ||
|
||
describe('EventStream', () => { | ||
beforeEach<EventStreamTestContext>((context) => { | ||
(Realtime as any).Platform.Config.WebSocket = WebSocket; | ||
context.server = new Server('wss://realtime.ably.io/'); | ||
context.client = new Realtime(defaultClientConfig); | ||
}); | ||
|
||
afterEach<EventStreamTestContext>((context) => { | ||
context.server.stop(); | ||
}); | ||
|
||
it<EventStreamTestContext>('connects successfully with the Ably Client', async ({ client, server }) => { | ||
server.start(); | ||
const connectSuccess = await client.connection.whenState('connected'); | ||
expect(connectSuccess.current).toBe('connected'); | ||
}); | ||
|
||
it<EventStreamTestContext>('expects the injected client to be of the type RealtimePromise', ({ client }) => { | ||
const eventStream = new EventStream('test', client, { channel: 'foobar' }); | ||
expect(eventStream.name).toEqual('test'); | ||
expectTypeOf(eventStream.client).toMatchTypeOf<Types.RealtimePromise>(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Types } from 'ably'; | ||
import EventStreamOptions from './options/EventStreamOptions'; | ||
import EventEmitter from './utilities/EventEmitter'; | ||
|
||
const EVENT_STREAM_OPTIONS_DEFAULTS = {}; | ||
|
||
enum EventStreamState { | ||
/** | ||
* The event stream has been initialized but no attach has yet been attempted. | ||
*/ | ||
INITIALIZED = 'initialized', | ||
/** | ||
* This state is entered if the event stream encounters a failure condition that it cannot recover from. | ||
*/ | ||
FAILED = 'failed', | ||
} | ||
|
||
class EventStream<T> extends EventEmitter<any> { | ||
private options: EventStreamOptions; | ||
private connectionId?: string; | ||
private state: EventStreamState = EventStreamState.INITIALIZED; | ||
private data: T; | ||
|
||
constructor(readonly name: string, readonly client: Types.RealtimePromise, options: EventStreamOptions) { | ||
super(); | ||
this.options = { ...EVENT_STREAM_OPTIONS_DEFAULTS, ...options }; | ||
this.connectionId = this.client.connection.id; | ||
} | ||
} | ||
|
||
export default EventStream; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { it, describe, expect, expectTypeOf, beforeEach, afterEach } from 'vitest'; | ||
import { Realtime, Types } from 'ably/promises'; | ||
import { WebSocket } from 'mock-socket'; | ||
|
||
import Model from './Model'; | ||
import EventStream from './EventStream'; | ||
|
||
import Server from './utilities/test/mock-server.js'; | ||
import defaultClientConfig from './utilities/test/default-client-config.js'; | ||
|
||
interface ModelTestContext { | ||
client: Types.RealtimePromise; | ||
server: Server; | ||
} | ||
|
||
describe('Model', () => { | ||
beforeEach<ModelTestContext>((context) => { | ||
(Realtime as any).Platform.Config.WebSocket = WebSocket; | ||
context.server = new Server('wss://realtime.ably.io/'); | ||
context.client = new Realtime(defaultClientConfig); | ||
}); | ||
|
||
afterEach<ModelTestContext>((context) => { | ||
context.server.stop(); | ||
}); | ||
|
||
it<ModelTestContext>('connects successfully with the Ably Client', async ({ client, server }) => { | ||
server.start(); | ||
const connectSuccess = await client.connection.whenState('connected'); | ||
expect(connectSuccess.current).toBe('connected'); | ||
}); | ||
|
||
it<ModelTestContext>('expects the injected client to be of the type RealtimePromise', ({ client }) => { | ||
const model = new Model('test', client); | ||
expect(model.name).toEqual('test'); | ||
expectTypeOf(model.client).toMatchTypeOf<Types.RealtimePromise>(); | ||
}); | ||
|
||
it<ModelTestContext>('expects model to be instantiated with the provided event streams', ({ client }) => { | ||
const model = new Model('test', client, { | ||
streams: [new EventStream('s1', client, { channel: 's1' }), new EventStream('s2', client, { channel: 's2' })], | ||
}); | ||
expect(model.name).toEqual('test'); | ||
expect(model.stream('s1')).toBeTruthy(); | ||
expect(model.stream('s1').name).toEqual('s1'); | ||
expect(model.stream('s2')).toBeTruthy(); | ||
expect(model.stream('s2').name).toEqual('s2'); | ||
expect(() => model.stream('s3')).toThrowError("stream with name 's3' not registered on model 'test'"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type EventStreamOptions = { | ||
channel: string; | ||
}; | ||
|
||
export default EventStreamOptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
type ModelOptions = {}; | ||
import EventStream from '../EventStream'; | ||
|
||
type ModelOptions = { | ||
streams: Array<EventStream<any>>; | ||
}; | ||
|
||
export default ModelOptions; |