-
Notifications
You must be signed in to change notification settings - Fork 1
/
Step13.ts
133 lines (115 loc) · 3.31 KB
/
Step13.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
module Step13 {
interface Observer<T> {
next(value: T): void;
error(err: any): void;
complete(): void;
}
type Teardown = () => void;
class Subscriber<T> implements Observer<T> {
closed: Boolean = false;
constructor(private destination: Observer<T>, private subscription: Subscription) {
subscription.add(() => (this.closed = true)); // if this is unsubscribed, don't let antyhing complete or error
}
next(value: T): void {
if (!this.closed) {
this.destination.next(value);
}
}
error(err: any): void {
if (!this.closed) {
this.closed = true;
}
this.destination.error(err);
// unsubscribe from the subscription so that it does it when you want to
this.subscription.unsubscribe();
}
complete(): void {
if (!this.closed) {
this.closed = true;
this.destination.complete();
// unsubscribe from the subscription so that it does it when you want to
this.subscription.unsubscribe();
}
}
}
class Subscription {
private teardowns: Array<Teardown> = [];
add(aTeardown: Teardown) {
this.teardowns.push(aTeardown);
}
unsubscribe() {
for (const teardown of this.teardowns) {
teardown();
}
this.teardowns = [];
}
}
class Observable<T> {
constructor(private Init: (observer: Observer<T>) => Teardown) {}
subscribe(observer: Observer<T>): Subscription {
const subscription = new Subscription();
const subscriber: Observer<T> = new Subscriber(observer, subscription);
subscription.add(this.Init(subscriber));
return subscription;
}
pipe<R>(...functions: Array<(source: Observable<any>) => Observable<any>>): Observable<R> {
return pipe(...functions)(this);
}
}
const map = <T, R>(aFunction: (value: T) => R) => (source: Observable<T>) => {
return new Observable<R>(subscriber => {
const subscription = source.subscribe({
next(value: T) {
subscriber.next(aFunction(value));
},
error(err: any) {
subscriber.error(err);
},
complete() {
subscriber.complete();
}
});
return () => {
subscription.unsubscribe();
};
});
};
const anObservable = new Observable((observer: Observer<number>) => {
let i = 0;
const id = setInterval(() => {
observer.next(i++);
if (i > 3) {
observer.complete();
observer.next(999999); // This doesn't work anymore.
}
}, 1000);
// return a basic teardown
return () => {
console.log('Tearing down!');
clearInterval(id);
};
});
const double = (x: number) => (x * 2);
const teardown = anObservable
.pipe(
map(x => x + 42),
map(double)
)
.subscribe({
next(value: number) {
console.log(value);
},
error(err: any) {
console.error(err);
},
complete() {
console.log('Complete!');
}
});
setTimeout(() => {
teardown.unsubscribe(); // This is now a Subscription
}, 5000); // doesn't tear down right away.
function pipe(...functions: Array<(source: Observable<any>) => Observable<any>>) {
return (source: Observable<any>) => functions.reduce((previous, aFunction) => aFunction(previous), source);
}
}