1 | declare global {
|
2 | // eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- It has to be an `interface` so that it can be merged.
|
3 | interface SymbolConstructor {
|
4 | readonly observable: symbol;
|
5 | }
|
6 | }
|
7 |
|
8 | /**
|
9 | @remarks
|
10 | The TC39 observable proposal defines a `closed` property, but some implementations (such as xstream) do not as of 10/08/2021.
|
11 | As well, some guidance on making an `Observable` to not include `closed` property.
|
12 | @see https://github.com/tc39/proposal-observable/blob/master/src/Observable.js#L129-L130
|
13 | @see https://github.com/staltz/xstream/blob/6c22580c1d84d69773ee4b0905df44ad464955b3/src/index.ts#L79-L85
|
14 | @see https://github.com/benlesh/symbol-observable#making-an-object-observable
|
15 |
|
16 | @category Observable
|
17 | */
|
18 | export type Unsubscribable = {
|
19 | unsubscribe(): void;
|
20 | };
|
21 |
|
22 | /**
|
23 | @category Observable
|
24 | */
|
25 | type OnNext<ValueType> = (value: ValueType) => void;
|
26 | /**
|
27 | @category Observable
|
28 | */
|
29 | type OnError = (error: unknown) => void;
|
30 | /**
|
31 | @category Observable
|
32 | */
|
33 | type OnComplete = () => void;
|
34 |
|
35 | /**
|
36 | @category Observable
|
37 | */
|
38 | export type Observer<ValueType> = {
|
39 | next: OnNext<ValueType>;
|
40 | error: OnError;
|
41 | complete: OnComplete;
|
42 | };
|
43 |
|
44 | /**
|
45 | Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
|
46 |
|
47 | @remarks
|
48 | The TC39 Observable proposal defines 2 forms of `subscribe()`:
|
49 | 1. Three callback arguments: `subscribe(observer: OnNext<ValueType>, onError?: OnError, onComplete?: OnComplete): Unsubscribable;`
|
50 | 2. A single `observer` argument: (as defined below)
|
51 |
|
52 | 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)
|
53 |
|
54 | @see https://github.com/tc39/proposal-observable#observable
|
55 | @see https://github.com/tc39/proposal-observable/blob/master/src/Observable.js#L246-L259
|
56 | @see https://benlesh.com/posts/learning-observable-by-building-observable/
|
57 |
|
58 | @category Observable
|
59 | */
|
60 | export type ObservableLike<ValueType = unknown> = {
|
61 | subscribe(observer?: Partial<Observer<ValueType>>): Unsubscribable;
|
62 | [Symbol.observable](): ObservableLike<ValueType>;
|
63 | };
|