-
Notifications
You must be signed in to change notification settings - Fork 25
/
binders.js
459 lines (426 loc) · 19 KB
/
binders.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
var Scope = require("./scope");
var Observers = require("./observers");
var autoCancelPrevious = Observers.autoCancelPrevious;
var observeRangeChange = Observers.observeRangeChange;
var cancelEach = Observers.cancelEach;
var makeNotObserver = Observers.makeNotObserver;
var makeOrObserver = Observers.makeOrObserver;
var makeAndObserver = Observers.makeAndObserver;
var observeValue = Observers.observeValue;
var observeUndefined = Observers.makeLiteralObserver();
var trueScope = new Scope(true);
var falseScope = new Scope(false);
function getStackTrace() {
return new Error("").stack.replace(/^.*\n.*\n/, "\n");
}
exports.bindProperty = bindProperty;
var _bindProperty = bindProperty; // to bypass scope shadowing problems below
function bindProperty(object, key, observeValue, source, descriptor, trace) {
return observeValue(autoCancelPrevious(function replaceBoundPropertyValue(value) {
if (descriptor.isActive) {
return;
}
try {
descriptor.isActive = true;
trace && console.log("SET", trace.targetPath, "TO", value, "ON", object, "BECAUSE", trace.sourcePath, getStackTrace());
if (Array.isArray(object) && key >>> 0 === key) {
// TODO spec this case
object.set(key, value);
} else {
object[key] = value;
}
} finally {
descriptor.isActive = false;
}
}), source);
}
exports.makePropertyBinder = makePropertyBinder;
function makePropertyBinder(observeObject, observeKey) {
return function bindProperty(observeValue, source, target, descriptor, trace) {
return observeKey(autoCancelPrevious(function replaceKey(key) {
if (key == null) return;
function replaceObject(object) {
return (object == null)
? void 0
: (object.bindProperty)
? object.bindProperty(key, observeValue, source, descriptor, trace)
: replaceObject.bindProperty(object, key, observeValue, source, descriptor, trace);
};
replaceObject.bindProperty = _bindProperty;
return observeObject(autoCancelPrevious(replaceObject), target);
}), target);
};
}
exports.bindGet = bindGet;
var _bindGet = bindGet; // to bypass scope shadowing below
function bindGet(collection, key, observeValue, source, descriptor, trace) {
return observeValue(autoCancelPrevious(function replaceValue(value) {
if (descriptor.isActive) {
return;
}
try {
descriptor.isActive = true;
trace && console.log("SET FOR KEY", key, "TO", value, "ON", collection, "BECAUSE", trace.sourcePath, getStackTrace());
collection.set(key, value);
} finally {
descriptor.isActive = false;
}
}), source);
}
exports.makeGetBinder = makeGetBinder;
function makeGetBinder(observeCollection, observeKey) {
return function bindGet(observeValue, source, target, descriptor, trace) {
return observeCollection(autoCancelPrevious(function replaceCollection(collection) {
if (!collection) return;
function replaceKey(key) {
return (key == null)
? void 0
: replaceKey.bindGet(collection, key, observeValue, source, descriptor, trace);
};
replaceKey.bindGet = _bindGet;
return observeKey(autoCancelPrevious(replaceKey), target);
}), target);
};
}
exports.makeHasBinder = makeHasBinder;
function makeHasBinder(observeSet, observeValue) {
return function bindHas(observeHas, source, target, descriptor, trace) {
return observeSet(autoCancelPrevious(function replaceHasBindingSet(set) {
if (!set) return;
return observeValue(autoCancelPrevious(function replaceHasBindingValue(value) {
if (value == null) return;
function changeWhetherSetHas(has) {
// wait for the initial value to be updated by the
// other-way binding
if (has) { // should be in set
if (!changeWhetherSetHas.set.has(value)) {
trace && console.log("ADD", value, "TO", trace.targetPath, "BECAUSE", trace.sourcePath, getStackTrace());
changeWhetherSetHas.set.add(value);
}
} else { // should not be in set
while (changeWhetherSetHas.set.has(value)) {
trace && console.log("REMOVE", value, "FROM", trace.targetPath, "BECAUSE", trace.sourcePath, getStackTrace());
changeWhetherSetHas.set.delete(value);
}
}
};
changeWhetherSetHas.set = set;
return observeHas(autoCancelPrevious(changeWhetherSetHas), source);
}), target);
}), target);
};
}
// a == b <-> c
exports.makeEqualityBinder = makeEqualityBinder;
function makeEqualityBinder(bindLeft, observeRight) {
return function bindEquals(observeEquals, source, target, descriptor, trace) {
// c
return observeEquals(autoCancelPrevious(function changeWhetherEquals(equals) {
if (equals) {
trace && console.log("BIND", trace.targetPath, "TO", trace.sourcePath, getStackTrace());
// a <-> b
var cancel = bindLeft(observeRight, source, source, descriptor, trace);
return function cancelEqualityBinding() {
trace && console.log("UNBIND", trace.targetPath, "FROM", trace.sourcePath, getStackTrace());
};
}
}), target);
};
}
// collection.every{condition} <- everyCondition
exports.makeEveryBlockBinder = makeEveryBlockBinder;
function makeEveryBlockBinder(observeCollection, bindCondition, observeValue) {
return function bindEveryBlock(observeEveryCondition, source, target, descriptor, trace) {
return observeEveryCondition(autoCancelPrevious(function replaceCondition(condition) {
if (!condition) return;
return observeCollection(autoCancelPrevious(function replaceCollection(collection) {
if (!collection) return;
var cancelers = [];
function rangeChange(plus, minus, index) {
rangeChange.cancelEach(cancelers.slice(index, index + minus.length));
var plusMapped = [];
for(var i=0, countI = plus.length, scope;i<countI;i++) {
scope = target.nest(plus[i]);
plusMapped.push(rangeChange.bindCondition(observeValue, scope, scope, descriptor, trace));
}
cancelers.swap(index, minus.length, plusMapped);
};
rangeChange.cancelEach = cancelEach;
rangeChange.bindCondition = bindCondition;
var cancelRangeChange = observeRangeChange(collection, rangeChange, target);
return function cancelEveryBinding() {
cancelEach(cancelers);
cancelRangeChange();
};
}), target);
}), source);
};
};
exports.makeAndBinder = makeAndBinder;
function makeAndBinder(bindLeft, bindRight, observeLeft, observeRight, observeLeftBind, observeRightBind) {
var observeNotRight = makeNotObserver(observeRight);
var observeLeftAndNotRight = makeAndObserver(observeLeft, observeNotRight);
return function bindEveryBlock(observeAndCondition, source, target, descriptor, trace) {
return observeAndCondition(autoCancelPrevious(function replaceAndCondition(condition) {
if (condition == null) {
} else if (condition) {
var cancelLeft = bindLeft(observeLeftBind, trueScope, target, descriptor, trace);
var cancelRight = bindRight(observeRightBind, trueScope, target, descriptor, trace);
return function cancelAndBinding() {
cancelLeft();
cancelRight();
};
} else {
return bindLeft(observeLeftAndNotRight, target, target, descriptor, trace);
}
}), source);
};
}
exports.makeOrBinder = makeOrBinder;
function makeOrBinder(bindLeft, bindRight, observeLeft, observeRight, observeLeftBind, observeRightBind) {
var observeNotRight = makeNotObserver(observeRight);
var observeLeftOrNotRight = makeOrObserver(observeLeft, observeNotRight);
return function bindEveryBlock(observeOrCondition, source, target, descriptor, trace) {
return observeOrCondition(autoCancelPrevious(function replaceOrCondition(condition) {
if (condition == null) {
} else if (!condition) {
var cancelLeft = bindLeft(observeLeftBind, falseScope, target, descriptor, trace);
var cancelRight = bindRight(observeRightBind, falseScope, target, descriptor, trace); return function cancelOrBinding() {
cancelLeft();
cancelRight();
};
} else {
return bindLeft(observeLeftOrNotRight, target, target, descriptor, trace);
}
}), source);
};
}
// (a ? b : c) <- d
exports.makeConditionalBinder = makeConditionalBinder;
function makeConditionalBinder(observeCondition, bindConsequent, bindAlternate) {
return function bindCondition(observeSource, source, target, descriptor, trace) {
// a
return observeCondition(autoCancelPrevious(function replaceCondition(condition) {
if (condition == null) return;
if (condition) {
// b <- d
return bindConsequent(observeSource, source, target, descriptor, trace);
} else {
// c <- d
return bindAlternate(observeSource, source, target, descriptor, trace);
}
}), source);
};
}
// collection.only() <- value
exports.makeOnlyBinder = makeOnlyBinder;
function makeOnlyBinder(observeCollection) {
return function bindOnly(observeValue, sourceScope, targetScope, descriptor, trace) {
return observeCollection(autoCancelPrevious(function replaceCollection(collection) {
if (!collection) return;
return observeValue(autoCancelPrevious(function replaceOnlyValue(value) {
if (value == null) return;
if (collection.splice) {
collection.splice(0, collection.length, value);
} else if (collection.clear && collection.add) {
collection.clear();
collection.add(value);
}
}), sourceScope);
}), targetScope);
};
}
// collection.one() <- value
// empty the collection and set its one item to value
exports.makeOneBinder = makeOneBinder;
function makeOneBinder(observeCollection) {
return function bindOne(observeValue, sourceScope, targetScope, descriptor, trace) {
return observeCollection(autoCancelPrevious(function replaceCollection(collection) {
if (!collection) return;
return observeValue(autoCancelPrevious(function replaceOneValue(value) {
if (value == null) return;
// FIXME: this is debatable. If set to its current value, do we clear the rest of the collection?
// I err towards no, in part because this solves the case of two-way bindings, where we were always
// clearing out the source collection. That could be solved other ways without this little quirk.
if (value === collection.one()) return;
if (collection.splice) {
collection.splice(0, collection.length, value);
} else if (collection.clear && collection.add) {
collection.clear();
collection.add(value);
}
}), sourceScope);
}), targetScope);
};
}
// a.rangeContent() <- b
exports.makeRangeContentBinder = makeRangeContentBinder;
function makeRangeContentBinder(observeTarget, bindTarget) {
return function bindRangeContent(observeSource, sourceScope, targetScope, descriptor, trace) {
return observeTarget(autoCancelPrevious(function replaceRangeContentTarget(target) {
if (!target) {
return bindTarget(
Observers.makeLiteralObserver([]),
sourceScope,
targetScope,
descriptor,
trace
);
}
return observeSource(autoCancelPrevious(function replaceRangeContentSource(source) {
if (source === target) {
return;
}
if (!source) {
target.clear();
return;
}
if (!source.addRangeChangeListener) {
return;
}
function rangeContentSourceRangeChange(plus, minus, index) {
if (isActive(target))
return;
if (trace) {
console.log("SWAPPING", minus, "FOR", plus, "AT", index, "ON", trace.targetPath, getStackTrace());
}
if (target.swap) {
target.swap(index, minus.length, plus);
} else if (target.add && (target.remove || target["delete"])) {
var i, countI, item, deleteMethod;
for(i=0, countI = plus.length, item;(item = plus[i]); i++) {
target.add(item);
}
deleteMethod = target.remove || target["delete"];
for(i=0, countI = minus.length, item;(item = minus[i]); i++) {
target.deleteMethod(item);
}
}
}
source.addRangeChangeListener(rangeContentSourceRangeChange);
rangeContentSourceRangeChange(Array.from(source), Array.from(target), 0);
return function cancelRangeContentBinding() {
source.removeRangeChangeListener(rangeContentSourceRangeChange);
};
}), sourceScope);
}), targetScope);
};
}
exports.makeMapContentBinder = makeMapContentBinder;
function makeMapContentBinder(observeTarget) {
return function bindMapContent(observeSource, source, target, descriptor, trace) {
return observeTarget(autoCancelPrevious(function replaceMapContentBindingTarget(target) {
if (!target) return;
return observeSource(autoCancelPrevious(function replaceMapContentBindingSource(source) {
if (!source) {
target.clear();
return;
}
function mapChange(value, key) {
if (descriptor.isActive) {
return;
}
try {
descriptor.isActive = true;
if (value === undefined) {
if (trace) {
trace && console.log("DELETED", trace.targetPath, "FOR KEY", key, "ON", target, "BECAUSE", trace.sourcePath, getStackTrace());
}
if (Array.isArray(target)) {
target.splice(key, 1);
} else {
target["delete"](key);
}
} else {
if (trace) {
trace && console.log("SET", trace.targetPath, "FOR KEY", key, "TO", value, "ON", target, "BECAUSE", trace.sourcePath, getStackTrace());
}
target.set(key, value);
}
} finally {
descriptor.isActive = false;
}
}
target.clear();
source.forEach(mapChange);
return source.addMapChangeListener(mapChange);
}), source);
}), target);
};
}
// a.reversed() <-> b
exports.makeReversedBinder = makeReversedBinder;
function makeReversedBinder(observeTarget) {
return function bindReversed(observeSource, source, target, descriptor, trace) {
return observeTarget(autoCancelPrevious(function replaceReversedBindingTarget(target) {
if (!target) return;
return observeSource(autoCancelPrevious(function replaceReversedBindingSource(source) {
if (!source) {
target.clear();
return;
}
function rangeChange(plus, minus, index) {
if (isActive(target))
return;
var reflected = target.length - index - minus.length;
target.swap(reflected, minus.length, plus.reversed());
}
source.addRangeChangeListener(rangeChange);
rangeChange(source, target, 0);
return function cancelReversedBinding() {
source.removeRangeChangeListener(rangeChange);
};
}), source);
}), target);
};
}
exports.makeDefinedBinder = makeDefinedBinder;
function makeDefinedBinder(bindTarget) {
return function bindReversed(observeSource, sourceScope, targetScope, descriptor, trace) {
return observeSource(autoCancelPrevious(function replaceSource(condition) {
if (!condition) {
return bindTarget(
observeUndefined,
sourceScope,
targetScope,
descriptor,
trace
);
} else {
return Function.noop;
}
}), targetScope);
}
}
exports.makeParentBinder = makeParentBinder;
function makeParentBinder(bindTarget) {
return function bindParent(observeSource, sourceScope, targetScope, descriptor, trace) {
if (!targetScope.parent) {
return;
}
return bindTarget(observeSource, sourceScope, targetScope.parent, descriptor, trace);
};
}
exports.makeWithBinder = makeWithBinder;
function makeWithBinder(observeTarget, bindTarget) {
return function bindWith(observeSource, sourceScope, targetScope, descriptor, trace) {
return observeTarget(autoCancelPrevious(function replaceTarget(target) {
if (target == null) {
return;
}
return bindTarget(
observeSource,
sourceScope,
targetScope.nest(target),
descriptor,
trace
);
}), targetScope);
};
}
function isActive(target) {
return (
target.getRangeChangeDescriptor &&
target.getRangeChangeDescriptor().isActive
);
}