event-emission 0.2.0 → 0.3.0
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/README.md +158 -25
- package/dist/event-emission.d.ts +63 -14
- package/dist/event-emission.d.ts.map +1 -1
- package/dist/factory.d.ts +1 -1
- package/dist/factory.d.ts.map +1 -1
- package/dist/index.cjs +614 -291
- package/dist/index.cjs.map +8 -9
- package/dist/index.d.ts +1 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +589 -284
- package/dist/index.js.map +8 -9
- package/dist/interoperability.cjs +1719 -0
- package/dist/interoperability.cjs.map +15 -0
- package/dist/{interop.d.ts → interoperability.d.ts} +2 -1
- package/dist/interoperability.d.ts.map +1 -0
- package/dist/interoperability.js +1669 -0
- package/dist/interoperability.js.map +15 -0
- package/dist/observable.cjs +286 -0
- package/dist/observable.cjs.map +11 -0
- package/dist/observable.js +253 -0
- package/dist/observable.js.map +11 -0
- package/dist/observe.cjs +344 -0
- package/dist/observe.cjs.map +10 -0
- package/dist/observe.d.ts +6 -1
- package/dist/observe.d.ts.map +1 -1
- package/dist/observe.js +313 -0
- package/dist/observe.js.map +10 -0
- package/dist/symbols.d.ts +1 -1
- package/dist/types.cjs +35 -0
- package/dist/types.cjs.map +9 -0
- package/dist/types.d.ts +73 -25
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +3 -0
- package/dist/types.js.map +9 -0
- package/package.json +25 -1
- package/src/event-emission.ts +140 -21
- package/src/factory.ts +686 -230
- package/src/index.ts +4 -33
- package/src/{interop.ts → interoperability.ts} +34 -6
- package/src/observe.ts +54 -17
- package/src/symbols.ts +1 -1
- package/src/types.ts +115 -33
- package/dist/interop.d.ts.map +0 -1
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/symbols.ts", "../src/observable.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"/**\n * Symbol.observable polyfill for TC39 Observable interoperability.\n * This ensures the symbol exists even in environments that don't support it natively.\n */\nexport const SymbolObservable: symbol =\n (typeof Symbol === 'function' && (Symbol as { observable?: symbol }).observable) ||\n Symbol.for('@@observable');\n\n// Assign to make Symbol.observable available\nif (typeof Symbol === 'function') {\n (Symbol as { observable?: symbol }).observable = SymbolObservable;\n}\n",
|
|
6
|
+
"/* eslint-disable eslint-comments/disable-enable-pair */\n/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable @typescript-eslint/no-unsafe-assignment */\n/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n/* eslint-disable @typescript-eslint/no-unsafe-call */\n/* eslint-disable @typescript-eslint/no-unsafe-return */\n/* eslint-disable @typescript-eslint/no-unsafe-function-type */\n\nimport { SymbolObservable } from './symbols';\nimport type {\n ObservableLike,\n Observer as ObserverInterface,\n Subscription as SubscriptionInterface,\n} from './types';\n\n/**\n * Helper to get a method from an object.\n */\nfunction getMethod(obj: any, key: string): Function | undefined {\n if (obj === null || obj === undefined) return undefined;\n const value = obj[key];\n if (value == null) return undefined;\n if (typeof value !== 'function') {\n throw new TypeError(value + ' is not a function');\n }\n return value;\n}\n\n/**\n * Helper to report an error that cannot be delivered to an observer.\n */\nfunction hostReportError(e: any) {\n if (typeof queueMicrotask === 'function') {\n queueMicrotask(() => {\n throw e;\n });\n } else {\n setTimeout(() => {\n throw e;\n });\n }\n}\n\n/**\n * Normalized observer used within the Subscriber function.\n */\nexport interface SubscriptionObserver<T> {\n next(value: T): void;\n error(errorValue: unknown): void;\n complete(): void;\n readonly closed: boolean;\n}\n\n/**\n * Internal SubscriptionObserver class required by TC39 spec.\n */\nfunction SubscriptionObserverImpl(this: any, subscription: any) {\n this._subscription = subscription;\n}\n\nSubscriptionObserverImpl.prototype = Object.create(Object.prototype);\nObject.defineProperties(SubscriptionObserverImpl.prototype, {\n constructor: { value: Object, configurable: true, writable: true },\n closed: {\n get() {\n return this._subscription._closed;\n },\n configurable: true,\n },\n next: {\n value: function next(this: any, value: any) {\n const subscription = this._subscription;\n if (subscription._closed) return undefined;\n const observer = subscription._observer;\n try {\n const m = getMethod(observer, 'next');\n if (!m) return undefined;\n return m.call(observer, value);\n } catch (e) {\n try {\n this.error(e);\n } catch (err) {\n hostReportError(err);\n }\n }\n },\n configurable: true,\n writable: true,\n },\n error: {\n value: function error(this: any, errorValue: any) {\n const subscription = this._subscription;\n if (subscription._closed) throw errorValue;\n subscription._closed = true;\n const observer = subscription._observer;\n try {\n const m = getMethod(observer, 'error');\n if (m) {\n return m.call(observer, errorValue);\n }\n throw errorValue;\n } finally {\n subscription._cleanup();\n }\n },\n configurable: true,\n writable: true,\n },\n complete: {\n value: function complete(this: any, value: any) {\n const subscription = this._subscription;\n if (subscription._closed) return undefined;\n subscription._closed = true;\n const observer = subscription._observer;\n try {\n const m = getMethod(observer, 'complete');\n if (m) {\n return m.call(observer, value);\n }\n return undefined;\n } finally {\n subscription._cleanup();\n }\n },\n configurable: true,\n writable: true,\n },\n});\n\nObject.defineProperty(SubscriptionObserverImpl.prototype.next, 'length', { value: 1 });\nObject.defineProperty(SubscriptionObserverImpl.prototype.error, 'length', { value: 1 });\nObject.defineProperty(SubscriptionObserverImpl.prototype.complete, 'length', {\n value: 1,\n});\n\n/**\n * Internal Subscription class required by TC39 spec.\n */\nfunction Subscription(this: any, observer: any, subscriber: any) {\n this._observer = observer;\n this._cleanupFn = undefined;\n this._closed = false;\n\n const subscriptionObserver = new (SubscriptionObserverImpl as any)(this);\n\n try {\n const start = getMethod(observer, 'start');\n if (start) {\n start.call(observer, this);\n }\n } catch (e) {\n hostReportError(e);\n }\n\n if (this._closed) return;\n\n try {\n const cleanup = subscriber(subscriptionObserver);\n if (cleanup != null) {\n if (typeof cleanup !== 'function' && typeof cleanup.unsubscribe !== 'function') {\n throw new TypeError(cleanup + ' is not a function or a subscription');\n }\n this._cleanupFn = cleanup;\n if (this._closed) {\n this._cleanup();\n }\n }\n } catch (e) {\n subscriptionObserver.error(e);\n }\n}\n\nSubscription.prototype = Object.create(Object.prototype);\nObject.defineProperties(Subscription.prototype, {\n constructor: { value: Object, configurable: true, writable: true },\n closed: {\n get() {\n return this._closed;\n },\n configurable: true,\n },\n unsubscribe: {\n value: function unsubscribe(this: any) {\n if (this._closed) return;\n this._closed = true;\n this._cleanup();\n },\n configurable: true,\n writable: true,\n },\n _cleanup: {\n value: function _cleanup(this: any) {\n const cleanup = this._cleanupFn;\n if (!cleanup) return;\n this._cleanupFn = undefined;\n try {\n if (typeof cleanup === 'function') {\n cleanup();\n } else if (cleanup && typeof cleanup.unsubscribe === 'function') {\n cleanup.unsubscribe();\n }\n } catch (e) {\n hostReportError(e);\n }\n },\n configurable: true,\n writable: true,\n },\n});\n\n/**\n * A proper TC39 Observable implementation.\n */\nexport class Observable<T> implements ObservableLike<T> {\n private _subscriber: Subscriber<T>;\n\n constructor(subscriber: Subscriber<T>) {\n if (typeof subscriber !== 'function') {\n throw new TypeError('Observable initializer must be a function');\n }\n this._subscriber = subscriber;\n }\n\n subscribe(\n observerOrNext?: ObserverInterface<T> | ((value: T) => void),\n error?: (err: unknown) => void,\n complete?: () => void,\n ): SubscriptionInterface {\n let observer: any;\n if (typeof observerOrNext === 'function') {\n observer = {\n next: observerOrNext,\n error,\n complete,\n };\n } else if (typeof observerOrNext !== 'object' || observerOrNext === null) {\n throw new TypeError(observerOrNext + ' is not an object');\n } else {\n observer = observerOrNext;\n }\n\n return new (Subscription as any)(observer, this._subscriber);\n }\n\n [SymbolObservable](): Observable<T> {\n return this;\n }\n\n static of<U>(...items: U[]): Observable<U> {\n const C = typeof this === 'function' ? this : Observable;\n return new (C as any)((observer: SubscriptionObserverInterface<U>) => {\n for (let i = 0; i < items.length; ++i) {\n observer.next(items[i]);\n if (observer.closed) return;\n }\n observer.complete();\n });\n }\n\n static from<U>(x: any): Observable<U> {\n const C = typeof this === 'function' ? this : Observable;\n if (x == null) throw new TypeError(x + ' is not an object');\n\n const method = x[SymbolObservable];\n if (method != null) {\n if (typeof method !== 'function') {\n throw new TypeError(method + ' is not a function');\n }\n const observable = method.call(x);\n if (Object(observable) !== observable) {\n throw new TypeError(observable + ' is not an object');\n }\n if (observable.constructor === C) {\n return observable;\n }\n return new (C as any)((observer: SubscriptionObserverInterface<U>) =>\n observable.subscribe(observer),\n );\n }\n\n if (Symbol.iterator in x) {\n return new (C as any)((observer: SubscriptionObserverInterface<U>) => {\n for (const item of x as Iterable<U>) {\n observer.next(item);\n if (observer.closed) return;\n }\n observer.complete();\n });\n }\n\n throw new TypeError(x + ' is not observable');\n }\n}\n\n/**\n * Function called when a new subscription is created.\n */\nexport type Subscriber<T> = (\n observer: SubscriptionObserverInterface<T>,\n) => (() => void) | SubscriptionInterface | void;\n\n/**\n * Normalized observer used within the Subscriber function.\n */\nexport interface SubscriptionObserverInterface<T> {\n next(value: T): void;\n error(errorValue: unknown): void;\n complete(): void;\n readonly closed: boolean;\n}\n"
|
|
7
|
+
],
|
|
8
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIO,IAAM,mBACV,OAAO,WAAW,cAAe,OAAmC,cACrE,OAAO,IAAI,cAAc;AAG3B,IAAI,OAAO,WAAW,YAAY;AAAA,EAC/B,OAAmC,aAAa;AACnD;;;ACOA,SAAS,SAAS,CAAC,KAAU,KAAmC;AAAA,EAC9D,IAAI,QAAQ,QAAQ,QAAQ;AAAA,IAAW;AAAA,EACvC,MAAM,QAAQ,IAAI;AAAA,EAClB,IAAI,SAAS;AAAA,IAAM;AAAA,EACnB,IAAI,OAAO,UAAU,YAAY;AAAA,IAC/B,MAAM,IAAI,UAAU,QAAQ,oBAAoB;AAAA,EAClD;AAAA,EACA,OAAO;AAAA;AAMT,SAAS,eAAe,CAAC,GAAQ;AAAA,EAC/B,IAAI,OAAO,mBAAmB,YAAY;AAAA,IACxC,eAAe,MAAM;AAAA,MACnB,MAAM;AAAA,KACP;AAAA,EACH,EAAO;AAAA,IACL,WAAW,MAAM;AAAA,MAC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G;AAAA,QAChC,OAAO;AAAA,MACT;AAAA,MACA,OAAO,IAAK,EAAU,CAAC,aACrB,WAAW,UAAU,QAAQ,CAC/B;AAAA,IACF;AAAA,IAEA,IAAI,OAAO,YAAY,GAAG;AAAA,MACxB,OAAO,IAAK,EAAU,CAAC,aAA+C;AAAA,QACpE,WAAW,QAAQ,GAAkB;AAAA,UACnC,SAAS,KAAK,IAAI;AAAA,UAClB,IAAI,SAAS;AAAA,YAAQ;AAAA,QACvB;AAAA,QACA,SAAS,SAAS;AAAA,OACnB;AAAA,IACH;AAAA,IAEA,MAAM,IAAI,UAAU,IAAI,oBAAoB;AAAA;AAEhD;",
|
|
9
|
+
"debugId": "F80C646C437BF27964756E2164756E21",
|
|
10
|
+
"names": []
|
|
11
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/symbols.ts
|
|
3
|
+
var SymbolObservable = typeof Symbol === "function" && Symbol.observable || Symbol.for("@@observable");
|
|
4
|
+
if (typeof Symbol === "function") {
|
|
5
|
+
Symbol.observable = SymbolObservable;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// src/observable.ts
|
|
9
|
+
function getMethod(obj, key) {
|
|
10
|
+
if (obj === null || obj === undefined)
|
|
11
|
+
return;
|
|
12
|
+
const value = obj[key];
|
|
13
|
+
if (value == null)
|
|
14
|
+
return;
|
|
15
|
+
if (typeof value !== "function") {
|
|
16
|
+
throw new TypeError(value + " is not a function");
|
|
17
|
+
}
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
function hostReportError(e) {
|
|
21
|
+
if (typeof queueMicrotask === "function") {
|
|
22
|
+
queueMicrotask(() => {
|
|
23
|
+
throw e;
|
|
24
|
+
});
|
|
25
|
+
} else {
|
|
26
|
+
setTimeout(() => {
|
|
27
|
+
throw e;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function SubscriptionObserverImpl(subscription) {
|
|
32
|
+
this._subscription = subscription;
|
|
33
|
+
}
|
|
34
|
+
SubscriptionObserverImpl.prototype = Object.create(Object.prototype);
|
|
35
|
+
Object.defineProperties(SubscriptionObserverImpl.prototype, {
|
|
36
|
+
constructor: { value: Object, configurable: true, writable: true },
|
|
37
|
+
closed: {
|
|
38
|
+
get() {
|
|
39
|
+
return this._subscription._closed;
|
|
40
|
+
},
|
|
41
|
+
configurable: true
|
|
42
|
+
},
|
|
43
|
+
next: {
|
|
44
|
+
value: function next(value) {
|
|
45
|
+
const subscription = this._subscription;
|
|
46
|
+
if (subscription._closed)
|
|
47
|
+
return;
|
|
48
|
+
const observer = subscription._observer;
|
|
49
|
+
try {
|
|
50
|
+
const m = getMethod(observer, "next");
|
|
51
|
+
if (!m)
|
|
52
|
+
return;
|
|
53
|
+
return m.call(observer, value);
|
|
54
|
+
} catch (e) {
|
|
55
|
+
try {
|
|
56
|
+
this.error(e);
|
|
57
|
+
} catch (err) {
|
|
58
|
+
hostReportError(err);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
configurable: true,
|
|
63
|
+
writable: true
|
|
64
|
+
},
|
|
65
|
+
error: {
|
|
66
|
+
value: function error(errorValue) {
|
|
67
|
+
const subscription = this._subscription;
|
|
68
|
+
if (subscription._closed)
|
|
69
|
+
throw errorValue;
|
|
70
|
+
subscription._closed = true;
|
|
71
|
+
const observer = subscription._observer;
|
|
72
|
+
try {
|
|
73
|
+
const m = getMethod(observer, "error");
|
|
74
|
+
if (m) {
|
|
75
|
+
return m.call(observer, errorValue);
|
|
76
|
+
}
|
|
77
|
+
throw errorValue;
|
|
78
|
+
} finally {
|
|
79
|
+
subscription._cleanup();
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
configurable: true,
|
|
83
|
+
writable: true
|
|
84
|
+
},
|
|
85
|
+
complete: {
|
|
86
|
+
value: function complete(value) {
|
|
87
|
+
const subscription = this._subscription;
|
|
88
|
+
if (subscription._closed)
|
|
89
|
+
return;
|
|
90
|
+
subscription._closed = true;
|
|
91
|
+
const observer = subscription._observer;
|
|
92
|
+
try {
|
|
93
|
+
const m = getMethod(observer, "complete");
|
|
94
|
+
if (m) {
|
|
95
|
+
return m.call(observer, value);
|
|
96
|
+
}
|
|
97
|
+
return;
|
|
98
|
+
} finally {
|
|
99
|
+
subscription._cleanup();
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
configurable: true,
|
|
103
|
+
writable: true
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
Object.defineProperty(SubscriptionObserverImpl.prototype.next, "length", { value: 1 });
|
|
107
|
+
Object.defineProperty(SubscriptionObserverImpl.prototype.error, "length", { value: 1 });
|
|
108
|
+
Object.defineProperty(SubscriptionObserverImpl.prototype.complete, "length", {
|
|
109
|
+
value: 1
|
|
110
|
+
});
|
|
111
|
+
function Subscription(observer, subscriber) {
|
|
112
|
+
this._observer = observer;
|
|
113
|
+
this._cleanupFn = undefined;
|
|
114
|
+
this._closed = false;
|
|
115
|
+
const subscriptionObserver = new SubscriptionObserverImpl(this);
|
|
116
|
+
try {
|
|
117
|
+
const start = getMethod(observer, "start");
|
|
118
|
+
if (start) {
|
|
119
|
+
start.call(observer, this);
|
|
120
|
+
}
|
|
121
|
+
} catch (e) {
|
|
122
|
+
hostReportError(e);
|
|
123
|
+
}
|
|
124
|
+
if (this._closed)
|
|
125
|
+
return;
|
|
126
|
+
try {
|
|
127
|
+
const cleanup = subscriber(subscriptionObserver);
|
|
128
|
+
if (cleanup != null) {
|
|
129
|
+
if (typeof cleanup !== "function" && typeof cleanup.unsubscribe !== "function") {
|
|
130
|
+
throw new TypeError(cleanup + " is not a function or a subscription");
|
|
131
|
+
}
|
|
132
|
+
this._cleanupFn = cleanup;
|
|
133
|
+
if (this._closed) {
|
|
134
|
+
this._cleanup();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
} catch (e) {
|
|
138
|
+
subscriptionObserver.error(e);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
Subscription.prototype = Object.create(Object.prototype);
|
|
142
|
+
Object.defineProperties(Subscription.prototype, {
|
|
143
|
+
constructor: { value: Object, configurable: true, writable: true },
|
|
144
|
+
closed: {
|
|
145
|
+
get() {
|
|
146
|
+
return this._closed;
|
|
147
|
+
},
|
|
148
|
+
configurable: true
|
|
149
|
+
},
|
|
150
|
+
unsubscribe: {
|
|
151
|
+
value: function unsubscribe() {
|
|
152
|
+
if (this._closed)
|
|
153
|
+
return;
|
|
154
|
+
this._closed = true;
|
|
155
|
+
this._cleanup();
|
|
156
|
+
},
|
|
157
|
+
configurable: true,
|
|
158
|
+
writable: true
|
|
159
|
+
},
|
|
160
|
+
_cleanup: {
|
|
161
|
+
value: function _cleanup() {
|
|
162
|
+
const cleanup = this._cleanupFn;
|
|
163
|
+
if (!cleanup)
|
|
164
|
+
return;
|
|
165
|
+
this._cleanupFn = undefined;
|
|
166
|
+
try {
|
|
167
|
+
if (typeof cleanup === "function") {
|
|
168
|
+
cleanup();
|
|
169
|
+
} else if (cleanup && typeof cleanup.unsubscribe === "function") {
|
|
170
|
+
cleanup.unsubscribe();
|
|
171
|
+
}
|
|
172
|
+
} catch (e) {
|
|
173
|
+
hostReportError(e);
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
configurable: true,
|
|
177
|
+
writable: true
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
class Observable {
|
|
182
|
+
_subscriber;
|
|
183
|
+
constructor(subscriber) {
|
|
184
|
+
if (typeof subscriber !== "function") {
|
|
185
|
+
throw new TypeError("Observable initializer must be a function");
|
|
186
|
+
}
|
|
187
|
+
this._subscriber = subscriber;
|
|
188
|
+
}
|
|
189
|
+
subscribe(observerOrNext, error2, complete2) {
|
|
190
|
+
let observer;
|
|
191
|
+
if (typeof observerOrNext === "function") {
|
|
192
|
+
observer = {
|
|
193
|
+
next: observerOrNext,
|
|
194
|
+
error: error2,
|
|
195
|
+
complete: complete2
|
|
196
|
+
};
|
|
197
|
+
} else if (typeof observerOrNext !== "object" || observerOrNext === null) {
|
|
198
|
+
throw new TypeError(observerOrNext + " is not an object");
|
|
199
|
+
} else {
|
|
200
|
+
observer = observerOrNext;
|
|
201
|
+
}
|
|
202
|
+
return new Subscription(observer, this._subscriber);
|
|
203
|
+
}
|
|
204
|
+
[SymbolObservable]() {
|
|
205
|
+
return this;
|
|
206
|
+
}
|
|
207
|
+
static of(...items) {
|
|
208
|
+
const C = typeof this === "function" ? this : Observable;
|
|
209
|
+
return new C((observer) => {
|
|
210
|
+
for (let i = 0;i < items.length; ++i) {
|
|
211
|
+
observer.next(items[i]);
|
|
212
|
+
if (observer.closed)
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
observer.complete();
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
static from(x) {
|
|
219
|
+
const C = typeof this === "function" ? this : Observable;
|
|
220
|
+
if (x == null)
|
|
221
|
+
throw new TypeError(x + " is not an object");
|
|
222
|
+
const method = x[SymbolObservable];
|
|
223
|
+
if (method != null) {
|
|
224
|
+
if (typeof method !== "function") {
|
|
225
|
+
throw new TypeError(method + " is not a function");
|
|
226
|
+
}
|
|
227
|
+
const observable = method.call(x);
|
|
228
|
+
if (Object(observable) !== observable) {
|
|
229
|
+
throw new TypeError(observable + " is not an object");
|
|
230
|
+
}
|
|
231
|
+
if (observable.constructor === C) {
|
|
232
|
+
return observable;
|
|
233
|
+
}
|
|
234
|
+
return new C((observer) => observable.subscribe(observer));
|
|
235
|
+
}
|
|
236
|
+
if (Symbol.iterator in x) {
|
|
237
|
+
return new C((observer) => {
|
|
238
|
+
for (const item of x) {
|
|
239
|
+
observer.next(item);
|
|
240
|
+
if (observer.closed)
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
observer.complete();
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
throw new TypeError(x + " is not observable");
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
export {
|
|
250
|
+
Observable
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
//# debugId=281EED53C44E423E64756E2164756E21
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/symbols.ts", "../src/observable.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"/**\n * Symbol.observable polyfill for TC39 Observable interoperability.\n * This ensures the symbol exists even in environments that don't support it natively.\n */\nexport const SymbolObservable: symbol =\n (typeof Symbol === 'function' && (Symbol as { observable?: symbol }).observable) ||\n Symbol.for('@@observable');\n\n// Assign to make Symbol.observable available\nif (typeof Symbol === 'function') {\n (Symbol as { observable?: symbol }).observable = SymbolObservable;\n}\n",
|
|
6
|
+
"/* eslint-disable eslint-comments/disable-enable-pair */\n/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable @typescript-eslint/no-unsafe-assignment */\n/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n/* eslint-disable @typescript-eslint/no-unsafe-call */\n/* eslint-disable @typescript-eslint/no-unsafe-return */\n/* eslint-disable @typescript-eslint/no-unsafe-function-type */\n\nimport { SymbolObservable } from './symbols';\nimport type {\n ObservableLike,\n Observer as ObserverInterface,\n Subscription as SubscriptionInterface,\n} from './types';\n\n/**\n * Helper to get a method from an object.\n */\nfunction getMethod(obj: any, key: string): Function | undefined {\n if (obj === null || obj === undefined) return undefined;\n const value = obj[key];\n if (value == null) return undefined;\n if (typeof value !== 'function') {\n throw new TypeError(value + ' is not a function');\n }\n return value;\n}\n\n/**\n * Helper to report an error that cannot be delivered to an observer.\n */\nfunction hostReportError(e: any) {\n if (typeof queueMicrotask === 'function') {\n queueMicrotask(() => {\n throw e;\n });\n } else {\n setTimeout(() => {\n throw e;\n });\n }\n}\n\n/**\n * Normalized observer used within the Subscriber function.\n */\nexport interface SubscriptionObserver<T> {\n next(value: T): void;\n error(errorValue: unknown): void;\n complete(): void;\n readonly closed: boolean;\n}\n\n/**\n * Internal SubscriptionObserver class required by TC39 spec.\n */\nfunction SubscriptionObserverImpl(this: any, subscription: any) {\n this._subscription = subscription;\n}\n\nSubscriptionObserverImpl.prototype = Object.create(Object.prototype);\nObject.defineProperties(SubscriptionObserverImpl.prototype, {\n constructor: { value: Object, configurable: true, writable: true },\n closed: {\n get() {\n return this._subscription._closed;\n },\n configurable: true,\n },\n next: {\n value: function next(this: any, value: any) {\n const subscription = this._subscription;\n if (subscription._closed) return undefined;\n const observer = subscription._observer;\n try {\n const m = getMethod(observer, 'next');\n if (!m) return undefined;\n return m.call(observer, value);\n } catch (e) {\n try {\n this.error(e);\n } catch (err) {\n hostReportError(err);\n }\n }\n },\n configurable: true,\n writable: true,\n },\n error: {\n value: function error(this: any, errorValue: any) {\n const subscription = this._subscription;\n if (subscription._closed) throw errorValue;\n subscription._closed = true;\n const observer = subscription._observer;\n try {\n const m = getMethod(observer, 'error');\n if (m) {\n return m.call(observer, errorValue);\n }\n throw errorValue;\n } finally {\n subscription._cleanup();\n }\n },\n configurable: true,\n writable: true,\n },\n complete: {\n value: function complete(this: any, value: any) {\n const subscription = this._subscription;\n if (subscription._closed) return undefined;\n subscription._closed = true;\n const observer = subscription._observer;\n try {\n const m = getMethod(observer, 'complete');\n if (m) {\n return m.call(observer, value);\n }\n return undefined;\n } finally {\n subscription._cleanup();\n }\n },\n configurable: true,\n writable: true,\n },\n});\n\nObject.defineProperty(SubscriptionObserverImpl.prototype.next, 'length', { value: 1 });\nObject.defineProperty(SubscriptionObserverImpl.prototype.error, 'length', { value: 1 });\nObject.defineProperty(SubscriptionObserverImpl.prototype.complete, 'length', {\n value: 1,\n});\n\n/**\n * Internal Subscription class required by TC39 spec.\n */\nfunction Subscription(this: any, observer: any, subscriber: any) {\n this._observer = observer;\n this._cleanupFn = undefined;\n this._closed = false;\n\n const subscriptionObserver = new (SubscriptionObserverImpl as any)(this);\n\n try {\n const start = getMethod(observer, 'start');\n if (start) {\n start.call(observer, this);\n }\n } catch (e) {\n hostReportError(e);\n }\n\n if (this._closed) return;\n\n try {\n const cleanup = subscriber(subscriptionObserver);\n if (cleanup != null) {\n if (typeof cleanup !== 'function' && typeof cleanup.unsubscribe !== 'function') {\n throw new TypeError(cleanup + ' is not a function or a subscription');\n }\n this._cleanupFn = cleanup;\n if (this._closed) {\n this._cleanup();\n }\n }\n } catch (e) {\n subscriptionObserver.error(e);\n }\n}\n\nSubscription.prototype = Object.create(Object.prototype);\nObject.defineProperties(Subscription.prototype, {\n constructor: { value: Object, configurable: true, writable: true },\n closed: {\n get() {\n return this._closed;\n },\n configurable: true,\n },\n unsubscribe: {\n value: function unsubscribe(this: any) {\n if (this._closed) return;\n this._closed = true;\n this._cleanup();\n },\n configurable: true,\n writable: true,\n },\n _cleanup: {\n value: function _cleanup(this: any) {\n const cleanup = this._cleanupFn;\n if (!cleanup) return;\n this._cleanupFn = undefined;\n try {\n if (typeof cleanup === 'function') {\n cleanup();\n } else if (cleanup && typeof cleanup.unsubscribe === 'function') {\n cleanup.unsubscribe();\n }\n } catch (e) {\n hostReportError(e);\n }\n },\n configurable: true,\n writable: true,\n },\n});\n\n/**\n * A proper TC39 Observable implementation.\n */\nexport class Observable<T> implements ObservableLike<T> {\n private _subscriber: Subscriber<T>;\n\n constructor(subscriber: Subscriber<T>) {\n if (typeof subscriber !== 'function') {\n throw new TypeError('Observable initializer must be a function');\n }\n this._subscriber = subscriber;\n }\n\n subscribe(\n observerOrNext?: ObserverInterface<T> | ((value: T) => void),\n error?: (err: unknown) => void,\n complete?: () => void,\n ): SubscriptionInterface {\n let observer: any;\n if (typeof observerOrNext === 'function') {\n observer = {\n next: observerOrNext,\n error,\n complete,\n };\n } else if (typeof observerOrNext !== 'object' || observerOrNext === null) {\n throw new TypeError(observerOrNext + ' is not an object');\n } else {\n observer = observerOrNext;\n }\n\n return new (Subscription as any)(observer, this._subscriber);\n }\n\n [SymbolObservable](): Observable<T> {\n return this;\n }\n\n static of<U>(...items: U[]): Observable<U> {\n const C = typeof this === 'function' ? this : Observable;\n return new (C as any)((observer: SubscriptionObserverInterface<U>) => {\n for (let i = 0; i < items.length; ++i) {\n observer.next(items[i]);\n if (observer.closed) return;\n }\n observer.complete();\n });\n }\n\n static from<U>(x: any): Observable<U> {\n const C = typeof this === 'function' ? this : Observable;\n if (x == null) throw new TypeError(x + ' is not an object');\n\n const method = x[SymbolObservable];\n if (method != null) {\n if (typeof method !== 'function') {\n throw new TypeError(method + ' is not a function');\n }\n const observable = method.call(x);\n if (Object(observable) !== observable) {\n throw new TypeError(observable + ' is not an object');\n }\n if (observable.constructor === C) {\n return observable;\n }\n return new (C as any)((observer: SubscriptionObserverInterface<U>) =>\n observable.subscribe(observer),\n );\n }\n\n if (Symbol.iterator in x) {\n return new (C as any)((observer: SubscriptionObserverInterface<U>) => {\n for (const item of x as Iterable<U>) {\n observer.next(item);\n if (observer.closed) return;\n }\n observer.complete();\n });\n }\n\n throw new TypeError(x + ' is not observable');\n }\n}\n\n/**\n * Function called when a new subscription is created.\n */\nexport type Subscriber<T> = (\n observer: SubscriptionObserverInterface<T>,\n) => (() => void) | SubscriptionInterface | void;\n\n/**\n * Normalized observer used within the Subscriber function.\n */\nexport interface SubscriptionObserverInterface<T> {\n next(value: T): void;\n error(errorValue: unknown): void;\n complete(): void;\n readonly closed: boolean;\n}\n"
|
|
7
|
+
],
|
|
8
|
+
"mappings": ";;AAIO,IAAM,mBACV,OAAO,WAAW,cAAe,OAAmC,cACrE,OAAO,IAAI,cAAc;AAG3B,IAAI,OAAO,WAAW,YAAY;AAAA,EAC/B,OAAmC,aAAa;AACnD;;;ACOA,SAAS,SAAS,CAAC,KAAU,KAAmC;AAAA,EAC9D,IAAI,QAAQ,QAAQ,QAAQ;AAAA,IAAW;AAAA,EACvC,MAAM,QAAQ,IAAI;AAAA,EAClB,IAAI,SAAS;AAAA,IAAM;AAAA,EACnB,IAAI,OAAO,UAAU,YAAY;AAAA,IAC/B,MAAM,IAAI,UAAU,QAAQ,oBAAoB;AAA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U,SAAS,oBAAoB;AAAA,MACnD;AAAA,MACA,MAAM,aAAa,OAAO,KAAK,CAAC;AAAA,MAChC,IAAI,OAAO,UAAU,MAAM,YAAY;AAAA,QACrC,MAAM,IAAI,UAAU,aAAa,mBAAmB;AAAA,MACtD;AAAA,MACA,IAAI,WAAW,gBAAgB,GAAG;AAAA,QAChC,OAAO;AAAA,MACT;AAAA,MACA,OAAO,IAAK,EAAU,CAAC,aACrB,WAAW,UAAU,QAAQ,CAC/B;AAAA,IACF;AAAA,IAEA,IAAI,OAAO,YAAY,GAAG;AAAA,MACxB,OAAO,IAAK,EAAU,CAAC,aAA+C;AAAA,QACpE,WAAW,QAAQ,GAAkB;AAAA,UACnC,SAAS,KAAK,IAAI;AAAA,UAClB,IAAI,SAAS;AAAA,YAAQ;AAAA,QACvB;AAAA,QACA,SAAS,SAAS;AAAA,OACnB;AAAA,IACH;AAAA,IAEA,MAAM,IAAI,UAAU,IAAI,oBAAoB;AAAA;AAEhD;",
|
|
9
|
+
"debugId": "281EED53C44E423E64756E2164756E21",
|
|
10
|
+
"names": []
|
|
11
|
+
}
|
package/dist/observe.cjs
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
// @bun @bun-cjs
|
|
2
|
+
(function(exports, require, module, __filename, __dirname) {var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
7
|
+
var __toCommonJS = (from) => {
|
|
8
|
+
var entry = __moduleCache.get(from), desc;
|
|
9
|
+
if (entry)
|
|
10
|
+
return entry;
|
|
11
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function")
|
|
13
|
+
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
14
|
+
get: () => from[key],
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
}));
|
|
17
|
+
__moduleCache.set(from, entry);
|
|
18
|
+
return entry;
|
|
19
|
+
};
|
|
20
|
+
var __export = (target, all) => {
|
|
21
|
+
for (var name in all)
|
|
22
|
+
__defProp(target, name, {
|
|
23
|
+
get: all[name],
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
set: (newValue) => all[name] = () => newValue
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// src/observe.ts
|
|
31
|
+
var exports_observe = {};
|
|
32
|
+
__export(exports_observe, {
|
|
33
|
+
setupEventForwarding: () => setupEventForwarding,
|
|
34
|
+
isObserved: () => isObserved,
|
|
35
|
+
getOriginal: () => getOriginal,
|
|
36
|
+
createObservableProxy: () => createObservableProxy,
|
|
37
|
+
PROXY_MARKER: () => PROXY_MARKER,
|
|
38
|
+
ORIGINAL_TARGET: () => ORIGINAL_TARGET
|
|
39
|
+
});
|
|
40
|
+
module.exports = __toCommonJS(exports_observe);
|
|
41
|
+
var PROXY_MARKER = Symbol.for("@lasercat/event-emission/proxy");
|
|
42
|
+
var ORIGINAL_TARGET = Symbol.for("@lasercat/event-emission/original");
|
|
43
|
+
var ARRAY_MUTATORS = new Set([
|
|
44
|
+
"push",
|
|
45
|
+
"pop",
|
|
46
|
+
"shift",
|
|
47
|
+
"unshift",
|
|
48
|
+
"splice",
|
|
49
|
+
"sort",
|
|
50
|
+
"reverse",
|
|
51
|
+
"fill",
|
|
52
|
+
"copyWithin"
|
|
53
|
+
]);
|
|
54
|
+
function isProxyable(value) {
|
|
55
|
+
return value !== null && typeof value === "object" && !isProxied(value) && !(value instanceof Date) && !(value instanceof RegExp) && !(value instanceof Map) && !(value instanceof Set) && !(value instanceof WeakMap) && !(value instanceof WeakSet) && !(value instanceof Promise) && !(value instanceof Error) && !(value instanceof ArrayBuffer) && !ArrayBuffer.isView(value);
|
|
56
|
+
}
|
|
57
|
+
function isProxied(value) {
|
|
58
|
+
return typeof value === "object" && value !== null && value[PROXY_MARKER] === true;
|
|
59
|
+
}
|
|
60
|
+
function isArrayMutator(prop) {
|
|
61
|
+
return typeof prop === "string" && ARRAY_MUTATORS.has(prop);
|
|
62
|
+
}
|
|
63
|
+
function cloneAlongPath(obj, path) {
|
|
64
|
+
const isArray = Array.isArray(obj);
|
|
65
|
+
const rootClone = isArray ? [...obj] : { ...obj };
|
|
66
|
+
if (!path || isArray) {
|
|
67
|
+
return rootClone;
|
|
68
|
+
}
|
|
69
|
+
const parts = path.split(".");
|
|
70
|
+
const result = rootClone;
|
|
71
|
+
let current = result;
|
|
72
|
+
for (let i = 0;i < parts.length; i++) {
|
|
73
|
+
const key = parts[i];
|
|
74
|
+
const value = current[key];
|
|
75
|
+
if (value !== null && typeof value === "object") {
|
|
76
|
+
current[key] = Array.isArray(value) ? [...value] : { ...value };
|
|
77
|
+
if (i < parts.length - 1) {
|
|
78
|
+
current = current[key];
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
function cloneForComparison(obj, strategy, changedPath, deepClone) {
|
|
87
|
+
if (obj === null || typeof obj !== "object")
|
|
88
|
+
return obj;
|
|
89
|
+
switch (strategy) {
|
|
90
|
+
case "shallow":
|
|
91
|
+
return Array.isArray(obj) ? [...obj] : { ...obj };
|
|
92
|
+
case "deep":
|
|
93
|
+
if (deepClone) {
|
|
94
|
+
return deepClone(obj);
|
|
95
|
+
}
|
|
96
|
+
if (typeof structuredClone !== "function") {
|
|
97
|
+
throw new Error("structuredClone is not available in this runtime; provide observe.deepClone, or use cloneStrategy 'path' or 'shallow'.");
|
|
98
|
+
}
|
|
99
|
+
return structuredClone(obj);
|
|
100
|
+
case "path":
|
|
101
|
+
return cloneAlongPath(obj, changedPath);
|
|
102
|
+
default:
|
|
103
|
+
return obj;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function computeArrayDiff(method, before, _after, args) {
|
|
107
|
+
switch (method) {
|
|
108
|
+
case "push":
|
|
109
|
+
return { added: args };
|
|
110
|
+
case "pop":
|
|
111
|
+
return { removed: before.length > 0 ? [before[before.length - 1]] : [] };
|
|
112
|
+
case "shift":
|
|
113
|
+
return { removed: before.length > 0 ? [before[0]] : [] };
|
|
114
|
+
case "unshift":
|
|
115
|
+
return { added: args };
|
|
116
|
+
case "splice": {
|
|
117
|
+
const [start, deleteCount, ...items] = args;
|
|
118
|
+
const actualStart = start < 0 ? Math.max(before.length + start, 0) : Math.min(start, before.length);
|
|
119
|
+
const actualDeleteCount = Math.min(deleteCount ?? before.length - actualStart, before.length - actualStart);
|
|
120
|
+
return {
|
|
121
|
+
removed: before.slice(actualStart, actualStart + actualDeleteCount),
|
|
122
|
+
added: items
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
case "sort":
|
|
126
|
+
case "reverse":
|
|
127
|
+
case "fill":
|
|
128
|
+
case "copyWithin":
|
|
129
|
+
return {};
|
|
130
|
+
default:
|
|
131
|
+
return {};
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
var proxyRegistry = new WeakMap;
|
|
135
|
+
function getContextRegistry(target) {
|
|
136
|
+
let contextMap = proxyRegistry.get(target);
|
|
137
|
+
if (!contextMap) {
|
|
138
|
+
contextMap = new WeakMap;
|
|
139
|
+
proxyRegistry.set(target, contextMap);
|
|
140
|
+
}
|
|
141
|
+
return contextMap;
|
|
142
|
+
}
|
|
143
|
+
function createArrayMethodInterceptor(array, method, path, context) {
|
|
144
|
+
const original = array[method];
|
|
145
|
+
return function(...args) {
|
|
146
|
+
const previousState = cloneForComparison(context.originalRoot, context.options.cloneStrategy, path, context.options.deepClone);
|
|
147
|
+
const previousItems = [...array];
|
|
148
|
+
const result = original.apply(this, args);
|
|
149
|
+
const { added, removed } = computeArrayDiff(method, previousItems, array, args);
|
|
150
|
+
const methodEventPath = path ? `update:${path}.${method}` : `update:${method}`;
|
|
151
|
+
const arrayEventPath = path ? `update:${path}` : "update:";
|
|
152
|
+
context.eventTarget.dispatchEvent({
|
|
153
|
+
type: methodEventPath,
|
|
154
|
+
detail: {
|
|
155
|
+
method,
|
|
156
|
+
args,
|
|
157
|
+
result,
|
|
158
|
+
added,
|
|
159
|
+
removed,
|
|
160
|
+
current: context.originalRoot,
|
|
161
|
+
previous: previousState
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
if (path) {
|
|
165
|
+
context.eventTarget.dispatchEvent({
|
|
166
|
+
type: arrayEventPath,
|
|
167
|
+
detail: {
|
|
168
|
+
value: array,
|
|
169
|
+
current: context.originalRoot,
|
|
170
|
+
previous: previousState
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
context.eventTarget.dispatchEvent({
|
|
175
|
+
type: "update",
|
|
176
|
+
detail: {
|
|
177
|
+
current: context.originalRoot,
|
|
178
|
+
previous: previousState
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
return result;
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
function createObservableProxyInternal(target, path, context) {
|
|
185
|
+
const contextRegistry = getContextRegistry(target);
|
|
186
|
+
const existing = contextRegistry.get(context);
|
|
187
|
+
if (existing) {
|
|
188
|
+
return existing.proxy;
|
|
189
|
+
}
|
|
190
|
+
const proxy = new Proxy(target, {
|
|
191
|
+
get(obj, prop, receiver) {
|
|
192
|
+
if (prop === PROXY_MARKER)
|
|
193
|
+
return true;
|
|
194
|
+
if (prop === ORIGINAL_TARGET)
|
|
195
|
+
return obj;
|
|
196
|
+
if (typeof prop === "symbol") {
|
|
197
|
+
return Reflect.get(obj, prop, receiver);
|
|
198
|
+
}
|
|
199
|
+
const value = Reflect.get(obj, prop, receiver);
|
|
200
|
+
if (Array.isArray(obj) && isArrayMutator(prop)) {
|
|
201
|
+
return createArrayMethodInterceptor(obj, prop, path, context);
|
|
202
|
+
}
|
|
203
|
+
if (context.options.deep && isProxyable(value)) {
|
|
204
|
+
const nestedPath = path ? `${path}.${prop}` : prop;
|
|
205
|
+
return createObservableProxyInternal(value, nestedPath, context);
|
|
206
|
+
}
|
|
207
|
+
return value;
|
|
208
|
+
},
|
|
209
|
+
set(obj, prop, value, receiver) {
|
|
210
|
+
if (typeof prop === "symbol") {
|
|
211
|
+
return Reflect.set(obj, prop, value, receiver);
|
|
212
|
+
}
|
|
213
|
+
const oldValue = Reflect.get(obj, prop, receiver);
|
|
214
|
+
if (Object.is(oldValue, value)) {
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
const propPath = path ? `${path}.${prop}` : prop;
|
|
218
|
+
const previousState = cloneForComparison(context.originalRoot, context.options.cloneStrategy, propPath, context.options.deepClone);
|
|
219
|
+
const success = Reflect.set(obj, prop, value, receiver);
|
|
220
|
+
if (success) {
|
|
221
|
+
context.eventTarget.dispatchEvent({
|
|
222
|
+
type: `update:${propPath}`,
|
|
223
|
+
detail: {
|
|
224
|
+
value,
|
|
225
|
+
current: context.originalRoot,
|
|
226
|
+
previous: previousState
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
context.eventTarget.dispatchEvent({
|
|
230
|
+
type: "update",
|
|
231
|
+
detail: {
|
|
232
|
+
current: context.originalRoot,
|
|
233
|
+
previous: previousState
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
return success;
|
|
238
|
+
},
|
|
239
|
+
deleteProperty(obj, prop) {
|
|
240
|
+
if (typeof prop === "symbol") {
|
|
241
|
+
return Reflect.deleteProperty(obj, prop);
|
|
242
|
+
}
|
|
243
|
+
const propPath = path ? `${path}.${String(prop)}` : String(prop);
|
|
244
|
+
const previousState = cloneForComparison(context.originalRoot, context.options.cloneStrategy, propPath, context.options.deepClone);
|
|
245
|
+
const success = Reflect.deleteProperty(obj, prop);
|
|
246
|
+
if (success) {
|
|
247
|
+
context.eventTarget.dispatchEvent({
|
|
248
|
+
type: `update:${propPath}`,
|
|
249
|
+
detail: {
|
|
250
|
+
value: undefined,
|
|
251
|
+
current: context.originalRoot,
|
|
252
|
+
previous: previousState
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
context.eventTarget.dispatchEvent({
|
|
256
|
+
type: "update",
|
|
257
|
+
detail: {
|
|
258
|
+
current: context.originalRoot,
|
|
259
|
+
previous: previousState
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
return success;
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
contextRegistry.set(context, {
|
|
267
|
+
proxy,
|
|
268
|
+
path
|
|
269
|
+
});
|
|
270
|
+
return proxy;
|
|
271
|
+
}
|
|
272
|
+
function isEventTarget(obj) {
|
|
273
|
+
return typeof obj === "object" && obj !== null && typeof obj.addEventListener === "function" && typeof obj.removeEventListener === "function" && typeof obj.dispatchEvent === "function";
|
|
274
|
+
}
|
|
275
|
+
function setupEventForwarding(source, target) {
|
|
276
|
+
const handlers = new Map;
|
|
277
|
+
const sourceAddEventListener = source.addEventListener.bind(source);
|
|
278
|
+
const sourceRemoveEventListener = source.removeEventListener.bind(source);
|
|
279
|
+
const forwardHandler = (type) => (event) => {
|
|
280
|
+
const detail = event.detail ?? event;
|
|
281
|
+
target.dispatchEvent({
|
|
282
|
+
type,
|
|
283
|
+
detail
|
|
284
|
+
});
|
|
285
|
+
};
|
|
286
|
+
const originalAddEventListener = target.addEventListener.bind(target);
|
|
287
|
+
const wrappedAddEventListener = (type, listener, options) => {
|
|
288
|
+
if (!handlers.has(type) && type !== "update" && !type.startsWith("update:")) {
|
|
289
|
+
const handler = forwardHandler(type);
|
|
290
|
+
handlers.set(type, handler);
|
|
291
|
+
sourceAddEventListener(type, handler);
|
|
292
|
+
}
|
|
293
|
+
return originalAddEventListener(type, listener, options);
|
|
294
|
+
};
|
|
295
|
+
target.addEventListener = wrappedAddEventListener;
|
|
296
|
+
return () => {
|
|
297
|
+
target.addEventListener = originalAddEventListener;
|
|
298
|
+
for (const [type, handler] of handlers) {
|
|
299
|
+
sourceRemoveEventListener(type, handler);
|
|
300
|
+
}
|
|
301
|
+
handlers.clear();
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
function isObserved(obj) {
|
|
305
|
+
return isProxied(obj);
|
|
306
|
+
}
|
|
307
|
+
function getOriginal(proxy) {
|
|
308
|
+
if (!isProxied(proxy)) {
|
|
309
|
+
return proxy;
|
|
310
|
+
}
|
|
311
|
+
return proxy[ORIGINAL_TARGET] ?? proxy;
|
|
312
|
+
}
|
|
313
|
+
function createObservableProxy(target, eventTarget, options) {
|
|
314
|
+
const resolvedOptions = {
|
|
315
|
+
deep: options?.deep ?? true,
|
|
316
|
+
cloneStrategy: options?.cloneStrategy ?? "path",
|
|
317
|
+
deepClone: options?.deepClone
|
|
318
|
+
};
|
|
319
|
+
const context = {
|
|
320
|
+
eventTarget,
|
|
321
|
+
originalRoot: target,
|
|
322
|
+
options: resolvedOptions
|
|
323
|
+
};
|
|
324
|
+
const proxy = createObservableProxyInternal(target, "", context);
|
|
325
|
+
if (isEventTarget(target)) {
|
|
326
|
+
const cleanupForwarding = setupEventForwarding(target, eventTarget);
|
|
327
|
+
const maybeComplete = eventTarget.complete;
|
|
328
|
+
if (typeof maybeComplete === "function") {
|
|
329
|
+
const originalComplete = maybeComplete.bind(eventTarget);
|
|
330
|
+
let cleaned = false;
|
|
331
|
+
eventTarget.complete = () => {
|
|
332
|
+
if (!cleaned) {
|
|
333
|
+
cleaned = true;
|
|
334
|
+
cleanupForwarding();
|
|
335
|
+
}
|
|
336
|
+
return originalComplete();
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return proxy;
|
|
341
|
+
}
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
//# debugId=A44AD03CF1F776F164756E2164756E21
|