-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateModule.lua
72 lines (57 loc) · 1.74 KB
/
StateModule.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
local Class = require('thirdparty/middleclass/middleclass');
local Signal = require('common/Signal');
local Utils = require('common/Utils');
local Module = Class("StateModule");
function Module:initialize(id)
self.id = id;
self._stateChangd = false;
self.loadPhase = false;
self._submodules = {};
self.stateChanged = Signal();
end
function Module:SetLoadPhase(loading)
self.loadPhase = loading;
for id, module in pairs(self._submodules) do
module:SetLoadPhase(loading);
end
end
function Module:IsLoadPhase()
return self.loadPhase;
end
function Module:AddSubmodule(module)
if not Utils.IsClassOrSubClass(module.class, Module) then
error("Module "..tostring(module).." is not from class "..Module.name);
end
self._submodules[module.id] = module;
module.stateChanged:connect(self.StateChanged, self);
end
function Module:RemoveSubmodule(module)
self._submodules[module.id] = nil;
module.stateChanged:disconnect(self.StateChanged, self);
end
function Module:LoadSubmodulesState(data)
if type(data) ~= 'table' then return; end
for id, module in pairs(self._submodules) do
module:LoadState(data[module.id]);
end
end
function Module:DumpSubmodulesState()
local data = {};
for id, module in pairs(self._submodules) do
data[id] = module:DumpState();
end
return data;
end
function Module:StateChanged(force)
-- print(self, "on loading:", self.loadPhase, force)
if self._stateChanged or self.loadPhase == true and not force then return; end
self._stateChanged = true;
self.stateChanged:emit();
end
function Module:StateClean()
self._stateChanged = false;
end
function Module:IsStateChanged()
return self._stateChanged;
end
return Module;