@okikio/observables 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +578 -0
- package/esm/_dnt.polyfills.d.ts +20 -0
- package/esm/_dnt.polyfills.d.ts.map +1 -0
- package/esm/_dnt.polyfills.js +12 -0
- package/esm/_spec.d.ts +260 -0
- package/esm/_spec.d.ts.map +1 -0
- package/esm/_spec.js +1 -0
- package/esm/_types.d.ts +141 -0
- package/esm/_types.d.ts.map +1 -0
- package/esm/_types.js +20 -0
- package/esm/error.d.ts +331 -0
- package/esm/error.d.ts.map +1 -0
- package/esm/error.js +408 -0
- package/esm/events.d.ts +320 -0
- package/esm/events.d.ts.map +1 -0
- package/esm/events.js +451 -0
- package/esm/helpers/_types.d.ts +188 -0
- package/esm/helpers/_types.d.ts.map +1 -0
- package/esm/helpers/_types.js +1 -0
- package/esm/helpers/mod.d.ts +90 -0
- package/esm/helpers/mod.d.ts.map +1 -0
- package/esm/helpers/mod.js +90 -0
- package/esm/helpers/operations/batch.d.ts +109 -0
- package/esm/helpers/operations/batch.d.ts.map +1 -0
- package/esm/helpers/operations/batch.js +140 -0
- package/esm/helpers/operations/combination.d.ts +162 -0
- package/esm/helpers/operations/combination.d.ts.map +1 -0
- package/esm/helpers/operations/combination.js +350 -0
- package/esm/helpers/operations/conditional.d.ts +211 -0
- package/esm/helpers/operations/conditional.d.ts.map +1 -0
- package/esm/helpers/operations/conditional.js +280 -0
- package/esm/helpers/operations/core.d.ts +198 -0
- package/esm/helpers/operations/core.d.ts.map +1 -0
- package/esm/helpers/operations/core.js +264 -0
- package/esm/helpers/operations/errors.d.ts +277 -0
- package/esm/helpers/operations/errors.d.ts.map +1 -0
- package/esm/helpers/operations/errors.js +378 -0
- package/esm/helpers/operations/mod.d.ts +26 -0
- package/esm/helpers/operations/mod.d.ts.map +1 -0
- package/esm/helpers/operations/mod.js +25 -0
- package/esm/helpers/operations/timing.d.ts +206 -0
- package/esm/helpers/operations/timing.d.ts.map +1 -0
- package/esm/helpers/operations/timing.js +457 -0
- package/esm/helpers/operators.d.ts +520 -0
- package/esm/helpers/operators.d.ts.map +1 -0
- package/esm/helpers/operators.js +563 -0
- package/esm/helpers/pipe.d.ts +118 -0
- package/esm/helpers/pipe.d.ts.map +1 -0
- package/esm/helpers/pipe.js +129 -0
- package/esm/helpers/utils.d.ts +142 -0
- package/esm/helpers/utils.d.ts.map +1 -0
- package/esm/helpers/utils.js +193 -0
- package/esm/mod.d.ts +863 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +861 -0
- package/esm/observable.d.ts +1610 -0
- package/esm/observable.d.ts.map +1 -0
- package/esm/observable.js +1970 -0
- package/esm/package.json +3 -0
- package/esm/queue.d.ts +201 -0
- package/esm/queue.d.ts.map +1 -0
- package/esm/queue.js +273 -0
- package/esm/symbol.d.ts +60 -0
- package/esm/symbol.d.ts.map +1 -0
- package/esm/symbol.js +132 -0
- package/package.json +96 -0
- package/script/_dnt.polyfills.d.ts +20 -0
- package/script/_dnt.polyfills.d.ts.map +1 -0
- package/script/_dnt.polyfills.js +13 -0
- package/script/_spec.d.ts +260 -0
- package/script/_spec.d.ts.map +1 -0
- package/script/_spec.js +2 -0
- package/script/_types.d.ts +141 -0
- package/script/_types.d.ts.map +1 -0
- package/script/_types.js +22 -0
- package/script/error.d.ts +331 -0
- package/script/error.d.ts.map +1 -0
- package/script/error.js +414 -0
- package/script/events.d.ts +320 -0
- package/script/events.d.ts.map +1 -0
- package/script/events.js +458 -0
- package/script/helpers/_types.d.ts +188 -0
- package/script/helpers/_types.d.ts.map +1 -0
- package/script/helpers/_types.js +2 -0
- package/script/helpers/mod.d.ts +90 -0
- package/script/helpers/mod.d.ts.map +1 -0
- package/script/helpers/mod.js +106 -0
- package/script/helpers/operations/batch.d.ts +109 -0
- package/script/helpers/operations/batch.d.ts.map +1 -0
- package/script/helpers/operations/batch.js +144 -0
- package/script/helpers/operations/combination.d.ts +162 -0
- package/script/helpers/operations/combination.d.ts.map +1 -0
- package/script/helpers/operations/combination.js +355 -0
- package/script/helpers/operations/conditional.d.ts +211 -0
- package/script/helpers/operations/conditional.d.ts.map +1 -0
- package/script/helpers/operations/conditional.js +286 -0
- package/script/helpers/operations/core.d.ts +198 -0
- package/script/helpers/operations/core.d.ts.map +1 -0
- package/script/helpers/operations/core.js +272 -0
- package/script/helpers/operations/errors.d.ts +277 -0
- package/script/helpers/operations/errors.d.ts.map +1 -0
- package/script/helpers/operations/errors.js +387 -0
- package/script/helpers/operations/mod.d.ts +26 -0
- package/script/helpers/operations/mod.d.ts.map +1 -0
- package/script/helpers/operations/mod.js +41 -0
- package/script/helpers/operations/timing.d.ts +206 -0
- package/script/helpers/operations/timing.d.ts.map +1 -0
- package/script/helpers/operations/timing.js +464 -0
- package/script/helpers/operators.d.ts +520 -0
- package/script/helpers/operators.d.ts.map +1 -0
- package/script/helpers/operators.js +570 -0
- package/script/helpers/pipe.d.ts +118 -0
- package/script/helpers/pipe.d.ts.map +1 -0
- package/script/helpers/pipe.js +132 -0
- package/script/helpers/utils.d.ts +142 -0
- package/script/helpers/utils.d.ts.map +1 -0
- package/script/helpers/utils.js +200 -0
- package/script/mod.d.ts +863 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +877 -0
- package/script/observable.d.ts +1610 -0
- package/script/observable.d.ts.map +1 -0
- package/script/observable.js +1984 -0
- package/script/package.json +3 -0
- package/script/queue.d.ts +201 -0
- package/script/queue.d.ts.map +1 -0
- package/script/queue.js +286 -0
- package/script/symbol.d.ts +60 -0
- package/script/symbol.d.ts.map +1 -0
- package/script/symbol.js +135 -0
package/esm/_spec.d.ts
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import type { Symbol } from "./symbol.js";
|
|
2
|
+
/**
|
|
3
|
+
* Defines the minimal contract for subscribable objects in the Observable ecosystem.
|
|
4
|
+
*
|
|
5
|
+
* ObservableProtocol is the core interface that all Observable-like objects must
|
|
6
|
+
* implement. It represents the object returned by `[Symbol.observable]()` that
|
|
7
|
+
* consumers actually call `.subscribe()` on.
|
|
8
|
+
*
|
|
9
|
+
* This interface is designed for interoperability between different Observable
|
|
10
|
+
* implementations, allowing libraries to work with any object that conforms to
|
|
11
|
+
* this protocol.
|
|
12
|
+
*
|
|
13
|
+
* In the TC39 proposal, this is the interface that the object returned by
|
|
14
|
+
* `[Symbol.observable]()` must satisfy.
|
|
15
|
+
*
|
|
16
|
+
* @typeParam T - Type of values emitted by this Observable.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* // Example of an object implementing ObservableProtocol
|
|
21
|
+
* const source: ObservableProtocol<number> = {
|
|
22
|
+
* subscribe(observer) {
|
|
23
|
+
* observer.next?.(1);
|
|
24
|
+
* observer.next?.(2);
|
|
25
|
+
* observer.complete?.();
|
|
26
|
+
* return { unsubscribe() {} };
|
|
27
|
+
* }
|
|
28
|
+
* };
|
|
29
|
+
*
|
|
30
|
+
* // Using the protocol
|
|
31
|
+
* const subscription = source.subscribe({
|
|
32
|
+
* next: value => console.log(value)
|
|
33
|
+
* });
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export interface ObservableProtocol<T> {
|
|
37
|
+
/**
|
|
38
|
+
* Subscribes to this Observable with an observer object.
|
|
39
|
+
*
|
|
40
|
+
* This method is the primary way to consume values from an Observable.
|
|
41
|
+
* It begins the subscription process, connecting the consumer (observer)
|
|
42
|
+
* to the producer, and returns a subscription object that can be used
|
|
43
|
+
* to cancel the subscription.
|
|
44
|
+
*
|
|
45
|
+
* @param observer - Object with callback methods to handle notifications
|
|
46
|
+
* @returns A subscription object for cancellation
|
|
47
|
+
*
|
|
48
|
+
* @specref § 3.1 Observable.prototype.subscribe
|
|
49
|
+
*/
|
|
50
|
+
subscribe(observer: SpecObserver<T>): SpecSubscription;
|
|
51
|
+
/**
|
|
52
|
+
* Subscribes to this Observable with individual callback functions.
|
|
53
|
+
*
|
|
54
|
+
* This is a convenience overload that allows subscribing with individual
|
|
55
|
+
* functions instead of an observer object. Internally, these callbacks
|
|
56
|
+
* are wrapped into an observer object.
|
|
57
|
+
*
|
|
58
|
+
* @param next - Function to handle each emitted value
|
|
59
|
+
* @param error - Optional function to handle errors
|
|
60
|
+
* @param complete - Optional function to handle completion
|
|
61
|
+
* @returns A subscription object for cancellation
|
|
62
|
+
*
|
|
63
|
+
* @specref § 3.1 Observable.prototype.subscribe
|
|
64
|
+
*/
|
|
65
|
+
subscribe(next: (value: T) => void, error?: (error: unknown) => void, complete?: () => void): SpecSubscription;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Represents a cancellable connection to an Observable.
|
|
69
|
+
*
|
|
70
|
+
* A SpecSubscription is returned by the `subscribe` method and provides
|
|
71
|
+
* a way to cancel the subscription, stopping the delivery of values and
|
|
72
|
+
* releasing any resources associated with it.
|
|
73
|
+
*
|
|
74
|
+
* This interface is intentionally minimal, matching the TC39 proposal's
|
|
75
|
+
* requirements for subscription objects.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const subscription = observable.subscribe({
|
|
80
|
+
* next: value => console.log(value)
|
|
81
|
+
* });
|
|
82
|
+
*
|
|
83
|
+
* // Later, to cancel:
|
|
84
|
+
* subscription.unsubscribe();
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export interface SpecSubscription {
|
|
88
|
+
/**
|
|
89
|
+
* Cancels the subscription and releases associated resources.
|
|
90
|
+
*
|
|
91
|
+
* When called:
|
|
92
|
+
* - Execution of the Observable is stopped
|
|
93
|
+
* - No further notifications are delivered to the observer
|
|
94
|
+
* - Resources associated with the subscription are released
|
|
95
|
+
* - If already closed (unsubscribed, errored, or completed), this is a no-op
|
|
96
|
+
*
|
|
97
|
+
* This method is idempotent - calling it multiple times has the same
|
|
98
|
+
* effect as calling it once.
|
|
99
|
+
*
|
|
100
|
+
* @specref § 4.2.2 %SubscriptionPrototype%.unsubscribe
|
|
101
|
+
*/
|
|
102
|
+
unsubscribe(): void;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Defines a consumer of Observable notifications.
|
|
106
|
+
*
|
|
107
|
+
* An Observer is an object with optional callback methods that receive
|
|
108
|
+
* notifications from an Observable:
|
|
109
|
+
* - `next`: Called for each value emitted by the Observable
|
|
110
|
+
* - `error`: Called when an error occurs (terminal)
|
|
111
|
+
* - `complete`: Called when the Observable finishes normally (terminal)
|
|
112
|
+
* - `start`: Called immediately after subscription is established
|
|
113
|
+
*
|
|
114
|
+
* All methods are optional. If a method is missing, the corresponding
|
|
115
|
+
* notification is effectively ignored.
|
|
116
|
+
*
|
|
117
|
+
* @typeParam T - Type of values this observer can receive.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* const observer: SpecObserver<number> = {
|
|
122
|
+
* start(subscription) {
|
|
123
|
+
* console.log('Starting subscription');
|
|
124
|
+
* // Save subscription reference if needed later
|
|
125
|
+
* },
|
|
126
|
+
* next(value) {
|
|
127
|
+
* console.log('Received value:', value);
|
|
128
|
+
* },
|
|
129
|
+
* error(err) {
|
|
130
|
+
* console.error('Error occurred:', err);
|
|
131
|
+
* },
|
|
132
|
+
* complete() {
|
|
133
|
+
* console.log('Observable completed');
|
|
134
|
+
* }
|
|
135
|
+
* };
|
|
136
|
+
*
|
|
137
|
+
* observable.subscribe(observer);
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
export interface SpecObserver<T> {
|
|
141
|
+
/**
|
|
142
|
+
* Called immediately after subscription is established.
|
|
143
|
+
*
|
|
144
|
+
* This callback:
|
|
145
|
+
* - Receives the subscription object as a parameter
|
|
146
|
+
* - Runs before any other observer methods
|
|
147
|
+
* - Allows the observer to store the subscription for later cancellation
|
|
148
|
+
* - Can throw exceptions, which are reported but don't prevent subscription
|
|
149
|
+
*
|
|
150
|
+
* If this method throws, the error is reported to the host environment
|
|
151
|
+
* but the subscription is still established.
|
|
152
|
+
*
|
|
153
|
+
* @param subscription - The subscription object created by this subscribe call
|
|
154
|
+
* @specref § 4.2 CreateSubscription
|
|
155
|
+
*/
|
|
156
|
+
start?(subscription: SpecSubscription): void;
|
|
157
|
+
/**
|
|
158
|
+
* Receives the next value in the sequence.
|
|
159
|
+
*
|
|
160
|
+
* This is the main data channel of an Observable. It:
|
|
161
|
+
* - Is called zero or more times, once per emitted value
|
|
162
|
+
* - Receives each value as its only parameter
|
|
163
|
+
* - Is never called after error or complete
|
|
164
|
+
* - Is never called after unsubscribe
|
|
165
|
+
*
|
|
166
|
+
* If this method throws, the error is delivered to the `error` method.
|
|
167
|
+
*
|
|
168
|
+
* @param value - The value emitted by the Observable
|
|
169
|
+
* @specref § 5.2.4 %SubscriptionObserverPrototype%.next
|
|
170
|
+
*/
|
|
171
|
+
next?(value: T): void;
|
|
172
|
+
/**
|
|
173
|
+
* Handles an error notification.
|
|
174
|
+
*
|
|
175
|
+
* This is called when:
|
|
176
|
+
* - The Observable encounters an error during execution
|
|
177
|
+
* - The observer's next or complete methods throw
|
|
178
|
+
*
|
|
179
|
+
* After this method is called:
|
|
180
|
+
* - The subscription is considered closed
|
|
181
|
+
* - No more notifications will be delivered
|
|
182
|
+
* - Resources associated with the subscription are released
|
|
183
|
+
*
|
|
184
|
+
* If this method throws, the error is reported to the host environment.
|
|
185
|
+
*
|
|
186
|
+
* @param error - The error that occurred
|
|
187
|
+
* @specref § 5.2.5 %SubscriptionObserverPrototype%.error
|
|
188
|
+
*/
|
|
189
|
+
error?(error: unknown): void;
|
|
190
|
+
/**
|
|
191
|
+
* Handles successful completion of the Observable.
|
|
192
|
+
*
|
|
193
|
+
* This is called when:
|
|
194
|
+
* - The Observable has finished emitting values normally
|
|
195
|
+
* - No more values will be emitted
|
|
196
|
+
*
|
|
197
|
+
* After this method is called:
|
|
198
|
+
* - The subscription is considered closed
|
|
199
|
+
* - No more notifications will be delivered
|
|
200
|
+
* - Resources associated with the subscription are released
|
|
201
|
+
*
|
|
202
|
+
* If this method throws, the error is delivered to the `error` method.
|
|
203
|
+
*
|
|
204
|
+
* @specref § 5.2.6 %SubscriptionObserverPrototype%.complete
|
|
205
|
+
*/
|
|
206
|
+
complete?(): void;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Defines an object that can be converted to an Observable.
|
|
210
|
+
*
|
|
211
|
+
* This interface represents anything that has a `[Symbol.observable]()` method,
|
|
212
|
+
* making it interoperable with the Observable ecosystem. It's the TypeScript
|
|
213
|
+
* equivalent of the TC39 proposal's "Observable-like" concept.
|
|
214
|
+
*
|
|
215
|
+
* The method must return an ObservableProtocol that can be subscribed to.
|
|
216
|
+
* This enables interoperability between different Observable implementations
|
|
217
|
+
* and allows conversion of custom types to Observables.
|
|
218
|
+
*
|
|
219
|
+
* @typeParam T - Type of values the resulting Observable will emit.
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```ts
|
|
223
|
+
* // A simple string sequence as an Observable-like
|
|
224
|
+
* const greetings: SpecObservable<string> = {
|
|
225
|
+
* [Symbol.observable]() {
|
|
226
|
+
* return {
|
|
227
|
+
* subscribe(observer) {
|
|
228
|
+
* observer.next?.('Hello');
|
|
229
|
+
* observer.next?.('World');
|
|
230
|
+
* observer.complete?.();
|
|
231
|
+
* return { unsubscribe() {} };
|
|
232
|
+
* }
|
|
233
|
+
* };
|
|
234
|
+
* }
|
|
235
|
+
* };
|
|
236
|
+
*
|
|
237
|
+
* // Convert to an Observable
|
|
238
|
+
* const observable = Observable.from(greetings);
|
|
239
|
+
* ```
|
|
240
|
+
*
|
|
241
|
+
* @see https://tc39.es/proposal-observable/#observable-interface
|
|
242
|
+
* @specref § 3.3 Observable.prototype[@@observable]
|
|
243
|
+
*/
|
|
244
|
+
export interface SpecObservable<T> {
|
|
245
|
+
/**
|
|
246
|
+
* Returns an object that conforms to the ObservableProtocol.
|
|
247
|
+
*
|
|
248
|
+
* This method acts as a conversion point that transforms any object
|
|
249
|
+
* into an Observable-like entity that can be subscribed to.
|
|
250
|
+
*
|
|
251
|
+
* When called:
|
|
252
|
+
* - It should return an object with a `subscribe` method
|
|
253
|
+
* - That object must conform to the ObservableProtocol interface
|
|
254
|
+
* - The returned object becomes the actual subscription target
|
|
255
|
+
*
|
|
256
|
+
* @returns An ObservableProtocol that can be subscribed to
|
|
257
|
+
*/
|
|
258
|
+
[Symbol.observable](): ObservableProtocol<T>;
|
|
259
|
+
}
|
|
260
|
+
//# sourceMappingURL=_spec.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_spec.d.ts","sourceRoot":"","sources":["../src/_spec.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,WAAW,kBAAkB,CAAC,CAAC;IACnC;;;;;;;;;;;;OAYG;IACH,SAAS,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,gBAAgB,CAAC;IAEvD;;;;;;;;;;;;;OAaG;IACH,SAAS,CACP,IAAI,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,EACxB,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,EAChC,QAAQ,CAAC,EAAE,MAAM,IAAI,GACpB,gBAAgB,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;;;;;;;;;;OAaG;IACH,WAAW,IAAI,IAAI,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,WAAW,YAAY,CAAC,CAAC;IAC7B;;;;;;;;;;;;;;OAcG;IACH,KAAK,CAAC,CAAC,YAAY,EAAE,gBAAgB,GAAG,IAAI,CAAC;IAE7C;;;;;;;;;;;;;OAaG;IACH,IAAI,CAAC,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IAEtB;;;;;;;;;;;;;;;;OAgBG;IACH,KAAK,CAAC,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAAC;IAE7B;;;;;;;;;;;;;;;OAeG;IACH,QAAQ,CAAC,IAAI,IAAI,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,WAAW,cAAc,CAAC,CAAC;IAC/B;;;;;;;;;;;;OAYG;IACH,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,kBAAkB,CAAC,CAAC,CAAC,CAAC;CAC9C"}
|
package/esm/_spec.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/esm/_types.d.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public observer and subscription types for the Observable entrypoints.
|
|
3
|
+
*
|
|
4
|
+
* This module collects the runtime-facing interfaces that show up whenever you
|
|
5
|
+
* subscribe to an Observable. It defines the enhanced `Observer<T>` and
|
|
6
|
+
* `Subscription` shapes used by this package, then re-exports the lower-level
|
|
7
|
+
* spec types from `./_spec.ts` for callers that need proposal-aligned building
|
|
8
|
+
* blocks.
|
|
9
|
+
*
|
|
10
|
+
* Use this entrypoint when you are writing libraries, adapters, or tests that
|
|
11
|
+
* need to talk about Observable contracts without importing the full runtime
|
|
12
|
+
* implementation. In day-to-day app code, you will usually consume these types
|
|
13
|
+
* indirectly through `Observable`, `EventBus`, or operator helpers. They live
|
|
14
|
+
* here so the public type surface stays easy to find and stable across
|
|
15
|
+
* entrypoints.
|
|
16
|
+
*
|
|
17
|
+
* @module
|
|
18
|
+
*/
|
|
19
|
+
import "./_dnt.polyfills.js";
|
|
20
|
+
import type { SpecObserver, SpecSubscription } from "./_spec.js";
|
|
21
|
+
import type { Symbol } from "./symbol.js";
|
|
22
|
+
/**
|
|
23
|
+
* Enhanced Observer interface for our implementation.
|
|
24
|
+
*
|
|
25
|
+
* This extends the minimal SpecObserver with additional capabilities and
|
|
26
|
+
* type-safety for our specific Observable implementation. It provides:
|
|
27
|
+
*
|
|
28
|
+
* 1. Type-safe access to our enhanced Subscription object
|
|
29
|
+
* 2. Consistent method signatures for notification handling
|
|
30
|
+
* 3. Same optional methods as the spec but with our own types
|
|
31
|
+
*
|
|
32
|
+
* While the base spec only requires minimal functionality, our extended
|
|
33
|
+
* Observer provides more guarantees and features.
|
|
34
|
+
*
|
|
35
|
+
* @typeParam T - Type of values this observer can receive.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* import { Observable } from './observable.ts';
|
|
40
|
+
* import type { Observer, Subscription } from './_types.ts';
|
|
41
|
+
*
|
|
42
|
+
* const timerObserver: Observer<number> = {
|
|
43
|
+
* start(subscription) {
|
|
44
|
+
* console.log('Timer started');
|
|
45
|
+
* // Can access enhanced subscription properties
|
|
46
|
+
* console.log('Subscription active:', !subscription.closed);
|
|
47
|
+
* },
|
|
48
|
+
* next(value) {
|
|
49
|
+
* console.log('Tick:', value);
|
|
50
|
+
* },
|
|
51
|
+
* complete() {
|
|
52
|
+
* console.log('Timer completed');
|
|
53
|
+
* }
|
|
54
|
+
* };
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export interface Observer<T> extends SpecObserver<T> {
|
|
58
|
+
/**
|
|
59
|
+
* Called immediately after subscribing with our enhanced Subscription type.
|
|
60
|
+
*
|
|
61
|
+
* This override ensures the subscription passed to start() is our
|
|
62
|
+
* enhanced Subscription type with additional properties and methods,
|
|
63
|
+
* not just the minimal SpecSubscription.
|
|
64
|
+
*
|
|
65
|
+
* @param subscription - Our enhanced Subscription object
|
|
66
|
+
* @specref § 4.2 CreateSubscription
|
|
67
|
+
*/
|
|
68
|
+
start?(subscription: Subscription): void;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Enhanced Subscription interface with additional features.
|
|
72
|
+
*
|
|
73
|
+
* Extends the minimal SpecSubscription with:
|
|
74
|
+
* 1. A `closed` property to check subscription state
|
|
75
|
+
* 2. Support for `using` blocks via Symbol.dispose
|
|
76
|
+
* 3. Support for async cleanup via Symbol.asyncDispose
|
|
77
|
+
* 4. String tag for proper toString() behavior
|
|
78
|
+
*
|
|
79
|
+
* These enhancements make subscriptions more useful and ergonomic
|
|
80
|
+
* while maintaining compatibility with the core specification.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* import { Observable } from './observable.ts';
|
|
85
|
+
*
|
|
86
|
+
* const source = Observable.of(1, 2, 3);
|
|
87
|
+
*
|
|
88
|
+
* // Standard usage
|
|
89
|
+
* const sub = source.subscribe((value) => console.log(value));
|
|
90
|
+
* console.log('Active:', !sub.closed);
|
|
91
|
+
* sub.unsubscribe();
|
|
92
|
+
*
|
|
93
|
+
* // With using blocks (automatically unsubscribes at block end)
|
|
94
|
+
* {
|
|
95
|
+
* using sub = source.subscribe((value) => console.log(value));
|
|
96
|
+
* // Use subscription here...
|
|
97
|
+
* } // Subscription cleaned up here
|
|
98
|
+
*
|
|
99
|
+
* // With async using blocks
|
|
100
|
+
* async function example() {
|
|
101
|
+
* await using sub = source.subscribe((value) => console.log(value));
|
|
102
|
+
* // Use subscription here...
|
|
103
|
+
* } // Awaits cleanup here
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export interface Subscription extends SpecSubscription, Disposable, AsyncDisposable {
|
|
107
|
+
/**
|
|
108
|
+
* Indicates whether this subscription is closed.
|
|
109
|
+
*
|
|
110
|
+
* A subscription becomes closed when:
|
|
111
|
+
* - `unsubscribe()` is called explicitly
|
|
112
|
+
* - The Observable calls observer.error()
|
|
113
|
+
* - The Observable calls observer.complete()
|
|
114
|
+
*
|
|
115
|
+
* Once closed, a subscription cannot be reopened, and no further
|
|
116
|
+
* notifications will be delivered to the observer.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* import { Observable } from './observable.ts';
|
|
121
|
+
*
|
|
122
|
+
* const obs = Observable.of(1, 2, 3);
|
|
123
|
+
* const sub = obs.subscribe(() => {});
|
|
124
|
+
* console.log(sub.closed); // false
|
|
125
|
+
*
|
|
126
|
+
* sub.unsubscribe();
|
|
127
|
+
* console.log(sub.closed); // true
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
readonly closed: boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Provides a standard string tag for the object.
|
|
133
|
+
*
|
|
134
|
+
* Used by Object.prototype.toString to identify this object type.
|
|
135
|
+
* This ensures that `Object.prototype.toString.call(subscription)`
|
|
136
|
+
* returns "[object Subscription]".
|
|
137
|
+
*/
|
|
138
|
+
readonly [Symbol.toStringTag]: "Subscription";
|
|
139
|
+
}
|
|
140
|
+
export type * from "./_spec.js";
|
|
141
|
+
//# sourceMappingURL=_types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_types.d.ts","sourceRoot":"","sources":["../src/_types.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,qBAAqB,CAAC;AAE7B,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACjE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,WAAW,QAAQ,CAAC,CAAC,CAAE,SAAQ,YAAY,CAAC,CAAC,CAAC;IAClD;;;;;;;;;OASG;IACH,KAAK,CAAC,CAAC,YAAY,EAAE,YAAY,GAAG,IAAI,CAAC;CAC1C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,WAAW,YACf,SAAQ,gBAAgB,EAAE,UAAU,EAAE,eAAe;IACrD;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IAwBzB;;;;;;OAMG;IACH,QAAQ,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,EAAE,cAAc,CAAC;CAC/C;AAED,mBAAmB,YAAY,CAAC"}
|
package/esm/_types.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// @filename: _types.ts
|
|
2
|
+
/**
|
|
3
|
+
* Public observer and subscription types for the Observable entrypoints.
|
|
4
|
+
*
|
|
5
|
+
* This module collects the runtime-facing interfaces that show up whenever you
|
|
6
|
+
* subscribe to an Observable. It defines the enhanced `Observer<T>` and
|
|
7
|
+
* `Subscription` shapes used by this package, then re-exports the lower-level
|
|
8
|
+
* spec types from `./_spec.ts` for callers that need proposal-aligned building
|
|
9
|
+
* blocks.
|
|
10
|
+
*
|
|
11
|
+
* Use this entrypoint when you are writing libraries, adapters, or tests that
|
|
12
|
+
* need to talk about Observable contracts without importing the full runtime
|
|
13
|
+
* implementation. In day-to-day app code, you will usually consume these types
|
|
14
|
+
* indirectly through `Observable`, `EventBus`, or operator helpers. They live
|
|
15
|
+
* here so the public type surface stays easy to find and stable across
|
|
16
|
+
* entrypoints.
|
|
17
|
+
*
|
|
18
|
+
* @module
|
|
19
|
+
*/
|
|
20
|
+
import "./_dnt.polyfills.js";
|