-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathops.lua
48 lines (38 loc) · 946 Bytes
/
ops.lua
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
local utils = require("acid.utils")
local composable = function(map)
map.type = "command"
map.update_payload = function(this, fn)
local old_payload_fn = this.payload
this.payload = function()
return fn(old_payload_fn())
end
return this
end
map.with_handler = function(this, fn)
this.handler = fn
return this
end
map.with_payload = function(this, value)
this.payload = function() return value end
return this
end
map.build = function(this)
return this.payload(), this.handler
end
return map
end
local nop = function(_) return end
local new = function(op)
return composable{}:with_payload{op = op, id = utils.ulid()}
end
local for_op = function(op)
return function(map)
return new(op)
:with_handler(nop)
:update_payload(function(orig) return utils.merge(orig, map) end)
end
end
return setmetatable({}, {__index = function(_, op)
return for_op(op)
end
})