@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.
Files changed (211) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +280 -0
  3. package/dist/cdn/reactive.cjs +2803 -0
  4. package/dist/cdn/reactive.d.ts +11 -0
  5. package/dist/cdn/reactive.d.ts.map +1 -0
  6. package/dist/cdn/reactive.global.js +2 -0
  7. package/dist/cdn/reactive.js +10 -0
  8. package/dist/cdn/reactive.mjs +2787 -0
  9. package/dist/cdn/signal.cjs +299 -0
  10. package/dist/cdn/signal.d.ts +19 -0
  11. package/dist/cdn/signal.d.ts.map +1 -0
  12. package/dist/cdn/signal.global.js +2 -0
  13. package/dist/cdn/signal.js +18 -0
  14. package/dist/cdn/signal.mjs +276 -0
  15. package/dist/index.cjs +2803 -0
  16. package/dist/index.d.ts +2 -0
  17. package/dist/index.d.ts.map +1 -0
  18. package/dist/index.js +1 -0
  19. package/dist/index.mjs +2787 -0
  20. package/dist/lib/index.cjs +2804 -0
  21. package/dist/lib/index.d.ts +8 -0
  22. package/dist/lib/index.d.ts.map +1 -0
  23. package/dist/lib/index.js +11 -0
  24. package/dist/lib/index.mjs +2789 -0
  25. package/dist/lib/observable/index.cjs +386 -0
  26. package/dist/lib/observable/index.d.ts +4 -0
  27. package/dist/lib/observable/index.d.ts.map +1 -0
  28. package/dist/lib/observable/index.js +3 -0
  29. package/dist/lib/observable/index.mjs +364 -0
  30. package/dist/lib/observable/observable.cjs +384 -0
  31. package/dist/lib/observable/observable.d.ts +246 -0
  32. package/dist/lib/observable/observable.d.ts.map +1 -0
  33. package/dist/lib/observable/observable.js +268 -0
  34. package/dist/lib/observable/observable.mjs +361 -0
  35. package/dist/lib/observable/subscription.cjs +180 -0
  36. package/dist/lib/observable/subscription.d.ts +58 -0
  37. package/dist/lib/observable/subscription.d.ts.map +1 -0
  38. package/dist/lib/observable/subscription.js +177 -0
  39. package/dist/lib/observable/subscription.mjs +157 -0
  40. package/dist/lib/observable/types.cjs +18 -0
  41. package/dist/lib/observable/types.d.ts +44 -0
  42. package/dist/lib/observable/types.d.ts.map +1 -0
  43. package/dist/lib/observable/types.js +1 -0
  44. package/dist/lib/observable/types.mjs +0 -0
  45. package/dist/lib/predicates.cjs +44 -0
  46. package/dist/lib/predicates.d.ts +37 -0
  47. package/dist/lib/predicates.d.ts.map +1 -0
  48. package/dist/lib/predicates.js +62 -0
  49. package/dist/lib/predicates.mjs +23 -0
  50. package/dist/lib/signal/api.cjs +592 -0
  51. package/dist/lib/signal/api.d.ts +227 -0
  52. package/dist/lib/signal/api.d.ts.map +1 -0
  53. package/dist/lib/signal/api.js +228 -0
  54. package/dist/lib/signal/api.mjs +569 -0
  55. package/dist/lib/signal/cached-resource.cjs +726 -0
  56. package/dist/lib/signal/cached-resource.d.ts +86 -0
  57. package/dist/lib/signal/cached-resource.d.ts.map +1 -0
  58. package/dist/lib/signal/cached-resource.js +250 -0
  59. package/dist/lib/signal/cached-resource.mjs +704 -0
  60. package/dist/lib/signal/graph/children.cjs +326 -0
  61. package/dist/lib/signal/graph/children.d.ts +2 -0
  62. package/dist/lib/signal/graph/children.d.ts.map +1 -0
  63. package/dist/lib/signal/graph/children.js +30 -0
  64. package/dist/lib/signal/graph/children.mjs +303 -0
  65. package/dist/lib/signal/graph/compat.cjs +590 -0
  66. package/dist/lib/signal/graph/compat.d.ts +66 -0
  67. package/dist/lib/signal/graph/compat.d.ts.map +1 -0
  68. package/dist/lib/signal/graph/compat.js +197 -0
  69. package/dist/lib/signal/graph/compat.mjs +568 -0
  70. package/dist/lib/signal/graph/core.cjs +295 -0
  71. package/dist/lib/signal/graph/core.d.ts +99 -0
  72. package/dist/lib/signal/graph/core.d.ts.map +1 -0
  73. package/dist/lib/signal/graph/core.js +268 -0
  74. package/dist/lib/signal/graph/core.mjs +273 -0
  75. package/dist/lib/signal/graph/debounced.cjs +680 -0
  76. package/dist/lib/signal/graph/debounced.d.ts +67 -0
  77. package/dist/lib/signal/graph/debounced.d.ts.map +1 -0
  78. package/dist/lib/signal/graph/debounced.js +92 -0
  79. package/dist/lib/signal/graph/debounced.mjs +657 -0
  80. package/dist/lib/signal/graph/derived.cjs +508 -0
  81. package/dist/lib/signal/graph/derived.d.ts +14 -0
  82. package/dist/lib/signal/graph/derived.d.ts.map +1 -0
  83. package/dist/lib/signal/graph/derived.js +65 -0
  84. package/dist/lib/signal/graph/derived.mjs +485 -0
  85. package/dist/lib/signal/graph/detect.cjs +66 -0
  86. package/dist/lib/signal/graph/detect.d.ts +17 -0
  87. package/dist/lib/signal/graph/detect.d.ts.map +1 -0
  88. package/dist/lib/signal/graph/detect.js +39 -0
  89. package/dist/lib/signal/graph/detect.mjs +45 -0
  90. package/dist/lib/signal/graph/error-boundary.cjs +313 -0
  91. package/dist/lib/signal/graph/error-boundary.d.ts +16 -0
  92. package/dist/lib/signal/graph/error-boundary.d.ts.map +1 -0
  93. package/dist/lib/signal/graph/error-boundary.js +34 -0
  94. package/dist/lib/signal/graph/error-boundary.mjs +290 -0
  95. package/dist/lib/signal/graph/index.cjs +305 -0
  96. package/dist/lib/signal/graph/index.d.ts +22 -0
  97. package/dist/lib/signal/graph/index.d.ts.map +1 -0
  98. package/dist/lib/signal/graph/index.js +21 -0
  99. package/dist/lib/signal/graph/index.mjs +283 -0
  100. package/dist/lib/signal/graph/observe.cjs +198 -0
  101. package/dist/lib/signal/graph/observe.d.ts +185 -0
  102. package/dist/lib/signal/graph/observe.d.ts.map +1 -0
  103. package/dist/lib/signal/graph/observe.js +198 -0
  104. package/dist/lib/signal/graph/observe.mjs +175 -0
  105. package/dist/lib/signal/graph/resource.cjs +632 -0
  106. package/dist/lib/signal/graph/resource.d.ts +39 -0
  107. package/dist/lib/signal/graph/resource.d.ts.map +1 -0
  108. package/dist/lib/signal/graph/resource.js +165 -0
  109. package/dist/lib/signal/graph/resource.mjs +609 -0
  110. package/dist/lib/signal/graph/runtime.cjs +522 -0
  111. package/dist/lib/signal/graph/runtime.d.ts +80 -0
  112. package/dist/lib/signal/graph/runtime.d.ts.map +1 -0
  113. package/dist/lib/signal/graph/runtime.js +358 -0
  114. package/dist/lib/signal/graph/runtime.mjs +499 -0
  115. package/dist/lib/signal/graph/signal-next.cjs +1249 -0
  116. package/dist/lib/signal/graph/signal-next.d.ts +38 -0
  117. package/dist/lib/signal/graph/signal-next.d.ts.map +1 -0
  118. package/dist/lib/signal/graph/signal-next.js +44 -0
  119. package/dist/lib/signal/graph/signal-next.mjs +1227 -0
  120. package/dist/lib/signal/graph/ssr-hooks.cjs +89 -0
  121. package/dist/lib/signal/graph/ssr-hooks.d.ts +51 -0
  122. package/dist/lib/signal/graph/ssr-hooks.d.ts.map +1 -0
  123. package/dist/lib/signal/graph/ssr-hooks.js +80 -0
  124. package/dist/lib/signal/graph/ssr-hooks.mjs +68 -0
  125. package/dist/lib/signal/graph/state.cjs +72 -0
  126. package/dist/lib/signal/graph/state.d.ts +76 -0
  127. package/dist/lib/signal/graph/state.d.ts.map +1 -0
  128. package/dist/lib/signal/graph/state.js +99 -0
  129. package/dist/lib/signal/graph/state.mjs +49 -0
  130. package/dist/lib/signal/graph/suspense.cjs +313 -0
  131. package/dist/lib/signal/graph/suspense.d.ts +13 -0
  132. package/dist/lib/signal/graph/suspense.d.ts.map +1 -0
  133. package/dist/lib/signal/graph/suspense.js +36 -0
  134. package/dist/lib/signal/graph/suspense.mjs +290 -0
  135. package/dist/lib/signal/index-legacy.cjs +2582 -0
  136. package/dist/lib/signal/index-legacy.d.ts +12 -0
  137. package/dist/lib/signal/index-legacy.d.ts.map +1 -0
  138. package/dist/lib/signal/index-legacy.js +11 -0
  139. package/dist/lib/signal/index-legacy.mjs +2560 -0
  140. package/dist/lib/signal/index.cjs +1350 -0
  141. package/dist/lib/signal/index.d.ts +19 -0
  142. package/dist/lib/signal/index.d.ts.map +1 -0
  143. package/dist/lib/signal/index.js +19 -0
  144. package/dist/lib/signal/index.mjs +1328 -0
  145. package/dist/lib/signal/list.cjs +561 -0
  146. package/dist/lib/signal/list.d.ts +39 -0
  147. package/dist/lib/signal/list.d.ts.map +1 -0
  148. package/dist/lib/signal/list.js +172 -0
  149. package/dist/lib/signal/list.mjs +538 -0
  150. package/dist/lib/signal/resource-api.cjs +638 -0
  151. package/dist/lib/signal/resource-api.d.ts +19 -0
  152. package/dist/lib/signal/resource-api.d.ts.map +1 -0
  153. package/dist/lib/signal/resource-api.js +27 -0
  154. package/dist/lib/signal/resource-api.mjs +615 -0
  155. package/dist/lib/signal/signal.cjs +2035 -0
  156. package/dist/lib/signal/signal.d.ts +303 -0
  157. package/dist/lib/signal/signal.d.ts.map +1 -0
  158. package/dist/lib/signal/signal.js +1233 -0
  159. package/dist/lib/signal/signal.mjs +2012 -0
  160. package/dist/lib/signal/types.cjs +38 -0
  161. package/dist/lib/signal/types.d.ts +133 -0
  162. package/dist/lib/signal/types.d.ts.map +1 -0
  163. package/dist/lib/signal/types.js +38 -0
  164. package/dist/lib/signal/types.mjs +17 -0
  165. package/dist/lib/signal/utilities.cjs +1024 -0
  166. package/dist/lib/signal/utilities.d.ts +247 -0
  167. package/dist/lib/signal/utilities.d.ts.map +1 -0
  168. package/dist/lib/signal/utilities.js +346 -0
  169. package/dist/lib/signal/utilities.mjs +1002 -0
  170. package/dist/lib/store/api.cjs +662 -0
  171. package/dist/lib/store/api.d.ts +40 -0
  172. package/dist/lib/store/api.d.ts.map +1 -0
  173. package/dist/lib/store/api.js +63 -0
  174. package/dist/lib/store/api.mjs +646 -0
  175. package/dist/lib/store/index.cjs +741 -0
  176. package/dist/lib/store/index.d.ts +16 -0
  177. package/dist/lib/store/index.d.ts.map +1 -0
  178. package/dist/lib/store/index.js +13 -0
  179. package/dist/lib/store/index.mjs +726 -0
  180. package/dist/lib/store/setter.types.cjs +18 -0
  181. package/dist/lib/store/setter.types.d.ts +131 -0
  182. package/dist/lib/store/setter.types.d.ts.map +1 -0
  183. package/dist/lib/store/setter.types.js +65 -0
  184. package/dist/lib/store/setter.types.mjs +0 -0
  185. package/dist/lib/store/store.cjs +708 -0
  186. package/dist/lib/store/store.d.ts +142 -0
  187. package/dist/lib/store/store.d.ts.map +1 -0
  188. package/dist/lib/store/store.js +812 -0
  189. package/dist/lib/store/store.mjs +694 -0
  190. package/dist/lib/store/types.cjs +18 -0
  191. package/dist/lib/store/types.d.ts +57 -0
  192. package/dist/lib/store/types.d.ts.map +1 -0
  193. package/dist/lib/store/types.js +1 -0
  194. package/dist/lib/store/types.mjs +0 -0
  195. package/dist/lib/store/utils.cjs +43 -0
  196. package/dist/lib/store/utils.d.ts +13 -0
  197. package/dist/lib/store/utils.d.ts.map +1 -0
  198. package/dist/lib/store/utils.js +29 -0
  199. package/dist/lib/store/utils.mjs +22 -0
  200. package/dist/lib/subject.cjs +450 -0
  201. package/dist/lib/subject.d.ts +14 -0
  202. package/dist/lib/subject.d.ts.map +1 -0
  203. package/dist/lib/subject.js +81 -0
  204. package/dist/lib/subject.mjs +427 -0
  205. package/dist/tsconfig.lib.tsbuildinfo +1 -0
  206. package/dist/version.generated.cjs +27 -0
  207. package/dist/version.generated.d.ts +3 -0
  208. package/dist/version.generated.d.ts.map +1 -0
  209. package/dist/version.generated.js +3 -0
  210. package/dist/version.generated.mjs +7 -0
  211. package/package.json +115 -0
