-
Notifications
You must be signed in to change notification settings - Fork 2
/
events.js
1146 lines (937 loc) · 28.4 KB
/
events.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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// TurtleScript FRS-style event system. Heavily based on Flapjax, and
// as such it is:
/*
* Copyright (c) 2006-2009, The Flapjax Team. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the Brown University, the Flapjax Team, nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
define(["text!events.js", "!timeouts"], function make_flapjax(events_source, timeouts) {
var setTimeout = timeouts.setTimeout;
var clearTimeout = timeouts.clearTimeout;
var slice = function(arr, start, stop) {
return Array.prototype.slice.call(arr, start, stop);
};
var zip = function(arrays) {
var ret = [], i = 0;
if (arrays.length > 0) {
while (i < arrays[0].length) {
ret.push([]);
arrays.forEach(function(arr) {
ret[i].push(arr[i]);
});
i += 1;
}
}
return ret;
};
//map: (a * ... -> z) * [a] * ... -> [z]
var map = function (fn) {
var arrays = slice(arguments, 1);
var ret = [];
if (arrays.length === 1) {
arrays[0].forEach(function(e) {
ret.push(fn(e));
});
} else if (arrays.length > 1) {
var o = {};
zip(arrays).forEach(function(args) {
ret.push(fn.apply(o, args));
});
}
return ret;
};
//filter: (a -> Boolean) * Array a -> Array a
var filter = function (predFn, arr) {
var res = [];
arr.forEach(function(elem) {
if (predFn(elem)) { res.push(elem); }
});
return res;
};
//////////////////////////////////////////////////////////////////////////////
// Flapjax core
// Sentinel value returned by updaters to stop propagation.
var doNotPropagate = { };
//Pulse: Stamp * Path * Obj
var Pulse = function (stamp, value) {
// Timestamps are used by liftB (and ifE). Since liftB may receive multiple
// update signals in the same run of the evaluator, it only propagates the
// signal if it has a new stamp.
this.stamp = stamp;
this.value = value;
};
//Probably can optimize as we expect increasing insert runs etc
// XXX csa: replace Math.floor with >>1
var PQ = function () {
var ctx = this;
ctx.val = [];
this.insert = function (kv) {
ctx.val.push(kv);
var kvpos = ctx.val.length-1;
while(kvpos > 0 && kv.k < ctx.val[Math.floor((kvpos-1)/2)].k) {
var oldpos = kvpos;
kvpos = Math.floor((kvpos-1)/2);
ctx.val[oldpos] = ctx.val[kvpos];
ctx.val[kvpos] = kv;
}
};
this.isEmpty = function () {
return ctx.val.length === 0;
};
this.pop = function () {
if(ctx.val.length === 1) {
return ctx.val.pop();
}
var ret = ctx.val.shift();
ctx.val.unshift(ctx.val.pop());
var kvpos = 0;
var kv = ctx.val[0];
while(1) {
var leftChild = (kvpos*2+1 < ctx.val.length ? ctx.val[kvpos*2+1].k : kv.k+1);
var rightChild = (kvpos*2+2 < ctx.val.length ? ctx.val[kvpos*2+2].k : kv.k+1);
if(leftChild > kv.k && rightChild > kv.k) {
break;
}
if(leftChild < rightChild) {
ctx.val[kvpos] = ctx.val[kvpos*2+1];
ctx.val[kvpos*2+1] = kv;
kvpos = kvpos*2+1;
}
else {
ctx.val[kvpos] = ctx.val[kvpos*2+2];
ctx.val[kvpos*2+2] = kv;
kvpos = kvpos*2+2;
}
}
return ret;
};
};
var lastRank = 0;
var stamp = 1;
var nextStamp = function () {
stamp += 1;
return stamp;
};
//propagatePulse: Pulse * Array Node ->
//Send the pulse to each node
var propagatePulse = function (pulse, node) {
var queue = PQ.New(); //topological queue for current timestep
queue.insert({k:node.rank,n:node,v:pulse});
var len = 1;
while (len) {
var qv = queue.pop();
len-=1;
var nextPulse = qv.n.updater(Pulse.New(qv.v.stamp, qv.v.value));
var weaklyHeld = true;
if (nextPulse !== doNotPropagate) {
qv.n.sendsTo.forEach(function(n) {
weaklyHeld = weaklyHeld && n.weaklyHeld;
len+=1;
queue.insert({k:n.rank,n:n,v:nextPulse});
});
if (qv.n.sendsTo.length > 0 && weaklyHeld) {
qv.n.weaklyHeld = true;
}
}
}
};
//Event: Array Node b * ( (Pulse a -> Void) * Pulse b -> Void)
var EventStream = function (nodes,updater) {
this.updater = updater;
this.sendsTo = []; //forward link
this.weaklyHeld = false;
nodes.forEach(function(n) {
n.attachListener(this);
}, this);
lastRank += 1;
this.rank = lastRank;
};
EventStream.prototype = {};
//createNode: Array Node a * ( (Pulse b ->) * (Pulse a) -> Void) -> Node b
var createNode = function (nodes, updater) {
return EventStream.New(nodes,updater);
};
//note that this creates a new timestamp and new event queue
var sendEvent = function (node, value) {
if (!EventStream.hasInstance(node)) {
Object.Throw('sendEvent: expected Event as first arg');
} //SAFETY
propagatePulse(Pulse.New(nextStamp(), value),node);
};
var genericAttachListener = function(node, dependent) {
node.sendsTo.push(dependent);
if(node.rank > dependent.rank) {
var lowest = lastRank+1;
var q = [dependent];
while(q.length) {
var cur = q.splice(0,1)[0];
lastRank += 1;
cur.rank = lastRank;
q = q.concat(cur.sendsTo);
}
}
};
var genericRemoveListener = function (node, dependent, isWeakReference) {
var foundSending = false;
var i = 0;
while( i < node.sendsTo.length && !foundSending ) {
if (node.sendsTo[i] === dependent) {
node.sendsTo.splice(i, 1);
foundSending = true;
}
i += 1;
}
if (isWeakReference === true && node.sendsTo.length === 0) {
node.weaklyHeld = true;
}
return foundSending;
};
//attachListener: Node * Node -> Void
//flow from node to dependent
//note: does not add flow as counting for rank nor updates parent ranks
EventStream.prototype.attachListener = function(dependent) {
if (!EventStream.hasInstance(dependent)) {
Object.Throw('attachListener: expected an EventStream');
}
genericAttachListener(this, dependent);
};
//note: does not remove flow as counting for rank nor updates parent ranks
EventStream.prototype.removeListener = function (dependent, isWeak) {
if (!EventStream.hasInstance(dependent)) {
Object.Throw('removeListener: expected an EventStream');
}
genericRemoveListener(this, dependent, isWeak);
};
// An internalE is a node that simply propagates all pulses it receives. It's used internally by various
// combinators.
var internalE = function(dependsOn) {
return createNode(dependsOn || [ ],function(pulse) { return pulse; });
};
var zeroE = function() {
return createNode([],function(pulse) {
Object.Throw ('zeroE : received a value; zeroE should not receive a value; the value was ' + pulse.value);
});
};
var oneE = function(val) {
var sent = false;
var evt = createNode([],function(pulse) {
if (sent) {
Object.Throw ('oneE : received an extra value');
}
sent = true;
return pulse;
});
setTimeout(function() { sendEvent(evt,val); },0);
return evt;
};
// a.k.a. mplus; mergeE(e1,e2) == mergeE(e2,e1)
var mergeE = function() {
if (arguments.length === 0) {
return zeroE();
}
else {
var deps = slice(arguments,0);
return internalE(deps);
}
};
EventStream.prototype.mergeE = function() {
var deps = slice(arguments,0);
deps.push(this);
return internalE(deps);
};
EventStream.prototype.constantE = function(constantValue) {
return createNode([this],function(pulse) {
pulse.value = constantValue;
return pulse;
});
};
var constantE = function(e,v) { return e.constantE(v); };
//This is up here so we can add things to its prototype that are in flapjax.combinators
var Behavior = function (event, init, updater) {
if (!EventStream.hasInstance(event)) {
Object.Throw ('Behavior: expected event as second arg');
}
var behave = this;
this.last = init;
//sendEvent to this might impact other nodes that depend on this event
//sendBehavior defaults to this one
this.underlyingRaw = event;
//unexposed, sendEvent to this will only impact dependents of this behaviour
this.underlying = createNode([event], updater
? function (p) {
behave.last = updater(p.value);
p.value = behave.last; return p;
}
: function (p) {
behave.last = p.value;
return p;
});
};
Behavior.prototype = {};
Behavior.prototype.valueNow = function() {
return this.last;
};
var valueNow = function(behavior) { return behavior.valueNow(); };
var constantB;
Behavior.prototype.changes = function() {
return this.underlying;
};
var changes = function (behave) { return behave.changes(); };
var receiverE = function() {
var evt = internalE();
evt.sendEvent = function(value) {
propagatePulse(Pulse.New(nextStamp(), value),evt);
};
return evt;
};
// bindE :: EventStream a * (a -> EventStream b) -> EventStream b
EventStream.prototype.bindE = function(k) {
/* m.sendsTo resultE
* resultE.sendsTo prevE
* prevE.sendsTo returnE
*/
var m = this;
var prevE = false;
var outE = createNode([],function(pulse) { return pulse; });
outE.name = "bind outE";
var inE = createNode([m], function (pulse) {
if (prevE) {
prevE.removeListener(outE, true);
}
prevE = k(pulse.value);
if (EventStream.hasInstance(prevE)) {
prevE.attachListener(outE);
}
else {
Object.Throw("bindE : expected EventStream");
}
return doNotPropagate;
});
inE.name = "bind inE";
return outE;
};
EventStream.prototype.mapE = function(f) {
if (!Function.hasInstance(f)) {
Object.Throw ('mapE : expected a function as the first argument; received ' + f);
}
return createNode([this],function(pulse) {
pulse.value = f(pulse.value);
return pulse;
});
};
EventStream.prototype.notE = function() { return this.mapE(function(v) { return !v; }); };
var notE = function(e) { return e.notE(); };
EventStream.prototype.filterE = function(pred) {
if (!Function.hasInstance(pred)) {
Object.Throw ('filterE : expected predicate; received ' + pred);
}
// Can be a bindE
return createNode([this], function(pulse) {
return pred(pulse.value) ? pulse : doNotPropagate;
});
};
var filterE = function(e,p) { return e.filterE(p); };
// Fires just once.
EventStream.prototype.onceE = function() {
var done = false;
// Alternately: this.collectE(0,\n v -> (n+1,v)).filterE(\(n,v) -> n == 1).mapE(fst)
return createNode([this],function(pulse) {
if (!done) { done = true; return pulse; }
else { return doNotPropagate; }
});
};
var onceE = function(e) { return e.onceE(); };
EventStream.prototype.skipFirstE = function() {
var skipped = false;
return createNode([this],function(pulse) {
if (skipped)
{ return pulse; }
else
{ return doNotPropagate; }
});
};
var skipFirstE = function(e) { return e.skipFirstE(); };
EventStream.prototype.collectE = function(init,fold) {
var acc = init;
return this.mapE(
function (n) {
var next = fold(n, acc);
acc = next;
return next;
});
};
var collectE = function(e,i,f) { return e.collectE(i,f); };
// a.k.a. join
EventStream.prototype.switchE = function() {
return this.bindE(function(v) { return v; });
};
var recE = function(fn) {
var inE = receiverE();
var outE = fn(inE);
outE.mapE(function(x) {
inE.sendEvent(x); });
return outE;
};
var switchE = function(e) { return e.switchE(); };
EventStream.prototype.ifE = function(thenE,elseE) {
var testStamp = -1;
var testValue = false;
createNode([this],function(pulse) { testStamp = pulse.stamp; testValue = pulse.value; return doNotPropagate; });
// XXX broken? CSA tried to fix..
return mergeE(createNode([thenE],function(pulse) { return (testValue && (testStamp === pulse.stamp)) ? pulse : doNotPropagate; }),
createNode([elseE],function(pulse) { return (!testValue && (testStamp === pulse.stamp)) ? pulse : doNotPropagate; }));
};
var ifE = function(test,thenE,elseE) {
if (EventStream.hasInstance(test))
{ return test.ifE(thenE,elseE); }
else
{ return test ? thenE : elseE; }
};
var andE = function (/* . nodes */) {
var nodes = slice(arguments, 0);
var acc = (nodes.length > 0)?
nodes[nodes.length - 1] : oneE(true);
var i = nodes.length - 2;
while (i >= 0) {
acc = ifE(
nodes[i],
acc,
nodes[i].constantE(false));
i -= 1;
}
return acc;
};
EventStream.prototype.andE = function( /* others */ ) {
var deps = [this].concat(slice(arguments,0));
return andE.apply(this,deps);
};
var orE = function () {
var nodes = slice(arguments, 0);
var acc = (nodes.length > 2)?
nodes[nodes.length - 1] : oneE(false);
var i = nodes.length - 2;
while (i >= 0) {
acc = ifE(
nodes[i],
nodes[i],
acc);
i -= 1;
}
return acc;
};
EventStream.prototype.orE = function(/*others*/) {
var deps = [this].concat(slice(arguments,0));
return orE.apply(this,deps);
};
var delayStaticE = function (event, time) {
var resE = internalE();
createNode([event], function (p) {
setTimeout(function () { sendEvent(resE, p.value);}, time );
return doNotPropagate;
});
return resE;
};
//delayE: Event a * [Behavior] Number -> Event a
EventStream.prototype.delayE = function (time) {
var event = this;
if (Behavior.hasInstance(time)) {
var receiverEE = internalE();
var link =
{
from: event,
towards: delayStaticE(event, valueNow(time))
};
//TODO: Change semantics such that we are always guaranteed to get an event going out?
var switcherE =
createNode(
[changes(time)],
function (p) {
link.from.removeListener(link.towards);
link =
{
from: event,
towards: delayStaticE(event, p.value)
};
sendEvent(receiverEE, link.towards);
return doNotPropagate;
});
var resE = receiverEE.switchE();
sendEvent(switcherE, valueNow(time));
return resE;
} else { return delayStaticE(event, time); }
};
var delayE = function(sourceE,interval) {
return sourceE.delayE(interval);
};
//mapE: ([Event] (. Array a -> b)) . Array [Event] a -> [Event] b
var mapE = function (fn /*, [node0 | val0], ...*/) {
// if (!Function.hasInstance(fn)) { Object.Throw ('mapE: expected fn as second arg'); } //SAFETY
var valsOrNodes = slice(arguments, 0);
//selectors[i]() returns either the node or real val, optimize real vals
var selectors = [];
var selectI = 0;
var nodes = [];
valsOrNodes.forEach(function(vn) {
if (EventStream.hasInstance(vn)) {
nodes.push(vn);
selectors.push(
(function(ii) {
return function(realArgs) {
return realArgs[ii];
};
})(selectI));
selectI+=1;
} else {
selectors.push(
(function(aa) {
return function () {
return aa;
};
})(vn));
}
});
var context = this;
var nofnodes = slice(selectors,1);
if (nodes.length === 0) {
return oneE(fn.apply(context, valsOrNodes));
} else if ((nodes.length === 1) && Function.hasInstance(fn)) {
return nodes[0].mapE(
function () {
var args = arguments;
return fn.apply(
context,
map(function (s) {return s(args);}, nofnodes));
});
} else if (nodes.length === 1) {
return fn.mapE(
function (v) {
var args = arguments;
return v.apply(
context,
map(function (s) {return s(args);}, nofnodes));
});
/* XXX CSA BROKEN: createTimeSyncNode no longer defined...
} else if (Function.hasInstance(fn)) {
return createTimeSyncNode(nodes).mapE(
function (arr) {
return fn.apply(
this,
map(function (s) { return s(arr); }, nofnodes));
});
} else if (EventStream.hasInstance(fn)) {
return createTimeSyncNode(nodes).mapE(
function (arr) {
return arr[0].apply(
this,
map(function (s) {return s(arr); }, nofnodes));
});
XXX end broken bit */
} else { Object.Throw('unknown mapE case'); }
};
EventStream.prototype.snapshotE = function (valueB) {
return createNode([this], function (pulse) {
pulse.value = valueNow(valueB);
return pulse;
});
};
var snapshotE = function(triggerE,valueB) {
return triggerE.snapshotE(valueB);
};
// XXX CSA this is more specific notion of equality than original
var _default_eq_ = function(x,y) { return x===y; };
var _default_cp_ = function(x) { return x; };
EventStream.prototype.filterRepeatsE = function(optStart, optEq, optClone) {
var hadFirst = (optStart === undefined) ? false : true;
var prev = optStart;
var eq = optEq ? optEq : _default_eq_;
var cp = optClone ? optClone : _default_cp_;
return this.filterE(function (v) {
if (!hadFirst || !eq(prev, v)) {
hadFirst = true;
prev = cp(v);
return true;
}
else {
return false;
}
});
};
var filterRepeatsE = function(sourceE,optStart) {
return sourceE.filterRepeatsE(optStart);
};
//credit Pete Hopkins
// extensively hacked by CSA
var calmE = function (triggerE, timeB) {
if (!Behavior.hasInstance(timeB)) {
timeB = constantB(timeB);
}
var out = internalE();
// state
var lastTime = 0;
var towards = null;
var lastVal, sawMoreRecent;
var towardsFunc = function() {
towards = null;
if (sawMoreRecent) {
sendEvent(out, lastVal);
}
};
var timeChangedE = timeB.changes().mapE(function() {
return sawMoreRecent ? lastVal : doNotPropagate;
});
return createNode(
[triggerE, out, timeChangedE],
function (p) {
var curTime = now();
var left = (curTime - lastTime) - timeB.valueNow();
if (towards) { clearTimeout(towards); }
if (left >= 0) {
lastTime = curTime;
sawMoreRecent = false;
lastVal = null; // free memory
towards = setTimeout( towardsFunc, timeB.valueNow() );
// propagate directly.
return p;
} else {
// hmm, don't propagate this yet, but maybe do it later.
sawMoreRecent = true;
lastVal = p;
towards = setTimeout( towardsFunc, left );
return doNotPropagate;
}
});
};
//calmE: Event a * [Behavior] Number -> Event a
EventStream.prototype.calmE = function(timeB) {
return calmE(this, timeB);
};
EventStream.prototype.blindE = function (time) {
return createNode(
[this],
function () {
var intervalFn =
Behavior.hasInstance(time)?
function () { return valueNow(time); }
: function () { return time; };
var lastSent = now() - intervalFn() - 1;
return function (p) {
var curTime = now();
if (curTime - lastSent > intervalFn()) {
lastSent = curTime;
return p;
}
else { return doNotPropagate; }
};
}());
};
var blindE = function(sourceE,interval) {
return sourceE.blindE(interval);
};
EventStream.prototype.startsWith = function(init) {
return Behavior.New(this,init);
};
var startsWith = function(e,init) {
if (!EventStream.hasInstance(e)) {
Object.Throw('startsWith: expected EventStream; received ' + e);
}
return e.startsWith(init);
};
//TODO optionally append to objects
//createConstantB: a -> Behavior a
constantB = function (val) {
return Behavior.New(internalE(), val);
};
var liftB = function (fn /* . behaves */) {
var args = slice(arguments, 1);
//dependencies
var constituentsE =
map(changes,
filter(function (v) { return Behavior.hasInstance(v); },
slice(arguments, 0)));
//calculate new vals
var getCur = function (v) {
return Behavior.hasInstance(v) ? v.last : v;
};
var ctx = this;
var getRes = function () {
return getCur(fn).apply(ctx, map(getCur, args));
};
if(constituentsE.length === 1) {
return Behavior.New(constituentsE[0],getRes(),getRes);
}
//gen/send vals @ appropriate time
var prevStamp = -1;
var mid = createNode(constituentsE, function (p) {
if (p.stamp !== prevStamp) {
prevStamp = p.stamp;
return p;
}
else {
return doNotPropagate;
}
});
return Behavior.New(mid,getRes(),getRes);
};
Behavior.prototype.liftB = function(/* args */) {
var args= slice(arguments,0).concat([this]);
return liftB.apply(this,args);
};
Behavior.prototype.switchB = function() {
var behaviourCreatorsB = this;
var init = valueNow(behaviourCreatorsB);
var prevSourceE = null;
var receiverE = internalE.New();
//XXX could result in out-of-order propagation! Fix!
var makerE =
createNode(
[changes(behaviourCreatorsB)],
function (p) {
if (!Behavior.hasInstance(p.value)) { Object.Throw ('switchB: expected Behavior as value of Behavior of first argument'); } //SAFETY
if (prevSourceE !== null) {
prevSourceE.removeListener(receiverE);
}
prevSourceE = changes(p.value);
prevSourceE.attachListener(receiverE);
sendEvent(receiverE, valueNow(p.value));
return doNotPropagate;
});
if (Behavior.hasInstance(init)) {
sendEvent(makerE, init);
}
return startsWith(
receiverE,
Behavior.hasInstance(init)? valueNow(init) : init);
};
var switchB = function (b) { return b.switchB(); };
/* XXX CSA omitted
//TODO test, signature
var timerB = function(interval) {
return startsWith(timerE(interval), now());
};
*/
//TODO test, signature
var delayStaticB = function (triggerB, time, init) {
return startsWith(delayStaticE(changes(triggerB), time), init);
};
//TODO test, signature
Behavior.prototype.delayB = function (time, init) {
var triggerB = this;
if (Behavior.hasInstance(time)) {
return startsWith(
delayE(
changes(triggerB),
time),
arguments.length > 3 ? init : valueNow(triggerB));
} else {
return delayStaticB(
triggerB,
time,
arguments.length > 3 ? init : valueNow(triggerB));
}
};
var delayB = function(srcB, timeB, init) {
return srcB.delayB(timeB,init);
};
//artificially send a pulse to underlying event node of a behaviour
//note: in use, might want to use a receiver node as a proxy or an identity map
Behavior.prototype.sendBehavior = function(val) {
sendEvent(this.underlyingRaw,val);
};
var sendBehavior = function (b,v) { b.sendBehavior(v); };
Behavior.prototype.ifB = function(trueB,falseB) {
var testB = this;
//TODO auto conversion for behaviour funcs
if (!Behavior.hasInstance(trueB)) { trueB = constantB(trueB); }
if (!Behavior.hasInstance(falseB)) { falseB = constantB(falseB); }
return liftB(function(te,t,f) { return te ? t : f; },testB,trueB,falseB);
};
var ifB = function(test,cons,altr) {
if (!Behavior.hasInstance(test)) { test = constantB(test); }
return test.ifB(cons,altr);
};
//condB: . [Behavior boolean, Behavior a] -> Behavior a
var condB = function (/* . pairs */ ) {
var pairs = slice(arguments, 0);
return liftB.apply({},[function() {
var i=0;
while (i<pairs.length) {
if(arguments[i]) {
return arguments[pairs.length+i];
}
i+=1;
}
return undefined;
}].concat(map(function(pair) {return pair[0];},pairs).concat(map(function(pair) {return pair[1];},pairs))));
};
var andB = function (/* . behaves */) {
return liftB.apply({},[function() {
var i=0;
while (i<arguments.length) {
if(!arguments[i]) { return false; }
i+=1;
}
return true;
}].concat(slice(arguments,0)));
};
Behavior.prototype.andB = function() {
return andB([this].concat(arguments));
};
var orB = function (/* . behaves */ ) {
return liftB.apply({},[function() {
var i=0;
while (i<arguments.length) {
if(arguments[i]) { return true; }