Created
July 14, 2016 08:24
-
-
Save trsdln/5b48bfeeba7fba7c5355589f271a4384 to your computer and use it in GitHub Desktop.
Centralized notification sending
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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