2019-07-15 08:12:52 +02:00
|
|
|
import {
|
2020-02-03 10:24:25 +01:00
|
|
|
isFunction,
|
|
|
|
filter,
|
|
|
|
map,
|
|
|
|
uniqBy,
|
|
|
|
keys,
|
|
|
|
difference,
|
|
|
|
join,
|
|
|
|
reduce,
|
|
|
|
find,
|
|
|
|
} from "lodash/fp"
|
|
|
|
import { compileExpression, compileCode } from "../common/compileCode"
|
|
|
|
import { $ } from "../common"
|
|
|
|
import { _executeAction } from "./execute"
|
|
|
|
import { BadRequestError, NotFoundError } from "../common/errors"
|
2019-07-15 08:12:52 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
export const initialiseActions = (
|
|
|
|
subscribe,
|
|
|
|
behaviourSources,
|
|
|
|
actions,
|
|
|
|
triggers,
|
|
|
|
apis
|
|
|
|
) => {
|
|
|
|
validateSources(behaviourSources, actions)
|
|
|
|
subscribeTriggers(subscribe, behaviourSources, actions, triggers, apis)
|
|
|
|
return createActionsCollection(behaviourSources, actions)
|
|
|
|
}
|
2019-07-15 08:12:52 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const createActionsCollection = (behaviourSources, actions) =>
|
|
|
|
$(actions, [
|
|
|
|
reduce((all, a) => {
|
|
|
|
all[a.name] = opts => _executeAction(behaviourSources, a, opts)
|
|
|
|
return all
|
|
|
|
}, {}),
|
|
|
|
])
|
2019-07-15 08:12:52 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const subscribeTriggers = (
|
|
|
|
subscribe,
|
|
|
|
behaviourSources,
|
|
|
|
actions,
|
|
|
|
triggers,
|
|
|
|
apis
|
|
|
|
) => {
|
2019-07-15 08:12:52 +02:00
|
|
|
const createOptions = (optionsCreator, eventContext) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
if (!optionsCreator) return {}
|
|
|
|
const create = compileCode(optionsCreator)
|
|
|
|
return create({ context: eventContext, apis })
|
|
|
|
}
|
2019-07-15 08:12:52 +02:00
|
|
|
|
|
|
|
const shouldRunTrigger = (trigger, eventContext) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
if (!trigger.condition) return true
|
|
|
|
const shouldRun = compileExpression(trigger.condition)
|
|
|
|
return shouldRun({ context: eventContext })
|
|
|
|
}
|
2019-07-15 08:12:52 +02:00
|
|
|
|
|
|
|
for (let trig of triggers) {
|
|
|
|
subscribe(trig.eventName, async (ev, ctx) => {
|
|
|
|
if (shouldRunTrigger(trig, ctx)) {
|
|
|
|
await _executeAction(
|
|
|
|
behaviourSources,
|
|
|
|
find(a => a.name === trig.actionName)(actions),
|
2020-02-03 10:24:25 +01:00
|
|
|
createOptions(trig.optionsCreator, ctx)
|
|
|
|
)
|
2019-07-15 08:12:52 +02:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
})
|
2019-07-15 08:12:52 +02:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|
2019-07-15 08:12:52 +02:00
|
|
|
|
|
|
|
const validateSources = (behaviourSources, actions) => {
|
|
|
|
const declaredSources = $(actions, [
|
|
|
|
uniqBy(a => a.behaviourSource),
|
|
|
|
map(a => a.behaviourSource),
|
2020-02-03 10:24:25 +01:00
|
|
|
])
|
2019-07-15 08:12:52 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const suppliedSources = keys(behaviourSources)
|
2019-07-15 08:12:52 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const missingSources = difference(declaredSources, suppliedSources)
|
2019-07-15 08:12:52 +02:00
|
|
|
|
|
|
|
if (missingSources.length > 0) {
|
2020-02-03 10:24:25 +01:00
|
|
|
throw new BadRequestError(
|
|
|
|
`Declared behaviour sources are not supplied: ${join(
|
|
|
|
", ",
|
|
|
|
missingSources
|
|
|
|
)}`
|
|
|
|
)
|
2019-07-15 08:12:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const missingBehaviours = $(actions, [
|
2020-02-03 10:24:25 +01:00
|
|
|
filter(
|
|
|
|
a => !isFunction(behaviourSources[a.behaviourSource][a.behaviourName])
|
|
|
|
),
|
2019-07-15 08:12:52 +02:00
|
|
|
map(a => `Action: ${a.name} : ${a.behaviourSource}.${a.behaviourName}`),
|
2020-02-03 10:24:25 +01:00
|
|
|
])
|
2019-07-15 08:12:52 +02:00
|
|
|
|
|
|
|
if (missingBehaviours.length > 0) {
|
2020-02-03 10:24:25 +01:00
|
|
|
throw new NotFoundError(
|
|
|
|
`Missing behaviours: could not find behaviour functions: ${join(
|
|
|
|
", ",
|
|
|
|
missingBehaviours
|
|
|
|
)}`
|
|
|
|
)
|
2019-07-15 08:12:52 +02:00
|
|
|
}
|
2020-02-03 10:24:25 +01:00
|
|
|
}
|