-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.lua
160 lines (121 loc) · 3.78 KB
/
next.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
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
--====================================================================--
--
-- Code by Piotr Machowski <piotr@machowski.co>
--
-- inspired by DMC Lua Library: Promises
--====================================================================--
local VERSION = "0.1.0"
--====================================================================--
-- Setup, Constants
local tinsert = table.insert
local Promise, Deferred -- forward declaration
--====================================================================--
-- Promise Class
--====================================================================--
Promise = {}
Promise.__index = Promise
Promise.NAME = "Promise Instance"
setmetatable(Promise, { __call = function(_, ...) return Promise.new(...) end })
--== State Constants
Promise.STATE_PENDING = 'pending'
Promise.STATE_RESOLVED = 'resolved'
Promise.STATE_REJECTED = 'rejected'
--====================================================================--
-- constructor function
function Promise:new( )
local o = setmetatable( {}, Promise )
o._state = Promise.STATE_PENDING
o._done_cbs = {}
o._fail_cbs = {}
o._result = nil
o._reason = nil
return o
end
--====================================================================--
--====================================================================--
--== Public Methods
function Promise:state()
return self._state
end
function Promise:resolve( ... )
-- print("Promise:resolve")
self._state = Promise.STATE_RESOLVED
self._result = {...}
self:_execute( self._done_cbs, ... )
end
function Promise:reject( ... )
-- print("Promise:reject")
self._state = Promise.STATE_REJECTED
self._reason = {...}
self:_execute( self._fail_cbs, ... )
end
function Promise:done( callback )
-- print("Promise:done")
if self._state == Promise.STATE_RESOLVED then
callback( unpack( self._result ) )
else
self:_addCallback( self._done_cbs, callback )
end
end
function Promise:fail( errback )
-- print("Promise:fail")
if self._state == Promise.STATE_REJECTED then
errback( unpack( self._reason ) )
else
self:_addCallback( self._fail_cbs, errback )
end
end
function Promise:andThen( callback, errback )
if callback then self:done( callback ) end
if errback then self:fail( errback ) end
return self
end
function Promise:next( ... )
return self:andThen(...)
end
--====================================================================--
--== Private Methods
function Promise:_addCallback( list, func )
tinsert( list, #list+1, func )
end
function Promise:_execute( list, ... )
-- print("Promise:_execute")
for i=1,#list do
list[i]( ... )
end
end
--====================================================================--
-- Deferred Class
--====================================================================--
Deferred = {}
Deferred.__index = Deferred
Deferred.NAME = "Deferred Instance"
setmetatable(Deferred, { __call = function(_, ...) return Deferred.new(...) end })
--====================================================================--
-- constructor function
function Deferred:new( )
local o = setmetatable( {}, Deferred )
o.promise = Promise:new()
return o
end
--====================================================================--
--====================================================================--
--== Public Methods
function Deferred:resolve( ... )
self.promise:resolve( ... )
end
function Deferred:reject( ... )
self.promise:reject( ... )
end
function Deferred:addCallbacks( callback, errback )
local promise = self.promise
if callback then promise:done( callback ) end
if errback then promise:fail( errback ) end
end
--====================================================================--
-- Promise Module Facade
--====================================================================--
return {
Promise = Promise,
Deferred = Deferred,
}