util/promise.lua

Thu, 03 Dec 2020 17:05:27 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 03 Dec 2020 17:05:27 +0000
changeset 0
550f506de75a
permissions
-rw-r--r--

Initial commit

0
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local promise_methods = {};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local promise_mt = { __name = "promise", __index = promise_methods };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local xpcall = require "util.xpcall".xpcall;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 function promise_mt:__tostring()
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 return "promise (" .. (self._state or "invalid") .. ")";
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local function is_promise(o)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local mt = getmetatable(o);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 return mt == promise_mt;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local function wrap_handler(f, resolve, reject, default)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 if not f then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 return default;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 return function (param)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local ok, ret = xpcall(f, debug.traceback, param);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 if ok then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 resolve(ret);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 else
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 reject(ret);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local function next_pending(self, on_fulfilled, on_rejected, resolve, reject)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 table.insert(self._pending_on_fulfilled, wrap_handler(on_fulfilled, resolve, reject, resolve));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 table.insert(self._pending_on_rejected, wrap_handler(on_rejected, resolve, reject, reject));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local function next_fulfilled(promise, on_fulfilled, on_rejected, resolve, reject) -- luacheck: ignore 212/on_rejected
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 wrap_handler(on_fulfilled, resolve, reject, resolve)(promise.value);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local function next_rejected(promise, on_fulfilled, on_rejected, resolve, reject) -- luacheck: ignore 212/on_fulfilled
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 wrap_handler(on_rejected, resolve, reject, reject)(promise.reason);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local function promise_settle(promise, new_state, new_next, cbs, value)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 if promise._state ~= "pending" then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 return;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 promise._state = new_state;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 promise._next = new_next;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 for _, cb in ipairs(cbs) do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 cb(value);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 -- No need to keep references to callbacks
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 promise._pending_on_fulfilled = nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 promise._pending_on_rejected = nil;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 return true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 local function new_resolve_functions(p)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local resolved = false;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 local function _resolve(v)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 if resolved then return; end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 resolved = true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 if is_promise(v) then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 v:next(new_resolve_functions(p));
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 elseif promise_settle(p, "fulfilled", next_fulfilled, p._pending_on_fulfilled, v) then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 p.value = v;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 local function _reject(e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 if resolved then return; end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 resolved = true;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 if promise_settle(p, "rejected", next_rejected, p._pending_on_rejected, e) then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 p.reason = e;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 return _resolve, _reject;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 local function new(f)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 local p = setmetatable({ _state = "pending", _next = next_pending, _pending_on_fulfilled = {}, _pending_on_rejected = {} }, promise_mt);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 if f then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 local resolve, reject = new_resolve_functions(p);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 local ok, ret = xpcall(f, debug.traceback, resolve, reject);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 if not ok and p._state == "pending" then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 reject(ret);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 return p;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 local function all(promises)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 return new(function (resolve, reject)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 local count, total, results = 0, #promises, {};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 for i = 1, total do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 promises[i]:next(function (v)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 results[i] = v;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 count = count + 1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 if count == total then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 resolve(results);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 end, reject);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 local function all_settled(promises)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 return new(function (resolve)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 local count, total, results = 0, #promises, {};
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 for i = 1, total do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 promises[i]:next(function (v)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 results[i] = { status = "fulfilled", value = v };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 count = count + 1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 if count == total then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 resolve(results);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 end, function (e)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 results[i] = { status = "rejected", reason = e };
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 count = count + 1;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 if count == total then
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 resolve(results);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 local function race(promises)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 return new(function (resolve, reject)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 for i = 1, #promises do
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 promises[i]:next(resolve, reject);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 local function resolve(v)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 return new(function (_resolve)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 _resolve(v);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 local function reject(v)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 return new(function (_, _reject)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 _reject(v);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 local function try(f)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 return resolve():next(function () return f(); end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 function promise_methods:next(on_fulfilled, on_rejected)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 return new(function (resolve, reject) --luacheck: ignore 431/resolve 431/reject
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 self:_next(on_fulfilled, on_rejected, resolve, reject);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 end);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 function promise_methods:catch(on_rejected)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 return self:next(nil, on_rejected);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 function promise_methods:finally(on_finally)
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 local function _on_finally(value) on_finally(); return value; end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 local function _on_catch_finally(err) on_finally(); return reject(err); end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 return self:next(_on_finally, _on_catch_finally);
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 end
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 return {
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 new = new;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 resolve = resolve;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 reject = reject;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 all = all;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 all_settled = all_settled;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 race = race;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 try = try;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 is_promise = is_promise;
550f506de75a Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 }

mercurial