-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
318 lines (269 loc) · 7.21 KB
/
main.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*eslint-env browser, node, es6 */
/*jslint white: true, es6: true*/
/*jshint esversion: 6*/
function isObject(value) {
try {
Object.create(value);
return value !== null;
} catch (ignore) {
return false;
}
}
function timestamp(obj) {
if (!obj.hasOwnProperty("date")) {
Object.defineProperty(obj, "date", {
configurable: false,
enumerable: true,
writable: false,
value: new Date()
});
}
}
const reservedPropertyNames = ["branch", "branches", "version"];
const reservedNames = reservedPropertyNames.join();
const hasOwnProp = Object.prototype.hasOwnProperty;
function immutable() {
throw new Error("Immutable");
}
const immutableArrayViewer = {
isExtensible: () => false,
preventExtensions: immutable,
defineProperty: immutable,
deleteProperty: immutable,
set: immutable
};
function io() {
const root = {};
let numVersions = 0;
const listeners = [];
let currentBranch = root;
Object.defineProperty(root, "root", {
configurable: false,
enumerable: false,
writable: false,
value: root
});
Object.defineProperty(root, "on", {
configurable: false,
enumerable: false,
writable: false,
value: Object.freeze(function on(listener) {
listeners.push(listener);
return () => root.off(listener);
})
});
Object.defineProperty(root, "off", {
configurable: false,
enumerable: false,
writable: false,
value: Object.freeze(function off(listener) {
let index = listeners.indexOf(listener);
while (index !== -1) {
listeners.splice(index, 1);
index = listeners.indexOf(listener);
}
})
});
function notifyListener(listener) {
listener(currentBranch);
}
function emit() {
listeners.forEach(notifyListener);
}
function branchFrom(parent, parentBranches) {
return function branchUsing(...args) {
const numParams = args.length;
if (numParams === 0 || numParams > 2) {
throw new Error("Wrong number of parameters");
}
const branch = Object.create(parent);
if (numParams === 2) {
const [key, value] = args;
if (
typeof key !== "string" ||
reservedPropertyNames.indexOf(key) !== -1
) {
throw new TypeError(
"Key must be string and not in: " + reservedNames
);
}
Object.defineProperty(branch, key, {
configurable: false,
enumerable: true,
writable: false,
value: value
});
} else if (numParams === 1) {
const [props] = args;
if (!isObject(props)) {
throw new TypeError("Argument must be object");
}
Object.keys(props).forEach(function setProp(key) {
if (reservedPropertyNames.indexOf(key) !== -1) {
return;
}
Object.defineProperty(branch, key, {
value: props[key],
configurable: false,
enumerable: true,
writable: false
});
});
}
timestamp(branch);
Object.defineProperty(branch, "version", {
value: numVersions,
configurable: false,
enumerable: true,
writable: false
});
numVersions += 1;
const branches = [];
const branchesView = new Proxy(branches, immutableArrayViewer);
Object.defineProperty(branch, "branches", {
configurable: false,
enumerable: true,
writable: false,
value: branchesView
});
Object.defineProperty(branch, "branch", {
configurable: false,
enumerable: false,
writable: false,
value: Object.freeze(branchFrom(branch, branches))
});
Object.freeze(branch);
parentBranches.push(branch);
root.currentBranch = branch;
return branch;
};
}
Object.defineProperty(root, "numVersions", {
configurable: false,
enumerable: true,
get: function getNumVersions() {
return numVersions;
}
});
let view = Object.create(null);
Object.defineProperty(root, "view", {
configurable: false,
enumerable: true,
get: function getView() {
return view;
}
});
Object.defineProperty(root, "currentBranch", {
configurable: false,
enumerable: false,
get: function getCurrentBranch() {
return currentBranch;
},
set: function setCurrentBranch(branch) {
function setViewProp(key) {
if (!hasOwnProp.call(view, key) && !hasOwnProp.call(root, key)) {
view[key] = branch[key];
}
}
if (currentBranch === branch) {
return branch;
}
if (!root.isPrototypeOf(branch)) {
throw new TypeError(
"Only infinite objects which are extensions of this root are allowed"
);
}
if (Object.getPrototypeOf(branch) === currentBranch) {
Object.keys(branch).forEach(function setProp(key) {
view[key] = branch[key];
});
} else {
view = Object.create(null);
let obj = branch;
while (obj !== root) {
Object.keys(obj).forEach(setViewProp);
obj = Object.getPrototypeOf(obj);
}
}
currentBranch = branch;
emit();
return currentBranch;
}
});
const rootBranches = [];
const branchesView = new Proxy(rootBranches, immutableArrayViewer);
Object.defineProperty(root, "branch", {
configurable: false,
enumerable: false,
writable: false,
value: Object.freeze(branchFrom(root, rootBranches))
});
Object.defineProperty(root, "branches", {
configurable: false,
enumerable: true,
writable: false,
value: branchesView
});
Object.freeze(root);
return root;
}
function walkBranches(source, sources, parents, branch) {
source.branches.forEach(function walkBranch(child) {
const childVersion = child.version;
sources[childVersion] = child;
parents[childVersion] = branch;
});
}
function fromParsedJSON(obj) {
const root = io();
const versions = [];
versions.length = obj.numVersions;
const parents = Array.apply(null, versions);
const sources = Array.apply(null, versions);
walkBranches(obj, sources, parents, root);
const branches = sources.map(function importBranch(source) {
const branch = parents[source.version].branch(source);
walkBranches(source, sources, parents, branch);
return branch;
});
root.currentBranch = branches[obj.view.version];
return root;
}
function fromJSON(json) {
return fromParsedJSON(JSON.parse(json));
}
function from(obj) {
const root = io();
root.branch(obj);
return root;
}
Object.defineProperty(io, "from", {
configurable: false,
enumerable: false,
writable: false,
value: from
});
Object.defineProperty(io, "fromJSON", {
configurable: false,
enumerable: false,
writable: false,
value: fromJSON
});
Object.defineProperty(io, "fromParsedJSON", {
configurable: false,
enumerable: false,
writable: false,
value: fromParsedJSON
});
Object.defineProperty(io, "reservedPropertyNames", {
configurable: false,
enumerable: false,
writable: false,
value: reservedPropertyNames
});
Object.freeze(reservedPropertyNames);
Object.freeze(fromParsedJSON);
Object.freeze(fromJSON);
Object.freeze(from);
Object.freeze(io);
export default io;