-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects.js
190 lines (168 loc) · 4.86 KB
/
objects.js
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
const fs = require('fs');
const yaml = require('js-yaml');
const parse = require('path').parse;
const watcher = require('./watcher');
const misc = require('./misc');
class ObjectManager extends watcher {
constructor(options) {
super('objects');
this.types = new Map();
this.typeDefinitions = new Map();
this.objects = new Map();
this.instances = new Map();
this.mixins = new Map();
this.namedInstances = new Map();
this.deferredInstances = new Set();
this.initialLoad = true;
}
start() {
super.start();
this.initialLoad = false;
global.logger.info('Loading instances...');
let instances = 0;
for (let file of misc.walkSync(`${global.config.instancePath}`)) {
this.get(parse(file).name).dirty = false;
instances += 1;
}
global.logger.info(`Loaded ${instances} instances`);
this.checkDeferred();
}
addPackage(path) {
super.addPackage(path);
for (let file of misc.walkSync(`${path}/mixins/`)) {
this.addMixin(require(`./../${file}`))
}
for (let file of misc.walkSync(`${path}/types/`)) {
let {name, mixins} = yaml.safeLoad(fs.readFileSync(file));
this.defineType(name, ...mixins);
}
}
defer(path, data) {
this.deferredInstances.add({path, data});
}
load(path, data) {
if (!data) {
data = yaml.safeLoad(fs.readFileSync(path));
}
if (data.type == 'instance') {
if (this.initialLoad || !this.objectReady(data.of)) {
this.defer(path, data);
return;
}
let instance = this.instances.get(data.name);
if (!instance) {
let object = this.objects.get(data.of);
instance = new object;
object.new(instance);
instance.uid = data.name;
this.instances.set(instance.uid, instance);
}
instance.modify(data);
} else {
let obj = this.objects.get(data.id);
if (!obj) {
global.logger.info(`Defining a new object: ${data.id}`);
obj = this.types.get(data.type)(data);
this.objects.set(data.id, obj);
} else {
obj.modify(data);
}
this.checkDeferred();
}
}
checkDeferred() {
let deferred = Array.from(this.deferredInstances);
this.deferredInstances = new Set();
for (let instance of deferred) {
this.load(instance.path, instance.data);
}
}
objectReady(id) {
if (!this.objects.has(id)) {
return false;
} else {
return this.objects.get(id).ready();
}
}
instanceReady(uid) {
if (uid == undefined) {
return true;
} else {
return this.instances.has(uid);
}
}
addMixin(mixin) {
global.logger.info(`Defining mixin: ${mixin.name}`);
this.mixins.set(mixin.name, mixin);
}
defineType(name, ...mixins) {
let definition = this.typeDefinitions.get(name);
if (!definition) {
definition = [];
this.typeDefinitions.set(name, definition);
global.logger.info(`Defining type: ${name}, ${mixins}`);
} else {
global.logger.info(`Ammending type with: ${name}, ${mixins}`);
}
for (let mixin of mixins) {
definition.push(mixin);
}
}
initializePackages() {
super.initializePackages();
this.finalizeTypes();
}
finalizeTypes() {
global.logger.info('Finalizing types');
for (let [name, definition] of this.typeDefinitions) {
let mixinFunctions = definition.map((m) => {
return this.mixins.get(m);
});
mixinFunctions.sort((a,b) => { return a.priority > b.priority; });
this.types.set(name, (data) => {
let objectClass;
for (let mixin of mixinFunctions) {
objectClass = mixin(objectClass);
}
objectClass.initialize(data);
objectClass.modify(data);
objectClass.prototype.mixins = new Set(definition);
return objectClass;
});
if (definition.includes('Internal')) {
this.load(name, {name: name, id: name, type: name});
}
}
}
new(id, ...args) {
let object = this.objects.get(id);
let instance = new object;
instance = object.new(instance, ...args);
this.instances.set(instance.uid, instance);
return instance;
}
loadInstance(data) {
let object = this.objects.get(data.id);
let instance = new object;
object.load(instance, data);
this.instances.set(instance.uid, instance);
return instance;
}
get(uid) {
if (this.instances.has(uid)) {
return this.instances.get(uid);
} else {
let fname = `${global.config.instancePath}/${uid}`;
if (fs.existsSync(fname)) {
let data = yaml.safeLoad(fs.readFileSync(fname));
// We should probably be validating the data here...
let object = this.objects.get(data.id);
let instance = new object;
this.instances.set(uid, instance);
object.load(instance, data);
return instance;
}
}
}
}
module.exports = ObjectManager;