-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathdomain.lua
More file actions
216 lines (180 loc) · 4.88 KB
/
domain.lua
File metadata and controls
216 lines (180 loc) · 4.88 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
214
215
216
local Class = require "luanode.class"
local EventEmitter = require "luanode.event_emitter"
local utils = require "luanode.utils"
-- TODO: sacar el seeall
module(..., package.seeall)
local endMethods = {'end', 'abort', 'destroy', 'destroySoon'}
local stack = {}
Domain = Class:InheritsFrom(EventEmitter)
local function pop (arr)
local d = arr[table.getn(arr)]
table.remove(arr)
return d
end
local function instanceof (obj1,obj2)
obj2 = tostring (obj2)
local mt = getmetatable (obj1)
while true do
if mt == nil then return false end
if tostring (mt) == super then return true end
mt = getmetatable (mt)
end
end
local function indexOf (arr, elem)
local result
if "table" == type(arr) then
for i=1,#arr do
if elem = arr[i] then
result = i
break
end
end
end
return result
end
local function splice (arr, index, howmany)
local new_arr = {}
local table_size = table.getn(arr)
if index < 1 then
index = 1
end
if howmany < 0 then
howmany = 0
end
if index > table_size then
index = table_size + 1
howmany = 0
end
if index + howmany > table_size then
howmany = table_size - index + 1
end
for i=1,index do
table.insert(new_arr,arr[i])
end
for i=index + howmany - 1,table_size do
table.insert(new_arr,arr[i])
end
return new_arr
end
local function extend (parent, obj1, obj2)
if type(obj2) ~= "table" then
return
end
for i,v in pairs(obj2) do
table.insert(obj1,i,v)
end
if type(parent) ~= "table" then
return obj1 or parent
end
temp_obj = obj1 or {}
temp_obj.__index = parent
return setmetatable(parent, temp_obj)
end
local function apply (cb, self, obj)
if type(cb) == "function" then
cb = Object:extend()
setmetatable(cb:new(),{__index = self})
cb(obj)
end
end
function Domain:enter ()
if this._disposed then
return
end
table.insert(stack,this)
end
function Domain:exit ()
if this._disposed then
return
end
local d
repeat
d = pop(stack)
until d ~= nil && d.__index ~= self
process.domain = stack[table.getn(stack)];
end
function Domain:add (ee)
if this._disposed then
return
end
if ee.domain.__index == this then
return
end
if ee.domain then
ee.domain.remove(ee)
end
if this.domain && instanceof(ee,Domain) then
for d = this.domain do
if ee.__index == d then
return
end
end
end
ee.domain = this;
table.insert(this.members, ee);
end
function Domain:remove(ee)
ee.domain = nil
local index = indexOf(this.members, ee)
if (index != -1) then
splice(this.members, index, 1)
end
end
function Domain:bind (cb, interceptError)
--if cb throws, catch it here.
local self = this;
local b = function ()
--disposing turns functions into no-ops
if self._disposed then
return
end
if instanceof(this,Domain) then
return cb.apply(this, arguments)
end
--only intercept first-arg errors if explicitly requested.
if interceptError && arguments[0] && instanceof(arguments[0],Error) then
local er = arguments[0]
extend(utils,er, {
domainBound = cb,
domainThrown = false,
domain = self
})
self.emit('error', er)
return
end
--remove first-arg if intercept as assumed to be the error-arg
if (interceptError) then
local len = table.getn(arguments)
local args
switch (len) then
case 0:
case 1:
--no args that we care about.
args = {}
break
case 2:
--optimization for most common case: cb(er, data)
args = {arguments[1]}
break
default:
--slower for less common case: cb(er, foo, bar, baz, ...)
args = {}
for i = 1,len do
args[i] = arguments[i+1]
end
break
end
self.enter()
local ret = cb.apply(this, args)
self.exit()
return ret;
end
self.enter()
var ret = cb.apply(this, arguments)
self.exit()
return ret
end
b.domain = this
return b
end
Domain._disposed = true