From 852c172b6ab6a7cb120d4f044c5bec516589c81c Mon Sep 17 00:00:00 2001 From: Matthew Fenwick Date: Fri, 1 Oct 2021 13:12:16 -0400 Subject: [PATCH] Apply collection level namespace config to dedicated channels --- lib/mongo/Mutator.js | 10 +++++----- lib/mongo/lib/dispatchers.js | 26 ++++++++++++++------------ lib/redis/RedisSubscriber.js | 2 +- lib/utils/getDedicatedChannel.js | 5 +++-- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/lib/mongo/Mutator.js b/lib/mongo/Mutator.js index 78400e8b..fe16970b 100644 --- a/lib/mongo/Mutator.js +++ b/lib/mongo/Mutator.js @@ -79,7 +79,7 @@ export default class Mutator { } dispatchInsert( - config.optimistic, + config, this._name, config._channels, doc @@ -196,7 +196,7 @@ export default class Mutator { const { fields } = getFields(modifier); dispatchUpdate( - config.optimistic, + config, this._name, config._channels, docs, @@ -259,7 +259,7 @@ export default class Mutator { } dispatchInsert( - config.optimistic, + config, this._name, config._channels, doc @@ -284,7 +284,7 @@ export default class Mutator { docs = this.find(selector).fetch(); dispatchUpdate( - config.optimistic, + config, this._name, config._channels, docs, @@ -358,7 +358,7 @@ export default class Mutator { } dispatchRemove( - config.optimistic, + config, this._name, config._channels, docs diff --git a/lib/mongo/lib/dispatchers.js b/lib/mongo/lib/dispatchers.js index f49b1a72..47aa538b 100644 --- a/lib/mongo/lib/dispatchers.js +++ b/lib/mongo/lib/dispatchers.js @@ -8,15 +8,13 @@ import getDedicatedChannel from '../../utils/getDedicatedChannel'; import Config from '../../config'; import OptimisticInvocation from '../OptimisticInvocation'; -const dispatchEvents = function(optimistic, collectionName, channels, events) { +const dispatchEvents = function(config, collectionName, channels, events) { + const { optimistic } = config; if (optimistic) { OptimisticInvocation.withValue(true, () => { events.forEach(event => { const docId = event[RedisPipe.DOC]._id; - const dedicatedChannel = getDedicatedChannel( - collectionName, - docId - ); + const dedicatedChannel = getDedicatedChannel(collectionName, docId, config); RedisSubscriptionManager.process(dedicatedChannel, event); channels.forEach(channelName => { @@ -37,20 +35,22 @@ const dispatchEvents = function(optimistic, collectionName, channels, events) { channels.forEach(channelName => { client.publish(channelName, message); }); + const docId = event[RedisPipe.DOC]._id; - const dedicatedChannel = getDedicatedChannel(collectionName, docId); + const dedicatedChannel = getDedicatedChannel(collectionName, docId, config); client.publish(dedicatedChannel, message); }); }); }; const dispatchUpdate = function( - optimistic, + config, collectionName, channels, docs, fields ) { + const { optimistic } = config; const uid = optimistic ? RedisSubscriptionManager.uid : null; const events = docs.map(doc => ({ @@ -60,10 +60,11 @@ const dispatchUpdate = function( [RedisPipe.UID]: uid, })); - dispatchEvents(optimistic, collectionName, channels, events); + dispatchEvents(config, collectionName, channels, events); }; -const dispatchRemove = function(optimistic, collectionName, channels, docs) { +const dispatchRemove = function(config, collectionName, channels, docs) { + const { optimistic } = config; const uid = optimistic ? RedisSubscriptionManager.uid : null; const events = docs.map(doc => ({ @@ -72,10 +73,11 @@ const dispatchRemove = function(optimistic, collectionName, channels, docs) { [RedisPipe.UID]: uid, })); - dispatchEvents(optimistic, collectionName, channels, events); + dispatchEvents(config, collectionName, channels, events); }; -const dispatchInsert = function(optimistic, collectionName, channels, doc) { +const dispatchInsert = function(config, collectionName, channels, doc) { + const { optimistic } = config; const uid = optimistic ? RedisSubscriptionManager.uid : null; const event = { @@ -84,7 +86,7 @@ const dispatchInsert = function(optimistic, collectionName, channels, doc) { [RedisPipe.UID]: uid, }; - dispatchEvents(optimistic, collectionName, channels, [event]); + dispatchEvents(config, collectionName, channels, [event]); }; export { dispatchInsert, dispatchUpdate, dispatchRemove }; diff --git a/lib/redis/RedisSubscriber.js b/lib/redis/RedisSubscriber.js index e2791ea7..22968524 100644 --- a/lib/redis/RedisSubscriber.js +++ b/lib/redis/RedisSubscriber.js @@ -39,7 +39,7 @@ export default class RedisSubscriber { this.observableCollection.selector ); - return ids.map(id => getDedicatedChannel(collectionName, id)); + return ids.map(id => getDedicatedChannel(collectionName, id, this.observableCollection.options)); default: throw new Meteor.Error( `Strategy could not be found: ${this.strategy}` diff --git a/lib/utils/getDedicatedChannel.js b/lib/utils/getDedicatedChannel.js index 686ac854..da7b450a 100644 --- a/lib/utils/getDedicatedChannel.js +++ b/lib/utils/getDedicatedChannel.js @@ -1,7 +1,8 @@ import { MongoID } from 'meteor/mongo-id'; import getChannelName from './getChannelName'; -export default function getDedicatedChannel(collectionName, docId){ - const channelName = `${collectionName}::${MongoID.idStringify(docId)}`; +// TODO do we want to support the multiple `namespaces` config for dedicated channels? +export default function getDedicatedChannel(collectionName, docId, { namespace }){ + const channelName = `${namespace ? (namespace + '::') : ''}${collectionName}::${MongoID.idStringify(docId)}`; return getChannelName(channelName); }