forked from baconjs/bacon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.js
85 lines (74 loc) · 1.75 KB
/
source.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
// build-dependencies: _
function Source(obs, sync, lazy = false) {
this.obs = obs;
this.sync = sync;
this.lazy = lazy;
this.queue = [];
}
extend(Source.prototype, {
_isSource: true,
subscribe(sink) { return this.obs.dispatcher.subscribe(sink); },
toString() { return this.obs.toString(); },
markEnded() {
this.ended = true;
return true;
},
consume() {
if (this.lazy) {
return { value: _.always(this.queue[0]) };
} else {
return this.queue[0];
}
},
push(x) {
this.queue = [x];
return [x];
},
mayHave() { return true; },
hasAtLeast() { return this.queue.length; },
flatten: true
});
function ConsumingSource() {
Source.apply(this, arguments);
}
inherit(ConsumingSource, Source);
extend(ConsumingSource.prototype, {
consume() { return this.queue.shift(); },
push(x) { return this.queue.push(x); },
mayHave(c) { return !this.ended || this.queue.length >= c; },
hasAtLeast(c) { return this.queue.length >= c; },
flatten: false
});
function BufferingSource(obs) {
Source.call(this, obs, true);
}
inherit(BufferingSource, Source);
extend(BufferingSource.prototype, {
consume() {
const values = this.queue;
this.queue = [];
return {
value: function() {
return values;
}
};
},
push(x) { return this.queue.push(x.value()); },
hasAtLeast() { return true; }
});
Source.isTrigger = function(s) {
if (s != null ? s._isSource : void 0) {
return s.sync;
} else {
return s != null ? s._isEventStream : void 0;
}
};
Source.fromObservable = function(s) {
if (s != null ? s._isSource : void 0) {
return s;
} else if (s != null ? s._isProperty : void 0) {
return new Source(s, false);
} else {
return new ConsumingSource(s, true);
}
};