1
0
Fork 0
mirror of synced 2024-07-07 07:15:43 +12:00
budibase/packages/server/src/events/index.js

51 lines
1.2 KiB
JavaScript
Raw Normal View History

const EventEmitter = require("events").EventEmitter
2020-05-25 09:54:08 +12:00
/**
* keeping event emitter in one central location as it might be used for things other than
* automations (what it was for originally) - having a central emitter will be useful in the
* future.
*/
2020-06-01 04:12:52 +12: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, table = null) {
let event = {
record,
instanceId,
tableId: record.tableId,
}
if (table) {
event.table = table
}
event.id = record._id
if (record._rev) {
event.revision = record._rev
}
this.emit(eventName, event)
}
emitTable(eventName, instanceId, table = null) {
const tableId = table._id
let event = {
table: {
...table,
tableId: tableId,
},
instanceId,
tableId: tableId,
}
event.id = tableId
if (table._rev) {
event.revision = table._rev
}
this.emit(eventName, event)
}
}
const emitter = new BudibaseEmitter()
2020-06-01 04:12:52 +12:00
module.exports = emitter