-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
213 lines (164 loc) · 4.31 KB
/
main.lua
File metadata and controls
213 lines (164 loc) · 4.31 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
-- orbit.lua
-- single-file coroutine-native flow runtime
-- inspired by Orbit but rewritten idiomatically for Lua
--
-- features:
-- - nodes
-- - conditional transitions
-- - retries
-- - sync + coroutine unified
-- - batch execution
-- - tiny API
--
-- usage:
--
-- local pf = require("orbit")
--
-- local a = pf.node(function(shared)
-- print("A")
-- shared.count = (shared.count or 0) + 1
-- return "next"
-- end)
--
-- local b = pf.node(function(shared)
-- print("B", shared.count)
-- end)
--
-- a:to("next", b)
--
-- pf.run(a, {})
local M = {}
----------------------------------------------------------------------
-- helpers
----------------------------------------------------------------------
local function sleep(sec)
if package.loaded["socket"] then
require("socket").sleep(sec)
end
end
local function copy(tbl)
local t = {}
for k, v in pairs(tbl or {}) do
t[k] = v
end
return t
end
----------------------------------------------------------------------
-- node
----------------------------------------------------------------------
local Node = {}
Node.__index = Node
function Node:new(fn, opts)
opts = opts or {}
return setmetatable({
fn = fn,
next = {},
retries = opts.retries or 1,
wait = opts.wait or 0,
params = opts.params or {},
batch = opts.batch or false,
}, self)
end
function Node:to(action, target)
self.next[action or "default"] = target
return target
end
function Node:run_once(shared, item)
return self.fn(shared, item, self.params)
end
function Node:run(shared, item)
local last_err
for i = 1, self.retries do
local ok, res = pcall(self.run_once, self, shared, item)
if ok then
return res
end
last_err = res
if self.wait > 0 then
sleep(self.wait)
end
end
error(last_err)
end
----------------------------------------------------------------------
-- constructors
----------------------------------------------------------------------
function M.node(fn, opts)
return Node:new(fn, opts)
end
function M.batch(fn, opts)
opts = opts or {}
opts.batch = true
return Node:new(fn, opts)
end
----------------------------------------------------------------------
-- flow runner
----------------------------------------------------------------------
function M.run(start, shared, params)
shared = shared or {}
local current = start
local action = nil
while current do
current.params = copy(params)
if current.batch then
local items = current:run(shared) or {}
for _, item in ipairs(items) do
action = current:run(shared, item)
end
else
action = current:run(shared)
end
current = current.next[action or "default"]
end
return action
end
----------------------------------------------------------------------
-- coroutine runner
----------------------------------------------------------------------
function M.async(start, shared, params)
return coroutine.create(function()
return M.run(start, shared, params)
end)
end
function M.await(co)
local ok, res = coroutine.resume(co)
if not ok then
error(res)
end
return res
end
----------------------------------------------------------------------
-- pipeline syntax sugar
----------------------------------------------------------------------
function Node:__shr(other)
return self:to("default", other)
end
----------------------------------------------------------------------
-- example
----------------------------------------------------------------------
if ... == nil then
local shared = {}
local fetch = M.node(function(shared)
print("fetch")
shared.value = 42
return "ok"
end)
local process = M.node(function(shared)
print("process", shared.value)
if shared.value > 40 then
return "large"
end
return "small"
end)
local large = M.node(function(shared)
print("large value")
end)
local small = M.node(function(shared)
print("small value")
end)
fetch:to("ok", process)
process:to("large", large)
process:to("small", small)
M.run(fetch, shared)
end
return M