-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core/room: add channel manager, fix bug where channel options not app…
…lied This change fixes the bug described in #411 whereby channel options are overwritten by successive uses of the same channel, rather than merged together. In doing this, we also fix the bug in relation to newly introduce spec point CHA-RC3. We were previously depending on the soft-deprecated behaviour in RTS3c which would allow you to update channel options via setOptions. The change is achieved by adding a channel manager, whose job it is to register all the options for a given channel prior to the first channels.get() call, and call channels.get with the amalgamated options. Closes #411
- Loading branch information
Showing
10 changed files
with
306 additions
and
63 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,46 @@ | ||
import * as Ably from 'ably'; | ||
|
||
import { Logger } from './logger.js'; | ||
import { DEFAULT_CHANNEL_OPTIONS } from './version.js'; | ||
|
||
export type ChannelOptionsMerger = (options: Ably.ChannelOptions) => Ably.ChannelOptions; | ||
|
||
export class ChannelManager { | ||
private readonly _realtime: Ably.Realtime; | ||
private readonly _logger: Logger; | ||
private readonly _registeredOptions = new Map<string, Ably.ChannelOptions>(); | ||
private readonly _requestedChannels = new Set<string>(); | ||
|
||
constructor(realtime: Ably.Realtime, logger: Logger) { | ||
logger.trace('ChannelManager();'); | ||
this._realtime = realtime; | ||
this._logger = logger; | ||
} | ||
|
||
mergeOptions(channelName: string, merger: ChannelOptionsMerger): void { | ||
this._logger.trace('ChannelManager.registerOptions();', { channelName }); | ||
if (this._requestedChannels.has(channelName)) { | ||
this._logger.error('channel options cannot be modified after the channel has been requested', { channelName }); | ||
throw new Ably.ErrorInfo('channel options cannot be modified after the channel has been requested', 40000, 400); | ||
} | ||
|
||
const currentOpts = this._registeredOptions.get(channelName) ?? DEFAULT_CHANNEL_OPTIONS; | ||
this._registeredOptions.set(channelName, merger(currentOpts)); | ||
} | ||
|
||
get(channelName: string): Ably.RealtimeChannel { | ||
this._logger.trace('ChannelManager.get();', { channelName }); | ||
this._requestedChannels.add(channelName); | ||
return this._realtime.channels.get( | ||
channelName, | ||
this._registeredOptions.get(channelName) ?? DEFAULT_CHANNEL_OPTIONS, | ||
); | ||
} | ||
|
||
release(channelName: string): void { | ||
this._logger.trace('ChannelManager.release();', { channelName }); | ||
this._requestedChannels.delete(channelName); | ||
this._registeredOptions.delete(channelName); | ||
this._realtime.channels.release(channelName); | ||
} | ||
} |
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
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.