Skip to content

Commit 498d26d

Browse files
authored
Merge pull request #2190 from ably/release-warning
Improve docstring for channels#release and add log
2 parents c90f1c3 + 861bdc7 commit 498d26d

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

ably.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2718,7 +2718,7 @@ export declare interface Channels<T> {
27182718
*/
27192719
getDerived(name: string, deriveOptions: DeriveOptions, channelOptions?: ChannelOptions): T;
27202720
/**
2721-
* Releases a {@link Channel} or {@link RealtimeChannel} object, deleting it, and enabling it to be garbage collected. To release a channel, the {@link ChannelState} must be `INITIALIZED`, `DETACHED`, or `FAILED`.
2721+
* Releases all SDK-held references to a {@link Channel} or {@link RealtimeChannel} object, enabling it to be garbage collected. Warning: this method has no guardrails; using a channel reference after it has been released is undefined behaviour. It can be useful for applications that work with a continually changing set of channels on a single client and need to avoid unbounded memory growth; if this does not describe you, don't call it. Realtime channels not already in the `INITIALIZED`, `DETACHED`, or `FAILED` state are detached before release.
27222722
*
27232723
* @param name - The channel name.
27242724
*/

src/common/lib/client/baserealtime.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,29 @@ class Channels extends EventEmitter {
223223
* Please do not use this unless you know what you're doing */
224224
release(name: string) {
225225
name = String(name);
226+
Logger.logAction(this.logger, Logger.LOG_MAJOR, 'Channels.release()', 'Releasing references to channel ' + name);
226227
const channel = this.all[name];
227228
if (!channel) {
228229
return;
229230
}
230-
const releaseErr = channel.getReleaseErr();
231-
if (releaseErr) {
232-
throw releaseErr;
231+
const s = channel.state;
232+
if (s === 'initialized' || s === 'detached' || s === 'failed') {
233+
delete this.all[name];
234+
return;
233235
}
234-
delete this.all[name];
236+
channel
237+
.detach()
238+
.catch((err) => {
239+
Logger.logAction(
240+
this.logger,
241+
Logger.LOG_ERROR,
242+
'Channels.release()',
243+
'Error detaching channel ' + name + ' prior to release: ' + Utils.inspectError(err),
244+
);
245+
})
246+
.then(() => {
247+
delete this.all[name];
248+
});
235249
}
236250
}
237251

0 commit comments

Comments
 (0)