Page MenuHomeSealhub

event-manager.js
No OneTemporary

event-manager.js

const Promise = require("bluebird");
const PatternEmitter = require("pattern-emitter-promise").EventEmitter;
PatternEmitter.setPromiseLibrary(Promise);
class EventManager {
constructor(app) {
app.ee = PatternEmitter();
app.on = app.ee.on.bind(app.ee);
app.off = app.ee.off.bind(app.ee);
app.emit = app.ee.emit.bind(app.ee);
app.removeAllListeners = app.ee.removeAllListeners.bind(app.ee);
this.app = app;
}
// TODO: this function is a symptom of leaking abstraction
// we need to abstract logic of intentions that base on secret tokens
subscribeToIntentionCreate(collection_name, template_name) {
this.subscribeTo(
collection_name,
"create",
"post",
async (context, path, params, intent) => {
const token = (await this.app.run_action(
new this.app.Sealious.SuperContext(),
["collections", collection_name, intent.id],
"show"
)).body.token;
const message = await this.app.MailTemplates[template_name](
this.app,
{
email_address: intent.body.email,
token,
}
);
return message.send(this.app);
}
);
}
subscribeTo(collection_pattern, action_name, when, callback) {
this.app.on(
new RegExp(
`${when}:collections\.${collection_pattern}:${action_name}`
),
callback
);
}
}
module.exports = EventManager;

File Metadata

Mime Type
text/plain
Expires
Wed, May 7, 19:38 (1 d, 18 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
667361
Default Alt Text
event-manager.js (1 KB)

Event Timeline