Skip to content

Instantly share code, notes, and snippets.

@trsdln
Created July 14, 2016 08:24
Show Gist options
  • Save trsdln/5b48bfeeba7fba7c5355589f271a4384 to your computer and use it in GitHub Desktop.
Save trsdln/5b48bfeeba7fba7c5355589f271a4384 to your computer and use it in GitHub Desktop.
Centralized notification sending
class LazyBlazeTemplateCompiler {
constructor() {
//store compiled templates in key-value store
this._templateCache = {};
}
compile(templateName) {
//check if template already compiled
//compile if it isn't yet compiled
const handleBarsText = Assets.getText(`notification-templates/${templateName}`);
//return compiled template
return this._templateCache[templateName];
}
}
const blazeCompiler = new LazyBlazeTemplateCompiler();
class NotificationSender {
constructor(subject, template, templateData) {
//store options
}
_renderTemplate(notificationType) {
//render template according to notification type
// as you see templates for each notification type are
// stored in separate catalog under
// "/private/notification-templates/<template-type>/<template-name>"
return blazeCompiler.compile(`${notificationType}/${this.template}`)(this.templateData);
}
sendEmail(receiverId) {
//todo
return this;//allows chaining
}
sendBuiltIn(receiverId) {
//todo
return this;//allows chaining
}
sendBrowser(receiverId) {
//todo
return this;//allows chaining
}
sendMobile(receiverId) {
//todo
return this;//allows chaining
}
//any other notification type you would like is here
sendAll() {
//call others methods from above
//chaining doesn't make sense
}
}
//usage example
const receiver = Meteor.userId();
const notificationSender = new NotificationSender(
'Hello world notification',
'hello-world',
{ customText: 'some text here' }
);
notificationSender.sendMobile(receiver).sendBuiltin(receiver); // just send some notification types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment