blob: 5d8da67bc13d0e2d92c344fa5282b2b657233b7f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
local domain = require("clever_f.domain")
local sequence_coordinator = require("clever_f.sequence_coordinator")
local M = {}
local ActionFacade = {}
ActionFacade.__index = ActionFacade
M.ActionFacade = ActionFacade
local facade_records = setmetatable({}, { __mode = "k" })
local function fail(message, level)
error(message, (level or 1) + 1)
end
function ActionFacade.new(options)
if ActionFacade.is(options) then
return options
end
local coordinator
if sequence_coordinator.SequenceCoordinator.is(options) then
coordinator = options
elseif type(options) == "table" then
coordinator = options.coordinator
or options.sequence_coordinator
or sequence_coordinator.new(options)
else
fail("ActionFacade options must be a table", 2)
end
if not sequence_coordinator.SequenceCoordinator.is(coordinator) then
fail("ActionFacade requires a SequenceCoordinator", 2)
end
local facade = setmetatable({}, ActionFacade)
facade_records[facade] = { coordinator = coordinator }
return facade
end
function ActionFacade.is(value)
return type(value) == "table" and facade_records[value] ~= nil
end
function ActionFacade:coordinator()
return facade_records[self].coordinator
end
function ActionFacade:primary(descriptor)
local outcome = self:coordinator():primary(descriptor)
if not domain.ActionOutcome.is(outcome) then
fail("SequenceCoordinator must return an ActionOutcome", 2)
end
return outcome
end
ActionFacade.invoke_primary = ActionFacade.primary
ActionFacade.start = ActionFacade.primary
function M.new(options)
return ActionFacade.new(options)
end
setmetatable(M, {
__call = function(_, options)
return ActionFacade.new(options)
end,
})
return M
|