-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduler.lua
More file actions
110 lines (91 loc) · 2.13 KB
/
Scheduler.lua
File metadata and controls
110 lines (91 loc) · 2.13 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
local MAX_UPDATE_COUNT = 100;
local queue = {}; -- array
local has = {};
local circular = {};
local waiting = false;
local flushing = false;
local index = 1; -- flushingSchedulerQueueIndex
local resetSchedulerState;
local resetSchedulerState;
local queueWatcher;
local nextTickHandler;
local nextTick;
resetSchedulerState = function ()
index = 1;
queue = {};
has = {};
circular = {};
waiting = false;
flushing = false;
end
flushSchedulerQueue = function ()
flushing = true;
table.sort(queue,function(a,b)
return a.id < b.id;
end)
index = 1;
while index <= #queue do
local watcher = queue[index];
has[watcher] = nil;
watcher:run();
if has[watcher] then
circular[watcher] = (circular[watcher] or 0) + 1;
if circular[watcher] > MAX_UPDATE_COUNT then
error('circulard update!');
break;
end
end
index = index + 1;
end
resetSchedulerState();
end
queueWatcher = function (watcher)
if not has[watcher] then
has[watcher] = true;
if not flushing then
table.insert(queue,watcher);
else
local i = #queue;
while i > index and queue[i].id > watcher.id do
i = i - 1;
end
table.insert(queue,i + 1,watcher);
end
if not waiting then
waiting = true;
nextTick(flushSchedulerQueue);
end
end
end
local timerFun = function() end;
local function setTimerFun(v)
timerFun = v;
end
local function getTimerFun()
return timerFun;
end
local callbacks = {}; -- array
local pending = false;
nextTickHandler = function ()
pending = false;
local copies = shallowcopy(callbacks);
callbacks = {};
for _,v in ipairs(copies) do
v();
end
end
nextTick = function (cb)
table.insert(callbacks,function()
if cb then
cb();
end
end)
if not pending then
pending = true;
timerFun(nextTickHandler);
end
end
return {
queueWatcher = queueWatcher;
setTimerFun = setTimerFun;
}