aboutsummaryrefslogtreecommitdiffstats
path: root/util/promise.lua
blob: b566048869c7a4e82a6dd7a31091f2f1320addd6 (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
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
local promise_methods = {};
local promise_mt = { __name = "promise", __index = promise_methods };

function promise_mt:__tostring()
	return  "promise (" .. (self._state or "invalid") .. ")";
end

local function is_promise(o)
	local mt = getmetatable(o);
	return mt == promise_mt;
end

local function next_pending(self, on_fulfilled, on_rejected)
	table.insert(self._pending_on_fulfilled, on_fulfilled);
	table.insert(self._pending_on_rejected, on_rejected);
end

local function next_fulfilled(promise, on_fulfilled, on_rejected) -- luacheck: ignore 212/on_rejected
	on_fulfilled(promise.value);
end

local function next_rejected(promise, on_fulfilled, on_rejected) -- luacheck: ignore 212/on_fulfilled
	on_rejected(promise.reason);
end

local function promise_settle(promise, new_state, new_next, cbs, value)
	if promise._state ~= "pending" then
		return;
	end
	promise._state = new_state;
	promise._next = new_next;
	for _, cb in ipairs(cbs) do
		cb(value);
	end
	return true;
end

local function new_resolve_functions(p)
	local resolved = false;
	local function _resolve(v)
		if resolved then return; end
		resolved = true;
		if is_promise(v) then
			v:next(new_resolve_functions(p));
		elseif promise_settle(p, "fulfilled", next_fulfilled, p._pending_on_fulfilled, v) then
			p.value = v;
		end

	end
	local function _reject(e)
		if resolved then return; end
		resolved = true;
		if is_promise(e) then
			e:next(new_resolve_functions(p));
		elseif promise_settle(p, "rejected", next_rejected, p._pending_on_rejected, e) then
			p.reason = e;
		end
	end
	return _resolve, _reject;
end

local function wrap_handler(f, resolve, reject)
	return function (param)
		local ok, ret = pcall(f, param);
		if ok then
			resolve(ret);
		else
			reject(ret);
		end
	end;
end

local function new(f)
	local p = setmetatable({ _state = "pending", _next = next_pending, _pending_on_fulfilled = {}, _pending_on_rejected = {} }, promise_mt);
	if f then
		local resolve, reject = new_resolve_functions(p);
		local ok, ret = pcall(f, resolve, reject);
		if not ok and p._state == "pending" then
			reject(ret);
		end
	end
	return p;
end

local function all(promises)
	return new(function (resolve, reject)
		local count, total, results = 0, #promises, {};
		for i = 1, total do
			promises[i]:next(function (v)
				results[i] = v;
				count = count + 1;
				if count == total then
					resolve(results);
				end
			end, reject);
		end
	end);
end

local function race(promises)
	return new(function (resolve, reject)
		for i = 1, #promises do
			promises[i]:next(resolve, reject);
		end
	end);
end

local function resolve(v)
	return new(function (_resolve)
		_resolve(v);
	end);
end

local function reject(v)
	return new(function (_, _reject)
		_reject(v);
	end);
end

function promise_methods:next(on_fulfilled, on_rejected)
	return new(function (resolve, reject) --luacheck: ignore 431/resolve 431/reject
		self:_next(
			on_fulfilled and wrap_handler(on_fulfilled, resolve, reject) or nil,
			on_rejected and wrap_handler(on_rejected, resolve, reject) or nil
		);
	end);
end

function promise_methods:catch(on_rejected)
	return self:next(nil, on_rejected);
end

function promise_methods:finally(on_finally)
	local function _on_finally(value) on_finally(); return value; end
	local function _on_catch_finally(err) on_finally(); return reject(err); end
	return self:next(_on_finally, _on_catch_finally);
end

return {
	new = new;
	resolve = resolve;
	reject = reject;
	all = all;
	race = race;
}