2020-02-03 10:24:25 +01:00
|
|
|
import { eventsList } from "@budibase/core"
|
|
|
|
import { filter, union, has, map } from "lodash/fp"
|
|
|
|
import records from "./records"
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const allEventsOfType = type => filter(e => e.endsWith(`:${type}`))(eventsList)
|
2019-06-07 15:18:10 +02:00
|
|
|
|
|
|
|
const getEventNamespace = ev => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const parts = ev.split(":")
|
|
|
|
return `${parts[0]}:${parts[1]}`
|
2019-06-07 15:18:10 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const hasRecord = has("record")
|
2019-06-07 15:18:10 +02:00
|
|
|
|
|
|
|
export const register = (app, logTimeElapsed, eventNamespaces = []) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const onCompleteEvents =
|
|
|
|
eventNamespaces.length === 0
|
|
|
|
? allEventsOfType("onComplete")
|
|
|
|
: map(e => `${e}:onComplete`)(eventNamespaces)
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const onErrorEvents =
|
|
|
|
eventNamespaces.length === 0
|
|
|
|
? allEventsOfType("onError")
|
|
|
|
: map(e => `${e}:onError`)(eventNamespaces)
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
for (let ev of union(onCompleteEvents)(onErrorEvents)) {
|
|
|
|
app.subscribe(ev, (_, ctx) => {
|
|
|
|
const info = hasRecord(ctx) ? ctx.record.type() : ""
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
logTimeElapsed(ev, ctx.elapsed, info)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|