@@ -0,0 +1,180 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/lib/observable/subscription.ts
21
+ var subscription_exports = {};
22
+ __export(subscription_exports, {
23
+ Subscription: () => Subscription
24
+ });
25
+ module.exports = __toCommonJS(subscription_exports);
26
+
27
+ // src/lib/predicates.ts
28
+ function isFunction(value) {
29
+ return typeof value === "function" && !/^class\s/.test(Function.prototype.toString.call(value));
30
+ }
31
+ function isObject(value) {
32
+ if (value == null) return false;
33
+ if (Array.isArray(value)) return false;
34
+ if (typeof value === "function") return false;
35
+ if (value instanceof Date) return false;
36
+ if (value instanceof RegExp) return false;
37
+ if (value instanceof Map) return false;
38
+ if (value instanceof Set) return false;
39
+ if (value instanceof Error) return false;
40
+ return typeof value === "object";
41
+ }
42
+
43
+ // src/lib/observable/subscription.ts
44
+ var Subscription = class _Subscription {
45
+ /**
46
+ * Creates a new Subscription.
47
+ */
48
+ constructor(unsubscribe) {
49
+ /** @private */
50
+ this._closed = false;
51
+ /** @private */
52
+ this._parentOrParents = null;
53
+ /** @private */
54
+ this._subscriptions = null;
55
+ if (unsubscribe) {
56
+ this._subscriptions = [unsubscribe];
57
+ }
58
+ }
59
+ /**
60
+ * Whether the subscription is closed.
61
+ */
62
+ get closed() {
63
+ return this._closed;
64
+ }
65
+ /**
66
+ * Disposes the resources held by the subscription.
67
+ * May be called multiple times (idempotent).
68
+ */
69
+ unsubscribe() {
70
+ if (this._closed) {
71
+ return;
72
+ }
73
+ this._closed = true;
74
+ const { _parentOrParents, _subscriptions } = this;
75
+ if (_parentOrParents instanceof _Subscription) {
76
+ _parentOrParents.remove(this);
77
+ } else if (_parentOrParents !== null) {
78
+ for (const parent of _parentOrParents) {
79
+ parent.remove(this);
80
+ }
81
+ }
82
+ if (_subscriptions) {
83
+ for (const sub of _subscriptions) {
84
+ if (isFunction(sub) && !(sub instanceof _Subscription)) {
85
+ try {
86
+ sub();
87
+ } catch (error) {
88
+ console.error("Error in unsubscribe function:", error);
89
+ }
90
+ } else if (isObject(sub) && typeof sub.unsubscribe === "function") {
91
+ try {
92
+ sub.unsubscribe();
93
+ } catch (error) {
94
+ console.error("Error unsubscribing child:", error);
95
+ }
96
+ }
97
+ }
98
+ this._subscriptions = null;
99
+ }
100
+ }
101
+ /**
102
+ * Adds a child subscription or cleanup function to this subscription.
103
+ * When this subscription is unsubscribed, all added subscriptions/functions will be unsubscribed/called.
104
+ *
105
+ * @throws {TypeError} If the argument is not a subscription-like object or function
106
+ */
107
+ add(subscription) {
108
+ if (!subscription || subscription === this) {
109
+ return this;
110
+ }
111
+ if (subscription.closed) {
112
+ return this;
113
+ }
114
+ let { _closed, _subscriptions } = this;
115
+ if (_closed) {
116
+ if (isFunction(subscription)) {
117
+ try {
118
+ subscription();
119
+ } catch (error) {
120
+ console.error("Error in unsubscribe function:", error);
121
+ }
122
+ } else if (subscription.unsubscribe) {
123
+ try {
124
+ subscription.unsubscribe();
125
+ } catch (error) {
126
+ console.error("Error unsubscribing:", error);
127
+ }
128
+ }
129
+ return this;
130
+ }
131
+ if (!_subscriptions) {
132
+ this._subscriptions = [];
133
+ }
134
+ this._subscriptions.push(subscription);
135
+ if (subscription instanceof _Subscription) {
136
+ subscription._addParent(this);
137
+ }
138
+ return this;
139
+ }
140
+ /**
141
+ * Removes a child subscription from this subscription.
142
+ */
143
+ remove(subscription) {
144
+ const { _subscriptions } = this;
145
+ if (!_subscriptions) {
146
+ return;
147
+ }
148
+ const index = _subscriptions.indexOf(subscription);
149
+ if (index !== -1) {
150
+ _subscriptions.splice(index, 1);
151
+ subscription._removeParent(this);
152
+ }
153
+ }
154
+ /** @private */
155
+ _addParent(parent) {
156
+ const { _parentOrParents } = this;
157
+ if (!_parentOrParents) {
158
+ this._parentOrParents = parent;
159
+ } else if (Array.isArray(_parentOrParents)) {
160
+ _parentOrParents.push(parent);
161
+ } else {
162
+ this._parentOrParents = [_parentOrParents, parent];
163
+ }
164
+ }
165
+ /** @private */
166
+ _removeParent(parent) {
167
+ const { _parentOrParents } = this;
168
+ if (_parentOrParents === parent) {
169
+ this._parentOrParents = null;
170
+ } else if (Array.isArray(_parentOrParents)) {
171
+ const index = _parentOrParents.indexOf(parent);
172
+ if (index !== -1) {
173
+ _parentOrParents.splice(index, 1);
174
+ if (_parentOrParents.length === 1) {
175
+ this._parentOrParents = _parentOrParents[0];
176
+ }
177
+ }
178
+ }
179
+ }
180
+ };
@@ -0,0 +1,58 @@
1
+ import { SubscriptionLike, Unsubscribable } from './types.js';
2
+ /**
3
+ * Represents a disposable resource, such as the execution of an Observable.
4
+ * This is the primary mechanism for cleaning up Observable executions.
5
+ *
6
+ * @class Subscription
7
+ * @implements {SubscriptionLike}
8
+ *
9
+ * @example
10
+ * // Basic usage
11
+ * const subscription = new Subscription();
12
+ * subscription.add(() => console.log('Cleanup'));
13
+ * subscription.unsubscribe(); // Logs: Cleanup
14
+ *
15
+ * @example
16
+ * // Adding child subscriptions
17
+ * const parent = new Subscription();
18
+ * const child = new Subscription();
19
+ * parent.add(child);
20
+ *
21
+ * // Unsubscribing parent also unsubscribes child
22
+ * parent.unsubscribe();
23
+ * console.log(child.closed); // true
24
+ */
25
+ export declare class Subscription implements SubscriptionLike {
26
+ /** @private */ private _closed;
27
+ /** @private */ private _parentOrParents;
28
+ /** @private */ private _subscriptions;
29
+ /**
30
+ * Creates a new Subscription.
31
+ */
32
+ constructor(unsubscribe?: Unsubscribable);
33
+ /**
34
+ * Whether the subscription is closed.
35
+ */
36
+ get closed(): boolean;
37
+ /**
38
+ * Disposes the resources held by the subscription.
39
+ * May be called multiple times (idempotent).
40
+ */
41
+ unsubscribe(): void;
42
+ /**
43
+ * Adds a child subscription or cleanup function to this subscription.
44
+ * When this subscription is unsubscribed, all added subscriptions/functions will be unsubscribed/called.
45
+ *
46
+ * @throws {TypeError} If the argument is not a subscription-like object or function
47
+ */
48
+ add(subscription: SubscriptionLike | Unsubscribable): this;
49
+ /**
50
+ * Removes a child subscription from this subscription.
51
+ */
52
+ remove(subscription: Subscription): void;
53
+ /** @private */
54
+ private _addParent;
55
+ /** @private */
56
+ private _removeParent;
57
+ }
58
+ //# sourceMappingURL=subscription.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subscription.d.ts","sourceRoot":"","sources":["../../../src/lib/observable/subscription.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAiB,cAAc,EAAE,MAAM,YAAY,CAAC;AAE7E;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,YAAa,YAAW,gBAAgB;IACnD,eAAe,CAAC,OAAO,CAAC,OAAO,CAAS;IACxC,eAAe,CAAC,OAAO,CAAC,gBAAgB,CAGxB;IAChB,eAAe,CAAC,OAAO,CAAC,cAAc,CAEtB;IAEhB;;OAEG;gBACS,WAAW,CAAC,EAAE,cAAc;IAMxC;;OAEG;IACH,IAAI,MAAM,IAAI,OAAO,CAEpB;IAED;;;OAGG;IACH,WAAW,IAAI,IAAI;IAuCnB;;;;;OAKG;IACH,GAAG,CAAC,YAAY,EAAE,gBAAgB,GAAG,cAAc,GAAG,IAAI;IA8C1D;;OAEG;IACH,MAAM,CAAC,YAAY,EAAE,YAAY,GAAG,IAAI;IAexC,eAAe;IACf,OAAO,CAAC,UAAU;IAWlB,eAAe;IACf,OAAO,CAAC,aAAa;CActB"}
@@ -0,0 +1,177 @@
1
+ import { isObject, isFunction } from '../predicates.js';
2
+ /**
3
+ * Represents a disposable resource, such as the execution of an Observable.
4
+ * This is the primary mechanism for cleaning up Observable executions.
5
+ *
6
+ * @class Subscription
7
+ * @implements {SubscriptionLike}
8
+ *
9
+ * @example
10
+ * // Basic usage
11
+ * const subscription = new Subscription();
12
+ * subscription.add(() => console.log('Cleanup'));
13
+ * subscription.unsubscribe(); // Logs: Cleanup
14
+ *
15
+ * @example
16
+ * // Adding child subscriptions
17
+ * const parent = new Subscription();
18
+ * const child = new Subscription();
19
+ * parent.add(child);
20
+ *
21
+ * // Unsubscribing parent also unsubscribes child
22
+ * parent.unsubscribe();
23
+ * console.log(child.closed); // true
24
+ */
25
+ export class Subscription {
26
+ /** @private */ _closed = false;
27
+ /** @private */ _parentOrParents = null;
28
+ /** @private */ _subscriptions = null;
29
+ /**
30
+ * Creates a new Subscription.
31
+ */
32
+ constructor(unsubscribe) {
33
+ if (unsubscribe) {
34
+ this._subscriptions = [unsubscribe];
35
+ }
36
+ }
37
+ /**
38
+ * Whether the subscription is closed.
39
+ */
40
+ get closed() {
41
+ return this._closed;
42
+ }
43
+ /**
44
+ * Disposes the resources held by the subscription.
45
+ * May be called multiple times (idempotent).
46
+ */
47
+ unsubscribe() {
48
+ if (this._closed) {
49
+ return;
50
+ }
51
+ this._closed = true;
52
+ const { _parentOrParents, _subscriptions } = this;
53
+ // Remove from parent
54
+ if (_parentOrParents instanceof Subscription) {
55
+ _parentOrParents.remove(this);
56
+ }
57
+ else if (_parentOrParents !== null) {
58
+ for (const parent of _parentOrParents) {
59
+ parent.remove(this);
60
+ }
61
+ }
62
+ // Unsubscribe all child subscriptions
63
+ if (_subscriptions) {
64
+ for (const sub of _subscriptions) {
65
+ if (isFunction(sub) && !(sub instanceof Subscription)) {
66
+ try {
67
+ sub();
68
+ }
69
+ catch (error) {
70
+ // Don't let errors in cleanup functions break unsubscribe chain
71
+ console.error('Error in unsubscribe function:', error);
72
+ }
73
+ }
74
+ else if (isObject(sub) && typeof sub.unsubscribe === 'function') {
75
+ try {
76
+ sub.unsubscribe();
77
+ }
78
+ catch (error) {
79
+ console.error('Error unsubscribing child:', error);
80
+ }
81
+ }
82
+ }
83
+ this._subscriptions = null;
84
+ }
85
+ }
86
+ /**
87
+ * Adds a child subscription or cleanup function to this subscription.
88
+ * When this subscription is unsubscribed, all added subscriptions/functions will be unsubscribed/called.
89
+ *
90
+ * @throws {TypeError} If the argument is not a subscription-like object or function
91
+ */
92
+ add(subscription) {
93
+ if (!subscription || subscription === this) {
94
+ return this;
95
+ }
96
+ // If subscription is already closed, do nothing
97
+ if (subscription.closed) {
98
+ return this;
99
+ }
100
+ let { _closed, _subscriptions } = this;
101
+ // If this subscription is already closed, unsubscribe the added subscription immediately
102
+ if (_closed) {
103
+ if (isFunction(subscription)) {
104
+ try {
105
+ subscription();
106
+ }
107
+ catch (error) {
108
+ console.error('Error in unsubscribe function:', error);
109
+ }
110
+ }
111
+ else if (subscription.unsubscribe) {
112
+ try {
113
+ subscription.unsubscribe();
114
+ }
115
+ catch (error) {
116
+ console.error('Error unsubscribing:', error);
117
+ }
118
+ }
119
+ return this;
120
+ }
121
+ // Initialize subscriptions array if needed
122
+ if (!_subscriptions) {
123
+ this._subscriptions = [];
124
+ }
125
+ // Add to subscriptions array
126
+ this._subscriptions.push(subscription);
127
+ // Set parent reference
128
+ if (subscription instanceof Subscription) {
129
+ subscription._addParent(this);
130
+ }
131
+ return this;
132
+ }
133
+ /**
134
+ * Removes a child subscription from this subscription.
135
+ */
136
+ remove(subscription) {
137
+ const { _subscriptions } = this;
138
+ if (!_subscriptions) {
139
+ return;
140
+ }
141
+ const index = _subscriptions.indexOf(subscription);
142
+ if (index !== -1) {
143
+ _subscriptions.splice(index, 1);
144
+ // Remove parent reference
145
+ subscription._removeParent(this);
146
+ }
147
+ }
148
+ /** @private */
149
+ _addParent(parent) {
150
+ const { _parentOrParents } = this;
151
+ if (!_parentOrParents) {
152
+ this._parentOrParents = parent;
153
+ }
154
+ else if (Array.isArray(_parentOrParents)) {
155
+ _parentOrParents.push(parent);
156
+ }
157
+ else {
158
+ this._parentOrParents = [_parentOrParents, parent];
159
+ }
160
+ }
161
+ /** @private */
162
+ _removeParent(parent) {
163
+ const { _parentOrParents } = this;
164
+ if (_parentOrParents === parent) {
165
+ this._parentOrParents = null;
166
+ }
167
+ else if (Array.isArray(_parentOrParents)) {
168
+ const index = _parentOrParents.indexOf(parent);
169
+ if (index !== -1) {
170
+ _parentOrParents.splice(index, 1);
171
+ if (_parentOrParents.length === 1) {
172
+ this._parentOrParents = _parentOrParents[0];
173
+ }
174
+ }
175
+ }
176
+ }
177
+ }
@@ -0,0 +1,157 @@
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
+ export {
156
+ Subscription
157
+ };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+
16
+ // src/lib/observable/types.ts
17
+ var types_exports = {};
18
+ module.exports = __toCommonJS(types_exports);
@@ -0,0 +1,44 @@
1
+ import { Subscription } from "./subscription.js";
2
+ /**
3
+ * Type representing a function that can be called to unsubscribe from an Observable.
4
+ *
5
+ * @typedef {function(): void} Unsubscribable
6
+ */
7
+ export type Unsubscribable = () => void;
8
+ /**
9
+ * Type representing any object that can be unsubscribed.
10
+ *
11
+ * @interface SubscriptionLike
12
+ * @property {boolean} closed - Whether the subscription is closed
13
+ * @property {function(): void} unsubscribe - Function to unsubscribe
14
+ */
15
+ export interface SubscriptionLike {
16
+ closed: boolean;
17
+ unsubscribe(): void;
18
+ }
19
+ export type TeardownLogic = Subscription | Unsubscribable | (() => void) | void;
20
+ /**
21
+ * Next observer in a chain
22
+ */
23
+ export interface NextObserver<T> {
24
+ next: (value: T) => void;
25
+ error?: (error: any) => void;
26
+ complete?: () => void;
27
+ }
28
+ /**
29
+ * Error observer in a chain
30
+ */
31
+ export interface ErrorObserver<T> {
32
+ next?: (value: T) => void;
33
+ error: (error: any) => void;
34
+ complete?: () => void;
35
+ }
36
+ /**
37
+ * Completion observer in a chain
38
+ */
39
+ export interface CompletionObserver<T> {
40
+ next?: (value: T) => void;
41
+ error?: (error: any) => void;
42
+ complete: () => void;
43
+ }
44
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/observable/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD;;;;GAIG;AACH,MAAM,MAAM,cAAc,GAAG,MAAM,IAAI,CAAC;AAExC;;;;;;GAMG;AACH,MAAM,WAAW,gBAAgB;IAC7B,MAAM,EAAE,OAAO,CAAC;IAChB,WAAW,IAAI,IAAI,CAAC;CACvB;AAED,MAAM,MAAM,aAAa,GAAG,YAAY,GAAG,cAAc,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG,IAAI,CAAC;AAG9E;;GAEG;AACH,MAAM,WAAW,YAAY,CAAC,CAAC;IAC7B,IAAI,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;IACzB,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,IAAI,CAAC;IAC7B,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC;IAC9B,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;IAC1B,KAAK,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,IAAI,CAAC;IAC5B,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB,CAAC,CAAC;IACnC,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC;IAC1B,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,IAAI,CAAC;IAC7B,QAAQ,EAAE,MAAM,IAAI,CAAC;CACtB"}
@@ -0,0 +1 @@
1
+ export {};
File without changes
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/lib/predicates.ts
21
+ var predicates_exports = {};
22
+ __export(predicates_exports, {
23
+ isFunction: () => isFunction,
24
+ isObject: () => isObject,
25
+ isPromise: () => isPromise
26
+ });
27
+ module.exports = __toCommonJS(predicates_exports);
28
+ function isFunction(value) {
29
+ return typeof value === "function" && !/^class\s/.test(Function.prototype.toString.call(value));
30
+ }
31
+ function isObject(value) {
32
+ if (value == null) return false;
33
+ if (Array.isArray(value)) return false;
34
+ if (typeof value === "function") return false;
35
+ if (value instanceof Date) return false;
36
+ if (value instanceof RegExp) return false;
37
+ if (value instanceof Map) return false;
38
+ if (value instanceof Set) return false;
39
+ if (value instanceof Error) return false;
40
+ return typeof value === "object";
41
+ }
42
+ function isPromise(value) {
43
+ return value instanceof Promise || !!value && (typeof value === "object" || typeof value === "function") && "then" in value;
44
+ }