-
Notifications
You must be signed in to change notification settings - Fork 28
/
Observable.js
187 lines (166 loc) · 4.41 KB
/
Observable.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
var _ = require('./_');
var genIterator = require('./genIterator');
var Promise = _.Promise;
/**
* Create a composable observable object.
* Promise can't resolve multiple times, this class makes it possible, so
* that you can easily map, filter and even back pressure events in a promise way.
* For live example: [Double Click Demo](https://jsbin.com/niwuti/edit?html,js,output).
* @version_added v0.7.2
* @param {Function} executor `(next) ->` It's optional.
* @return {Observable}
* @example
* ```js
* var Observable = require("yaku/lib/Observable");
* var linear = new Observable();
*
* var x = 0;
* setInterval(linear.next, 1000, x++);
*
* // Wait for 2 sec then emit the next value.
* var quad = linear.subscribe(async x => {
* await sleep(2000);
* return x * x;
* });
*
* var another = linear.subscribe(x => -x);
*
* quad.subscribe(
* value => { console.log(value); },
* reason => { console.error(reason); }
* );
*
* // Emit error
* linear.error(new Error("reason"));
*
* // Unsubscribe an observable.
* quad.unsubscribe();
*
* // Unsubscribe all subscribers.
* linear.subscribers = [];
* ```
* @example
* Use it with DOM.
* ```js
* var filter = fn => v => fn(v) ? v : new Promise(() => {});
*
* var keyup = new Observable((next) => {
* document.querySelector('input').onkeyup = next;
* });
*
* var keyupText = keyup.subscribe(e => e.target.value);
*
* // Now we only get the input when the text length is greater than 3.
* var keyupTextGT3 = keyupText.subscribe(filter(text => text.length > 3));
*
* keyupTextGT3.subscribe(v => console.log(v));
* ```
*/
var Observable = module.exports = function Observable (executor) {
var self = this;
genHandler(self);
self.subscribers = [];
executor && executor(self.next, self.error);
};
_.extendPrototype(Observable, {
/**
* Emit a value.
* @param {Any} value
* so that the event will go to `onError` callback.
*/
next: null,
/**
* Emit an error.
* @param {Any} value
*/
error: null,
/**
* The publisher observable of this.
* @type {Observable}
*/
publisher: null,
/**
* All the subscribers subscribed this observable.
* @type {Array}
*/
subscribers: null,
/**
* It will create a new Observable, like promise.
* @param {Function} onNext
* @param {Function} onError
* @return {Observable}
*/
subscribe: function (onNext, onError) {
var self = this, subscriber = new Observable();
subscriber._onNext = onNext;
subscriber._onError = onError;
subscriber._nextErr = genNextErr(subscriber.next);
subscriber.publisher = self;
self.subscribers.push(subscriber);
return subscriber;
},
/**
* Unsubscribe this.
*/
unsubscribe: function () {
var publisher = this.publisher;
publisher && publisher.subscribers.splice(publisher.subscribers.indexOf(this), 1);
}
});
function genHandler (self) {
self.next = function (val) {
var i = 0, len = self.subscribers.length, subscriber;
while (i < len) {
subscriber = self.subscribers[i++];
Promise.resolve(val).then(
subscriber._onNext,
subscriber._onError
).then(
subscriber.next,
subscriber._nextErr
);
}
};
self.error = function (err) {
self.next(Promise.reject(err));
};
}
function genNextErr (next) {
return function (reason) {
next(Promise.reject(reason));
};
}
/**
* Merge multiple observables into one.
* @version_added 0.9.6
* @param {Iterable} iterable
* @return {Observable}
* @example
* ```js
* var Observable = require("yaku/lib/Observable");
* var sleep = require("yaku/lib/sleep");
*
* var src = new Observable(next => setInterval(next, 1000, 0));
*
* var a = src.subscribe(v => v + 1; });
* var b = src.subscribe((v) => sleep(10, v + 2));
*
* var out = Observable.merge([a, b]);
*
* out.subscribe((v) => {
* console.log(v);
* })
* ```
*/
Observable.merge = function merge (iterable) {
var iter = genIterator(iterable);
return new Observable(function (next) {
var item;
function onError (e) {
next(Promise.reject(e));
}
while (!(item = iter.next()).done) {
item.value.subscribe(next, onError);
}
});
};