mirror of https://github.com/Budibase/budibase.git
8 changed files with 108 additions and 31 deletions
@ -0,0 +1,51 @@ |
|||
const { rowEmission, tableEmission } = require("./utils") |
|||
const mainEmitter = require("./index") |
|||
|
|||
// max number of automations that can chain on top of each other
|
|||
const MAX_AUTOMATION_CHAIN = 5 |
|||
|
|||
/** |
|||
* Special emitter which takes the count of automation runs which have occurred and blocks an |
|||
* automation from running if it has reached the maximum number of chained automations runs. |
|||
* This essentially "fakes" the normal emitter to add some functionality in-between to stop automations |
|||
* from getting stuck endlessly chaining. |
|||
*/ |
|||
class AutomationEmitter { |
|||
constructor(chainCount) { |
|||
this.chainCount = chainCount |
|||
this.metadata = { |
|||
automationChainCount: chainCount, |
|||
} |
|||
} |
|||
|
|||
emitRow(eventName, appId, row, table = null) { |
|||
// don't emit even if we've reached max automation chain
|
|||
if (this.chainCount > MAX_AUTOMATION_CHAIN) { |
|||
return |
|||
} |
|||
rowEmission({ |
|||
emitter: mainEmitter, |
|||
eventName, |
|||
appId, |
|||
row, |
|||
table, |
|||
metadata: this.metadata, |
|||
}) |
|||
} |
|||
|
|||
emitTable(eventName, appId, table = null) { |
|||
// don't emit even if we've reached max automation chain
|
|||
if (this.chainCount > MAX_AUTOMATION_CHAIN) { |
|||
return |
|||
} |
|||
tableEmission({ |
|||
emitter: mainEmitter, |
|||
eventName, |
|||
appId, |
|||
table, |
|||
metadata: this.metadata, |
|||
}) |
|||
} |
|||
} |
|||
|
|||
module.exports = AutomationEmitter |
|||
@ -0,0 +1,38 @@ |
|||
exports.rowEmission = ({ emitter, eventName, appId, row, table, metadata }) => { |
|||
let event = { |
|||
row, |
|||
appId, |
|||
tableId: row.tableId, |
|||
} |
|||
if (table) { |
|||
event.table = table |
|||
} |
|||
event.id = row._id |
|||
if (row._rev) { |
|||
event.revision = row._rev |
|||
} |
|||
if (metadata) { |
|||
event.metadata = metadata |
|||
} |
|||
emitter.emit(eventName, event) |
|||
} |
|||
|
|||
exports.tableEmission = ({ emitter, eventName, appId, table, metadata }) => { |
|||
const tableId = table._id |
|||
let event = { |
|||
table: { |
|||
...table, |
|||
tableId: tableId, |
|||
}, |
|||
appId, |
|||
tableId: tableId, |
|||
} |
|||
event.id = tableId |
|||
if (table._rev) { |
|||
event.revision = table._rev |
|||
} |
|||
if (metadata) { |
|||
event.metadata = metadata |
|||
} |
|||
emitter.emit(eventName, event) |
|||
} |
|||
Loading…
Reference in new issue