-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
185 lines (173 loc) · 4.01 KB
/
index.ts
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
export class Observable {
_subscribe;
constructor(subscribe) {
this._subscribe = subscribe;
}
pipe(...operations) {
return operations.reduce((prev, fn) => fn(prev), this);
}
subscribe(observer) {
const defaultObserver = {
next: () => { },
error: () => { },
complete: () => { }
}
if (typeof observer === 'function') {
return this._subscribe({ ...defaultObserver, next: observer });
} else {
return this._subscribe({ ...defaultObserver, ...observer });
}
}
}
export function of(...args) {
return new Observable(observer => {
args.forEach(arg => {
observer.next(arg);
})
observer.complete();
return {
unsubscribe: () => { }
}
})
}
export function fromEvent(element, event) {
return new Observable(observer => {
const handler = e => observer.next(e);
element.addEventListener(event, handler);
return {
unsubscribe: () => element.removeEventListener(event, handler)
};
});
}
/**
* @param param array or promise
*/
export function from(param) {
if (Array.isArray(param)) {
return new Observable(observer => {
param.forEach(val => observer.next(val));
observer.complete();
return {
unsubscribe: () => { }
}
});
}
return new Observable(observer => {
let canceld = false;
Promise.resolve(param)
.then(val => {
if (!canceld) {
observer.next(val);
observer.complete();
}
})
.catch(e => {
observer.error(e);
});
return {
unsubscribe: () => { canceld = true }
}
})
}
export function interval(delay) {
return new Observable(observer => {
let index = 0;
const time = setInterval((() => {
observer.next(index++)
}), delay)
return {
unsubscribe: () => clearInterval(time)
}
})
}
export function timer(delay) {
return new Observable(observer => {
const time = setTimeout((() => {
observer.next(0)
}), delay)
return {
unsubscribe: () => clearTimeout(time)
}
})
}
export function filter(fn) {
return (observable) => (
new Observable(observer => {
observable.subscribe({
next: val => fn(val) ? observer.next(val) : () => { },
error: err => observer.error(err),
complete: () => observer.complete(),
})
})
)
}
export function map(fn) {
return (observable) => (
new Observable(observer => {
observable.subscribe({
next: val => observer.next(fn(val)),
error: err => observer.error(err),
complete: () => observer.complete(),
})
})
)
}
export function take(num) {
return (observable) => (
new Observable(observer => {
let times = 0;
const subscription = observable.subscribe({
next: val => {
times++;
if (num >= times) {
observer.next(val)
} else {
observer.complete()
subscription.unsubscribe()
}
},
error: err => observer.error(err),
complete: () => observer.complete(),
})
})
)
}
export function tap(fn) {
return (observable) => (
new Observable(observer => {
observable.subscribe({
next: val => {
fn(val);
observer.next(val);
},
error: err => observer.error(err),
complete: () => observer.complete(),
})
})
)
}
export function merge(...observables) {
return (observable) => {
let completeNum = 0;
if (observable) {
observables = [observable, ...observables];
}
return new Observable(observer => {
observables.forEach(observable => {
observable.subscribe({
next: val => observer.next(val),
error: err => {
observables.forEach(observable.unsubscribe());
observer.error(err)
},
complete: () => {
completeNum++;
if (completeNum === observables.length) {
observer.complete()
}
},
})
})
})
}
}