-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDep.lua
More file actions
46 lines (37 loc) · 916 Bytes
/
Dep.lua
File metadata and controls
46 lines (37 loc) · 916 Bytes
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
local Dep = class('Dep');
Dep.target = nil;--Watcher
function Dep:ctor()
self.subs = {}; --Array<Watcher>
end
function Dep:addSub(sub) -- call by Watcher
table.insert(self.subs,sub)
end
function Dep:removeSub(sub) -- call by Watcher
table.removebyvalue(self.subs,sub)
end
function Dep:depend() -- call by observe
if Dep.target then
Dep.target:addDep(self);
end
end
function Dep:notify() -- call by observe
local subs = shallowcopy(self.subs);
for _,watcher in ipairs(subs) do
watcher:update();
end
end
local targetStack = {};
local function pushTarget(_target) -- call by Watcher
if Dep.target then
table.insert(targetStack,_target);
end
Dep.target = _target;
end
local function popTarget() -- call by Watcher
Dep.target = table.remove(targetStack);
end
return {
Dep = Dep;
pushTarget = pushTarget;
popTarget = popTarget;
}