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 {
|
2020-10-29 11:28:27 +01:00
|
|
|
emitRow(eventName, appId, row, table = null) {
|
2020-09-25 19:05:26 +02:00
|
|
|
let event = {
|
2020-10-09 20:10:28 +02:00
|
|
|
row,
|
2020-10-29 11:28:27 +01:00
|
|
|
appId,
|
2020-10-09 20:10:28 +02:00
|
|
|
tableId: row.tableId,
|
2020-09-25 19:05:26 +02:00
|
|
|
}
|
2020-10-09 19:49:23 +02:00
|
|
|
if (table) {
|
|
|
|
event.table = table
|
2020-09-25 19:05:26 +02:00
|
|
|
}
|
2020-10-09 20:10:28 +02:00
|
|
|
event.id = row._id
|
|
|
|
if (row._rev) {
|
|
|
|
event.revision = row._rev
|
2020-09-25 19:05:26 +02:00
|
|
|
}
|
|
|
|
this.emit(eventName, event)
|
|
|
|
}
|
|
|
|
|
2020-10-29 11:28:27 +01:00
|
|
|
emitTable(eventName, appId, table = null) {
|
2020-10-09 19:49:23 +02:00
|
|
|
const tableId = table._id
|
2020-09-25 19:05:26 +02:00
|
|
|
let event = {
|
2020-10-09 19:49:23 +02:00
|
|
|
table: {
|
|
|
|
...table,
|
|
|
|
tableId: tableId,
|
2020-09-29 19:03:29 +02:00
|
|
|
},
|
2020-10-29 11:28:27 +01:00
|
|
|
appId,
|
2020-10-09 19:49:23 +02:00
|
|
|
tableId: tableId,
|
2020-09-25 19:05:26 +02:00
|
|
|
}
|
2020-10-09 19:49:23 +02:00
|
|
|
event.id = tableId
|
|
|
|
if (table._rev) {
|
|
|
|
event.revision = table._rev
|
2020-09-25 19:05:26 +02:00
|
|
|
}
|
|
|
|
this.emit(eventName, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const emitter = new BudibaseEmitter()
|
2020-05-31 18:12:52 +02:00
|
|
|
|
|
|
|
module.exports = emitter
|