2020-05-28 21:20:03 +02:00
|
|
|
const EventEmitter = require("events").EventEmitter
|
2020-05-24 23:54:08 +02:00
|
|
|
|
2020-09-10 16:00:21 +02:00
|
|
|
/**
|
|
|
|
* keeping event emitter in one central location as it might be used for things other than
|
2020-09-21 14:49:34 +02:00
|
|
|
* automations (what it was for originally) - having a central emitter will be useful in the
|
2020-09-10 16:00:21 +02:00
|
|
|
* future.
|
|
|
|
*/
|
2020-05-31 18:12:52 +02:00
|
|
|
|
2020-09-25 19:05:26 +02:00
|
|
|
/**
|
|
|
|
* Extending the standard emitter to some syntactic sugar and standardisation to the emitted event.
|
|
|
|
* This is specifically quite important for mustache used in automations.
|
|
|
|
*/
|
|
|
|
class BudibaseEmitter extends EventEmitter {
|
|
|
|
emitRecord(eventName, instanceId, record, model = null) {
|
|
|
|
let event = {
|
|
|
|
record,
|
|
|
|
instanceId,
|
|
|
|
modelId: record.modelId,
|
|
|
|
}
|
|
|
|
if (model) {
|
|
|
|
event.model = model
|
|
|
|
}
|
|
|
|
if (record._id) {
|
|
|
|
event.id = record._id
|
|
|
|
}
|
|
|
|
if (record._rev) {
|
|
|
|
event.revision = record._rev
|
|
|
|
}
|
|
|
|
this.emit(eventName, event)
|
|
|
|
}
|
|
|
|
|
|
|
|
emitModel(eventName, instanceId, model = null) {
|
|
|
|
let event = {
|
|
|
|
model,
|
|
|
|
instanceId,
|
|
|
|
modelId: model._id,
|
|
|
|
}
|
|
|
|
if (model._id) {
|
|
|
|
event.id = model._id
|
|
|
|
}
|
|
|
|
if (model._rev) {
|
|
|
|
event.revision = model._rev
|
|
|
|
}
|
|
|
|
this.emit(eventName, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const emitter = new BudibaseEmitter()
|
2020-05-31 18:12:52 +02:00
|
|
|
|
|
|
|
module.exports = emitter
|