forked from dmvaldman/samsara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
samsara.umd.js
7617 lines (6716 loc) · 242 KB
/
samsara.umd.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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.Samsara = {})));
}(this, function (exports) { 'use strict';
/* Copyright © 2015-2016 David Valdman */
var STATE = {
NONE : -1,
START : 0,
UPDATE : 1,
END : 2
};
var currentState = STATE.START;
/**
* SUE specified the global state of the application, whether it is in a
* `start`, `update` or `end` state. This is necessary for coordinating
* `resize` events with `start`, `update`, `end` states in stream.
*
* @class SUE
* @namespace Core
* @static
* @private
*/
var SUE = {};
SUE.STATES = STATE;
SUE.set = function set(state){
currentState = state;
};
SUE.get = function get(){
return currentState;
};
/* Copyright © 2015 David Valdman */
/**
* Queue that batches `update` events.
* This queue is traversed after the `preTickQueue` but before `dirtQueue`
* by the Engine.
*
* @private
*/
var postTickQueue = [];
/* Copyright © 2015 David Valdman */
/**
* Queue that batches `start` events.
* This queue is traversed first (but after DOM events are executed) by the Engine.
*
* @private
*/
var preTickQueue = [];
/* Copyright © 2015 David Valdman */
/**
* Queue that batches `end` and `dirty` events.
* This queue is traversed after the `postTickQueue` by the Engine.
*
* @private
*/
var dirtyQueue = [];
/* Copyright © 2015 David Valdman */
/**
* This queue is executed before the postTickQueue and after the preTickQueue.
* however, it differs in that the Engine does not clear the queue.
* This must be done manually.
*
* @private
*/
var tickQueue = [];
var rafId = 0;
/**
* Engine is a singleton object that is required to run a Samsara application.
* It is the "heartbeat" of the application, managing the batching of streams
* and creating `RootNodes` and `Contexts` to begin render trees.
*
* It also listens and can respond to DOM events on the HTML `<body>` tag
* and `window` object. For instance the `resize` event.
*
* @class Engine
* @namespace Core
* @static
* @private
* @uses Core.EventHandler
*/
var Engine = {};
/**
* Updates by a single frame of the application by looping through all function queues.
* This is repeatedly called within a requestAnimationFrame loop until the application
* is receiving no layout changes. At this point the requestAnimationFrame will be
* canceled until the next change.
*
* @private
* @method step
*/
Engine.step = function step() {
// browser events and their handlers happen before rendering begins
while (preTickQueue.length) (preTickQueue.shift())();
// tick signals base event flow coming in
SUE.set(SUE.STATES.UPDATE);
for (var i = 0; i < tickQueue.length; i++) tickQueue[i]();
// post tick is for resolving larger components from their incoming signals
while (postTickQueue.length) (postTickQueue.shift())();
SUE.set(SUE.STATES.END);
while (dirtyQueue.length) (dirtyQueue.shift())();
SUE.set(SUE.STATES.START);
};
/**
* Initiate the Engine's request animation frame loop.
*
* @method start
* @static
*/
Engine.start = function start(){
Engine.step();
rafId = window.requestAnimationFrame(start);
};
/**
* Stop the Engine's request animation frame loop.
*
* @method stop
* @static
*/
Engine.stop = function(){
window.cancelAnimationFrame(rafId);
};
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* @license MPL 2.0
* @copyright Famous Industries, Inc. 2014
*/
/* Modified work copyright © 2015-2016 David Valdman */
/**
* EventEmitter represents an asynchronous channel for broadcasting and receiving events.
*
* @example
*
* var eventEmitter = new EventEmitter();
*
* eventEmitter.on('send', function(payload){
* console.log(payload) // {data : 0}
* });
*
* // sometime later...
* eventEmitter.emit('send', {data : 0});
*
* @class EventEmitter
* @namespace Events
* @constructor
*/
function EventEmitter() {
this.listeners = {};
this._owner = this;
}
/**
* Broadcast an event on the `type` channel with an optional payload. This will call the handlers
* of all EventEmitters listening on the `type` channel with the (optional) data payload
* as its argument.
*
* @method emit
*
* @param type {String} Channel name
* @param data {Object} Payload
*/
EventEmitter.prototype.emit = function emit(type, data) {
if (data === false) return; // do not propagate
var handlers = this.listeners[type];
if (handlers) {
for (var i = 0; i < handlers.length; i++)
handlers[i].call(this._owner, data);
}
};
/**
* Alias for emit.
*
* @method trigger
*/
EventEmitter.prototype.trigger = EventEmitter.prototype.emit;
/**
* Adds a handler to the `type` channel which will be executed on `emit`.
*
* @method on
* @param type {String} Channel name
* @param handler {Function} Callback
*/
EventEmitter.prototype.on = function on(type, handler) {
if (!(type in this.listeners)) this.listeners[type] = [];
this.listeners[type].push(handler);
};
/**
* Behaves like `EventEmitter.prototype.on`, except the handler is only executed once.
*
* @method once
* @param type {String} Channel name (e.g., 'click')
* @param handler {Function} Callback
*/
EventEmitter.prototype.once = function once(type, handler){
var onceHandler = function(){
EventEmitter.prototype.off.call(this, type, onceHandler);
handler.apply(this, arguments);
}.bind(this);
this.on(type, onceHandler);
};
/**
* Removes the `handler` from the `type` channel. This undoes the work of `on`.
* If no type is provided, then all event listeners are removed.
* If a type is provided but no handler, then all listeners of that type are removed.
*
* @method off
* @param [type] {String} Channel name
* @param [handler] {Function} Callback
*/
EventEmitter.prototype.off = function off(type, handler) {
if (!type) {
this.listeners = {};
return;
}
var listener = this.listeners[type];
if (listener !== undefined) {
if (!handler) this.listeners[type] = []; // remove all listeners of given type
else {
var index = listener.indexOf(handler);
if (index >= 0) listener.splice(index, 1);
}
}
};
/**
* A convenience method to bind the provided object to all added handlers.
*
* @method bindThis
* @param owner {Object} Bound `this` context
*/
EventEmitter.prototype.bindThis = function bindThis(owner) {
this._owner = owner;
};
/**
* EventHandler extends EventEmitter to provide subscription methods.
* It also includes helper methods on the constructor for setting up Controllers and Views
* with input and output emitters.
*
* @example
*
* var eventHandlerA = new EventHandler();
* var eventHandlerB = new EventHandler();
*
* eventHandlerB.subscribe(eventHandlerA);
*
* eventHandlerB.on('name', function(payload){
* console.log(payload) // {data : 0}
* });
*
* eventHandlerA.emit('name', {data : 0});
*
* @class EventHandler
* @namespace Events
* @extends Events.EventEmitter
* @constructor
*/
function EventHandler() {
EventEmitter.apply(this, arguments);
this.upstream = []; // upstream event handlers
this.upstreamListeners = {}; // upstream listeners
}
EventHandler.prototype = Object.create(EventEmitter.prototype);
EventHandler.prototype.constructor = EventHandler;
/**
* Constructor helper method. Assign an event handler to receive an object's input events.
* Defines `trigger`, `subscribe` and `unsubscribe` methods on the class instance.
*
* @method setInputHandler
* @static
* @param object {Object} Class instance
* @param handler {EventHandler} EventHandler representing an input source
*/
EventHandler.setInputHandler = function setInputHandler(object, handler) {
object.trigger = handler.trigger.bind(handler);
object.subscribe = handler.subscribe.bind(handler);
object.unsubscribe = handler.unsubscribe.bind(handler);
};
/**
* Constructor helper method. Assign an event handler to emit an object's output events.
* Defines `emit`, `on` and `off` methods on the class instance.
*
* @method setOutputHandler
* @static
* @param object {Object} Object to provide on, off and emit methods
* @param handler {EventHandler} Handler assigned event handler
*/
EventHandler.setOutputHandler = function setOutputHandler(object, handler) {
handler.bindThis(object);
object.emit = handler.emit.bind(handler);
object.on = handler.on.bind(handler);
object.off = handler.off.bind(handler);
};
/**
* Constructor helper method. Given an events dictionary of {eventName : handler} pairs, attach them to
* a provided input handler for an object.
*
* @method setInputEvents
* @static
* @private
* @param object {Object} Object to provide on, off and emit methods
* @param handler {EventHandler} Handler assigned event handler
*/
EventHandler.setInputEvents = function setInputEvents(object, events, handlerIn){
for (var key in events) {
var handlerName = events[key];
var handler = (typeof handlerName === 'string')
? object[handlerName]
: handlerName;
if (handler) handlerIn.on(key, handler.bind(object));
}
};
/**
* Adds a handler to the `type` channel which will be executed on `emit`.
* Extends EventEmitter's `on` method.
*
* @method on
* @param type {String} Event channel name
* @param handler {Function} Handler
*/
EventHandler.prototype.on = function on(type, handler) {
EventEmitter.prototype.on.apply(this, arguments);
if (!(type in this.upstreamListeners)) {
var upstreamListener = this.trigger.bind(this, type);
this.upstreamListeners[type] = upstreamListener;
for (var i = 0; i < this.upstream.length; i++) {
this.upstream[i].on(type, upstreamListener);
}
}
};
/**
* Listen for events from an an upstream source.
*
* @method subscribe
* @param source {EventEmitter} Event source
*/
EventHandler.prototype.subscribe = function subscribe(source) {
var index = this.upstream.indexOf(source);
if (index < 0) {
this.upstream.push(source);
for (var type in this.upstreamListeners) {
source.on(type, this.upstreamListeners[type]);
}
}
return source;
};
/**
* Stop listening to events from an upstream source.
* Undoes work of `subscribe`.
*
* @method unsubscribe
* @param source {EventEmitter} Event source
*/
EventHandler.prototype.unsubscribe = function unsubscribe(source) {
var index = this.upstream.indexOf(source);
if (index >= 0) {
this.upstream.splice(index, 1);
for (var type in this.upstreamListeners) {
source.off(type, this.upstreamListeners[type]);
}
}
return source;
};
/**
* EventMapper modifies the data payload of an event based on
* a provided function.
*
* Note: it does not modify the event's `type`.
*
* @example
*
* var eventMapper = new EventMapper(function(payload){
* return payload.x + payload.y
* });
*
* var eventEmitter = new EventEmitter();
*
* eventMapper.subscribe(eventEmitter);
*
* eventMapper.on('name', function(value){
* alert(value);
* });
*
* eventEmitter.emit('name', {x : 1, y : 2}); // alerts 3
*
* @class EventMapper
* @namespace Events
* @constructor
* @param map {Function} Function to modify the event payload
*/
function EventMapper(map) {
EventHandler.call(this);
this._mappingFunction = map;
}
EventMapper.prototype = Object.create(EventHandler.prototype);
EventMapper.prototype.constructor = EventMapper;
/**
* Emit mapped event.
*
* @method emit
* @param type {String} Channel name
* @param data {Object} Payload
*/
EventMapper.prototype.emit = function emit(type, data) {
var mappedData = this._mappingFunction(data);
EventHandler.prototype.emit.call(this, type, mappedData);
};
/**
* Alias of emit.
*
* @method trigger
* @param type {String} Channel name
* @param data {Object} Payload
*/
EventMapper.prototype.trigger = EventMapper.prototype.emit;
/**
* EventFilter regulates the broadcasting of events based on
* a specified condition prescribed by a provided function
* with the signature `(data) -> Boolean`
*
* @example
*
* var eventFilter = new EventFilter(function(payload){
* return (payload.value == 0);
* });
*
* var eventEmitter = new EventEmitter();
*
* eventFilter.subscribe(eventEmitter);
*
* eventFilter.on('click', function(data){
* alert('fired');
* });
*
* eventEmitter.emit('click', {value : 0}); // fired
* eventEmitter.emit('click', {value : 1}); // doesn't fire
*
* @class EventFilter
* @namespace Events
* @constructor
* @param filter {Function} Function returning a Boolean
*/
function EventFilter(filter) {
EventHandler.call(this);
this._condition = filter;
}
EventFilter.prototype = Object.create(EventHandler.prototype);
EventFilter.prototype.constructor = EventFilter;
/**
* Emit event if the condition is satisfied.
*
* @method emit
* @param type {String} Channel name
* @param data {Object} Payload
*/
EventFilter.prototype.emit = function emit(type, data) {
//TODO: add start/update/end logic
if (!this._condition(data)) return;
EventHandler.prototype.emit.apply(this, arguments);
};
/**
* Alias of emit.
*
* @method trigger
* @param type {String} Channel name
* @param data {Object} Payload
*/
EventFilter.prototype.trigger = EventFilter.prototype.emit;
/**
* EventSplitter routes events to various event destinations
* based on custom logic. The return of the provided splitter
* function should be of type EventEmitter.
*
* @example
*
* var eventEmitter = new EventEmitter();
* var eventEmitterX = new eventEmitter();
* var eventEmitterY = new eventEmitter();
*
* var eventSplitter = new EventSplitter(function(payload){
* return (payload.x > payload.y)
* ? eventEmitterX;
* : eventEmitterY;
* });
*
* eventSplitter.subscribe(eventEmitter);
*
* eventEmitterX.on('move', function(){
* console.log('x is bigger')
* });
*
* eventEmitterY.on('move', function(){
* console.log('y is bigger')
* })
*
* eventEmitter.emit('move', {x : 3, y : 2}); // x is bigger
*
* @class EventSplitter
* @private
* @namespace Events
* @constructor
* @param splitter {Function}
*/
function EventSplitter(splitter) {
EventHandler.call(this);
this._splitter = splitter;
}
EventSplitter.prototype = Object.create(EventHandler.prototype);
EventSplitter.prototype.constructor = EventSplitter;
/**
* Emit event.
*
* @method emit
* @param type {String} Channel name
* @param data {Object} Payload
*/
EventSplitter.prototype.emit = function emit(type, data) {
var target = this._splitter.apply(this, arguments);
if (target && target.emit instanceof Function)
target.emit(type, data);
};
/**
* Alias of emit.
*
* @method trigger
* @param type {String} Channel name
* @param data {Object} Payload
*/
EventSplitter.prototype.trigger = EventSplitter.prototype.emit;
/**
* A SimpleStream wraps an EventHandler and provides convenience
* methods of `map`, `filter`, `split`, and `pluck` to
* transform one stream into another.
*
* @example
*
* @class SimpleStream
* @extends Core.EventHandler
* @private
* @namespace Streams
* @constructor
*/
function SimpleStream(){
EventHandler.call(this);
}
SimpleStream.prototype = Object.create(EventHandler.prototype);
SimpleStream.prototype.constructor = SimpleStream;
/**
* Map converts the current stream into a new stream
* with a modified (mapped) data payload.
*
* @method map
* @param mapperFn {Function} Function to map event payload
*/
SimpleStream.prototype.map = function(mapperFn){
var stream = new SimpleStream();
var mapper = new EventMapper(mapperFn);
stream.subscribe(mapper).subscribe(this);
return stream;
};
/**
* Filter converts the current stream into a new stream
* that only emits if the filter condition is satisfied.
* The function should return a Boolean.
*
* @method filter
* @param filterFn {Function} Function to filter event payload
*/
SimpleStream.prototype.filter = function(filterFn){
var filter = new EventFilter(filterFn);
var filteredStream = new SimpleStream();
filteredStream.subscribe(filter).subscribe(this);
return filteredStream;
};
/**
* Split maps one of several streams based on custom logic.
* The function should return an EventEmitter.
*
* @method split
* @param splitterFn {Function} Splitter function
*/
SimpleStream.prototype.split = function(splitterFn){
var splitter = new EventSplitter(splitterFn);
var splitStream = new SimpleStream();
splitStream.subscribe(splitter).subscribe(this);
return splitStream;
};
/**
* Pluck is an opinionated mapper. It projects a Stream
* onto one of its return values.
*
* Useful if a Stream returns an array or an object.
*
* @method pluck
* @param key {String|Number} Key to project event payload onto
*/
SimpleStream.prototype.pluck = function(key){
return this.map(function(value){
return value[key];
});
};
//TODO: can this be inherited by other streams?
SimpleStream.merge = function(){};
/**
* Lift is like map, except it maps several event sources,
* not only one.
*
* @example
*
* var liftedStream = SimpleStream.lift(function(payload1, payload2){
* return payload1 + payload2;
* }, [stream2, stream2]);
*
* liftedStream.on('name'), function(data){
* // data = 3;
* });
*
* stream2.emit('name', 1);
* stream2.emit('name', 2);
*
* @method lift
* @static
* @param map {Function} Function to map stream payloads
* @param streams {Array|Object} Stream sources
*/
SimpleStream.lift = function(map, streams){
//TODO: fix comma separated arguments
var mergedStream = (streams instanceof Array)
? this.merge(streams)
: this.merge.apply(null, Array.prototype.splice.call(arguments, 1));
var mappedStream = new EventMapper(function liftMap(data){
return map.apply(null, data);
});
var liftedStream = new SimpleStream();
liftedStream.subscribe(mappedStream).subscribe(mergedStream);
return liftedStream;
};
var EVENTS = {
START : 'start',
UPDATE : 'update',
END : 'end',
RESIZE : 'resize'
};
/**
* Stream listens to `resize`, `start`, `update` and `end` events and
* emits `start`, `update` and `end` events. `Resize` events get
* unified with `start`, `update`, and `end` events depending on
* when they are fired within Samsara's engine cycle.
*
* If listening to multiple sources, Stream emits a single event per
* Engine cycle.
*
* @example
*
* var position = new Transitionable([0,0]);
* var size = new EventEmitter();
*
* var translationStream = Stream.lift(function(position, size){
* var translation = [
* position[0] + size[0],
* position[1] + size[1]
* ];
*
* return Transform.translate(translation);
* }, [positionStream, sizeStream]);
*
* translationStream.on('start', function(transform){
* console.log(transform);
* });
*
* translationStream.on('update', function(transform){
* console.log(transform);
* });
*
* translationStream.on('end', function(transform){
* console.log(transform);
* });
*
* position.set([100, 50], {duration : 500});
* size.emit('resize', [100,100]);
*
* @class Stream
* @extends Streams.SimpleStream
* @namespace Streams
* @param [options] {Object} Options
* @param [options.start] {Function} Custom logic to map the `start` event
* @param [options.update] {Function} Custom logic to map the `update` event
* @param [options.end] {Function} Custom logic to map the `end` event
* @constructor
*/
function Stream(options){
this._eventInput = new EventHandler();
this._eventOutput = new EventHandler();
EventHandler.setInputHandler(this, this._eventInput);
EventHandler.setOutputHandler(this, this._eventOutput);
var counter = 0;
var isUpdating = false;
var dirtyStart = false;
var dirtyUpdate = false;
var dirtyEnd = false;
function start(data){
var payload = options && options.start ? options.start(data) : data;
if (payload !== false) this.emit(EVENTS.START, payload);
dirtyStart = false;
}
function update(data){
var payload = options && options.update ? options.update(data) : data;
if (payload !== false) this.emit(EVENTS.UPDATE, payload);
dirtyUpdate = false;
}
function end(data){
var payload = options && options.end ? options.end(data) : data;
if (payload !== false) this.emit(EVENTS.END, payload);
dirtyEnd = false;
}
this._eventInput.on(EVENTS.START, function(data){
counter++;
if (dirtyStart || isUpdating) return false;
dirtyStart = true;
preTickQueue.push(start.bind(this, data));
}.bind(this));
this._eventInput.on(EVENTS.UPDATE, function(data){
isUpdating = true;
if (dirtyUpdate) return false;
dirtyUpdate = true;
postTickQueue.push(update.bind(this, data));
}.bind(this));
this._eventInput.on(EVENTS.END, function(data){
counter--;
if (isUpdating && counter > 0){
update.call(this, data);
return false;
}
isUpdating = false;
if (dirtyEnd) return;
dirtyEnd = true;
dirtyQueue.push(end.bind(this, data));
}.bind(this));
this._eventInput.on(EVENTS.RESIZE, function(data){
switch (SUE.get()){
case SUE.STATES.START:
this.trigger(EVENTS.START, data);
break;
case SUE.STATES.UPDATE:
this.trigger(EVENTS.UPDATE, data);
break;
case SUE.STATES.END:
this.trigger(EVENTS.END, data);
break;
}
}.bind(this));
}
Stream.prototype = Object.create(SimpleStream.prototype);
Stream.prototype.constructor = Stream;
/**
* Lift is like map, except it maps several event sources,
* not only one.
*
* @example
*
* var liftedStream = Stream.lift(function(payload1, payload2){
* return payload1 + payload2;
* }, [stream2, stream2]);
*
* liftedStream.on('name'), function(data){
* // data = 3;
* });
*
* stream2.emit('name', 1);
* stream2.emit('name', 2);
*
* @method lift
* @static
* @param map {Function} Function to map stream payloads
* @param streams {Array|Object} Stream sources
*/
Stream.lift = SimpleStream.lift;
/**
* Batches events for provided object of streams in
* {key : stream} pairs. Emits one event per Engine cycle.
*
* @method merge
* @static
* @param streams {Object} Dictionary of `resize` streams
*/
Stream.merge = function(streamObj){
var mergedStream = new Stream();
var mergedData = (streamObj instanceof Array) ? [] : {};
mergedStream.addStream = function(key, stream){
var mapper = (function(key){
return new EventMapper(function(data){
mergedData[key] = data;
return mergedData;
});
})(key);
mergedStream.subscribe(mapper).subscribe(stream);
};
for (var key in streamObj){
var stream = streamObj[key];
mergedStream.addStream(key, stream);
}
return mergedStream;
};
/**
* An Observable is a stream for events set discretely in time, as opposed to continuously.
* It emits appropriate `start` and `end` events upon calling the `set` method.
*
* @class Observable
* @constructor
* @private
* @extends Streams.Stream
* @param value {Number, String, Array, Object} Value
*/
function Observable(value){
SimpleStream.call(this);
this.value = value;
if (value !== undefined) this.set(value);
}
Observable.prototype = Object.create(SimpleStream.prototype);
Observable.prototype.constructor = Observable;
/**
* Getter for the provided value.
*
* @method get
* @return {Number, String, Array, Object}
*/
Observable.prototype.get = function(){
return this.value;
};
/**
* Setter for the provided value.
*
* @method set
* @param value {Number, String, Array, Object} Value
*/
Observable.prototype.set = function(value){
var self = this;
preTickQueue.push(function(){
self.value = value;
self.emit('start', value);
dirtyQueue.push(function(){
self.emit('end', value);
});
});
};
/**
* Encapsulates a stream of layout data (transform, origin, align, opacity).
* Listens on start/update/end events, batches them, and emits them downstream
* to descendant layout nodes.
*
* @example
*
* var context = Context();
*
* var surface = new Surface({
* size : [100,100],
* properties : {background : 'red'}
* });
*
* var opacity = new Transitionable(1);
*
* var layout = new LayoutNode({
* transform : Transform.translateX(100),
* opacity : opacity
* });
*
* context.add(layout).add(surface);
* context.mount(document.body)
*
* opacity.set(0, {duration : 1000});
*
* @class LayoutNode
* @constructor
* @namespace Core
* @private
* @param sources {Object} Object of layout sources
* @param [sources.transform] {Stream|Transform} Transform source
* @param [sources.align] {Stream|Array} Align source
* @param [sources.origin] {Stream|Array} Origin source
* @param [sources.opacity] {Stream|Number} Opacity source
*/
function LayoutNode(sources) {
this.stream = _createStream(sources);
EventHandler.setOutputHandler(this, this.stream);
}
// Enumeration of types of layout properties
LayoutNode.KEYS = {
transform : 'transform',
origin : 'origin',
align : 'align',
opacity : 'opacity'
};
/**
* Introduce new data streams to the layout node in {key : value} pairs.
* Here the `key` is one of "transform", "origin", "align" or "opacity".
* The `value` is either a stream, or a simple type like a `Number` or `Array`.
* Simple types will be wrapped in an `Observerable` to emit appropriate events.
*
* @method set
* @param sources {Object} Object of data sources