-
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.
stream: add simple stream implementation
This initial Stream implementation adds a very simple wrapper around an Ably channel. Messages received on the channel are surfaced to the caller via a subscription callback. The Stream is an EventEmitter and exposes its state via events. Additionally the stream can be pauses, resumed and disposed. This will allow the Models SDK to manipulate the stream/channel according to the model's lifecycle. While not yet particularly useful, the idea is that the Stream will handle: - message de-duplication - message re-ordering - detect discontinuities (due to connection state recovery as well as due to failed connector publishes)
- Loading branch information
1 parent
a90688f
commit fc940a4
Showing
18 changed files
with
619 additions
and
195 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 |
---|---|---|
@@ -1,69 +1,33 @@ | ||
import { Types } from 'ably/promises'; | ||
|
||
const MOCK_CLIENT_ID = 'MOCK_CLIENT_ID'; | ||
const mockPromiseErrorNotImplemented = <T>(name: string): Promise<T> => new Promise((_, reject) => reject(new Error(`mock '${name}' not implemented`))); | ||
const mockNotImplemented = () => { throw new Error('not implemented') }; | ||
|
||
const mockPromisify = <T>(expectedReturnValue): Promise<T> => new Promise((resolve) => resolve(expectedReturnValue)); | ||
const methodReturningVoidPromise = () => mockPromisify<void>((() => {})()); | ||
type MockChannel = Partial<Types.RealtimeChannelPromise>; | ||
|
||
const mockPresence = { | ||
get: () => mockPromisify<Types.PresenceMessage[]>([]), | ||
update: () => mockPromisify<void>(undefined), | ||
enter: methodReturningVoidPromise, | ||
leave: methodReturningVoidPromise, | ||
subscriptions: { | ||
once: async (_, fn) => { | ||
return await fn(); | ||
}, | ||
}, | ||
subscribe: () => {}, | ||
}; | ||
const mockChannel: MockChannel = { | ||
on: mockNotImplemented, | ||
attach: () => mockPromiseErrorNotImplemented<void>('attach'), | ||
detach: () => mockPromiseErrorNotImplemented<void>('detach'), | ||
subscribe: () => mockPromiseErrorNotImplemented<void>('subscribe'), | ||
} | ||
|
||
const mockHistory = { | ||
items: [], | ||
first: () => mockPromisify(mockHistory), | ||
next: () => mockPromisify(mockHistory), | ||
current: () => mockPromisify(mockHistory), | ||
hasNext: () => false, | ||
isLast: () => true, | ||
}; | ||
type MockChannels = Partial<Types.Channels<MockChannel>>; | ||
|
||
const mockEmitter = { | ||
any: [], | ||
events: {}, | ||
anyOnce: [], | ||
eventsOnce: {}, | ||
}; | ||
const mockChannels: MockChannels = { | ||
get: () => mockChannel, | ||
release: () => {}, | ||
} | ||
|
||
const mockChannel = { | ||
presence: mockPresence, | ||
history: () => mockHistory, | ||
subscribe: () => {}, | ||
publish: () => {}, | ||
subscriptions: mockEmitter, | ||
}; | ||
type MockConnection = Partial<Types.ConnectionPromise>; | ||
|
||
class MockRealtime { | ||
public channels: { | ||
get: () => typeof mockChannel; | ||
}; | ||
public auth: { | ||
clientId: string; | ||
}; | ||
public connection: { | ||
id?: string; | ||
}; | ||
const mockConnection: MockConnection = { | ||
whenState: () => mockPromiseErrorNotImplemented<Types.ConnectionStateChange>('whenState') | ||
} | ||
|
||
constructor() { | ||
this.channels = { | ||
get: () => mockChannel, | ||
}; | ||
this.auth = { | ||
clientId: MOCK_CLIENT_ID, | ||
}; | ||
this.connection = { | ||
id: '1', | ||
}; | ||
} | ||
class MockRealtime { | ||
public channels = mockChannels; | ||
public connection = mockConnection; | ||
} | ||
|
||
export { MockRealtime as Realtime }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -49,6 +49,7 @@ | |
"vitest": "^0.29.8" | ||
}, | ||
"dependencies": { | ||
"ably": "^1.2.39" | ||
"ably": "^1.2.39", | ||
"rxjs": "^7.8.1" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.