forked from basiliskjs/basilisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basilisk.js
641 lines (529 loc) · 21.5 KB
/
basilisk.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
// Basilisk 0.1
// A simple library for immutable state.
//
// (c) 2012 Moo Print Ltd. under an MIT license
// see http://github.com/moodev/basilisk
(function (root, factory) {
if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
module.exports = factory(require('underscore'));
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['underscore'], factory);
} else {
// Browser globals
root.basilisk = factory(root._);
}
}(this, function (_) {
"use strict";
// bind to window or global, depending on environment.
var b, basilisk = b = {};
if (!_) {
throw "Basilisk depends on underscore: please ensure that underscore is loaded first.";
}
// TODO we log watcher-failure and a few other events - they are considered
// "broken" behaviours.
var log = function () { };
if (window && window.console && window.console.log) {
log = function () { window.console.log.apply(window.console, arguments); }
}
// Definitions
// -----------
//
// Helpers for class definition:
// whether defineProperty is available and functions according to ES5.1
var defineAvailable = false,
defineProperty = Object.defineProperty,
_eg = {};
// IE8 has a badly implemented version of defineProperty, so function detection
// is insufficient.
try {
Object.defineProperty(_eg, 'x', {value:13, writable:false});
try { _eg.x = 14; } catch (e2) {}
defineAvailable = (_eg.x === 13);
} catch (e) {
defineAvailable = false;
}
if (defineAvailable) {
Object.defineProperty(basilisk, 'isStrict', {
value: true,
writable: false,
enumerable: true
});
} else {
basilisk.isStrict = false;
}
// Struct
// ------
//
// Define a constructor for a structure: this is very similar to Python's
// namedtuple module. The resulting objects are immutable, and have very simple
// "withX" methods to allow you to create new objects based on old definitions.
b.struct = function (properties) {
var constructor,
propList = _.extend({}, properties);
// cannot have a property called with_ in the list.
if (properties['with_']) {
throw "You cannot have a property called with_ in your list: this is used within the Basilisk defition system.";
}
// generate the actual contructor.
if (!defineAvailable) {
constructor = function (sample) {
var self = this,
sample = sample || {},
origValues = _.extend({}, sample);
// microoptimisation
if (sample instanceof constructor) {
return sample;
}
if (!(self instanceof constructor)) {
throw "Object must be instantiated with new.";
}
// we don't actually have any guarantee of immutability
// in this scenario: we will have to rely on failure modes
// being caught in modern browsers.
_.each(propList, function (value, key) {
var valueFn = value.filter || _.identity;
self[key] = valueFn(sample[key] || undefined);
});
};
} else {
// create a fully immutable object chain.
constructor = function (sample) {
var self = this,
sample = sample || {};
if (sample instanceof constructor) {
return sample;
}
if (!(self instanceof constructor)) {
throw "Object must be instantiated with new.";
}
_.each(propList, function (value, key) {
var valueFn = value.filter || _.identity;
Object.defineProperty(self, key, {
value: valueFn(sample[key] || undefined),
writable: false,
enumerable: true
});
});
};
}
// Create a new version of the object, with the specified "adjusted"
// properties set to their specified values (and un-specified properties
// set to their value in the current object.)
constructor.prototype.with_ = function (adjustProperties) {
// *we* are a perfect reference sample, since defineProperty
// ensures immutability.
var self = this,
sample = {},
changed = false;
_.each(propList, function (value, key) {
var valueFn = value.filter || undefined,
strictEqual = value.strictEqual || undefined,
rawValue,
value;
if (Object.hasOwnProperty.call(adjustProperties, key)) {
rawValue = adjustProperties[key];
if (valueFn) {
value = valueFn(rawValue);
} else {
value = rawValue;
}
if (strictEqual) {
changed = changed || !strictEqual(value, self[key]);
} else {
changed = changed || (value !== self[key]);
}
sample[key] = value;
} else {
sample[key] = self[key];
}
});
if (!changed) { return self; }
return new constructor(sample);
}
// add a properties object, with the original properties defined.
constructor._properties = function () { return propList.slice(0, propList.length); };
// TODO: handle definitions for complex objects.
_.each(propList, function (value, key) {
var withLabel = key;
withLabel = withLabel[0].toUpperCase() + withLabel.slice(1, withLabel.length);
withLabel = 'with' + withLabel;
constructor.prototype[withLabel] = function (value) {
var adjust = {};
adjust[key] = value;
return this.with_(adjust);
}
});
return constructor;
};
// Definitions (backwards compatibility)
b.definitions = {};
basilisk.definitions.makeConstructor = b.struct;
// some very simple property objects. the float value is particularly useful.
basilisk.properties = {
float: {
strictEquals: function (a, b) {
return Math.abs(a - b) < 0.00001;
}
}
}
// Standard Watcher creation functions
// ----
//
// A *lot* of watching boils down to some very simple patterns:
// - watch only when a specified property changes
// - watch only when a specified property (on a path) changes
// - watch for a change in a particular key in a dictionary.
//
// Rather than leave a lot of boilerplate lying around (and hopefully to allow
// for optimisation and collapsing of functions), you can use the creation functions
// below here to create new functions which can be used as watchers.
b.watchers = {};
// TODO these are very much first-pass implementations: replace with something
// substantially more clever.
// Watcher function for a dotted path inside a particular atom.
// will call the "watcher" function with the "stemmed" properties
// @return fn watching a particular path.
b.watchers.path = function (path, watcher) {
// TODO refactor parts to use a ForwardList
var parts = path.split('.'),
// function to extract the specified path from the root.
// WILL mutate the parts parameter.
recur = function (parts, newVal, oldVal) {
var first = parts.shift(),
newRest, oldRest;
// if newval === oldval, there's no change so no chance of a watch.
if (newVal === oldVal) {
return;
}
if (oldVal !== undefined && _.has(oldVal, first)) {
oldRest = oldVal[first];
};
if (newVal !== undefined && _.has(newVal, first)) {
newRest = newVal[first];
}
if (parts.length == 0) {
// this is the leaf: if we see a difference in value,
// apply it.
if (newRest !== oldRest) {
watcher(newRest, oldRest);
}
} else {
recur(parts, newRest, oldRest);
}
};
return function (newVal, oldVal) {
recur(parts.slice(0, parts.length), newVal, oldVal);
}
}
// Utility datastructures/objects.
// ----
b.utils = {}
b.utils.Path = function (path) {
var parts = b.collections.ForwardList.from(path.split('.'));
// keep the actual parts immutable.
this.parts = function () { return parts; };
}
_.extend(b.utils.Path.prototype, {
/**
* Consume a particular struct.
*
* @param struct the basilisk.struct to consume
* @param tolerant (true) tolerate the property not being defined, or the struct being undefined
* @return the value at the end of the struct, or undefined.
*/
consume: function (struct, tolerant) {
var node = struct,
tolerant = !!tolerant;
if (arguments.length === 1) {
tolerant = true;
}
if (struct === undefined && !tolerant) {
throw "Cannot root on undefined struct when not tolerant.";
}
this.parts().each(function (part) {
if (node === undefined) { return; }
if (!tolerant && !node.hasOwnProperty(part)) {
throw "Cannot find part: " + part;
}
node = node[part];
});
return node;
},
/**
* Derive a new struct from the provided one struct, the current path, and the provided
* value to be set at the end of the path. Relies on each node having with_. Is *not*
* tolerant of a particular path not being present.
*
* @param struct the structure to derive from.
* @param value the value to set at the end of the structure.
* @param a struct derived from the original, with value set at the end of path.
*/
replace: function (struct, value) {
// TODO this recursive definition should work fine, provided that
// the stack depth is acceptable. Might be an issue on older IE's and Safari's.
var reverse = b.collections.ForwardList.from([]),
node = struct;
this.parts().each(function (part) {
reverse = reverse.shift([part, node]);
if (!node.hasOwnProperty(part)) {
throw "Cannot follow path: node lacks " + part;
}
node = node[part];
});
// we can now set the value, and then consume in reverse.
node = value;
reverse.each(function (step) {
var part = step[0],
parent = step[1],
desc = {};
desc[part] = node;
node = parent.with_(desc);
});
return node;
},
/**
* Apply the given change function to the value at the end of the path on
* the structure, and then return a new structure derived from the provided one.
*
* @param struct the structure from which to derive.
* @param changeFn a function to apply: the first parameter will be the value, the rest will
* be varags.
* @return a new structure, with changes applied by with_
*/
swap: function (struct, changeFn) {
var current = this.consume(struct, false),
changeArgs = [current];
// merge any further arguments.
changeArgs.push.apply(changeArgs, Array.prototype.slice.call(arguments, 2));
return this.replace(struct, changeFn.apply(undefined, changeArgs));
}
})
// Collections
// -----------
//
// Very simple collection objects. Not currently optimised for performance,
// though that would not be exceptionally difficult to accomplish.
b.collections = {};
// we routinely have methods that are called once, with no arguments.
// (eg. length, etc.). Since we're immutable, as long as we cache the result
// on "this", we can easily make ourselves perpetually cached.
var oncePerInstance = function (method, key) {
// we store our value on "this", but with a long (probably unique) key...
var storageKey = key || _.uniqueId('__basilisk-fn-oncePerInstance');
return function () {
var value;
if (arguments.length > 0) {
throw "Cannot supply any arguments to a once-per-instance function.";
}
if (!this.hasOwnProperty(storageKey)) {
value = method.apply(this, []);
if (defineAvailable) {
Object.defineProperty(this, storageKey, {
value: value,
writable: false,
enumerable: false
});
} else {
this[storageKey] = value;
}
}
return this[storageKey];
}
}
// define basic datastructures for forward list (singly linked list)
_.extend(b.collections, {
ForwardListNode: basilisk.definitions.makeConstructor({
rest: { noWith: true },
value: {}
}),
// A simple list, with O(1) shift and unshift. Suitable for simple stacks.
ForwardList: basilisk.definitions.makeConstructor({
head: {}
})
});
b.collections.ForwardListNode.prototype.length = oncePerInstance(function () {
if (this.rest === undefined) {
return 1;
} else {
return 1 + this.rest.length();
}
}, '_length');
// Instance methods on ForwardList.
_.extend(b.collections.ForwardList.prototype, {
// create a node with the specified value, and return a list with that as its head.
// this is a little more expensive here, but it makes iteration in the empty case
// simple.
shift: function (value) {
var self = this;
return new b.collections.ForwardList({
head: new b.collections.ForwardListNode({
rest: self.head,
value: value
})
});
},
length: oncePerInstance(function () {
if (this.head === undefined) {
return 0;
} else {
return this.head.length();
}
}, '_length'),
// return a list without the head node,
unshift: function () {
var self = this,
rest = undefined;
if (self.head) {
rest = self.head.rest;
} else {
return self;
}
return [this.head.value, new b.collections.ForwardList({
head: rest
})];
},
each: function (iterator, context) {
var idx = 0,
next = this.head;
while (next !== undefined && next !== null) {
iterator.apply(context, [next.value, idx, this, next]);
idx += 1;
next = next.rest;
}
},
/**
* Return a new ForwardList containing only those elements of this list for which
* the iterator function returns a truthy value.
*
* @param filterFn a function taking (value, index, list, node) -> boolean
* @return ForwardList
*/
filter: function (filterFn, context)
{
var values = [],
changedValue = false;
this.each(function (value, idx, list, node) {
if (filterFn.apply(this, arguments)) {
values.push(value);
} else {
changedValue = true;
}
});
if (!changedValue) {
return this;
} else {
return b.collections.ForwardList.from(values);
}
},
/**
* Returns the first value that matches the specified filter function.
*/
first: function (filterFn, context) {
var idx = 0,
filterFn = filterFn || function () { return true; },
next = this.head;
while (next !== undefined && next !== null) {
if (filterFn.apply(context, [next.value, idx, this, next])) {
return next.value;
}
idx += 1;
next = next.rest;
}
return undefined;
}
});
// Factory method: given an array-like object or a forward list, return a forward list.
b.collections.ForwardList.from = function (source) {
if (source instanceof b.collections.ForwardList) {
return source;
} else {
return _.reduceRight(source, function (memo, value) {
return memo.shift(value);
}, new b.collections.ForwardList({ head: null }));
}
}
// Atoms provide a way to manage shared, synchronous, independent state.
// http://clojure.org/atoms
//
// If supplied the validator must be a function (new, old) {} -> boolean,
// returning true if the changed value is valid, and false if not.
//
// the value must be immutable.
basilisk.Atom = function (initialValue, validator) {
if (!(this instanceof basilisk.Atom)) { throw "Please instantiate each atom separately." }
// Most state is held in closure, and thus can only be changed by these
// privileged methods.
var current,
self = this,
// TODO replace with a FixedVector.
watchers = b.collections.ForwardList.from([]),
// set the value if it passes the validator.
_set = function (possible) {
var old = current;
if (possible !== current) {
if (!validator(possible, current)) {
throw "Value does not pass validator.";
}
current = possible;
watchers.each(function (watcher) {
try {
watcher(current, old);
} catch (e) {
// do nothing
log("Watcher threw exception: this behaviour is broken.");
}
});
}
return current;
};
validator = validator || function () { return true; };
// we fail immediately if the initial value does not validate.
_.extend(self, {
// Retrieve the current value of the Atom.
deref: function () { return current; },
// Applies the given "change" function to the old state,
// checks that the result passes the validator, and updates the current state.
//
// Will be called as swapFunction(old, *args); The function should have no side-effects.
//
// Returns the new state or throws an exception.
swap: function (swapFunction) {
var possible, args = [current];
if (arguments.length > 1) {
args.push.apply(args, _.rest(argument));
}
possible = swapFunction.apply(null, args);
// update current, call watchers
return this.compareAndSet(current, possible);
},
// If oldVal matches current, change to using newVal.
// return true if changed, false if not changed.
compareAndSet: function (oldVal, newVal) {
if (oldVal === current) {
return (_set(newVal) === newVal);
} else {
return false;
}
},
// add a watcher. Will be called if the value changes.
addWatcher: function (watcher) {
if (watchers.first(function (compare) { return compare === watcher; }) === undefined) {
watchers = watchers.shift(watcher);
}
},
removeWatcher: function (watcher) {
watchers = watchers.filter(function (compare) { return compare !== watcher; });
},
watchers: function () { return watchers },
});
// shorter alias for cas.
this.cas = this.compareAndSet;
// set the initial state.
_set(initialValue);
};
return basilisk;
}));