Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Herald v2.0 Proposal
//this would be the start of what is now the 'inApp' runner
Herald._runners['mongo'] = function (message, options) {
if (!options.collection || !options.collection.insert) {
throw new Meteor.Error('Herald - mongo runner', 'No Mongo.Collection found, please set it in your defaults');
}
if (!options.userId) {
throw new Meteor.Error('Herald - mongo runner', 'You must give a user id');
}
options.collection.insert({
message: this.message,
userId: options.userId
});
}
Herald = class Herald {
constructor(message, options = {}) {
if (!_.isString(message) || !message.length) {
throw new Meteor.Error('Herald', `You must give Herald a message`);
}
if (!_.isObject(options)) {
throw new Meteor.Error('Herald', `herald constructor options must be in object form`);
}
this.message = message;
this.options = options;
//final sanity check
if (!_.isObject(Herald._runners)) {
throw new Meteor.Error('Herald', `Core runenrs have been mutated`);
}
}
send() {
for (let runner in this.getRunners()) {
this.sendRunner(runner);
}
}
getRunners() {
return this.constructor.runners;
}
sendRunner(runner) {
if (!Herald._runners[runner]) {
throw new Meteor.Error('Herald', `can't find runner ${runner}`);
}
Herald._runners[runner](this.message, _.extend(this.getRunners()[runner], this.options[runner]))
}
}
//class vars
Herald._runners = {}
Herald.runners = {}
// Define a runner like
// Herald._runners[name] = function (message, options) {
// // body...
// }
// users add default settings like
// Herald.runners = {
// name: {
// //settings for 'name'
// }
// }
// create courier with class extensions
// myCourier = class myCourier extends Herald {}
// you can set new defaults here with myCourier.runners = {}
//I still have to think about how to mange users so mark that up as a TODO
//this is what the package user need to do on a server config file (assumes ecmascript)
//extend Herald, this is analogous to the current addCourier system
InAppNotifications = class InAppNotifications extends Herald {};
//Let users define their own mongo collections for Herald in app notifications.
HeraldMongoNotifications = new Mongo.Collection('herald_mongo_notifications')
//finally set up the system defaults.
InAppNotifications.runner = {
mongo: { //this adds the 'mongo' runner to this Herald instance
collection: HeraldMongoNotifications //this are the defaults
}
}
//lastly this is how you would use it:
//server - maybe in a meteor method?
note = InAppNotifications('A note for you!', {mongo: {userId: 'someId'}});
note.send();
//client
HeraldMongoNotifications.findOne() //=> our note object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment