mvc-kit 2.12.5 → 2.13.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.
Files changed (186) hide show
  1. package/agent-config/bin/postinstall.mjs +4 -3
  2. package/agent-config/bin/setup.mjs +5 -1
  3. package/agent-config/claude-code/agents/mvc-kit-architect.md +11 -8
  4. package/agent-config/claude-code/skills/guide/SKILL.md +20 -7
  5. package/agent-config/claude-code/skills/guide/patterns.md +12 -0
  6. package/agent-config/claude-code/skills/guide/recipes.md +510 -0
  7. package/agent-config/claude-code/skills/guide/testing.md +297 -0
  8. package/agent-config/claude-code/skills/review/SKILL.md +3 -13
  9. package/agent-config/claude-code/skills/review/checklist.md +30 -5
  10. package/agent-config/claude-code/skills/scaffold/SKILL.md +4 -13
  11. package/agent-config/lib/install-claude.mjs +84 -25
  12. package/dist/Channel.cjs +276 -300
  13. package/dist/Channel.cjs.map +1 -1
  14. package/dist/Channel.js +275 -299
  15. package/dist/Channel.js.map +1 -1
  16. package/dist/Collection.cjs +424 -504
  17. package/dist/Collection.cjs.map +1 -1
  18. package/dist/Collection.js +423 -503
  19. package/dist/Collection.js.map +1 -1
  20. package/dist/Controller.cjs +70 -67
  21. package/dist/Controller.cjs.map +1 -1
  22. package/dist/Controller.js +69 -66
  23. package/dist/Controller.js.map +1 -1
  24. package/dist/EventBus.cjs +77 -88
  25. package/dist/EventBus.cjs.map +1 -1
  26. package/dist/EventBus.js +76 -87
  27. package/dist/EventBus.js.map +1 -1
  28. package/dist/Feed.cjs +81 -77
  29. package/dist/Feed.cjs.map +1 -1
  30. package/dist/Feed.js +80 -76
  31. package/dist/Feed.js.map +1 -1
  32. package/dist/Model.cjs +181 -207
  33. package/dist/Model.cjs.map +1 -1
  34. package/dist/Model.js +179 -205
  35. package/dist/Model.js.map +1 -1
  36. package/dist/Pagination.cjs +75 -73
  37. package/dist/Pagination.cjs.map +1 -1
  38. package/dist/Pagination.js +74 -72
  39. package/dist/Pagination.js.map +1 -1
  40. package/dist/Pending.cjs +255 -287
  41. package/dist/Pending.cjs.map +1 -1
  42. package/dist/Pending.js +253 -285
  43. package/dist/Pending.js.map +1 -1
  44. package/dist/PersistentCollection.cjs +242 -285
  45. package/dist/PersistentCollection.cjs.map +1 -1
  46. package/dist/PersistentCollection.js +241 -284
  47. package/dist/PersistentCollection.js.map +1 -1
  48. package/dist/Resource.cjs +166 -174
  49. package/dist/Resource.cjs.map +1 -1
  50. package/dist/Resource.js +164 -172
  51. package/dist/Resource.js.map +1 -1
  52. package/dist/Selection.cjs +84 -94
  53. package/dist/Selection.cjs.map +1 -1
  54. package/dist/Selection.js +83 -93
  55. package/dist/Selection.js.map +1 -1
  56. package/dist/Service.cjs +54 -55
  57. package/dist/Service.cjs.map +1 -1
  58. package/dist/Service.js +53 -54
  59. package/dist/Service.js.map +1 -1
  60. package/dist/Sorting.cjs +102 -101
  61. package/dist/Sorting.cjs.map +1 -1
  62. package/dist/Sorting.js +102 -101
  63. package/dist/Sorting.js.map +1 -1
  64. package/dist/Trackable.cjs +112 -80
  65. package/dist/Trackable.cjs.map +1 -1
  66. package/dist/Trackable.js +111 -79
  67. package/dist/Trackable.js.map +1 -1
  68. package/dist/ViewModel.cjs +528 -576
  69. package/dist/ViewModel.cjs.map +1 -1
  70. package/dist/ViewModel.js +525 -573
  71. package/dist/ViewModel.js.map +1 -1
  72. package/dist/bindPublicMethods.cjs +43 -24
  73. package/dist/bindPublicMethods.cjs.map +1 -1
  74. package/dist/bindPublicMethods.js +43 -24
  75. package/dist/bindPublicMethods.js.map +1 -1
  76. package/dist/errors.cjs +67 -68
  77. package/dist/errors.cjs.map +1 -1
  78. package/dist/errors.js +68 -71
  79. package/dist/errors.js.map +1 -1
  80. package/dist/mvc-kit.cjs +44 -46
  81. package/dist/mvc-kit.js +5 -32
  82. package/dist/produceDraft.cjs +105 -95
  83. package/dist/produceDraft.cjs.map +1 -1
  84. package/dist/produceDraft.js +106 -97
  85. package/dist/produceDraft.js.map +1 -1
  86. package/dist/react/components/CardList.cjs +30 -40
  87. package/dist/react/components/CardList.cjs.map +1 -1
  88. package/dist/react/components/CardList.js +31 -41
  89. package/dist/react/components/CardList.js.map +1 -1
  90. package/dist/react/components/DataTable.cjs +146 -169
  91. package/dist/react/components/DataTable.cjs.map +1 -1
  92. package/dist/react/components/DataTable.js +147 -170
  93. package/dist/react/components/DataTable.js.map +1 -1
  94. package/dist/react/components/InfiniteScroll.cjs +51 -42
  95. package/dist/react/components/InfiniteScroll.cjs.map +1 -1
  96. package/dist/react/components/InfiniteScroll.js +52 -43
  97. package/dist/react/components/InfiniteScroll.js.map +1 -1
  98. package/dist/react/components/types.cjs +10 -6
  99. package/dist/react/components/types.cjs.map +1 -1
  100. package/dist/react/components/types.js +11 -9
  101. package/dist/react/components/types.js.map +1 -1
  102. package/dist/react/guards.cjs +10 -6
  103. package/dist/react/guards.cjs.map +1 -1
  104. package/dist/react/guards.js +11 -9
  105. package/dist/react/guards.js.map +1 -1
  106. package/dist/react/provider.cjs +23 -20
  107. package/dist/react/provider.cjs.map +1 -1
  108. package/dist/react/provider.js +23 -21
  109. package/dist/react/provider.js.map +1 -1
  110. package/dist/react/use-event-bus.cjs +24 -20
  111. package/dist/react/use-event-bus.cjs.map +1 -1
  112. package/dist/react/use-event-bus.js +24 -21
  113. package/dist/react/use-event-bus.js.map +1 -1
  114. package/dist/react/use-instance.cjs +43 -36
  115. package/dist/react/use-instance.cjs.map +1 -1
  116. package/dist/react/use-instance.js +43 -36
  117. package/dist/react/use-instance.js.map +1 -1
  118. package/dist/react/use-local.cjs +48 -64
  119. package/dist/react/use-local.cjs.map +1 -1
  120. package/dist/react/use-local.js +47 -63
  121. package/dist/react/use-local.js.map +1 -1
  122. package/dist/react/use-model.cjs +84 -98
  123. package/dist/react/use-model.cjs.map +1 -1
  124. package/dist/react/use-model.js +84 -100
  125. package/dist/react/use-model.js.map +1 -1
  126. package/dist/react/use-singleton.cjs +19 -23
  127. package/dist/react/use-singleton.cjs.map +1 -1
  128. package/dist/react/use-singleton.js +16 -20
  129. package/dist/react/use-singleton.js.map +1 -1
  130. package/dist/react/use-subscribe-only.cjs +28 -22
  131. package/dist/react/use-subscribe-only.cjs.map +1 -1
  132. package/dist/react/use-subscribe-only.js +28 -22
  133. package/dist/react/use-subscribe-only.js.map +1 -1
  134. package/dist/react/use-teardown.cjs +20 -19
  135. package/dist/react/use-teardown.cjs.map +1 -1
  136. package/dist/react/use-teardown.js +20 -19
  137. package/dist/react/use-teardown.js.map +1 -1
  138. package/dist/react-native/NativeCollection.cjs +98 -78
  139. package/dist/react-native/NativeCollection.cjs.map +1 -1
  140. package/dist/react-native/NativeCollection.js +97 -77
  141. package/dist/react-native/NativeCollection.js.map +1 -1
  142. package/dist/react-native.cjs +2 -4
  143. package/dist/react-native.js +1 -4
  144. package/dist/react.cjs +24 -26
  145. package/dist/react.js +1 -17
  146. package/dist/singleton.cjs +28 -22
  147. package/dist/singleton.cjs.map +1 -1
  148. package/dist/singleton.js +29 -26
  149. package/dist/singleton.js.map +1 -1
  150. package/dist/walkPrototypeChain.cjs +20 -12
  151. package/dist/walkPrototypeChain.cjs.map +1 -1
  152. package/dist/walkPrototypeChain.js +21 -13
  153. package/dist/walkPrototypeChain.js.map +1 -1
  154. package/dist/web/IndexedDBCollection.cjs +53 -36
  155. package/dist/web/IndexedDBCollection.cjs.map +1 -1
  156. package/dist/web/IndexedDBCollection.js +52 -35
  157. package/dist/web/IndexedDBCollection.js.map +1 -1
  158. package/dist/web/WebStorageCollection.cjs +82 -84
  159. package/dist/web/WebStorageCollection.cjs.map +1 -1
  160. package/dist/web/WebStorageCollection.js +81 -83
  161. package/dist/web/WebStorageCollection.js.map +1 -1
  162. package/dist/web/idb.cjs +107 -99
  163. package/dist/web/idb.cjs.map +1 -1
  164. package/dist/web/idb.js +108 -105
  165. package/dist/web/idb.js.map +1 -1
  166. package/dist/web.cjs +4 -6
  167. package/dist/web.js +1 -5
  168. package/dist/wrapAsyncMethods.cjs +141 -168
  169. package/dist/wrapAsyncMethods.cjs.map +1 -1
  170. package/dist/wrapAsyncMethods.js +141 -168
  171. package/dist/wrapAsyncMethods.js.map +1 -1
  172. package/package.json +8 -8
  173. package/src/Pending.test.ts +1 -2
  174. package/src/Sorting.test.ts +1 -1
  175. package/src/produceDraft.test.ts +3 -3
  176. package/src/react/components/CardList.test.tsx +1 -1
  177. package/src/react/components/DataTable.test.tsx +1 -1
  178. package/src/react/components/InfiniteScroll.test.tsx +5 -5
  179. package/dist/mvc-kit.cjs.map +0 -1
  180. package/dist/mvc-kit.js.map +0 -1
  181. package/dist/react-native.cjs.map +0 -1
  182. package/dist/react-native.js.map +0 -1
  183. package/dist/react.cjs.map +0 -1
  184. package/dist/react.js.map +0 -1
  185. package/dist/web.cjs.map +0 -1
  186. package/dist/web.js.map +0 -1
package/dist/Channel.cjs CHANGED
@@ -1,302 +1,278 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const bindPublicMethods = require("./bindPublicMethods.cjs");
4
- const __DEV__ = typeof __MVC_KIT_DEV__ !== "undefined" && __MVC_KIT_DEV__;
5
- const PROTECTED_KEYS = /* @__PURE__ */ new Set(["receive", "disconnected", "addCleanup", "subscribeTo", "listenTo"]);
6
- const INITIAL_STATUS = Object.freeze({
7
- connected: false,
8
- reconnecting: false,
9
- attempt: 0,
10
- error: null
1
+ const require_bindPublicMethods = require("./bindPublicMethods.cjs");
2
+ //#region src/Channel.ts
3
+ var __DEV__ = typeof __MVC_KIT_DEV__ !== "undefined" && __MVC_KIT_DEV__;
4
+ var PROTECTED_KEYS = new Set([
5
+ "receive",
6
+ "disconnected",
7
+ "addCleanup",
8
+ "subscribeTo",
9
+ "listenTo"
10
+ ]);
11
+ var ConnectionState = /* @__PURE__ */ function(ConnectionState) {
12
+ ConnectionState[ConnectionState["Idle"] = 0] = "Idle";
13
+ ConnectionState[ConnectionState["Connecting"] = 1] = "Connecting";
14
+ ConnectionState[ConnectionState["Connected"] = 2] = "Connected";
15
+ ConnectionState[ConnectionState["Reconnecting"] = 3] = "Reconnecting";
16
+ ConnectionState[ConnectionState["Disposed"] = 4] = "Disposed";
17
+ return ConnectionState;
18
+ }(ConnectionState || {});
19
+ var INITIAL_STATUS = Object.freeze({
20
+ connected: false,
21
+ reconnecting: false,
22
+ attempt: 0,
23
+ error: null
11
24
  });
12
- class Channel {
13
- // Static config (subclass overrides)
14
- /** Base delay (ms) for reconnection backoff. */
15
- static RECONNECT_BASE = 1e3;
16
- /** Maximum delay cap (ms) for reconnection backoff. */
17
- static RECONNECT_MAX = 3e4;
18
- /** Exponential backoff multiplier for reconnection delay. */
19
- static RECONNECT_FACTOR = 2;
20
- /** Maximum number of reconnection attempts before giving up. */
21
- static MAX_ATTEMPTS = Infinity;
22
- // ── Internal state ──────────────────────────────────────────────
23
- _status = INITIAL_STATUS;
24
- _connState = 0;
25
- _disposed = false;
26
- _initialized = false;
27
- _listeners = /* @__PURE__ */ new Set();
28
- _handlers = /* @__PURE__ */ new Map();
29
- _abortController = null;
30
- _connectAbort = null;
31
- _reconnectTimer = null;
32
- _cleanups = null;
33
- constructor() {
34
- bindPublicMethods.bindPublicMethods(this, Object.prototype, PROTECTED_KEYS);
35
- }
36
- // ── Subscribable<ChannelStatus> ─────────────────────────────────
37
- /** Current connection status. */
38
- get state() {
39
- return this._status;
40
- }
41
- /** Subscribes to connection status changes. Returns an unsubscribe function. */
42
- subscribe(listener) {
43
- if (this._disposed) return () => {
44
- };
45
- this._listeners.add(listener);
46
- return () => {
47
- this._listeners.delete(listener);
48
- };
49
- }
50
- // ── Disposable / Initializable ──────────────────────────────────
51
- /** Whether this instance has been disposed. */
52
- get disposed() {
53
- return this._disposed;
54
- }
55
- /** Whether init() has been called. */
56
- get initialized() {
57
- return this._initialized;
58
- }
59
- /** AbortSignal that fires when this instance is disposed. Lazily created. */
60
- get disposeSignal() {
61
- if (!this._abortController) {
62
- this._abortController = new AbortController();
63
- }
64
- return this._abortController.signal;
65
- }
66
- /** Initializes the instance. Called automatically by React hooks after mount. */
67
- init() {
68
- if (this._initialized || this._disposed) return;
69
- this._initialized = true;
70
- return this.onInit?.();
71
- }
72
- /** Tears down the instance, releasing all subscriptions and resources. */
73
- dispose() {
74
- if (this._disposed) return;
75
- this._disposed = true;
76
- this._connState = 4;
77
- if (this._reconnectTimer !== null) {
78
- clearTimeout(this._reconnectTimer);
79
- this._reconnectTimer = null;
80
- }
81
- this._connectAbort?.abort();
82
- this._connectAbort = null;
83
- this._abortController?.abort();
84
- try {
85
- this.close();
86
- } catch {
87
- }
88
- if (this._cleanups) {
89
- for (const fn of this._cleanups) fn();
90
- this._cleanups = null;
91
- }
92
- this.onDispose?.();
93
- this._listeners.clear();
94
- this._handlers.clear();
95
- }
96
- // ── Connection control ──────────────────────────────────────────
97
- /** Initiates a connection with automatic reconnection on failure. */
98
- connect() {
99
- if (this._disposed) {
100
- if (__DEV__) {
101
- console.warn("[mvc-kit] connect() called after dispose — ignored.");
102
- }
103
- return;
104
- }
105
- if (__DEV__ && !this._initialized) {
106
- console.warn("[mvc-kit] connect() called before init().");
107
- }
108
- if (this._connState === 1 || this._connState === 2) {
109
- return;
110
- }
111
- if (this._reconnectTimer !== null) {
112
- clearTimeout(this._reconnectTimer);
113
- this._reconnectTimer = null;
114
- }
115
- this._attemptConnect(0);
116
- }
117
- /** Closes the connection and cancels any pending reconnection. */
118
- disconnect() {
119
- if (this._disposed) return;
120
- if (this._reconnectTimer !== null) {
121
- clearTimeout(this._reconnectTimer);
122
- this._reconnectTimer = null;
123
- }
124
- this._connectAbort?.abort();
125
- this._connectAbort = null;
126
- if (this._connState === 2 || this._connState === 1) {
127
- this._connState = 0;
128
- try {
129
- this.close();
130
- } catch {
131
- }
132
- } else {
133
- this._connState = 0;
134
- }
135
- this._setStatus({ connected: false, reconnecting: false, attempt: 0, error: null });
136
- }
137
- // ── Subclass signals ────────────────────────────────────────────
138
- /** Call from subclass when a message arrives from the transport. @protected */
139
- receive(type, payload) {
140
- if (this._disposed) {
141
- if (__DEV__) {
142
- console.warn(`[mvc-kit] receive("${String(type)}") called after dispose — ignored.`);
143
- }
144
- return;
145
- }
146
- const handlers = this._handlers.get(type);
147
- if (handlers) {
148
- for (const handler of handlers) {
149
- handler(payload);
150
- }
151
- }
152
- }
153
- /** Call from subclass when the transport connection drops unexpectedly. Triggers reconnection. @protected */
154
- disconnected() {
155
- if (this._disposed) return;
156
- if (this._connState !== 2 && this._connState !== 1) {
157
- return;
158
- }
159
- this._connectAbort?.abort();
160
- this._connectAbort = null;
161
- this._connState = 3;
162
- this._scheduleReconnect(1);
163
- }
164
- // ── Consumer API ────────────────────────────────────────────────
165
- /** Subscribes to a specific message type. Returns an unsubscribe function. */
166
- on(type, handler) {
167
- if (this._disposed) return () => {
168
- };
169
- let handlers = this._handlers.get(type);
170
- if (!handlers) {
171
- handlers = /* @__PURE__ */ new Set();
172
- this._handlers.set(type, handlers);
173
- }
174
- handlers.add(handler);
175
- return () => {
176
- handlers.delete(handler);
177
- };
178
- }
179
- /** Subscribes to a message type, auto-removing the handler after the first invocation. */
180
- once(type, handler) {
181
- const unsubscribe = this.on(type, (payload) => {
182
- unsubscribe();
183
- handler(payload);
184
- });
185
- return unsubscribe;
186
- }
187
- // ── Infrastructure ──────────────────────────────────────────────
188
- /** Registers a cleanup function to be called on dispose. @protected */
189
- addCleanup(fn) {
190
- if (!this._cleanups) {
191
- this._cleanups = [];
192
- }
193
- this._cleanups.push(fn);
194
- }
195
- /** Subscribes to an external Subscribable with automatic cleanup on dispose. @protected */
196
- subscribeTo(source, listener) {
197
- const unsubscribe = source.subscribe(listener);
198
- this.addCleanup(unsubscribe);
199
- return unsubscribe;
200
- }
201
- /** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */
202
- listenTo(source, event, handler) {
203
- const unsubscribe = source.on(event, handler);
204
- this.addCleanup(unsubscribe);
205
- return unsubscribe;
206
- }
207
- // ── Backoff ─────────────────────────────────────────────────────
208
- /** Computes the reconnect backoff delay with jitter for the given attempt number. @protected */
209
- _calculateDelay(attempt) {
210
- const ctor = this.constructor;
211
- const capped = Math.min(
212
- ctor.RECONNECT_BASE * Math.pow(ctor.RECONNECT_FACTOR, attempt),
213
- ctor.RECONNECT_MAX
214
- );
215
- return Math.random() * capped;
216
- }
217
- // ── Internals ───────────────────────────────────────────────────
218
- _setStatus(next) {
219
- const prev = this._status;
220
- if (prev.connected === next.connected && prev.reconnecting === next.reconnecting && prev.attempt === next.attempt && prev.error === next.error) {
221
- return;
222
- }
223
- this._status = Object.freeze(next);
224
- for (const listener of this._listeners) {
225
- listener(this._status, prev);
226
- }
227
- }
228
- _attemptConnect(attempt) {
229
- if (this._disposed) return;
230
- this._connState = 1;
231
- this._connectAbort?.abort();
232
- this._connectAbort = new AbortController();
233
- const signal = this._abortController ? AbortSignal.any([this._abortController.signal, this._connectAbort.signal]) : this._connectAbort.signal;
234
- this._setStatus({
235
- connected: false,
236
- reconnecting: attempt > 0,
237
- attempt,
238
- error: null
239
- });
240
- let result;
241
- try {
242
- result = this.open(signal);
243
- } catch (e) {
244
- this._onOpenFailed(attempt, e);
245
- return;
246
- }
247
- if (result && typeof result.then === "function") {
248
- result.then(
249
- () => this._onOpenSucceeded(),
250
- (e) => this._onOpenFailed(attempt, e)
251
- );
252
- } else {
253
- this._onOpenSucceeded();
254
- }
255
- }
256
- _onOpenSucceeded() {
257
- if (this._disposed) return;
258
- if (this._connState !== 1) return;
259
- this._connState = 2;
260
- this._setStatus({
261
- connected: true,
262
- reconnecting: false,
263
- attempt: 0,
264
- error: null
265
- });
266
- }
267
- _onOpenFailed(attempt, error) {
268
- if (this._disposed) return;
269
- if (this._connState === 0) return;
270
- this._connectAbort?.abort();
271
- this._connectAbort = null;
272
- this._connState = 3;
273
- this._scheduleReconnect(attempt + 1, error);
274
- }
275
- _scheduleReconnect(attempt, error) {
276
- const ctor = this.constructor;
277
- if (attempt > ctor.MAX_ATTEMPTS) {
278
- this._connState = 0;
279
- this._setStatus({
280
- connected: false,
281
- reconnecting: false,
282
- attempt,
283
- error: error instanceof Error ? error.message : "Max reconnection attempts reached"
284
- });
285
- return;
286
- }
287
- const errorMsg = error instanceof Error ? error.message : error ? String(error) : null;
288
- this._setStatus({
289
- connected: false,
290
- reconnecting: true,
291
- attempt,
292
- error: errorMsg
293
- });
294
- const delay = this._calculateDelay(attempt - 1);
295
- this._reconnectTimer = setTimeout(() => {
296
- this._reconnectTimer = null;
297
- this._attemptConnect(attempt);
298
- }, delay);
299
- }
300
- }
25
+ /**
26
+ * Abstract persistent connection with automatic reconnection and exponential backoff.
27
+ * Subclass to implement WebSocket, SSE, or other transport protocols.
28
+ */
29
+ var Channel = class {
30
+ /** Base delay (ms) for reconnection backoff. */
31
+ static RECONNECT_BASE = 1e3;
32
+ /** Maximum delay cap (ms) for reconnection backoff. */
33
+ static RECONNECT_MAX = 3e4;
34
+ /** Exponential backoff multiplier for reconnection delay. */
35
+ static RECONNECT_FACTOR = 2;
36
+ /** Maximum number of reconnection attempts before giving up. */
37
+ static MAX_ATTEMPTS = Infinity;
38
+ _status = INITIAL_STATUS;
39
+ _connState = ConnectionState.Idle;
40
+ _disposed = false;
41
+ _initialized = false;
42
+ _listeners = /* @__PURE__ */ new Set();
43
+ _handlers = /* @__PURE__ */ new Map();
44
+ _abortController = null;
45
+ _connectAbort = null;
46
+ _reconnectTimer = null;
47
+ _cleanups = null;
48
+ constructor() {
49
+ require_bindPublicMethods.bindPublicMethods(this, Object.prototype, PROTECTED_KEYS);
50
+ }
51
+ /** Current connection status. */
52
+ get state() {
53
+ return this._status;
54
+ }
55
+ /** Subscribes to connection status changes. Returns an unsubscribe function. */
56
+ subscribe(listener) {
57
+ if (this._disposed) return () => {};
58
+ this._listeners.add(listener);
59
+ return () => {
60
+ this._listeners.delete(listener);
61
+ };
62
+ }
63
+ /** Whether this instance has been disposed. */
64
+ get disposed() {
65
+ return this._disposed;
66
+ }
67
+ /** Whether init() has been called. */
68
+ get initialized() {
69
+ return this._initialized;
70
+ }
71
+ /** AbortSignal that fires when this instance is disposed. Lazily created. */
72
+ get disposeSignal() {
73
+ if (!this._abortController) this._abortController = new AbortController();
74
+ return this._abortController.signal;
75
+ }
76
+ /** Initializes the instance. Called automatically by React hooks after mount. */
77
+ init() {
78
+ if (this._initialized || this._disposed) return;
79
+ this._initialized = true;
80
+ return this.onInit?.();
81
+ }
82
+ /** Tears down the instance, releasing all subscriptions and resources. */
83
+ dispose() {
84
+ if (this._disposed) return;
85
+ this._disposed = true;
86
+ this._connState = ConnectionState.Disposed;
87
+ if (this._reconnectTimer !== null) {
88
+ clearTimeout(this._reconnectTimer);
89
+ this._reconnectTimer = null;
90
+ }
91
+ this._connectAbort?.abort();
92
+ this._connectAbort = null;
93
+ this._abortController?.abort();
94
+ try {
95
+ this.close();
96
+ } catch {}
97
+ if (this._cleanups) {
98
+ for (const fn of this._cleanups) fn();
99
+ this._cleanups = null;
100
+ }
101
+ this.onDispose?.();
102
+ this._listeners.clear();
103
+ this._handlers.clear();
104
+ }
105
+ /** Initiates a connection with automatic reconnection on failure. */
106
+ connect() {
107
+ if (this._disposed) {
108
+ if (__DEV__) console.warn("[mvc-kit] connect() called after dispose — ignored.");
109
+ return;
110
+ }
111
+ if (__DEV__ && !this._initialized) console.warn("[mvc-kit] connect() called before init().");
112
+ if (this._connState === ConnectionState.Connecting || this._connState === ConnectionState.Connected) return;
113
+ if (this._reconnectTimer !== null) {
114
+ clearTimeout(this._reconnectTimer);
115
+ this._reconnectTimer = null;
116
+ }
117
+ this._attemptConnect(0);
118
+ }
119
+ /** Closes the connection and cancels any pending reconnection. */
120
+ disconnect() {
121
+ if (this._disposed) return;
122
+ if (this._reconnectTimer !== null) {
123
+ clearTimeout(this._reconnectTimer);
124
+ this._reconnectTimer = null;
125
+ }
126
+ this._connectAbort?.abort();
127
+ this._connectAbort = null;
128
+ if (this._connState === ConnectionState.Connected || this._connState === ConnectionState.Connecting) {
129
+ this._connState = ConnectionState.Idle;
130
+ try {
131
+ this.close();
132
+ } catch {}
133
+ } else this._connState = ConnectionState.Idle;
134
+ this._setStatus({
135
+ connected: false,
136
+ reconnecting: false,
137
+ attempt: 0,
138
+ error: null
139
+ });
140
+ }
141
+ /** Call from subclass when a message arrives from the transport. @protected */
142
+ receive(type, payload) {
143
+ if (this._disposed) {
144
+ if (__DEV__) console.warn(`[mvc-kit] receive("${String(type)}") called after dispose — ignored.`);
145
+ return;
146
+ }
147
+ const handlers = this._handlers.get(type);
148
+ if (handlers) for (const handler of handlers) handler(payload);
149
+ }
150
+ /** Call from subclass when the transport connection drops unexpectedly. Triggers reconnection. @protected */
151
+ disconnected() {
152
+ if (this._disposed) return;
153
+ if (this._connState !== ConnectionState.Connected && this._connState !== ConnectionState.Connecting) return;
154
+ this._connectAbort?.abort();
155
+ this._connectAbort = null;
156
+ this._connState = ConnectionState.Reconnecting;
157
+ this._scheduleReconnect(1);
158
+ }
159
+ /** Subscribes to a specific message type. Returns an unsubscribe function. */
160
+ on(type, handler) {
161
+ if (this._disposed) return () => {};
162
+ let handlers = this._handlers.get(type);
163
+ if (!handlers) {
164
+ handlers = /* @__PURE__ */ new Set();
165
+ this._handlers.set(type, handlers);
166
+ }
167
+ handlers.add(handler);
168
+ return () => {
169
+ handlers.delete(handler);
170
+ };
171
+ }
172
+ /** Subscribes to a message type, auto-removing the handler after the first invocation. */
173
+ once(type, handler) {
174
+ const unsubscribe = this.on(type, (payload) => {
175
+ unsubscribe();
176
+ handler(payload);
177
+ });
178
+ return unsubscribe;
179
+ }
180
+ /** Registers a cleanup function to be called on dispose. @protected */
181
+ addCleanup(fn) {
182
+ if (!this._cleanups) this._cleanups = [];
183
+ this._cleanups.push(fn);
184
+ }
185
+ /** Subscribes to an external Subscribable with automatic cleanup on dispose. @protected */
186
+ subscribeTo(source, listener) {
187
+ const unsubscribe = source.subscribe(listener);
188
+ this.addCleanup(unsubscribe);
189
+ return unsubscribe;
190
+ }
191
+ /** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */
192
+ listenTo(source, event, handler) {
193
+ const unsubscribe = source.on(event, handler);
194
+ this.addCleanup(unsubscribe);
195
+ return unsubscribe;
196
+ }
197
+ /** Computes the reconnect backoff delay with jitter for the given attempt number. @protected */
198
+ _calculateDelay(attempt) {
199
+ const ctor = this.constructor;
200
+ const capped = Math.min(ctor.RECONNECT_BASE * Math.pow(ctor.RECONNECT_FACTOR, attempt), ctor.RECONNECT_MAX);
201
+ return Math.random() * capped;
202
+ }
203
+ _setStatus(next) {
204
+ const prev = this._status;
205
+ if (prev.connected === next.connected && prev.reconnecting === next.reconnecting && prev.attempt === next.attempt && prev.error === next.error) return;
206
+ this._status = Object.freeze(next);
207
+ for (const listener of this._listeners) listener(this._status, prev);
208
+ }
209
+ _attemptConnect(attempt) {
210
+ if (this._disposed) return;
211
+ this._connState = ConnectionState.Connecting;
212
+ this._connectAbort?.abort();
213
+ this._connectAbort = new AbortController();
214
+ const signal = this._abortController ? AbortSignal.any([this._abortController.signal, this._connectAbort.signal]) : this._connectAbort.signal;
215
+ this._setStatus({
216
+ connected: false,
217
+ reconnecting: attempt > 0,
218
+ attempt,
219
+ error: null
220
+ });
221
+ let result;
222
+ try {
223
+ result = this.open(signal);
224
+ } catch (e) {
225
+ this._onOpenFailed(attempt, e);
226
+ return;
227
+ }
228
+ if (result && typeof result.then === "function") result.then(() => this._onOpenSucceeded(), (e) => this._onOpenFailed(attempt, e));
229
+ else this._onOpenSucceeded();
230
+ }
231
+ _onOpenSucceeded() {
232
+ if (this._disposed) return;
233
+ if (this._connState !== ConnectionState.Connecting) return;
234
+ this._connState = ConnectionState.Connected;
235
+ this._setStatus({
236
+ connected: true,
237
+ reconnecting: false,
238
+ attempt: 0,
239
+ error: null
240
+ });
241
+ }
242
+ _onOpenFailed(attempt, error) {
243
+ if (this._disposed) return;
244
+ if (this._connState === ConnectionState.Idle) return;
245
+ this._connectAbort?.abort();
246
+ this._connectAbort = null;
247
+ this._connState = ConnectionState.Reconnecting;
248
+ this._scheduleReconnect(attempt + 1, error);
249
+ }
250
+ _scheduleReconnect(attempt, error) {
251
+ if (attempt > this.constructor.MAX_ATTEMPTS) {
252
+ this._connState = ConnectionState.Idle;
253
+ this._setStatus({
254
+ connected: false,
255
+ reconnecting: false,
256
+ attempt,
257
+ error: error instanceof Error ? error.message : "Max reconnection attempts reached"
258
+ });
259
+ return;
260
+ }
261
+ const errorMsg = error instanceof Error ? error.message : error ? String(error) : null;
262
+ this._setStatus({
263
+ connected: false,
264
+ reconnecting: true,
265
+ attempt,
266
+ error: errorMsg
267
+ });
268
+ const delay = this._calculateDelay(attempt - 1);
269
+ this._reconnectTimer = setTimeout(() => {
270
+ this._reconnectTimer = null;
271
+ this._attemptConnect(attempt);
272
+ }, delay);
273
+ }
274
+ };
275
+ //#endregion
301
276
  exports.Channel = Channel;
302
- //# sourceMappingURL=Channel.cjs.map
277
+
278
+ //# sourceMappingURL=Channel.cjs.map