@fluixi/reactive 1.0.0-alpha.100
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 +280 -0
- package/dist/cdn/reactive.cjs +2803 -0
- package/dist/cdn/reactive.d.ts +11 -0
- package/dist/cdn/reactive.d.ts.map +1 -0
- package/dist/cdn/reactive.global.js +2 -0
- package/dist/cdn/reactive.js +10 -0
- package/dist/cdn/reactive.mjs +2787 -0
- package/dist/cdn/signal.cjs +299 -0
- package/dist/cdn/signal.d.ts +19 -0
- package/dist/cdn/signal.d.ts.map +1 -0
- package/dist/cdn/signal.global.js +2 -0
- package/dist/cdn/signal.js +18 -0
- package/dist/cdn/signal.mjs +276 -0
- package/dist/index.cjs +2803 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +2787 -0
- package/dist/lib/index.cjs +2804 -0
- package/dist/lib/index.d.ts +8 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/lib/index.js +11 -0
- package/dist/lib/index.mjs +2789 -0
- package/dist/lib/observable/index.cjs +386 -0
- package/dist/lib/observable/index.d.ts +4 -0
- package/dist/lib/observable/index.d.ts.map +1 -0
- package/dist/lib/observable/index.js +3 -0
- package/dist/lib/observable/index.mjs +364 -0
- package/dist/lib/observable/observable.cjs +384 -0
- package/dist/lib/observable/observable.d.ts +246 -0
- package/dist/lib/observable/observable.d.ts.map +1 -0
- package/dist/lib/observable/observable.js +268 -0
- package/dist/lib/observable/observable.mjs +361 -0
- package/dist/lib/observable/subscription.cjs +180 -0
- package/dist/lib/observable/subscription.d.ts +58 -0
- package/dist/lib/observable/subscription.d.ts.map +1 -0
- package/dist/lib/observable/subscription.js +177 -0
- package/dist/lib/observable/subscription.mjs +157 -0
- package/dist/lib/observable/types.cjs +18 -0
- package/dist/lib/observable/types.d.ts +44 -0
- package/dist/lib/observable/types.d.ts.map +1 -0
- package/dist/lib/observable/types.js +1 -0
- package/dist/lib/observable/types.mjs +0 -0
- package/dist/lib/predicates.cjs +44 -0
- package/dist/lib/predicates.d.ts +37 -0
- package/dist/lib/predicates.d.ts.map +1 -0
- package/dist/lib/predicates.js +62 -0
- package/dist/lib/predicates.mjs +23 -0
- package/dist/lib/signal/api.cjs +592 -0
- package/dist/lib/signal/api.d.ts +227 -0
- package/dist/lib/signal/api.d.ts.map +1 -0
- package/dist/lib/signal/api.js +228 -0
- package/dist/lib/signal/api.mjs +569 -0
- package/dist/lib/signal/cached-resource.cjs +726 -0
- package/dist/lib/signal/cached-resource.d.ts +86 -0
- package/dist/lib/signal/cached-resource.d.ts.map +1 -0
- package/dist/lib/signal/cached-resource.js +250 -0
- package/dist/lib/signal/cached-resource.mjs +704 -0
- package/dist/lib/signal/graph/children.cjs +326 -0
- package/dist/lib/signal/graph/children.d.ts +2 -0
- package/dist/lib/signal/graph/children.d.ts.map +1 -0
- package/dist/lib/signal/graph/children.js +30 -0
- package/dist/lib/signal/graph/children.mjs +303 -0
- package/dist/lib/signal/graph/compat.cjs +590 -0
- package/dist/lib/signal/graph/compat.d.ts +66 -0
- package/dist/lib/signal/graph/compat.d.ts.map +1 -0
- package/dist/lib/signal/graph/compat.js +197 -0
- package/dist/lib/signal/graph/compat.mjs +568 -0
- package/dist/lib/signal/graph/core.cjs +295 -0
- package/dist/lib/signal/graph/core.d.ts +99 -0
- package/dist/lib/signal/graph/core.d.ts.map +1 -0
- package/dist/lib/signal/graph/core.js +268 -0
- package/dist/lib/signal/graph/core.mjs +273 -0
- package/dist/lib/signal/graph/debounced.cjs +680 -0
- package/dist/lib/signal/graph/debounced.d.ts +67 -0
- package/dist/lib/signal/graph/debounced.d.ts.map +1 -0
- package/dist/lib/signal/graph/debounced.js +92 -0
- package/dist/lib/signal/graph/debounced.mjs +657 -0
- package/dist/lib/signal/graph/derived.cjs +508 -0
- package/dist/lib/signal/graph/derived.d.ts +14 -0
- package/dist/lib/signal/graph/derived.d.ts.map +1 -0
- package/dist/lib/signal/graph/derived.js +65 -0
- package/dist/lib/signal/graph/derived.mjs +485 -0
- package/dist/lib/signal/graph/detect.cjs +66 -0
- package/dist/lib/signal/graph/detect.d.ts +17 -0
- package/dist/lib/signal/graph/detect.d.ts.map +1 -0
- package/dist/lib/signal/graph/detect.js +39 -0
- package/dist/lib/signal/graph/detect.mjs +45 -0
- package/dist/lib/signal/graph/error-boundary.cjs +313 -0
- package/dist/lib/signal/graph/error-boundary.d.ts +16 -0
- package/dist/lib/signal/graph/error-boundary.d.ts.map +1 -0
- package/dist/lib/signal/graph/error-boundary.js +34 -0
- package/dist/lib/signal/graph/error-boundary.mjs +290 -0
- package/dist/lib/signal/graph/index.cjs +305 -0
- package/dist/lib/signal/graph/index.d.ts +22 -0
- package/dist/lib/signal/graph/index.d.ts.map +1 -0
- package/dist/lib/signal/graph/index.js +21 -0
- package/dist/lib/signal/graph/index.mjs +283 -0
- package/dist/lib/signal/graph/observe.cjs +198 -0
- package/dist/lib/signal/graph/observe.d.ts +185 -0
- package/dist/lib/signal/graph/observe.d.ts.map +1 -0
- package/dist/lib/signal/graph/observe.js +198 -0
- package/dist/lib/signal/graph/observe.mjs +175 -0
- package/dist/lib/signal/graph/resource.cjs +632 -0
- package/dist/lib/signal/graph/resource.d.ts +39 -0
- package/dist/lib/signal/graph/resource.d.ts.map +1 -0
- package/dist/lib/signal/graph/resource.js +165 -0
- package/dist/lib/signal/graph/resource.mjs +609 -0
- package/dist/lib/signal/graph/runtime.cjs +522 -0
- package/dist/lib/signal/graph/runtime.d.ts +80 -0
- package/dist/lib/signal/graph/runtime.d.ts.map +1 -0
- package/dist/lib/signal/graph/runtime.js +358 -0
- package/dist/lib/signal/graph/runtime.mjs +499 -0
- package/dist/lib/signal/graph/signal-next.cjs +1249 -0
- package/dist/lib/signal/graph/signal-next.d.ts +38 -0
- package/dist/lib/signal/graph/signal-next.d.ts.map +1 -0
- package/dist/lib/signal/graph/signal-next.js +44 -0
- package/dist/lib/signal/graph/signal-next.mjs +1227 -0
- package/dist/lib/signal/graph/ssr-hooks.cjs +89 -0
- package/dist/lib/signal/graph/ssr-hooks.d.ts +51 -0
- package/dist/lib/signal/graph/ssr-hooks.d.ts.map +1 -0
- package/dist/lib/signal/graph/ssr-hooks.js +80 -0
- package/dist/lib/signal/graph/ssr-hooks.mjs +68 -0
- package/dist/lib/signal/graph/state.cjs +72 -0
- package/dist/lib/signal/graph/state.d.ts +76 -0
- package/dist/lib/signal/graph/state.d.ts.map +1 -0
- package/dist/lib/signal/graph/state.js +99 -0
- package/dist/lib/signal/graph/state.mjs +49 -0
- package/dist/lib/signal/graph/suspense.cjs +313 -0
- package/dist/lib/signal/graph/suspense.d.ts +13 -0
- package/dist/lib/signal/graph/suspense.d.ts.map +1 -0
- package/dist/lib/signal/graph/suspense.js +36 -0
- package/dist/lib/signal/graph/suspense.mjs +290 -0
- package/dist/lib/signal/index-legacy.cjs +2582 -0
- package/dist/lib/signal/index-legacy.d.ts +12 -0
- package/dist/lib/signal/index-legacy.d.ts.map +1 -0
- package/dist/lib/signal/index-legacy.js +11 -0
- package/dist/lib/signal/index-legacy.mjs +2560 -0
- package/dist/lib/signal/index.cjs +1350 -0
- package/dist/lib/signal/index.d.ts +19 -0
- package/dist/lib/signal/index.d.ts.map +1 -0
- package/dist/lib/signal/index.js +19 -0
- package/dist/lib/signal/index.mjs +1328 -0
- package/dist/lib/signal/list.cjs +561 -0
- package/dist/lib/signal/list.d.ts +39 -0
- package/dist/lib/signal/list.d.ts.map +1 -0
- package/dist/lib/signal/list.js +172 -0
- package/dist/lib/signal/list.mjs +538 -0
- package/dist/lib/signal/resource-api.cjs +638 -0
- package/dist/lib/signal/resource-api.d.ts +19 -0
- package/dist/lib/signal/resource-api.d.ts.map +1 -0
- package/dist/lib/signal/resource-api.js +27 -0
- package/dist/lib/signal/resource-api.mjs +615 -0
- package/dist/lib/signal/signal.cjs +2035 -0
- package/dist/lib/signal/signal.d.ts +303 -0
- package/dist/lib/signal/signal.d.ts.map +1 -0
- package/dist/lib/signal/signal.js +1233 -0
- package/dist/lib/signal/signal.mjs +2012 -0
- package/dist/lib/signal/types.cjs +38 -0
- package/dist/lib/signal/types.d.ts +133 -0
- package/dist/lib/signal/types.d.ts.map +1 -0
- package/dist/lib/signal/types.js +38 -0
- package/dist/lib/signal/types.mjs +17 -0
- package/dist/lib/signal/utilities.cjs +1024 -0
- package/dist/lib/signal/utilities.d.ts +247 -0
- package/dist/lib/signal/utilities.d.ts.map +1 -0
- package/dist/lib/signal/utilities.js +346 -0
- package/dist/lib/signal/utilities.mjs +1002 -0
- package/dist/lib/store/api.cjs +662 -0
- package/dist/lib/store/api.d.ts +40 -0
- package/dist/lib/store/api.d.ts.map +1 -0
- package/dist/lib/store/api.js +63 -0
- package/dist/lib/store/api.mjs +646 -0
- package/dist/lib/store/index.cjs +741 -0
- package/dist/lib/store/index.d.ts +16 -0
- package/dist/lib/store/index.d.ts.map +1 -0
- package/dist/lib/store/index.js +13 -0
- package/dist/lib/store/index.mjs +726 -0
- package/dist/lib/store/setter.types.cjs +18 -0
- package/dist/lib/store/setter.types.d.ts +131 -0
- package/dist/lib/store/setter.types.d.ts.map +1 -0
- package/dist/lib/store/setter.types.js +65 -0
- package/dist/lib/store/setter.types.mjs +0 -0
- package/dist/lib/store/store.cjs +708 -0
- package/dist/lib/store/store.d.ts +142 -0
- package/dist/lib/store/store.d.ts.map +1 -0
- package/dist/lib/store/store.js +812 -0
- package/dist/lib/store/store.mjs +694 -0
- package/dist/lib/store/types.cjs +18 -0
- package/dist/lib/store/types.d.ts +57 -0
- package/dist/lib/store/types.d.ts.map +1 -0
- package/dist/lib/store/types.js +1 -0
- package/dist/lib/store/types.mjs +0 -0
- package/dist/lib/store/utils.cjs +43 -0
- package/dist/lib/store/utils.d.ts +13 -0
- package/dist/lib/store/utils.d.ts.map +1 -0
- package/dist/lib/store/utils.js +29 -0
- package/dist/lib/store/utils.mjs +22 -0
- package/dist/lib/subject.cjs +450 -0
- package/dist/lib/subject.d.ts +14 -0
- package/dist/lib/subject.d.ts.map +1 -0
- package/dist/lib/subject.js +81 -0
- package/dist/lib/subject.mjs +427 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/dist/version.generated.cjs +27 -0
- package/dist/version.generated.d.ts +3 -0
- package/dist/version.generated.d.ts.map +1 -0
- package/dist/version.generated.js +3 -0
- package/dist/version.generated.mjs +7 -0
- package/package.json +115 -0
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { Subscription } from "./subscription.js";
|
|
2
|
+
/**
|
|
3
|
+
* A subscriber is both an Observer and a Subscription.
|
|
4
|
+
* This is the object that gets passed to the Observable's subscribe function.
|
|
5
|
+
*
|
|
6
|
+
* @class Subscriber
|
|
7
|
+
* @extends {Subscription}
|
|
8
|
+
* @implements {Observer<T>}
|
|
9
|
+
*/
|
|
10
|
+
export class Subscriber extends Subscription {
|
|
11
|
+
/** @private */ isStopped = false;
|
|
12
|
+
/** @private */ destination;
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new Subscriber.
|
|
15
|
+
*/
|
|
16
|
+
constructor(destinationOrNext, error, complete) {
|
|
17
|
+
super();
|
|
18
|
+
// Normalize arguments to observer object
|
|
19
|
+
let observer;
|
|
20
|
+
if (!destinationOrNext) {
|
|
21
|
+
observer = {};
|
|
22
|
+
}
|
|
23
|
+
else if (typeof destinationOrNext === 'object') {
|
|
24
|
+
observer = destinationOrNext;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
observer = {
|
|
28
|
+
next: destinationOrNext,
|
|
29
|
+
error: error,
|
|
30
|
+
complete: complete
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
this.destination = observer;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Sends the next value to the observer.
|
|
37
|
+
*/
|
|
38
|
+
next(value) {
|
|
39
|
+
if (!this.isStopped && this.destination.next) {
|
|
40
|
+
try {
|
|
41
|
+
this.destination.next(value);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
this.error(error);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Sends an error notification to the observer.
|
|
50
|
+
*/
|
|
51
|
+
error(err) {
|
|
52
|
+
if (!this.isStopped) {
|
|
53
|
+
this.isStopped = true;
|
|
54
|
+
if (this.destination.error) {
|
|
55
|
+
try {
|
|
56
|
+
this.destination.error(err);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
// Rethrow if error handler throws
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
throw err;
|
|
65
|
+
}
|
|
66
|
+
this.unsubscribe();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Sends a complete notification to the observer.
|
|
71
|
+
*/
|
|
72
|
+
complete() {
|
|
73
|
+
if (!this.isStopped) {
|
|
74
|
+
this.isStopped = true;
|
|
75
|
+
if (this.destination.complete) {
|
|
76
|
+
try {
|
|
77
|
+
this.destination.complete();
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
this.error(error);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
this.unsubscribe();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Unsubscribes the subscriber.
|
|
89
|
+
*/
|
|
90
|
+
unsubscribe() {
|
|
91
|
+
if (this.closed) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
this.isStopped = true;
|
|
95
|
+
super.unsubscribe();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* SafeSubscriber adds extra safety checks to prevent reentrant calls.
|
|
100
|
+
*
|
|
101
|
+
* @class SafeSubscriber
|
|
102
|
+
* @extends {Subscriber<T>}
|
|
103
|
+
*/
|
|
104
|
+
export class SafeSubscriber extends Subscriber {
|
|
105
|
+
/** @private */ _isUnsubscribing = false;
|
|
106
|
+
/** @private */ _parentSubscriber = null;
|
|
107
|
+
constructor(observerOrNext, error, complete, parentSubscriber) {
|
|
108
|
+
super(observerOrNext, error, complete);
|
|
109
|
+
this._parentSubscriber = parentSubscriber || null;
|
|
110
|
+
}
|
|
111
|
+
next(value) {
|
|
112
|
+
if (!this.isStopped && !this._isUnsubscribing) {
|
|
113
|
+
// Use try-catch to ensure errors don't break the subscriber
|
|
114
|
+
try {
|
|
115
|
+
super.next(value);
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
this.error(error);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
error(err) {
|
|
123
|
+
if (!this.isStopped && !this._isUnsubscribing) {
|
|
124
|
+
this._isUnsubscribing = true;
|
|
125
|
+
try {
|
|
126
|
+
super.error(err);
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
this._isUnsubscribing = false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
complete() {
|
|
134
|
+
if (!this.isStopped && !this._isUnsubscribing) {
|
|
135
|
+
this._isUnsubscribing = true;
|
|
136
|
+
try {
|
|
137
|
+
super.complete();
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
this._isUnsubscribing = false;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
unsubscribe() {
|
|
145
|
+
if (this.closed) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
this._isUnsubscribing = true;
|
|
149
|
+
super.unsubscribe();
|
|
150
|
+
this._isUnsubscribing = false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
export function operate(destination) {
|
|
154
|
+
return new Subscriber(destination.next, destination.error, destination.complete);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* A representation of any set of values over any amount of time.
|
|
158
|
+
* This is the most basic building block of RxJS.
|
|
159
|
+
*
|
|
160
|
+
* @class Observable
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* // Create an Observable from scratch
|
|
164
|
+
* const observable = new Observable<string>(subscriber => {
|
|
165
|
+
* subscriber.next('Hello');
|
|
166
|
+
* subscriber.next('World');
|
|
167
|
+
* subscriber.complete();
|
|
168
|
+
*
|
|
169
|
+
* return () => console.log('Cleanup');
|
|
170
|
+
* });
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* // Subscribe to an Observable
|
|
174
|
+
* const subscription = observable.subscribe({
|
|
175
|
+
* next: value => console.log(value),
|
|
176
|
+
* error: err => console.error(err),
|
|
177
|
+
* complete: () => console.log('Done')
|
|
178
|
+
* });
|
|
179
|
+
*
|
|
180
|
+
* // Later, unsubscribe
|
|
181
|
+
* subscription.unsubscribe();
|
|
182
|
+
*/
|
|
183
|
+
export class Observable extends Subscription {
|
|
184
|
+
/**
|
|
185
|
+
* The function that is called when the Observable is subscribed to.
|
|
186
|
+
* This function is given a Subscriber to which new values can be `next`ed,
|
|
187
|
+
* or an `error` or `complete` notification can be sent.
|
|
188
|
+
*
|
|
189
|
+
* @private
|
|
190
|
+
* @type {function(Subscriber<T>): (void | (() => void) | Subscription)}
|
|
191
|
+
*/
|
|
192
|
+
_subscribe;
|
|
193
|
+
/**
|
|
194
|
+
* Creates a new Observable.
|
|
195
|
+
*/
|
|
196
|
+
constructor(subscribe) {
|
|
197
|
+
super();
|
|
198
|
+
if (subscribe) {
|
|
199
|
+
this._subscribe = subscribe;
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
throw new TypeError('Observable constructor requires a subscribe function');
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
subscribe(observerOrNext, error, complete) {
|
|
206
|
+
// Create subscriber
|
|
207
|
+
let subscriber;
|
|
208
|
+
if (observerOrNext instanceof Subscriber) {
|
|
209
|
+
subscriber = observerOrNext;
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
subscriber = new SafeSubscriber(observerOrNext, error, complete);
|
|
213
|
+
}
|
|
214
|
+
// Add finalization logic
|
|
215
|
+
const { _subscribe } = this;
|
|
216
|
+
// Call the subscribe function
|
|
217
|
+
try {
|
|
218
|
+
const result = _subscribe.call(this, subscriber);
|
|
219
|
+
// Handle return value (could be a cleanup function or subscription)
|
|
220
|
+
if (result) {
|
|
221
|
+
if (typeof result === 'function') {
|
|
222
|
+
subscriber.add(result);
|
|
223
|
+
}
|
|
224
|
+
else if (typeof result.unsubscribe === 'function') {
|
|
225
|
+
subscriber.add(result);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
catch (err) {
|
|
230
|
+
// If subscribe function throws, error the subscriber
|
|
231
|
+
subscriber.error(err);
|
|
232
|
+
}
|
|
233
|
+
return subscriber;
|
|
234
|
+
}
|
|
235
|
+
pipe(...operations) {
|
|
236
|
+
if (operations.length === 0) {
|
|
237
|
+
return this;
|
|
238
|
+
}
|
|
239
|
+
// Apply operators in sequence
|
|
240
|
+
return operations.reduce((prev, fn) => fn(prev), this);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Converts the Observable to a Promise.
|
|
244
|
+
* The Promise resolves with the last emitted value when the Observable completes,
|
|
245
|
+
* or rejects if the Observable errors.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* // Convert Observable to Promise
|
|
249
|
+
* const value = await of(1, 2, 3).toPromise();
|
|
250
|
+
* console.log(value); // 3
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* // Handle empty Observable
|
|
254
|
+
* const value = await EMPTY.toPromise();
|
|
255
|
+
* console.log(value); // undefined
|
|
256
|
+
*/
|
|
257
|
+
toPromise() {
|
|
258
|
+
return new Promise((resolve, reject) => {
|
|
259
|
+
let lastValue;
|
|
260
|
+
this.subscribe({
|
|
261
|
+
next: value => lastValue = value,
|
|
262
|
+
error: reject,
|
|
263
|
+
complete: () => resolve(lastValue)
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
// Observable.prototype.toPromise = toPromise;
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
// src/lib/predicates.ts
|
|
2
|
+
function isFunction(value) {
|
|
3
|
+
return typeof value === "function" && !/^class\s/.test(Function.prototype.toString.call(value));
|
|
4
|
+
}
|
|
5
|
+
function isObject(value) {
|
|
6
|
+
if (value == null) return false;
|
|
7
|
+
if (Array.isArray(value)) return false;
|
|
8
|
+
if (typeof value === "function") return false;
|
|
9
|
+
if (value instanceof Date) return false;
|
|
10
|
+
if (value instanceof RegExp) return false;
|
|
11
|
+
if (value instanceof Map) return false;
|
|
12
|
+
if (value instanceof Set) return false;
|
|
13
|
+
if (value instanceof Error) return false;
|
|
14
|
+
return typeof value === "object";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// src/lib/observable/subscription.ts
|
|
18
|
+
var Subscription = class _Subscription {
|
|
19
|
+
/**
|
|
20
|
+
* Creates a new Subscription.
|
|
21
|
+
*/
|
|
22
|
+
constructor(unsubscribe) {
|
|
23
|
+
/** @private */
|
|
24
|
+
this._closed = false;
|
|
25
|
+
/** @private */
|
|
26
|
+
this._parentOrParents = null;
|
|
27
|
+
/** @private */
|
|
28
|
+
this._subscriptions = null;
|
|
29
|
+
if (unsubscribe) {
|
|
30
|
+
this._subscriptions = [unsubscribe];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Whether the subscription is closed.
|
|
35
|
+
*/
|
|
36
|
+
get closed() {
|
|
37
|
+
return this._closed;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Disposes the resources held by the subscription.
|
|
41
|
+
* May be called multiple times (idempotent).
|
|
42
|
+
*/
|
|
43
|
+
unsubscribe() {
|
|
44
|
+
if (this._closed) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
this._closed = true;
|
|
48
|
+
const { _parentOrParents, _subscriptions } = this;
|
|
49
|
+
if (_parentOrParents instanceof _Subscription) {
|
|
50
|
+
_parentOrParents.remove(this);
|
|
51
|
+
} else if (_parentOrParents !== null) {
|
|
52
|
+
for (const parent of _parentOrParents) {
|
|
53
|
+
parent.remove(this);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (_subscriptions) {
|
|
57
|
+
for (const sub of _subscriptions) {
|
|
58
|
+
if (isFunction(sub) && !(sub instanceof _Subscription)) {
|
|
59
|
+
try {
|
|
60
|
+
sub();
|
|
61
|
+
} catch (error) {
|
|
62
|
+
console.error("Error in unsubscribe function:", error);
|
|
63
|
+
}
|
|
64
|
+
} else if (isObject(sub) && typeof sub.unsubscribe === "function") {
|
|
65
|
+
try {
|
|
66
|
+
sub.unsubscribe();
|
|
67
|
+
} catch (error) {
|
|
68
|
+
console.error("Error unsubscribing child:", error);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
this._subscriptions = null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Adds a child subscription or cleanup function to this subscription.
|
|
77
|
+
* When this subscription is unsubscribed, all added subscriptions/functions will be unsubscribed/called.
|
|
78
|
+
*
|
|
79
|
+
* @throws {TypeError} If the argument is not a subscription-like object or function
|
|
80
|
+
*/
|
|
81
|
+
add(subscription) {
|
|
82
|
+
if (!subscription || subscription === this) {
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
if (subscription.closed) {
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
let { _closed, _subscriptions } = this;
|
|
89
|
+
if (_closed) {
|
|
90
|
+
if (isFunction(subscription)) {
|
|
91
|
+
try {
|
|
92
|
+
subscription();
|
|
93
|
+
} catch (error) {
|
|
94
|
+
console.error("Error in unsubscribe function:", error);
|
|
95
|
+
}
|
|
96
|
+
} else if (subscription.unsubscribe) {
|
|
97
|
+
try {
|
|
98
|
+
subscription.unsubscribe();
|
|
99
|
+
} catch (error) {
|
|
100
|
+
console.error("Error unsubscribing:", error);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
if (!_subscriptions) {
|
|
106
|
+
this._subscriptions = [];
|
|
107
|
+
}
|
|
108
|
+
this._subscriptions.push(subscription);
|
|
109
|
+
if (subscription instanceof _Subscription) {
|
|
110
|
+
subscription._addParent(this);
|
|
111
|
+
}
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Removes a child subscription from this subscription.
|
|
116
|
+
*/
|
|
117
|
+
remove(subscription) {
|
|
118
|
+
const { _subscriptions } = this;
|
|
119
|
+
if (!_subscriptions) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const index = _subscriptions.indexOf(subscription);
|
|
123
|
+
if (index !== -1) {
|
|
124
|
+
_subscriptions.splice(index, 1);
|
|
125
|
+
subscription._removeParent(this);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/** @private */
|
|
129
|
+
_addParent(parent) {
|
|
130
|
+
const { _parentOrParents } = this;
|
|
131
|
+
if (!_parentOrParents) {
|
|
132
|
+
this._parentOrParents = parent;
|
|
133
|
+
} else if (Array.isArray(_parentOrParents)) {
|
|
134
|
+
_parentOrParents.push(parent);
|
|
135
|
+
} else {
|
|
136
|
+
this._parentOrParents = [_parentOrParents, parent];
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/** @private */
|
|
140
|
+
_removeParent(parent) {
|
|
141
|
+
const { _parentOrParents } = this;
|
|
142
|
+
if (_parentOrParents === parent) {
|
|
143
|
+
this._parentOrParents = null;
|
|
144
|
+
} else if (Array.isArray(_parentOrParents)) {
|
|
145
|
+
const index = _parentOrParents.indexOf(parent);
|
|
146
|
+
if (index !== -1) {
|
|
147
|
+
_parentOrParents.splice(index, 1);
|
|
148
|
+
if (_parentOrParents.length === 1) {
|
|
149
|
+
this._parentOrParents = _parentOrParents[0];
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// src/lib/observable/observable.ts
|
|
157
|
+
var Subscriber = class extends Subscription {
|
|
158
|
+
/**
|
|
159
|
+
* Creates a new Subscriber.
|
|
160
|
+
*/
|
|
161
|
+
constructor(destinationOrNext, error, complete) {
|
|
162
|
+
super();
|
|
163
|
+
/** @private */
|
|
164
|
+
this.isStopped = false;
|
|
165
|
+
let observer;
|
|
166
|
+
if (!destinationOrNext) {
|
|
167
|
+
observer = {};
|
|
168
|
+
} else if (typeof destinationOrNext === "object") {
|
|
169
|
+
observer = destinationOrNext;
|
|
170
|
+
} else {
|
|
171
|
+
observer = {
|
|
172
|
+
next: destinationOrNext,
|
|
173
|
+
error,
|
|
174
|
+
complete
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
this.destination = observer;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Sends the next value to the observer.
|
|
181
|
+
*/
|
|
182
|
+
next(value) {
|
|
183
|
+
if (!this.isStopped && this.destination.next) {
|
|
184
|
+
try {
|
|
185
|
+
this.destination.next(value);
|
|
186
|
+
} catch (error) {
|
|
187
|
+
this.error(error);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Sends an error notification to the observer.
|
|
193
|
+
*/
|
|
194
|
+
error(err) {
|
|
195
|
+
if (!this.isStopped) {
|
|
196
|
+
this.isStopped = true;
|
|
197
|
+
if (this.destination.error) {
|
|
198
|
+
try {
|
|
199
|
+
this.destination.error(err);
|
|
200
|
+
} catch (error) {
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
} else {
|
|
204
|
+
throw err;
|
|
205
|
+
}
|
|
206
|
+
this.unsubscribe();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Sends a complete notification to the observer.
|
|
211
|
+
*/
|
|
212
|
+
complete() {
|
|
213
|
+
if (!this.isStopped) {
|
|
214
|
+
this.isStopped = true;
|
|
215
|
+
if (this.destination.complete) {
|
|
216
|
+
try {
|
|
217
|
+
this.destination.complete();
|
|
218
|
+
} catch (error) {
|
|
219
|
+
this.error(error);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
this.unsubscribe();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Unsubscribes the subscriber.
|
|
228
|
+
*/
|
|
229
|
+
unsubscribe() {
|
|
230
|
+
if (this.closed) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
this.isStopped = true;
|
|
234
|
+
super.unsubscribe();
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
var SafeSubscriber = class extends Subscriber {
|
|
238
|
+
constructor(observerOrNext, error, complete, parentSubscriber) {
|
|
239
|
+
super(observerOrNext, error, complete);
|
|
240
|
+
/** @private */
|
|
241
|
+
this._isUnsubscribing = false;
|
|
242
|
+
/** @private */
|
|
243
|
+
this._parentSubscriber = null;
|
|
244
|
+
this._parentSubscriber = parentSubscriber || null;
|
|
245
|
+
}
|
|
246
|
+
next(value) {
|
|
247
|
+
if (!this.isStopped && !this._isUnsubscribing) {
|
|
248
|
+
try {
|
|
249
|
+
super.next(value);
|
|
250
|
+
} catch (error) {
|
|
251
|
+
this.error(error);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
error(err) {
|
|
256
|
+
if (!this.isStopped && !this._isUnsubscribing) {
|
|
257
|
+
this._isUnsubscribing = true;
|
|
258
|
+
try {
|
|
259
|
+
super.error(err);
|
|
260
|
+
} finally {
|
|
261
|
+
this._isUnsubscribing = false;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
complete() {
|
|
266
|
+
if (!this.isStopped && !this._isUnsubscribing) {
|
|
267
|
+
this._isUnsubscribing = true;
|
|
268
|
+
try {
|
|
269
|
+
super.complete();
|
|
270
|
+
} finally {
|
|
271
|
+
this._isUnsubscribing = false;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
unsubscribe() {
|
|
276
|
+
if (this.closed) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
this._isUnsubscribing = true;
|
|
280
|
+
super.unsubscribe();
|
|
281
|
+
this._isUnsubscribing = false;
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
function operate(destination) {
|
|
285
|
+
return new Subscriber(destination.next, destination.error, destination.complete);
|
|
286
|
+
}
|
|
287
|
+
var Observable = class extends Subscription {
|
|
288
|
+
/**
|
|
289
|
+
* Creates a new Observable.
|
|
290
|
+
*/
|
|
291
|
+
constructor(subscribe) {
|
|
292
|
+
super();
|
|
293
|
+
if (subscribe) {
|
|
294
|
+
this._subscribe = subscribe;
|
|
295
|
+
} else {
|
|
296
|
+
throw new TypeError("Observable constructor requires a subscribe function");
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
subscribe(observerOrNext, error, complete) {
|
|
300
|
+
let subscriber;
|
|
301
|
+
if (observerOrNext instanceof Subscriber) {
|
|
302
|
+
subscriber = observerOrNext;
|
|
303
|
+
} else {
|
|
304
|
+
subscriber = new SafeSubscriber(observerOrNext, error, complete);
|
|
305
|
+
}
|
|
306
|
+
const { _subscribe } = this;
|
|
307
|
+
try {
|
|
308
|
+
const result = _subscribe.call(this, subscriber);
|
|
309
|
+
if (result) {
|
|
310
|
+
if (typeof result === "function") {
|
|
311
|
+
subscriber.add(result);
|
|
312
|
+
} else if (typeof result.unsubscribe === "function") {
|
|
313
|
+
subscriber.add(result);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
} catch (err) {
|
|
317
|
+
subscriber.error(err);
|
|
318
|
+
}
|
|
319
|
+
return subscriber;
|
|
320
|
+
}
|
|
321
|
+
pipe(...operations) {
|
|
322
|
+
if (operations.length === 0) {
|
|
323
|
+
return this;
|
|
324
|
+
}
|
|
325
|
+
return operations.reduce(
|
|
326
|
+
(prev, fn) => fn(prev),
|
|
327
|
+
this
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Converts the Observable to a Promise.
|
|
332
|
+
* The Promise resolves with the last emitted value when the Observable completes,
|
|
333
|
+
* or rejects if the Observable errors.
|
|
334
|
+
*
|
|
335
|
+
* @example
|
|
336
|
+
* // Convert Observable to Promise
|
|
337
|
+
* const value = await of(1, 2, 3).toPromise();
|
|
338
|
+
* console.log(value); // 3
|
|
339
|
+
*
|
|
340
|
+
* @example
|
|
341
|
+
* // Handle empty Observable
|
|
342
|
+
* const value = await EMPTY.toPromise();
|
|
343
|
+
* console.log(value); // undefined
|
|
344
|
+
*/
|
|
345
|
+
toPromise() {
|
|
346
|
+
return new Promise((resolve, reject) => {
|
|
347
|
+
let lastValue;
|
|
348
|
+
this.subscribe({
|
|
349
|
+
next: (value) => lastValue = value,
|
|
350
|
+
error: reject,
|
|
351
|
+
complete: () => resolve(lastValue)
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
export {
|
|
357
|
+
Observable,
|
|
358
|
+
SafeSubscriber,
|
|
359
|
+
Subscriber,
|
|
360
|
+
operate
|
|
361
|
+
};
|