-
-
Notifications
You must be signed in to change notification settings - Fork 677
Expand file tree
/
Copy pathobservable-like.d.ts
More file actions
78 lines (63 loc) · 2.44 KB
/
observable-like.d.ts
File metadata and controls
78 lines (63 loc) · 2.44 KB
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
declare global {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- It has to be an `interface` so that it can be merged.
interface SymbolConstructor {
readonly observable: symbol;
}
}
/**
@remarks
The TC39 observable proposal defines a `closed` property, but some implementations (such as xstream) do not as of 10/08/2021.
As well, some guidance on making an `Observable` to not include `closed` property.
@see https://github.com/tc39/proposal-observable/blob/master/src/Observable.js#L129-L130
@see https://github.com/staltz/xstream/blob/6c22580c1d84d69773ee4b0905df44ad464955b3/src/index.ts#L79-L85
@see https://github.com/benlesh/symbol-observable#making-an-object-observable
@category Observable
*/
// eslint-disable-next-line type-fest/require-exported-types
export type Unsubscribable = {
unsubscribe(): void;
};
/**
@category Observable
*/
type OnNext<ValueType> = (value: ValueType) => void;
/**
@category Observable
*/
type OnError = (error: unknown) => void;
/**
@category Observable
*/
type OnComplete = () => void;
/**
@category Observable
*/
// eslint-disable-next-line type-fest/require-exported-types
export type Observer<ValueType> = {
next: OnNext<ValueType>;
error: OnError;
complete: OnComplete;
};
/**
Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
You must import it as a sub-import:
@example
```
import type {ObservableLike} from 'type-fest/globals';
```
@remarks
The TC39 Observable proposal defines 2 forms of `subscribe()`:
1. Three callback arguments: `subscribe(observer: OnNext<ValueType>, onError?: OnError, onComplete?: OnComplete): Unsubscribable;`
2. A single `observer` argument: (as defined below)
But `Observable` implementations have evolved to preferring case 2 and some implementations choose not to implement case 1. Therefore, an `ObservableLike` cannot be trusted to implement the first case. (xstream and hand built observerables often do not implement case 1)
@see https://github.com/tc39/proposal-observable#observable
@see https://github.com/tc39/proposal-observable/blob/master/src/Observable.js#L246-L259
@see https://benlesh.com/posts/learning-observable-by-building-observable/
@category Observable
*/
// eslint-disable-next-line type-fest/require-exported-types
export type ObservableLike<ValueType = unknown> = {
subscribe(observer?: Partial<Observer<ValueType>>): Unsubscribable;
[Symbol.observable](): ObservableLike<ValueType>;
};
export {};