Skip to content

Commit

Permalink
Add channel registration and unregistration methods to Cable class
Browse files Browse the repository at this point in the history
- Implemented `registerChannels` method to register channels for a given context.
- Implemented `unregisterChannels` method to unregister channels, handling computed channels appropriately.
- Updated connection handling to return the instance of the Cable class after connection setup.
  • Loading branch information
mclintprojects committed Dec 14, 2024
1 parent 2065661 commit 3154ef3
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/cable.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export default class Cable {
if (connectImmediately) this._connect(this._connectionUrl);

this._attachConnectionObject();

return this;
}

/**
Expand Down Expand Up @@ -132,6 +134,37 @@ export default class Cable {
}
}

/**
* Registers channels for a given context.
* @param {Object} channels - An object containing the channel names and their configurations
* @param {Object} context - The context (typically a Vue component instance) for which the channels are being registered
*/
registerChannels(channels, context) {
Object.entries(channels).forEach((channelEntry) => {
this._registerChannel(channelEntry, context);
});
}

/**
* Unregisters channels for a given context.
* @param {Object} channels - An object containing the channel names and their configurations
* @param {Object} context - The context (typically a Vue component instance) for which the channels are being unregistered
*/
unregisterChannels(channels, context) {
Object.entries(channels).forEach((channelEntry) => {
const [channelName, channelValue] = channelEntry;

if (channelName === "computed") {
channelValue.forEach((channel) => {
const computedChannelName = channel.channelName.call(context);
this._removeChannel(computedChannelName, context._uid);
});
} else {
this._removeChannel(channelName, context._uid);
}
});
}

/**
* Called when a subscription to an Action Cable server channel successfully completes. Calls connected on the component channel
* @param {Object} channel - The component channel
Expand Down

0 comments on commit 3154ef3

Please sign in to comment.