mirror of
https://github.com/vale981/grapher
synced 2025-03-06 18:11:39 -05:00
46 lines
No EOL
1.2 KiB
JavaScript
46 lines
No EOL
1.2 KiB
JavaScript
import {check} from 'meteor/check';
|
|
|
|
const storage = '__reducers';
|
|
Object.assign(Mongo.Collection.prototype, {
|
|
/**
|
|
* @param data
|
|
*/
|
|
addReducers(data) {
|
|
if (!this[storage]) {
|
|
this[storage] = {};
|
|
}
|
|
|
|
_.each(data, (reducerConfig, reducerName) => {
|
|
if (!this[reducerConfig]) {
|
|
this[reducerConfig] = {};
|
|
}
|
|
|
|
if (this.getLinker(reducerName)) {
|
|
throw new Meteor.Error(`You cannot add the reducer with name: ${reducerName} because it is already defined as a link in ${this._name} collection`)
|
|
}
|
|
|
|
if (this[reducerConfig][reducerName]) {
|
|
throw new Meteor.Error(`You cannot add the reducer with name: ${reducerName} because it was already added to ${this._name} collection`)
|
|
}
|
|
|
|
check(reducerConfig, {
|
|
body: Object,
|
|
reduce: Function
|
|
});
|
|
|
|
_.extend(this[storage], {
|
|
[reducerName]: reducerConfig
|
|
});
|
|
});
|
|
},
|
|
|
|
/**
|
|
* @param name
|
|
* @returns {*}
|
|
*/
|
|
getReducer(name) {
|
|
if (this[storage]) {
|
|
return this[storage][name];
|
|
}
|
|
}
|
|
}); |