Skip to content

Commit

Permalink
Apply collection level namespace config to dedicated channels
Browse files Browse the repository at this point in the history
  • Loading branch information
mfen committed Oct 1, 2021
1 parent b2be29d commit 8841e5a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
29 changes: 23 additions & 6 deletions lib/mongo/Mutator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
dispatchRemove
} from "./lib/dispatchers";
import Config from "../config";
import { Events } from "../constants";
import { Events, RedisPipe } from "../constants";
import getDedicatedChannel from '../utils/getDedicatedChannel';

function runCallbackInBackground(fn) {
Meteor.defer(Meteor.bindEnvironment(fn));
Expand Down Expand Up @@ -78,10 +79,12 @@ export default class Mutator {
doc = Originals.findOne.call(this, docId);
}

const dedicatedChannel = getDedicatedChannel(this._name, doc._id, config);

dispatchInsert(
config.optimistic,
this._name,
config._channels,
[dedicatedChannel, ...config._channels],
doc
);

Expand Down Expand Up @@ -195,10 +198,14 @@ export default class Mutator {

const { fields } = getFields(modifier);

const dedicatedChannels = docs.map(({ _id }) => {
return getDedicatedChannel(this._name, _id, config);
});

dispatchUpdate(
config.optimistic,
this._name,
config._channels,
[...dedicatedChannels, ...config._channels],
docs,
fields
);
Expand Down Expand Up @@ -258,10 +265,12 @@ export default class Mutator {
doc = this.findOne(doc._id);
}

const dedicatedChannel = getDedicatedChannel(this._name, doc._id, config);

dispatchInsert(
config.optimistic,
this._name,
config._channels,
[dedicatedChannel, ...config._channels],
doc
);
} else {
Expand All @@ -283,10 +292,14 @@ export default class Mutator {

docs = this.find(selector).fetch();

const dedicatedChannels = docs.map(({ _id }) => {
return getDedicatedChannel(this._name, _id, config);
});

dispatchUpdate(
config.optimistic,
this._name,
config._channels,
[...dedicatedChannels, ...config._channels],
docs,
fields
);
Expand Down Expand Up @@ -357,10 +370,14 @@ export default class Mutator {
});
}

const dedicatedChannels = docs.map(({ _id }) => {
return getDedicatedChannel(this._name, _id, config);
});

dispatchRemove(
config.optimistic,
this._name,
config._channels,
[...dedicatedChannels, ...config._channels],
docs
);

Expand Down
11 changes: 0 additions & 11 deletions lib/mongo/lib/dispatchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@ import { EJSON } from 'meteor/ejson';
import { Events, RedisPipe } from '../../constants';
import RedisSubscriptionManager from '../../redis/RedisSubscriptionManager';
import { getRedisPusher } from '../../redis/getRedisClient';
import getDedicatedChannel from '../../utils/getDedicatedChannel';
import Config from '../../config';
import OptimisticInvocation from '../OptimisticInvocation';

const dispatchEvents = function(optimistic, collectionName, channels, events) {
if (optimistic) {
OptimisticInvocation.withValue(true, () => {
events.forEach(event => {
const docId = event[RedisPipe.DOC]._id;
const dedicatedChannel = getDedicatedChannel(
collectionName,
docId
);
RedisSubscriptionManager.process(dedicatedChannel, event);

channels.forEach(channelName => {
RedisSubscriptionManager.process(channelName, event);
});
Expand All @@ -37,9 +29,6 @@ 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);
client.publish(dedicatedChannel, message);
});
});
};
Expand Down
2 changes: 1 addition & 1 deletion lib/redis/RedisSubscriber.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
5 changes: 3 additions & 2 deletions lib/utils/getDedicatedChannel.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 8841e5a

Please sign in to comment.