-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.es5.js
313 lines (266 loc) · 7.18 KB
/
main.es5.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
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()
});
}
}
var reservedPropertyNames = ["branch", "branches", "version"];
var reservedNames = reservedPropertyNames.join();
var hasOwnProp = Object.prototype.hasOwnProperty;
function io() {
var root = {};
var listeners = [];
var numVersions = 0;
var 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 function off() {
root.off(listener);
};
})
});
Object.defineProperty(root, "off", {
configurable: false,
enumerable: false,
writable: false,
value: Object.freeze(function off(listener) {
var 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, addBranch) {
return function branchUsing(keyOrProps, value) {
var numParams = arguments.length;
if (numParams === 0 || numParams > 2) {
throw new Error("Wrong number of parameters");
}
var branch = Object.create(parent);
if (numParams === 2) {
if (
typeof keyOrProps !== "string" ||
reservedPropertyNames.indexOf(keyOrProps) !== -1
) {
throw new TypeError(
"Key must be string and not in: " + reservedNames
);
}
Object.defineProperty(branch, keyOrProps, {
configurable: false,
enumerable: true,
writable: false,
value: value
});
} else if (numParams === 1) {
if (!isObject(keyOrProps)) {
throw new TypeError("Argument must be object");
}
Object.keys(keyOrProps).forEach(function setProp(key) {
if (reservedPropertyNames.indexOf(key) !== -1) {
return;
}
Object.defineProperty(branch, key, {
value: keyOrProps[key],
configurable: false,
enumerable: true,
writable: false
});
});
}
timestamp(branch);
Object.defineProperty(branch, "version", {
value: numVersions,
configurable: false,
enumerable: true,
writable: false
});
numVersions += 1;
var branches = [];
var branchesView = Object.freeze(branches.slice());
function addBranchAndUpdateView(branch) {
branches.push(branch);
branchesView = Object.freeze(branches.slice());
}
Object.defineProperty(branch, "branches", {
configurable: false,
enumerable: true,
get: function getBranches() {
return branchesView;
}
});
Object.defineProperty(branch, "branch", {
configurable: false,
enumerable: false,
writable: false,
value: Object.freeze(branchFrom(branch, addBranchAndUpdateView))
});
Object.freeze(branch);
addBranch(branch);
root.currentBranch = branch;
return branch;
};
}
Object.defineProperty(root, "numVersions", {
configurable: false,
enumerable: true,
get: function getNumVersions() {
return numVersions;
}
});
var 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);
var obj = branch;
while (obj !== root) {
Object.keys(obj).forEach(setViewProp);
obj = Object.getPrototypeOf(obj);
}
}
currentBranch = branch;
emit();
return currentBranch;
}
});
var rootBranches = [];
var branchesView = Object.freeze(rootBranches.slice());
function addBranchAndUpdateView(branch) {
rootBranches.push(branch);
branchesView = Object.freeze(rootBranches.slice());
}
Object.defineProperty(root, "branch", {
configurable: false,
enumerable: false,
writable: false,
value: Object.freeze(branchFrom(root, addBranchAndUpdateView))
});
Object.defineProperty(root, "branches", {
configurable: false,
enumerable: true,
get: function getBranches() {
return branchesView;
}
});
Object.freeze(root);
return root;
}
function walkBranches(source, sources, parents, branch) {
source.branches.forEach(function walkBranch(child) {
var childVersion = child.version;
sources[childVersion] = child;
parents[childVersion] = branch;
});
}
function fromParsedJSON(obj) {
var root = io();
var versions = [];
versions.length = obj.numVersions;
var parents = Array.apply(null, versions);
var sources = Array.apply(null, versions);
walkBranches(obj, sources, parents, root);
var branches = sources.map(function importBranch(source) {
var 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) {
var 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;