-
Notifications
You must be signed in to change notification settings - Fork 5
/
identities.ts
55 lines (47 loc) · 1019 Bytes
/
identities.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
import { Producer, END } from './types';
export function create<T>(
f: (next: (x: T) => void, complete: (err?: any) => void) => void,
onComplete?: (err?: any) => void
): Producer<T> {
return (_, sink) => {
let ended = false;
const next = (x: T) => {
if (ended) return;
sink(1, x);
};
const complete = (err?: any) => {
if (ended) return;
sink(2, err);
onComplete?.(err);
ended = true;
};
sink(0, (_: END) => {
ended = true;
onComplete?.();
});
f(next, complete);
};
}
export function empty<T>(): Producer<T> {
return (_, sink) => {
let ended = false;
sink(0, () => {
ended = true;
});
if (!ended) sink(2);
};
}
export function never<T>(): Producer<T> {
return (_, sink) => {
sink(0, () => {});
};
}
export function throwError<T>(error: any): Producer<T> {
return (_, sink) => {
let ended = false;
sink(0, () => {
ended = true;
});
if (!ended) sink(2, error);
};
}