mvc-kit 2.5.5 → 2.6.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 CHANGED
@@ -497,18 +497,31 @@ class ChatViewModel extends ViewModel<State> {
497
497
  }
498
498
  ```
499
499
 
500
+ ### `listenTo(source, event, handler)` (protected)
501
+
502
+ Subscribe to a typed event on a Channel or EventBus with automatic cleanup on dispose (and reset, for ViewModels). The event counterpart to `subscribeTo`.
503
+
504
+ ```typescript
505
+ class ChatViewModel extends ViewModel<State> {
506
+ private channel = singleton(ChatChannel);
507
+
508
+ protected onInit() {
509
+ this.listenTo(this.channel, 'message', (msg) => {
510
+ this.set({ messages: [...this.state.messages, msg] });
511
+ });
512
+ }
513
+ }
514
+ ```
515
+
516
+ Available on ViewModel, Controller, Channel, and Model.
517
+
500
518
  ### `addCleanup(fn)` (protected)
501
519
 
502
- Register a teardown callback that fires on `dispose()`, after disposeSignal abort but before `onDispose()`. No manual bookkeeping needed.
520
+ Register a teardown callback that fires on `dispose()`, after disposeSignal abort but before `onDispose()`. Use for timers and external subscriptions not covered by `subscribeTo` or `listenTo`.
503
521
 
504
522
  ```typescript
505
523
  class DashboardController extends Controller {
506
524
  protected onInit() {
507
- // Subscriptions are automatically cleaned up on dispose
508
- const unsub = someStore.subscribe(() => this.refresh());
509
- this.addCleanup(unsub);
510
-
511
- // Timers too
512
525
  const id = setInterval(() => this.poll(), 5000);
513
526
  this.addCleanup(() => clearInterval(id));
514
527
  }
@@ -451,7 +451,24 @@ Persistence adds I/O overhead on every mutation. For high-churn data, this cause
451
451
 
452
452
  ---
453
453
 
454
- ## 19. Missing hydrate() for Async Adapters
454
+ ## 19. Using addCleanup for channel.on()/bus.on() Subscriptions
455
+
456
+ ```typescript
457
+ // BAD — manual addCleanup is easy to forget and not reset-safe
458
+ protected onInit() {
459
+ const unsub = this.channel.on('message', handler);
460
+ this.addCleanup(unsub);
461
+ }
462
+
463
+ // GOOD — listenTo auto-cleans up on dispose and reset
464
+ protected onInit() {
465
+ this.listenTo(this.channel, 'message', handler);
466
+ }
467
+ ```
468
+
469
+ ---
470
+
471
+ ## 20. Missing hydrate() for Async Adapters
455
472
 
456
473
  ```typescript
457
474
  // BAD — accessing items before hydrate() on async adapter
@@ -68,6 +68,7 @@ class MyVM extends ViewModel<State, { saved: { id: string }; error: void }> {
68
68
  ### Cleanup
69
69
  - `disposeSignal: AbortSignal` — Lazily created, auto-aborted on `dispose()`.
70
70
  - `subscribeTo(source: Subscribable, listener): void` — Auto-unsubscribe on dispose.
71
+ - `listenTo(source: EventSource, event, handler): void` — Subscribe to Channel/EventBus event with auto-cleanup on dispose and reset.
71
72
  - `addCleanup(fn: () => void): void` — Register teardown callback.
72
73
  - `subscribe(listener: Listener<S>): () => void` — Subscribe to state changes.
73
74
  - `dispose(): void` — Idempotent. `set()` and `emit()` become no-ops after dispose.
@@ -105,7 +106,7 @@ protected validate(state: S): ValidationErrors<S> {
105
106
  ```
106
107
 
107
108
  ### Lifecycle & Cleanup
108
- Same as ViewModel: `onInit()`, `onSet()`, `onDispose()`, `disposeSignal`, `subscribeTo()`, `addCleanup()`, `subscribe()`, `dispose()`.
109
+ Same as ViewModel: `onInit()`, `onSet()`, `onDispose()`, `disposeSignal`, `subscribeTo()`, `listenTo()`, `addCleanup()`, `subscribe()`, `dispose()`.
109
110
 
110
111
  ---
111
112
 
@@ -273,6 +274,7 @@ Stateless orchestrator. Component-scoped.
273
274
  - `onDispose(): void`
274
275
  - `disposeSignal: AbortSignal`
275
276
  - `subscribeTo(source, listener): void`
277
+ - `listenTo(source, event, handler): void`
276
278
  - `addCleanup(fn): void`
277
279
  - `dispose(): void`
278
280
 
@@ -88,7 +88,7 @@ class LocationsViewModel extends ViewModel<State> {
88
88
  }
89
89
  ```
90
90
 
91
- Use `subscribeTo()` only for imperative side effects (e.g. play a sound on new messages), not for deriving values.
91
+ Use `subscribeTo()` only for imperative side effects (e.g. play a sound on new messages), not for deriving values. Use `listenTo()` for Channel/EventBus event subscriptions — it auto-cleans up on dispose and reset.
92
92
 
93
93
  ---
94
94
 
@@ -34,7 +34,7 @@ import { NativeCollection } from 'mvc-kit/react-native';
34
34
  4. **One ViewModel per component** via `useLocal`. No `useEffect` for data loading — use `onInit()`.
35
35
  5. **No `useState`/`useMemo`/`useCallback`** — the ViewModel is the hook.
36
36
  6. **Components are declarative.** Read `state.x` for raw, `vm.x` for computed, `vm.async.x` for loading/error.
37
- 7. **Collections are encapsulated.** ViewModel getters read from collections directly — auto-tracking handles reactivity. Use `subscribeTo()` only for imperative side effects. Components never import Collections.
37
+ 7. **Collections are encapsulated.** ViewModel getters read from collections directly — auto-tracking handles reactivity. Use `subscribeTo()` only for imperative side effects. Use `listenTo()` for Channel/EventBus event subscriptions (auto-cleanup on dispose and reset). Components never import Collections.
38
38
  8. **Services are stateless.** Accept `AbortSignal`, throw `HttpError`, no knowledge of ViewModels.
39
39
  9. **Pass `this.disposeSignal`** to every async call.
40
40
  10. **Lifecycle**: `construct → init() → use → dispose()`. Hooks auto-call `init()`.
@@ -34,7 +34,7 @@ import { NativeCollection } from 'mvc-kit/react-native';
34
34
  4. **One ViewModel per component** via `useLocal`. No `useEffect` for data loading — use `onInit()`.
35
35
  5. **No `useState`/`useMemo`/`useCallback`** — the ViewModel is the hook.
36
36
  6. **Components are declarative.** Read `state.x` for raw, `vm.x` for computed, `vm.async.x` for loading/error.
37
- 7. **Collections are encapsulated.** ViewModel getters read from collections directly — auto-tracking handles reactivity. Use `subscribeTo()` only for imperative side effects. Components never import Collections.
37
+ 7. **Collections are encapsulated.** ViewModel getters read from collections directly — auto-tracking handles reactivity. Use `subscribeTo()` only for imperative side effects. Use `listenTo()` for Channel/EventBus event subscriptions (auto-cleanup on dispose and reset). Components never import Collections.
38
38
  8. **Services are stateless.** Accept `AbortSignal`, throw `HttpError`, no knowledge of ViewModels.
39
39
  9. **Pass `this.disposeSignal`** to every async call.
40
40
  10. **Lifecycle**: `construct → init() → use → dispose()`. Hooks auto-call `init()`.
package/dist/Channel.cjs CHANGED
@@ -193,6 +193,12 @@ class Channel {
193
193
  this.addCleanup(unsubscribe);
194
194
  return unsubscribe;
195
195
  }
196
+ /** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */
197
+ listenTo(source, event, handler) {
198
+ const unsubscribe = source.on(event, handler);
199
+ this.addCleanup(unsubscribe);
200
+ return unsubscribe;
201
+ }
196
202
  // ── Backoff ─────────────────────────────────────────────────────
197
203
  /** Computes the reconnect backoff delay with jitter for the given attempt number. @protected */
198
204
  _calculateDelay(attempt) {
@@ -1 +1 @@
1
- {"version":3,"file":"Channel.cjs","sources":["../src/Channel.ts"],"sourcesContent":["import type { Listener, Subscribable, Disposable, Initializable } from './types';\n\nconst __DEV__ = typeof __MVC_KIT_DEV__ !== 'undefined' && __MVC_KIT_DEV__;\n\n// ── Types ─────────────────────────────────────────────────────────\n\n/** Describes the current connection state of a Channel. */\nexport interface ChannelStatus {\n readonly connected: boolean;\n readonly reconnecting: boolean;\n readonly attempt: number;\n readonly error: string | null;\n}\n\ntype Handler<T> = (payload: T) => void;\n\nconst enum ConnectionState {\n Idle,\n Connecting,\n Connected,\n Reconnecting,\n Disposed,\n}\n\nconst INITIAL_STATUS: ChannelStatus = Object.freeze({\n connected: false,\n reconnecting: false,\n attempt: 0,\n error: null,\n});\n\n// ── Channel ───────────────────────────────────────────────────────\n\n/**\n * Abstract persistent connection with automatic reconnection and exponential backoff.\n * Subclass to implement WebSocket, SSE, or other transport protocols.\n */\nexport abstract class Channel<M extends Record<string, any>>\n implements Subscribable<ChannelStatus>, Initializable, Disposable\n{\n // Static config (subclass overrides)\n /** Base delay (ms) for reconnection backoff. */\n static RECONNECT_BASE = 1000;\n /** Maximum delay cap (ms) for reconnection backoff. */\n static RECONNECT_MAX = 30000;\n /** Exponential backoff multiplier for reconnection delay. */\n static RECONNECT_FACTOR = 2;\n /** Maximum number of reconnection attempts before giving up. */\n static MAX_ATTEMPTS = Infinity;\n\n // ── Internal state ──────────────────────────────────────────────\n private _status: ChannelStatus = INITIAL_STATUS;\n private _connState: ConnectionState = ConnectionState.Idle;\n private _disposed = false;\n private _initialized = false;\n private _listeners = new Set<Listener<ChannelStatus>>();\n private _handlers = new Map<keyof M, Set<Handler<unknown>>>();\n private _abortController: AbortController | null = null;\n private _connectAbort: AbortController | null = null;\n private _reconnectTimer: ReturnType<typeof setTimeout> | null = null;\n private _cleanups: (() => void)[] | null = null;\n\n // ── Subscribable<ChannelStatus> ─────────────────────────────────\n\n /** Current connection status. */\n get state(): ChannelStatus {\n return this._status;\n }\n\n /** Subscribes to connection status changes. Returns an unsubscribe function. */\n subscribe(listener: Listener<ChannelStatus>): () => void {\n if (this._disposed) return () => {};\n this._listeners.add(listener);\n return () => { this._listeners.delete(listener); };\n }\n\n // ── Disposable / Initializable ──────────────────────────────────\n\n /** Whether this instance has been disposed. */\n get disposed(): boolean {\n return this._disposed;\n }\n\n /** Whether init() has been called. */\n get initialized(): boolean {\n return this._initialized;\n }\n\n /** AbortSignal that fires when this instance is disposed. Lazily created. */\n get disposeSignal(): AbortSignal {\n if (!this._abortController) {\n this._abortController = new AbortController();\n }\n return this._abortController.signal;\n }\n\n /** Initializes the instance. Called automatically by React hooks after mount. */\n init(): void | Promise<void> {\n if (this._initialized || this._disposed) return;\n this._initialized = true;\n return this.onInit?.();\n }\n\n /** Tears down the instance, releasing all subscriptions and resources. */\n dispose(): void {\n if (this._disposed) return;\n this._disposed = true;\n this._connState = ConnectionState.Disposed;\n\n // Cancel pending reconnect\n if (this._reconnectTimer !== null) {\n clearTimeout(this._reconnectTimer);\n this._reconnectTimer = null;\n }\n\n // Abort per-connection signal\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n // Abort dispose signal\n this._abortController?.abort();\n\n // Close transport\n try { this.close(); } catch { /* swallow close errors during dispose */ }\n\n // Run cleanups\n if (this._cleanups) {\n for (const fn of this._cleanups) fn();\n this._cleanups = null;\n }\n\n this.onDispose?.();\n this._listeners.clear();\n this._handlers.clear();\n }\n\n // ── Subclass contract ───────────────────────────────────────────\n\n /** Establishes the underlying connection. Called internally by connect(). @protected */\n protected abstract open(signal: AbortSignal): void | Promise<void>;\n /** Tears down the underlying connection. Called internally by disconnect() and dispose(). @protected */\n protected abstract close(): void;\n\n // ── Connection control ──────────────────────────────────────────\n\n /** Initiates a connection with automatic reconnection on failure. */\n connect(): void {\n if (this._disposed) {\n if (__DEV__) {\n console.warn('[mvc-kit] connect() called after dispose — ignored.');\n }\n return;\n }\n if (__DEV__ && !this._initialized) {\n console.warn('[mvc-kit] connect() called before init().');\n }\n if (\n this._connState === ConnectionState.Connecting ||\n this._connState === ConnectionState.Connected\n ) {\n return;\n }\n\n // Cancel any pending reconnect\n if (this._reconnectTimer !== null) {\n clearTimeout(this._reconnectTimer);\n this._reconnectTimer = null;\n }\n\n this._attemptConnect(0);\n }\n\n /** Closes the connection and cancels any pending reconnection. */\n disconnect(): void {\n if (this._disposed) return;\n\n // Cancel pending reconnect\n if (this._reconnectTimer !== null) {\n clearTimeout(this._reconnectTimer);\n this._reconnectTimer = null;\n }\n\n // Abort current connection attempt\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n // Close transport\n if (\n this._connState === ConnectionState.Connected ||\n this._connState === ConnectionState.Connecting\n ) {\n this._connState = ConnectionState.Idle;\n try { this.close(); } catch { /* swallow */ }\n } else {\n this._connState = ConnectionState.Idle;\n }\n\n this._setStatus({ connected: false, reconnecting: false, attempt: 0, error: null });\n }\n\n // ── Subclass signals ────────────────────────────────────────────\n\n /** Call from subclass when a message arrives from the transport. @protected */\n protected receive<K extends keyof M>(type: K, payload: M[K]): void {\n if (this._disposed) {\n if (__DEV__) {\n console.warn(`[mvc-kit] receive(\"${String(type)}\") called after dispose — ignored.`);\n }\n return;\n }\n\n const handlers = this._handlers.get(type);\n if (handlers) {\n for (const handler of handlers) {\n handler(payload);\n }\n }\n }\n\n /** Call from subclass when the transport connection drops unexpectedly. Triggers reconnection. @protected */\n protected disconnected(): void {\n if (this._disposed) return;\n // Only trigger reconnect from connected or connecting states\n if (\n this._connState !== ConnectionState.Connected &&\n this._connState !== ConnectionState.Connecting\n ) {\n return;\n }\n\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n this._connState = ConnectionState.Reconnecting;\n this._scheduleReconnect(1);\n }\n\n // ── Consumer API ────────────────────────────────────────────────\n\n /** Subscribes to a specific message type. Returns an unsubscribe function. */\n on<K extends keyof M>(type: K, handler: Handler<M[K]>): () => void {\n if (this._disposed) return () => {};\n\n let handlers = this._handlers.get(type);\n if (!handlers) {\n handlers = new Set();\n this._handlers.set(type, handlers);\n }\n handlers.add(handler as Handler<unknown>);\n\n return () => { handlers!.delete(handler as Handler<unknown>); };\n }\n\n /** Subscribes to a message type, auto-removing the handler after the first invocation. */\n once<K extends keyof M>(type: K, handler: Handler<M[K]>): () => void {\n const unsubscribe = this.on(type, (payload) => {\n unsubscribe();\n handler(payload);\n });\n return unsubscribe;\n }\n\n // ── Infrastructure ──────────────────────────────────────────────\n\n /** Registers a cleanup function to be called on dispose. @protected */\n protected addCleanup(fn: () => void): void {\n if (!this._cleanups) {\n this._cleanups = [];\n }\n this._cleanups.push(fn);\n }\n\n /** Subscribes to an external Subscribable with automatic cleanup on dispose. @protected */\n protected subscribeTo<T>(source: Subscribable<T>, listener: Listener<T>): () => void {\n const unsubscribe = source.subscribe(listener);\n this.addCleanup(unsubscribe);\n return unsubscribe;\n }\n\n /** Lifecycle hook called at the end of init(). Override to load initial data. @protected */\n protected onInit?(): void | Promise<void>;\n /** Lifecycle hook called during dispose(). Override for custom teardown. @protected */\n protected onDispose?(): void;\n\n // ── Backoff ─────────────────────────────────────────────────────\n\n /** Computes the reconnect backoff delay with jitter for the given attempt number. @protected */\n protected _calculateDelay(attempt: number): number {\n const ctor = this.constructor as typeof Channel;\n const capped = Math.min(\n ctor.RECONNECT_BASE * Math.pow(ctor.RECONNECT_FACTOR, attempt),\n ctor.RECONNECT_MAX,\n );\n return Math.random() * capped;\n }\n\n // ── Internals ───────────────────────────────────────────────────\n\n private _setStatus(next: ChannelStatus): void {\n const prev = this._status;\n if (\n prev.connected === next.connected &&\n prev.reconnecting === next.reconnecting &&\n prev.attempt === next.attempt &&\n prev.error === next.error\n ) {\n return;\n }\n\n this._status = Object.freeze(next);\n for (const listener of this._listeners) {\n listener(this._status, prev);\n }\n }\n\n private _attemptConnect(attempt: number): void {\n if (this._disposed) return;\n\n this._connState = ConnectionState.Connecting;\n\n // Create per-connection abort controller\n this._connectAbort?.abort();\n this._connectAbort = new AbortController();\n\n const signal = this._abortController\n ? AbortSignal.any([this._abortController.signal, this._connectAbort.signal])\n : this._connectAbort.signal;\n\n this._setStatus({\n connected: false,\n reconnecting: attempt > 0,\n attempt,\n error: null,\n });\n\n let result: void | Promise<void>;\n try {\n result = this.open(signal);\n } catch (e) {\n this._onOpenFailed(attempt, e);\n return;\n }\n\n if (result && typeof (result as Promise<void>).then === 'function') {\n (result as Promise<void>).then(\n () => this._onOpenSucceeded(),\n (e) => this._onOpenFailed(attempt, e),\n );\n } else {\n this._onOpenSucceeded();\n }\n }\n\n private _onOpenSucceeded(): void {\n if (this._disposed) return;\n // Only transition if we're still connecting (disconnect may have been called)\n if (this._connState !== ConnectionState.Connecting) return;\n\n this._connState = ConnectionState.Connected;\n this._setStatus({\n connected: true,\n reconnecting: false,\n attempt: 0,\n error: null,\n });\n }\n\n private _onOpenFailed(attempt: number, error: unknown): void {\n if (this._disposed) return;\n // If disconnect was called during open, don't reconnect\n if (this._connState === ConnectionState.Idle) return;\n\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n this._connState = ConnectionState.Reconnecting;\n this._scheduleReconnect(attempt + 1, error);\n }\n\n private _scheduleReconnect(attempt: number, error?: unknown): void {\n const ctor = this.constructor as typeof Channel;\n\n if (attempt > ctor.MAX_ATTEMPTS) {\n this._connState = ConnectionState.Idle;\n this._setStatus({\n connected: false,\n reconnecting: false,\n attempt,\n error: error instanceof Error ? error.message : 'Max reconnection attempts reached',\n });\n return;\n }\n\n const errorMsg = error instanceof Error ? error.message : (error ? String(error) : null);\n\n this._setStatus({\n connected: false,\n reconnecting: true,\n attempt,\n error: errorMsg,\n });\n\n const delay = this._calculateDelay(attempt - 1);\n this._reconnectTimer = setTimeout(() => {\n this._reconnectTimer = null;\n this._attemptConnect(attempt);\n }, delay);\n }\n}\n"],"names":[],"mappings":";;AAEA,MAAM,UAAU,OAAO,oBAAoB,eAAe;AAsB1D,MAAM,iBAAgC,OAAO,OAAO;AAAA,EAClD,WAAW;AAAA,EACX,cAAc;AAAA,EACd,SAAS;AAAA,EACT,OAAO;AACT,CAAC;AAQM,MAAe,QAEtB;AAAA;AAAA;AAAA,EAGE,OAAO,iBAAiB;AAAA;AAAA,EAExB,OAAO,gBAAgB;AAAA;AAAA,EAEvB,OAAO,mBAAmB;AAAA;AAAA,EAE1B,OAAO,eAAe;AAAA;AAAA,EAGd,UAAyB;AAAA,EACzB,aAA8B;AAAA,EAC9B,YAAY;AAAA,EACZ,eAAe;AAAA,EACf,iCAAiB,IAAA;AAAA,EACjB,gCAAgB,IAAA;AAAA,EAChB,mBAA2C;AAAA,EAC3C,gBAAwC;AAAA,EACxC,kBAAwD;AAAA,EACxD,YAAmC;AAAA;AAAA;AAAA,EAK3C,IAAI,QAAuB;AACzB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,UAAU,UAA+C;AACvD,QAAI,KAAK,UAAW,QAAO,MAAM;AAAA,IAAC;AAClC,SAAK,WAAW,IAAI,QAAQ;AAC5B,WAAO,MAAM;AAAE,WAAK,WAAW,OAAO,QAAQ;AAAA,IAAG;AAAA,EACnD;AAAA;AAAA;AAAA,EAKA,IAAI,WAAoB;AACtB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,cAAuB;AACzB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,gBAA6B;AAC/B,QAAI,CAAC,KAAK,kBAAkB;AAC1B,WAAK,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B,WAAK,gBAAgB,OAAO;AAAA,IAC9B,GAAG,KAAK;AAAA,EACV;AACF;;"}
1
+ {"version":3,"file":"Channel.cjs","sources":["../src/Channel.ts"],"sourcesContent":["import type { Listener, Subscribable, Disposable, Initializable, EventSource } from './types';\n\nconst __DEV__ = typeof __MVC_KIT_DEV__ !== 'undefined' && __MVC_KIT_DEV__;\n\n// ── Types ─────────────────────────────────────────────────────────\n\n/** Describes the current connection state of a Channel. */\nexport interface ChannelStatus {\n readonly connected: boolean;\n readonly reconnecting: boolean;\n readonly attempt: number;\n readonly error: string | null;\n}\n\ntype Handler<T> = (payload: T) => void;\n\nconst enum ConnectionState {\n Idle,\n Connecting,\n Connected,\n Reconnecting,\n Disposed,\n}\n\nconst INITIAL_STATUS: ChannelStatus = Object.freeze({\n connected: false,\n reconnecting: false,\n attempt: 0,\n error: null,\n});\n\n// ── Channel ───────────────────────────────────────────────────────\n\n/**\n * Abstract persistent connection with automatic reconnection and exponential backoff.\n * Subclass to implement WebSocket, SSE, or other transport protocols.\n */\nexport abstract class Channel<M extends Record<string, any>>\n implements Subscribable<ChannelStatus>, Initializable, Disposable\n{\n // Static config (subclass overrides)\n /** Base delay (ms) for reconnection backoff. */\n static RECONNECT_BASE = 1000;\n /** Maximum delay cap (ms) for reconnection backoff. */\n static RECONNECT_MAX = 30000;\n /** Exponential backoff multiplier for reconnection delay. */\n static RECONNECT_FACTOR = 2;\n /** Maximum number of reconnection attempts before giving up. */\n static MAX_ATTEMPTS = Infinity;\n\n // ── Internal state ──────────────────────────────────────────────\n private _status: ChannelStatus = INITIAL_STATUS;\n private _connState: ConnectionState = ConnectionState.Idle;\n private _disposed = false;\n private _initialized = false;\n private _listeners = new Set<Listener<ChannelStatus>>();\n private _handlers = new Map<keyof M, Set<Handler<unknown>>>();\n private _abortController: AbortController | null = null;\n private _connectAbort: AbortController | null = null;\n private _reconnectTimer: ReturnType<typeof setTimeout> | null = null;\n private _cleanups: (() => void)[] | null = null;\n\n // ── Subscribable<ChannelStatus> ─────────────────────────────────\n\n /** Current connection status. */\n get state(): ChannelStatus {\n return this._status;\n }\n\n /** Subscribes to connection status changes. Returns an unsubscribe function. */\n subscribe(listener: Listener<ChannelStatus>): () => void {\n if (this._disposed) return () => {};\n this._listeners.add(listener);\n return () => { this._listeners.delete(listener); };\n }\n\n // ── Disposable / Initializable ──────────────────────────────────\n\n /** Whether this instance has been disposed. */\n get disposed(): boolean {\n return this._disposed;\n }\n\n /** Whether init() has been called. */\n get initialized(): boolean {\n return this._initialized;\n }\n\n /** AbortSignal that fires when this instance is disposed. Lazily created. */\n get disposeSignal(): AbortSignal {\n if (!this._abortController) {\n this._abortController = new AbortController();\n }\n return this._abortController.signal;\n }\n\n /** Initializes the instance. Called automatically by React hooks after mount. */\n init(): void | Promise<void> {\n if (this._initialized || this._disposed) return;\n this._initialized = true;\n return this.onInit?.();\n }\n\n /** Tears down the instance, releasing all subscriptions and resources. */\n dispose(): void {\n if (this._disposed) return;\n this._disposed = true;\n this._connState = ConnectionState.Disposed;\n\n // Cancel pending reconnect\n if (this._reconnectTimer !== null) {\n clearTimeout(this._reconnectTimer);\n this._reconnectTimer = null;\n }\n\n // Abort per-connection signal\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n // Abort dispose signal\n this._abortController?.abort();\n\n // Close transport\n try { this.close(); } catch { /* swallow close errors during dispose */ }\n\n // Run cleanups\n if (this._cleanups) {\n for (const fn of this._cleanups) fn();\n this._cleanups = null;\n }\n\n this.onDispose?.();\n this._listeners.clear();\n this._handlers.clear();\n }\n\n // ── Subclass contract ───────────────────────────────────────────\n\n /** Establishes the underlying connection. Called internally by connect(). @protected */\n protected abstract open(signal: AbortSignal): void | Promise<void>;\n /** Tears down the underlying connection. Called internally by disconnect() and dispose(). @protected */\n protected abstract close(): void;\n\n // ── Connection control ──────────────────────────────────────────\n\n /** Initiates a connection with automatic reconnection on failure. */\n connect(): void {\n if (this._disposed) {\n if (__DEV__) {\n console.warn('[mvc-kit] connect() called after dispose — ignored.');\n }\n return;\n }\n if (__DEV__ && !this._initialized) {\n console.warn('[mvc-kit] connect() called before init().');\n }\n if (\n this._connState === ConnectionState.Connecting ||\n this._connState === ConnectionState.Connected\n ) {\n return;\n }\n\n // Cancel any pending reconnect\n if (this._reconnectTimer !== null) {\n clearTimeout(this._reconnectTimer);\n this._reconnectTimer = null;\n }\n\n this._attemptConnect(0);\n }\n\n /** Closes the connection and cancels any pending reconnection. */\n disconnect(): void {\n if (this._disposed) return;\n\n // Cancel pending reconnect\n if (this._reconnectTimer !== null) {\n clearTimeout(this._reconnectTimer);\n this._reconnectTimer = null;\n }\n\n // Abort current connection attempt\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n // Close transport\n if (\n this._connState === ConnectionState.Connected ||\n this._connState === ConnectionState.Connecting\n ) {\n this._connState = ConnectionState.Idle;\n try { this.close(); } catch { /* swallow */ }\n } else {\n this._connState = ConnectionState.Idle;\n }\n\n this._setStatus({ connected: false, reconnecting: false, attempt: 0, error: null });\n }\n\n // ── Subclass signals ────────────────────────────────────────────\n\n /** Call from subclass when a message arrives from the transport. @protected */\n protected receive<K extends keyof M>(type: K, payload: M[K]): void {\n if (this._disposed) {\n if (__DEV__) {\n console.warn(`[mvc-kit] receive(\"${String(type)}\") called after dispose — ignored.`);\n }\n return;\n }\n\n const handlers = this._handlers.get(type);\n if (handlers) {\n for (const handler of handlers) {\n handler(payload);\n }\n }\n }\n\n /** Call from subclass when the transport connection drops unexpectedly. Triggers reconnection. @protected */\n protected disconnected(): void {\n if (this._disposed) return;\n // Only trigger reconnect from connected or connecting states\n if (\n this._connState !== ConnectionState.Connected &&\n this._connState !== ConnectionState.Connecting\n ) {\n return;\n }\n\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n this._connState = ConnectionState.Reconnecting;\n this._scheduleReconnect(1);\n }\n\n // ── Consumer API ────────────────────────────────────────────────\n\n /** Subscribes to a specific message type. Returns an unsubscribe function. */\n on<K extends keyof M>(type: K, handler: Handler<M[K]>): () => void {\n if (this._disposed) return () => {};\n\n let handlers = this._handlers.get(type);\n if (!handlers) {\n handlers = new Set();\n this._handlers.set(type, handlers);\n }\n handlers.add(handler as Handler<unknown>);\n\n return () => { handlers!.delete(handler as Handler<unknown>); };\n }\n\n /** Subscribes to a message type, auto-removing the handler after the first invocation. */\n once<K extends keyof M>(type: K, handler: Handler<M[K]>): () => void {\n const unsubscribe = this.on(type, (payload) => {\n unsubscribe();\n handler(payload);\n });\n return unsubscribe;\n }\n\n // ── Infrastructure ──────────────────────────────────────────────\n\n /** Registers a cleanup function to be called on dispose. @protected */\n protected addCleanup(fn: () => void): void {\n if (!this._cleanups) {\n this._cleanups = [];\n }\n this._cleanups.push(fn);\n }\n\n /** Subscribes to an external Subscribable with automatic cleanup on dispose. @protected */\n protected subscribeTo<T>(source: Subscribable<T>, listener: Listener<T>): () => void {\n const unsubscribe = source.subscribe(listener);\n this.addCleanup(unsubscribe);\n return unsubscribe;\n }\n\n /** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */\n protected listenTo<E extends Record<string, any>, K extends keyof E>(\n source: EventSource<E>,\n event: K,\n handler: (payload: E[K]) => void,\n ): () => void {\n const unsubscribe = source.on(event, handler);\n this.addCleanup(unsubscribe);\n return unsubscribe;\n }\n\n /** Lifecycle hook called at the end of init(). Override to load initial data. @protected */\n protected onInit?(): void | Promise<void>;\n /** Lifecycle hook called during dispose(). Override for custom teardown. @protected */\n protected onDispose?(): void;\n\n // ── Backoff ─────────────────────────────────────────────────────\n\n /** Computes the reconnect backoff delay with jitter for the given attempt number. @protected */\n protected _calculateDelay(attempt: number): number {\n const ctor = this.constructor as typeof Channel;\n const capped = Math.min(\n ctor.RECONNECT_BASE * Math.pow(ctor.RECONNECT_FACTOR, attempt),\n ctor.RECONNECT_MAX,\n );\n return Math.random() * capped;\n }\n\n // ── Internals ───────────────────────────────────────────────────\n\n private _setStatus(next: ChannelStatus): void {\n const prev = this._status;\n if (\n prev.connected === next.connected &&\n prev.reconnecting === next.reconnecting &&\n prev.attempt === next.attempt &&\n prev.error === next.error\n ) {\n return;\n }\n\n this._status = Object.freeze(next);\n for (const listener of this._listeners) {\n listener(this._status, prev);\n }\n }\n\n private _attemptConnect(attempt: number): void {\n if (this._disposed) return;\n\n this._connState = ConnectionState.Connecting;\n\n // Create per-connection abort controller\n this._connectAbort?.abort();\n this._connectAbort = new AbortController();\n\n const signal = this._abortController\n ? AbortSignal.any([this._abortController.signal, this._connectAbort.signal])\n : this._connectAbort.signal;\n\n this._setStatus({\n connected: false,\n reconnecting: attempt > 0,\n attempt,\n error: null,\n });\n\n let result: void | Promise<void>;\n try {\n result = this.open(signal);\n } catch (e) {\n this._onOpenFailed(attempt, e);\n return;\n }\n\n if (result && typeof (result as Promise<void>).then === 'function') {\n (result as Promise<void>).then(\n () => this._onOpenSucceeded(),\n (e) => this._onOpenFailed(attempt, e),\n );\n } else {\n this._onOpenSucceeded();\n }\n }\n\n private _onOpenSucceeded(): void {\n if (this._disposed) return;\n // Only transition if we're still connecting (disconnect may have been called)\n if (this._connState !== ConnectionState.Connecting) return;\n\n this._connState = ConnectionState.Connected;\n this._setStatus({\n connected: true,\n reconnecting: false,\n attempt: 0,\n error: null,\n });\n }\n\n private _onOpenFailed(attempt: number, error: unknown): void {\n if (this._disposed) return;\n // If disconnect was called during open, don't reconnect\n if (this._connState === ConnectionState.Idle) return;\n\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n this._connState = ConnectionState.Reconnecting;\n this._scheduleReconnect(attempt + 1, error);\n }\n\n private _scheduleReconnect(attempt: number, error?: unknown): void {\n const ctor = this.constructor as typeof Channel;\n\n if (attempt > ctor.MAX_ATTEMPTS) {\n this._connState = ConnectionState.Idle;\n this._setStatus({\n connected: false,\n reconnecting: false,\n attempt,\n error: error instanceof Error ? error.message : 'Max reconnection attempts reached',\n });\n return;\n }\n\n const errorMsg = error instanceof Error ? error.message : (error ? String(error) : null);\n\n this._setStatus({\n connected: false,\n reconnecting: true,\n attempt,\n error: errorMsg,\n });\n\n const delay = this._calculateDelay(attempt - 1);\n this._reconnectTimer = setTimeout(() => {\n this._reconnectTimer = null;\n this._attemptConnect(attempt);\n }, delay);\n }\n}\n"],"names":[],"mappings":";;AAEA,MAAM,UAAU,OAAO,oBAAoB,eAAe;AAsB1D,MAAM,iBAAgC,OAAO,OAAO;AAAA,EAClD,WAAW;AAAA,EACX,cAAc;AAAA,EACd,SAAS;AAAA,EACT,OAAO;AACT,CAAC;AAQM,MAAe,QAEtB;AAAA;AAAA;AAAA,EAGE,OAAO,iBAAiB;AAAA;AAAA,EAExB,OAAO,gBAAgB;AAAA;AAAA,EAEvB,OAAO,mBAAmB;AAAA;AAAA,EAE1B,OAAO,eAAe;AAAA;AAAA,EAGd,UAAyB;AAAA,EACzB,aAA8B;AAAA,EAC9B,YAAY;AAAA,EACZ,eAAe;AAAA,EACf,iCAAiB,IAAA;AAAA,EACjB,gCAAgB,IAAA;AAAA,EAChB,mBAA2C;AAAA,EAC3C,gBAAwC;AAAA,EACxC,kBAAwD;AAAA,EACxD,YAAmC;AAAA;AAAA;AAAA,EAK3C,IAAI,QAAuB;AACzB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,UAAU,UAA+C;AACvD,QAAI,KAAK,UAAW,QAAO,MAAM;AAAA,IAAC;AAClC,SAAK,WAAW,IAAI,QAAQ;AAC5B,WAAO,MAAM;AAAE,WAAK,WAAW,OAAO,QAAQ;AAAA,IAAG;AAAA,EACnD;AAAA;AAAA;AAAA,EAKA,IAAI,WAAoB;AACtB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,cAAuB;AACzB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,gBAA6B;AAC/B,QAAI,CAAC,KAAK,kBAAkB;AAC1B,WAAK,mBAAmB,IAAI,gBAAA;AAAA,IAC9B;AACA,WAAO,KAAK,iBAAiB;AAAA,EAC/B;AAAA;AAAA,EAGA,OAA6B;AAC3B,QAA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,WAAK,kBAAkB;AACvB,WAAK,gBAAgB,OAAO;AAAA,IAC9B,GAAG,KAAK;AAAA,EACV;AACF;;"}
package/dist/Channel.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Listener, Subscribable, Disposable, Initializable } from './types';
1
+ import type { Listener, Subscribable, Disposable, Initializable, EventSource } from './types';
2
2
  /** Describes the current connection state of a Channel. */
3
3
  export interface ChannelStatus {
4
4
  readonly connected: boolean;
@@ -64,6 +64,8 @@ export declare abstract class Channel<M extends Record<string, any>> implements
64
64
  protected addCleanup(fn: () => void): void;
65
65
  /** Subscribes to an external Subscribable with automatic cleanup on dispose. @protected */
66
66
  protected subscribeTo<T>(source: Subscribable<T>, listener: Listener<T>): () => void;
67
+ /** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */
68
+ protected listenTo<E extends Record<string, any>, K extends keyof E>(source: EventSource<E>, event: K, handler: (payload: E[K]) => void): () => void;
67
69
  /** Lifecycle hook called at the end of init(). Override to load initial data. @protected */
68
70
  protected onInit?(): void | Promise<void>;
69
71
  /** Lifecycle hook called during dispose(). Override for custom teardown. @protected */
@@ -1 +1 @@
1
- {"version":3,"file":"Channel.d.ts","sourceRoot":"","sources":["../src/Channel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAMjF,2DAA2D;AAC3D,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC;IAC/B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/B;AAED,KAAK,OAAO,CAAC,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,KAAK,IAAI,CAAC;AAmBvC;;;GAGG;AACH,8BAAsB,OAAO,CAAC,CA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AAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI;IAMpF,yGAAyG;IACzG,SAAS,CAAC,QAAQ,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EACjE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,EACtB,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,GAC/B,MAAM,IAAI;IAMb,4FAA4F;IAC5F,SAAS,CAAC,MAAM,CAAC,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IACzC,uFAAuF;IACvF,SAAS,CAAC,SAAS,CAAC,IAAI,IAAI;IAI5B,gGAAgG;IAChG,SAAS,CAAC,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAWlD,OAAO,CAAC,UAAU;IAiBlB,OAAO,CAAC,eAAe;IAsCvB,OAAO,CAAC,gBAAgB;IAcxB,OAAO,CAAC,aAAa;IAYrB,OAAO,CAAC,kBAAkB;CA6B3B"}
package/dist/Channel.js CHANGED
@@ -191,6 +191,12 @@ class Channel {
191
191
  this.addCleanup(unsubscribe);
192
192
  return unsubscribe;
193
193
  }
194
+ /** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */
195
+ listenTo(source, event, handler) {
196
+ const unsubscribe = source.on(event, handler);
197
+ this.addCleanup(unsubscribe);
198
+ return unsubscribe;
199
+ }
194
200
  // ── Backoff ─────────────────────────────────────────────────────
195
201
  /** Computes the reconnect backoff delay with jitter for the given attempt number. @protected */
196
202
  _calculateDelay(attempt) {
@@ -1 +1 @@
1
- {"version":3,"file":"Channel.js","sources":["../src/Channel.ts"],"sourcesContent":["import type { Listener, Subscribable, Disposable, Initializable } from './types';\n\nconst __DEV__ = typeof __MVC_KIT_DEV__ !== 'undefined' && __MVC_KIT_DEV__;\n\n// ── Types ─────────────────────────────────────────────────────────\n\n/** Describes the current connection state of a Channel. */\nexport interface ChannelStatus {\n readonly connected: boolean;\n readonly reconnecting: boolean;\n readonly attempt: number;\n readonly error: string | null;\n}\n\ntype Handler<T> = (payload: T) => void;\n\nconst enum ConnectionState {\n Idle,\n Connecting,\n Connected,\n Reconnecting,\n Disposed,\n}\n\nconst INITIAL_STATUS: ChannelStatus = Object.freeze({\n connected: false,\n reconnecting: false,\n attempt: 0,\n error: null,\n});\n\n// ── Channel ───────────────────────────────────────────────────────\n\n/**\n * Abstract persistent connection with automatic reconnection and exponential backoff.\n * Subclass to implement WebSocket, SSE, or other transport protocols.\n */\nexport abstract class Channel<M extends Record<string, any>>\n implements Subscribable<ChannelStatus>, Initializable, Disposable\n{\n // Static config (subclass overrides)\n /** Base delay (ms) for reconnection backoff. */\n static RECONNECT_BASE = 1000;\n /** Maximum delay cap (ms) for reconnection backoff. */\n static RECONNECT_MAX = 30000;\n /** Exponential backoff multiplier for reconnection delay. */\n static RECONNECT_FACTOR = 2;\n /** Maximum number of reconnection attempts before giving up. */\n static MAX_ATTEMPTS = Infinity;\n\n // ── Internal state ──────────────────────────────────────────────\n private _status: ChannelStatus = INITIAL_STATUS;\n private _connState: ConnectionState = ConnectionState.Idle;\n private _disposed = false;\n private _initialized = false;\n private _listeners = new Set<Listener<ChannelStatus>>();\n private _handlers = new Map<keyof M, Set<Handler<unknown>>>();\n private _abortController: AbortController | null = null;\n private _connectAbort: AbortController | null = null;\n private _reconnectTimer: ReturnType<typeof setTimeout> | null = null;\n private _cleanups: (() => void)[] | null = null;\n\n // ── Subscribable<ChannelStatus> ─────────────────────────────────\n\n /** Current connection status. */\n get state(): ChannelStatus {\n return this._status;\n }\n\n /** Subscribes to connection status changes. Returns an unsubscribe function. */\n subscribe(listener: Listener<ChannelStatus>): () => void {\n if (this._disposed) return () => {};\n this._listeners.add(listener);\n return () => { this._listeners.delete(listener); };\n }\n\n // ── Disposable / Initializable ──────────────────────────────────\n\n /** Whether this instance has been disposed. */\n get disposed(): boolean {\n return this._disposed;\n }\n\n /** Whether init() has been called. */\n get initialized(): boolean {\n return this._initialized;\n }\n\n /** AbortSignal that fires when this instance is disposed. Lazily created. */\n get disposeSignal(): AbortSignal {\n if (!this._abortController) {\n this._abortController = new AbortController();\n }\n return this._abortController.signal;\n }\n\n /** Initializes the instance. Called automatically by React hooks after mount. */\n init(): void | Promise<void> {\n if (this._initialized || this._disposed) return;\n this._initialized = true;\n return this.onInit?.();\n }\n\n /** Tears down the instance, releasing all subscriptions and resources. */\n dispose(): void {\n if (this._disposed) return;\n this._disposed = true;\n this._connState = ConnectionState.Disposed;\n\n // Cancel pending reconnect\n if (this._reconnectTimer !== null) {\n clearTimeout(this._reconnectTimer);\n this._reconnectTimer = null;\n }\n\n // Abort per-connection signal\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n // Abort dispose signal\n this._abortController?.abort();\n\n // Close transport\n try { this.close(); } catch { /* swallow close errors during dispose */ }\n\n // Run cleanups\n if (this._cleanups) {\n for (const fn of this._cleanups) fn();\n this._cleanups = null;\n }\n\n this.onDispose?.();\n this._listeners.clear();\n this._handlers.clear();\n }\n\n // ── Subclass contract ───────────────────────────────────────────\n\n /** Establishes the underlying connection. Called internally by connect(). @protected */\n protected abstract open(signal: AbortSignal): void | Promise<void>;\n /** Tears down the underlying connection. Called internally by disconnect() and dispose(). @protected */\n protected abstract close(): void;\n\n // ── Connection control ──────────────────────────────────────────\n\n /** Initiates a connection with automatic reconnection on failure. */\n connect(): void {\n if (this._disposed) {\n if (__DEV__) {\n console.warn('[mvc-kit] connect() called after dispose — ignored.');\n }\n return;\n }\n if (__DEV__ && !this._initialized) {\n console.warn('[mvc-kit] connect() called before init().');\n }\n if (\n this._connState === ConnectionState.Connecting ||\n this._connState === ConnectionState.Connected\n ) {\n return;\n }\n\n // Cancel any pending reconnect\n if (this._reconnectTimer !== null) {\n clearTimeout(this._reconnectTimer);\n this._reconnectTimer = null;\n }\n\n this._attemptConnect(0);\n }\n\n /** Closes the connection and cancels any pending reconnection. */\n disconnect(): void {\n if (this._disposed) return;\n\n // Cancel pending reconnect\n if (this._reconnectTimer !== null) {\n clearTimeout(this._reconnectTimer);\n this._reconnectTimer = null;\n }\n\n // Abort current connection attempt\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n // Close transport\n if (\n this._connState === ConnectionState.Connected ||\n this._connState === ConnectionState.Connecting\n ) {\n this._connState = ConnectionState.Idle;\n try { this.close(); } catch { /* swallow */ }\n } else {\n this._connState = ConnectionState.Idle;\n }\n\n this._setStatus({ connected: false, reconnecting: false, attempt: 0, error: null });\n }\n\n // ── Subclass signals ────────────────────────────────────────────\n\n /** Call from subclass when a message arrives from the transport. @protected */\n protected receive<K extends keyof M>(type: K, payload: M[K]): void {\n if (this._disposed) {\n if (__DEV__) {\n console.warn(`[mvc-kit] receive(\"${String(type)}\") called after dispose — ignored.`);\n }\n return;\n }\n\n const handlers = this._handlers.get(type);\n if (handlers) {\n for (const handler of handlers) {\n handler(payload);\n }\n }\n }\n\n /** Call from subclass when the transport connection drops unexpectedly. Triggers reconnection. @protected */\n protected disconnected(): void {\n if (this._disposed) return;\n // Only trigger reconnect from connected or connecting states\n if (\n this._connState !== ConnectionState.Connected &&\n this._connState !== ConnectionState.Connecting\n ) {\n return;\n }\n\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n this._connState = ConnectionState.Reconnecting;\n this._scheduleReconnect(1);\n }\n\n // ── Consumer API ────────────────────────────────────────────────\n\n /** Subscribes to a specific message type. Returns an unsubscribe function. */\n on<K extends keyof M>(type: K, handler: Handler<M[K]>): () => void {\n if (this._disposed) return () => {};\n\n let handlers = this._handlers.get(type);\n if (!handlers) {\n handlers = new Set();\n this._handlers.set(type, handlers);\n }\n handlers.add(handler as Handler<unknown>);\n\n return () => { handlers!.delete(handler as Handler<unknown>); };\n }\n\n /** Subscribes to a message type, auto-removing the handler after the first invocation. */\n once<K extends keyof M>(type: K, handler: Handler<M[K]>): () => void {\n const unsubscribe = this.on(type, (payload) => {\n unsubscribe();\n handler(payload);\n });\n return unsubscribe;\n }\n\n // ── Infrastructure ──────────────────────────────────────────────\n\n /** Registers a cleanup function to be called on dispose. @protected */\n protected addCleanup(fn: () => void): void {\n if (!this._cleanups) {\n this._cleanups = [];\n }\n this._cleanups.push(fn);\n }\n\n /** Subscribes to an external Subscribable with automatic cleanup on dispose. @protected */\n protected subscribeTo<T>(source: Subscribable<T>, listener: Listener<T>): () => void {\n const unsubscribe = source.subscribe(listener);\n this.addCleanup(unsubscribe);\n return unsubscribe;\n }\n\n /** Lifecycle hook called at the end of init(). Override to load initial data. @protected */\n protected onInit?(): void | Promise<void>;\n /** Lifecycle hook called during dispose(). Override for custom teardown. @protected */\n protected onDispose?(): void;\n\n // ── Backoff ─────────────────────────────────────────────────────\n\n /** Computes the reconnect backoff delay with jitter for the given attempt number. @protected */\n protected _calculateDelay(attempt: number): number {\n const ctor = this.constructor as typeof Channel;\n const capped = Math.min(\n ctor.RECONNECT_BASE * Math.pow(ctor.RECONNECT_FACTOR, attempt),\n ctor.RECONNECT_MAX,\n );\n return Math.random() * capped;\n }\n\n // ── Internals ───────────────────────────────────────────────────\n\n private _setStatus(next: ChannelStatus): void {\n const prev = this._status;\n if (\n prev.connected === next.connected &&\n prev.reconnecting === next.reconnecting &&\n prev.attempt === next.attempt &&\n prev.error === next.error\n ) {\n return;\n }\n\n this._status = Object.freeze(next);\n for (const listener of this._listeners) {\n listener(this._status, prev);\n }\n }\n\n private _attemptConnect(attempt: number): void {\n if (this._disposed) return;\n\n this._connState = ConnectionState.Connecting;\n\n // Create per-connection abort controller\n this._connectAbort?.abort();\n this._connectAbort = new AbortController();\n\n const signal = this._abortController\n ? AbortSignal.any([this._abortController.signal, this._connectAbort.signal])\n : this._connectAbort.signal;\n\n this._setStatus({\n connected: false,\n reconnecting: attempt > 0,\n attempt,\n error: null,\n });\n\n let result: void | Promise<void>;\n try {\n result = this.open(signal);\n } catch (e) {\n this._onOpenFailed(attempt, e);\n return;\n }\n\n if (result && typeof (result as Promise<void>).then === 'function') {\n (result as Promise<void>).then(\n () => this._onOpenSucceeded(),\n (e) => this._onOpenFailed(attempt, e),\n );\n } else {\n this._onOpenSucceeded();\n }\n }\n\n private _onOpenSucceeded(): void {\n if (this._disposed) return;\n // Only transition if we're still connecting (disconnect may have been called)\n if (this._connState !== ConnectionState.Connecting) return;\n\n this._connState = ConnectionState.Connected;\n this._setStatus({\n connected: true,\n reconnecting: false,\n attempt: 0,\n error: null,\n });\n }\n\n private _onOpenFailed(attempt: number, error: unknown): void {\n if (this._disposed) return;\n // If disconnect was called during open, don't reconnect\n if (this._connState === ConnectionState.Idle) return;\n\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n this._connState = ConnectionState.Reconnecting;\n this._scheduleReconnect(attempt + 1, error);\n }\n\n private _scheduleReconnect(attempt: number, error?: unknown): void {\n const ctor = this.constructor as typeof Channel;\n\n if (attempt > ctor.MAX_ATTEMPTS) {\n this._connState = ConnectionState.Idle;\n this._setStatus({\n connected: false,\n reconnecting: false,\n attempt,\n error: error instanceof Error ? error.message : 'Max reconnection attempts reached',\n });\n return;\n }\n\n const errorMsg = error instanceof Error ? error.message : (error ? String(error) : null);\n\n this._setStatus({\n connected: false,\n reconnecting: true,\n attempt,\n error: errorMsg,\n });\n\n const delay = this._calculateDelay(attempt - 1);\n this._reconnectTimer = setTimeout(() => {\n this._reconnectTimer = null;\n this._attemptConnect(attempt);\n }, delay);\n }\n}\n"],"names":[],"mappings":"AAEA,MAAM,UAAU,OAAO,oBAAoB,eAAe;AAsB1D,MAAM,iBAAgC,OAAO,OAAO;AAAA,EAClD,WAAW;AAAA,EACX,cAAc;AAAA,EACd,SAAS;AAAA,EACT,OAAO;AACT,CAAC;AAQM,MAAe,QAEtB;AAAA;AAAA;AAAA,EAGE,OAAO,iBAAiB;AAAA;AAAA,EAExB,OAAO,gBAAgB;AAAA;AAAA,EAEvB,OAAO,mBAAmB;AAAA;AAAA,EAE1B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QAAI,KAAK,UAAW;AAEpB,QAAI,KAAK,eAAe,EAAsB;AAE9C,SAAK,eAAe,MAAA;AACpB,SAAK,gBAAgB;AAErB,SAAK,aAAa;AAClB,SAAK,mBAAmB,UAAU,GAAG,KAAK;AAAA,EAC5C;AAAA,EAEQ,mBAAmB,SAAiB,OAAuB;AACjE,UAAM,OAAO,KAAK;AAElB,QAAI,UAAU,KAAK,cAAc;AAC/B,WAAK,aAAa;AAClB,WAAK,WAAW;AAAA,QACd,WAAW;AAAA,QACX,cAAc;AAAA,QACd;AAAA,QACA,OAAO,iBAAiB,QAAQ,MAAM,UAAU;AAAA,MAAA,CACjD;AACD;AAAA,IACF;AAEA,UAAM,WAAW,iBAAiB,QAAQ,MAAM,UAAW,QAAQ,OAAO,KAAK,IAAI;AAEnF,SAAK,WAAW;AAAA,MACd,WAAW;AAAA,MACX,cAAc;AAAA,MACd;AAAA,MACA,OAAO;AAAA,IAAA,CACR;AAED,UAAM,QAAQ,KAAK,gBAAgB,UAAU,CAAC;AAC9C,SAAK,kBAAkB,WAAW,MAAM;AACtC,WAAK,kBAAkB;AACvB,WAAK,gBAAgB,OAAO;AAAA,IAC9B,GAAG,KAAK;AAAA,EACV;AACF;"}
1
+ {"version":3,"file":"Channel.js","sources":["../src/Channel.ts"],"sourcesContent":["import type { Listener, Subscribable, Disposable, Initializable, EventSource } from './types';\n\nconst __DEV__ = typeof __MVC_KIT_DEV__ !== 'undefined' && __MVC_KIT_DEV__;\n\n// ── Types ─────────────────────────────────────────────────────────\n\n/** Describes the current connection state of a Channel. */\nexport interface ChannelStatus {\n readonly connected: boolean;\n readonly reconnecting: boolean;\n readonly attempt: number;\n readonly error: string | null;\n}\n\ntype Handler<T> = (payload: T) => void;\n\nconst enum ConnectionState {\n Idle,\n Connecting,\n Connected,\n Reconnecting,\n Disposed,\n}\n\nconst INITIAL_STATUS: ChannelStatus = Object.freeze({\n connected: false,\n reconnecting: false,\n attempt: 0,\n error: null,\n});\n\n// ── Channel ───────────────────────────────────────────────────────\n\n/**\n * Abstract persistent connection with automatic reconnection and exponential backoff.\n * Subclass to implement WebSocket, SSE, or other transport protocols.\n */\nexport abstract class Channel<M extends Record<string, any>>\n implements Subscribable<ChannelStatus>, Initializable, Disposable\n{\n // Static config (subclass overrides)\n /** Base delay (ms) for reconnection backoff. */\n static RECONNECT_BASE = 1000;\n /** Maximum delay cap (ms) for reconnection backoff. */\n static RECONNECT_MAX = 30000;\n /** Exponential backoff multiplier for reconnection delay. */\n static RECONNECT_FACTOR = 2;\n /** Maximum number of reconnection attempts before giving up. */\n static MAX_ATTEMPTS = Infinity;\n\n // ── Internal state ──────────────────────────────────────────────\n private _status: ChannelStatus = INITIAL_STATUS;\n private _connState: ConnectionState = ConnectionState.Idle;\n private _disposed = false;\n private _initialized = false;\n private _listeners = new Set<Listener<ChannelStatus>>();\n private _handlers = new Map<keyof M, Set<Handler<unknown>>>();\n private _abortController: AbortController | null = null;\n private _connectAbort: AbortController | null = null;\n private _reconnectTimer: ReturnType<typeof setTimeout> | null = null;\n private _cleanups: (() => void)[] | null = null;\n\n // ── Subscribable<ChannelStatus> ─────────────────────────────────\n\n /** Current connection status. */\n get state(): ChannelStatus {\n return this._status;\n }\n\n /** Subscribes to connection status changes. Returns an unsubscribe function. */\n subscribe(listener: Listener<ChannelStatus>): () => void {\n if (this._disposed) return () => {};\n this._listeners.add(listener);\n return () => { this._listeners.delete(listener); };\n }\n\n // ── Disposable / Initializable ──────────────────────────────────\n\n /** Whether this instance has been disposed. */\n get disposed(): boolean {\n return this._disposed;\n }\n\n /** Whether init() has been called. */\n get initialized(): boolean {\n return this._initialized;\n }\n\n /** AbortSignal that fires when this instance is disposed. Lazily created. */\n get disposeSignal(): AbortSignal {\n if (!this._abortController) {\n this._abortController = new AbortController();\n }\n return this._abortController.signal;\n }\n\n /** Initializes the instance. Called automatically by React hooks after mount. */\n init(): void | Promise<void> {\n if (this._initialized || this._disposed) return;\n this._initialized = true;\n return this.onInit?.();\n }\n\n /** Tears down the instance, releasing all subscriptions and resources. */\n dispose(): void {\n if (this._disposed) return;\n this._disposed = true;\n this._connState = ConnectionState.Disposed;\n\n // Cancel pending reconnect\n if (this._reconnectTimer !== null) {\n clearTimeout(this._reconnectTimer);\n this._reconnectTimer = null;\n }\n\n // Abort per-connection signal\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n // Abort dispose signal\n this._abortController?.abort();\n\n // Close transport\n try { this.close(); } catch { /* swallow close errors during dispose */ }\n\n // Run cleanups\n if (this._cleanups) {\n for (const fn of this._cleanups) fn();\n this._cleanups = null;\n }\n\n this.onDispose?.();\n this._listeners.clear();\n this._handlers.clear();\n }\n\n // ── Subclass contract ───────────────────────────────────────────\n\n /** Establishes the underlying connection. Called internally by connect(). @protected */\n protected abstract open(signal: AbortSignal): void | Promise<void>;\n /** Tears down the underlying connection. Called internally by disconnect() and dispose(). @protected */\n protected abstract close(): void;\n\n // ── Connection control ──────────────────────────────────────────\n\n /** Initiates a connection with automatic reconnection on failure. */\n connect(): void {\n if (this._disposed) {\n if (__DEV__) {\n console.warn('[mvc-kit] connect() called after dispose — ignored.');\n }\n return;\n }\n if (__DEV__ && !this._initialized) {\n console.warn('[mvc-kit] connect() called before init().');\n }\n if (\n this._connState === ConnectionState.Connecting ||\n this._connState === ConnectionState.Connected\n ) {\n return;\n }\n\n // Cancel any pending reconnect\n if (this._reconnectTimer !== null) {\n clearTimeout(this._reconnectTimer);\n this._reconnectTimer = null;\n }\n\n this._attemptConnect(0);\n }\n\n /** Closes the connection and cancels any pending reconnection. */\n disconnect(): void {\n if (this._disposed) return;\n\n // Cancel pending reconnect\n if (this._reconnectTimer !== null) {\n clearTimeout(this._reconnectTimer);\n this._reconnectTimer = null;\n }\n\n // Abort current connection attempt\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n // Close transport\n if (\n this._connState === ConnectionState.Connected ||\n this._connState === ConnectionState.Connecting\n ) {\n this._connState = ConnectionState.Idle;\n try { this.close(); } catch { /* swallow */ }\n } else {\n this._connState = ConnectionState.Idle;\n }\n\n this._setStatus({ connected: false, reconnecting: false, attempt: 0, error: null });\n }\n\n // ── Subclass signals ────────────────────────────────────────────\n\n /** Call from subclass when a message arrives from the transport. @protected */\n protected receive<K extends keyof M>(type: K, payload: M[K]): void {\n if (this._disposed) {\n if (__DEV__) {\n console.warn(`[mvc-kit] receive(\"${String(type)}\") called after dispose — ignored.`);\n }\n return;\n }\n\n const handlers = this._handlers.get(type);\n if (handlers) {\n for (const handler of handlers) {\n handler(payload);\n }\n }\n }\n\n /** Call from subclass when the transport connection drops unexpectedly. Triggers reconnection. @protected */\n protected disconnected(): void {\n if (this._disposed) return;\n // Only trigger reconnect from connected or connecting states\n if (\n this._connState !== ConnectionState.Connected &&\n this._connState !== ConnectionState.Connecting\n ) {\n return;\n }\n\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n this._connState = ConnectionState.Reconnecting;\n this._scheduleReconnect(1);\n }\n\n // ── Consumer API ────────────────────────────────────────────────\n\n /** Subscribes to a specific message type. Returns an unsubscribe function. */\n on<K extends keyof M>(type: K, handler: Handler<M[K]>): () => void {\n if (this._disposed) return () => {};\n\n let handlers = this._handlers.get(type);\n if (!handlers) {\n handlers = new Set();\n this._handlers.set(type, handlers);\n }\n handlers.add(handler as Handler<unknown>);\n\n return () => { handlers!.delete(handler as Handler<unknown>); };\n }\n\n /** Subscribes to a message type, auto-removing the handler after the first invocation. */\n once<K extends keyof M>(type: K, handler: Handler<M[K]>): () => void {\n const unsubscribe = this.on(type, (payload) => {\n unsubscribe();\n handler(payload);\n });\n return unsubscribe;\n }\n\n // ── Infrastructure ──────────────────────────────────────────────\n\n /** Registers a cleanup function to be called on dispose. @protected */\n protected addCleanup(fn: () => void): void {\n if (!this._cleanups) {\n this._cleanups = [];\n }\n this._cleanups.push(fn);\n }\n\n /** Subscribes to an external Subscribable with automatic cleanup on dispose. @protected */\n protected subscribeTo<T>(source: Subscribable<T>, listener: Listener<T>): () => void {\n const unsubscribe = source.subscribe(listener);\n this.addCleanup(unsubscribe);\n return unsubscribe;\n }\n\n /** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */\n protected listenTo<E extends Record<string, any>, K extends keyof E>(\n source: EventSource<E>,\n event: K,\n handler: (payload: E[K]) => void,\n ): () => void {\n const unsubscribe = source.on(event, handler);\n this.addCleanup(unsubscribe);\n return unsubscribe;\n }\n\n /** Lifecycle hook called at the end of init(). Override to load initial data. @protected */\n protected onInit?(): void | Promise<void>;\n /** Lifecycle hook called during dispose(). Override for custom teardown. @protected */\n protected onDispose?(): void;\n\n // ── Backoff ─────────────────────────────────────────────────────\n\n /** Computes the reconnect backoff delay with jitter for the given attempt number. @protected */\n protected _calculateDelay(attempt: number): number {\n const ctor = this.constructor as typeof Channel;\n const capped = Math.min(\n ctor.RECONNECT_BASE * Math.pow(ctor.RECONNECT_FACTOR, attempt),\n ctor.RECONNECT_MAX,\n );\n return Math.random() * capped;\n }\n\n // ── Internals ───────────────────────────────────────────────────\n\n private _setStatus(next: ChannelStatus): void {\n const prev = this._status;\n if (\n prev.connected === next.connected &&\n prev.reconnecting === next.reconnecting &&\n prev.attempt === next.attempt &&\n prev.error === next.error\n ) {\n return;\n }\n\n this._status = Object.freeze(next);\n for (const listener of this._listeners) {\n listener(this._status, prev);\n }\n }\n\n private _attemptConnect(attempt: number): void {\n if (this._disposed) return;\n\n this._connState = ConnectionState.Connecting;\n\n // Create per-connection abort controller\n this._connectAbort?.abort();\n this._connectAbort = new AbortController();\n\n const signal = this._abortController\n ? AbortSignal.any([this._abortController.signal, this._connectAbort.signal])\n : this._connectAbort.signal;\n\n this._setStatus({\n connected: false,\n reconnecting: attempt > 0,\n attempt,\n error: null,\n });\n\n let result: void | Promise<void>;\n try {\n result = this.open(signal);\n } catch (e) {\n this._onOpenFailed(attempt, e);\n return;\n }\n\n if (result && typeof (result as Promise<void>).then === 'function') {\n (result as Promise<void>).then(\n () => this._onOpenSucceeded(),\n (e) => this._onOpenFailed(attempt, e),\n );\n } else {\n this._onOpenSucceeded();\n }\n }\n\n private _onOpenSucceeded(): void {\n if (this._disposed) return;\n // Only transition if we're still connecting (disconnect may have been called)\n if (this._connState !== ConnectionState.Connecting) return;\n\n this._connState = ConnectionState.Connected;\n this._setStatus({\n connected: true,\n reconnecting: false,\n attempt: 0,\n error: null,\n });\n }\n\n private _onOpenFailed(attempt: number, error: unknown): void {\n if (this._disposed) return;\n // If disconnect was called during open, don't reconnect\n if (this._connState === ConnectionState.Idle) return;\n\n this._connectAbort?.abort();\n this._connectAbort = null;\n\n this._connState = ConnectionState.Reconnecting;\n this._scheduleReconnect(attempt + 1, error);\n }\n\n private _scheduleReconnect(attempt: number, error?: unknown): void {\n const ctor = this.constructor as typeof Channel;\n\n if (attempt > ctor.MAX_ATTEMPTS) {\n this._connState = ConnectionState.Idle;\n this._setStatus({\n connected: false,\n reconnecting: false,\n attempt,\n error: error instanceof Error ? error.message : 'Max reconnection attempts reached',\n });\n return;\n }\n\n const errorMsg = error instanceof Error ? error.message : (error ? String(error) : null);\n\n this._setStatus({\n connected: false,\n reconnecting: true,\n attempt,\n error: errorMsg,\n });\n\n const delay = this._calculateDelay(attempt - 1);\n this._reconnectTimer = setTimeout(() => {\n this._reconnectTimer = null;\n this._attemptConnect(attempt);\n }, delay);\n }\n}\n"],"names":[],"mappings":"AAEA,MAAM,UAAU,OAAO,oBAAoB,eAAe;AAsB1D,MAAM,iBAAgC,OAAO,OAAO;AAAA,EAClD,WAAW;AAAA,EACX,cAAc;AAAA,EACd,SAAS;AAAA,EACT,OAAO;AACT,CAAC;AAQM,MAAe,QAEtB;AAAA;AAAA;AAAA,EAGE,OAAO,iBAAiB;AAAA;AAAA,EAExB,OAAO,gBAAgB;AAAA;AAAA,EAEvB,OAAO,mBAAmB;AAAA;AAAA,EAE1B,OAAO,eAAe;AAAA;AAAA,EAGd,UAAyB;AAAA,EACzB,aAA8B;AAAA,EAC9B,YAAY;AAAA,EACZ,eAAe;AAAA,EACf,iCAAiB,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OAAsB;AAC3D,QAAI,KAAK,UAAW;AAEpB,QAAI,KAAK,eAAe,EAAsB;AAE9C,SAAK,eAAe,MAAA;AACpB,SAAK,gBAAgB;AAErB,SAAK,aAAa;AAClB,SAAK,mBAAmB,UAAU,GAAG,KAAK;AAAA,EAC5C;AAAA,EAEQ,mBAAmB,SAAiB,OAAuB;AACjE,UAAM,OAAO,KAAK;AAElB,QAAI,UAAU,KAAK,cAAc;AAC/B,WAAK,aAAa;AAClB,WAAK,WAAW;AAAA,QACd,WAAW;AAAA,QACX,cAAc;AAAA,QACd;AAAA,QACA,OAAO,iBAAiB,QAAQ,MAAM,UAAU;AAAA,MAAA,CACjD;AACD;AAAA,IACF;AAEA,UAAM,WAAW,iBAAiB,QAAQ,MAAM,UAAW,QAAQ,OAAO,KAAK,IAAI;AAEnF,SAAK,WAAW;AAAA,MACd,WAAW;AAAA,MACX,cAAc;AAAA,MACd;AAAA,MACA,OAAO;AAAA,IAAA,CACR;AAED,UAAM,QAAQ,KAAK,gBAAgB,UAAU,CAAC;AAC9C,SAAK,kBAAkB,WAAW,MAAM;AACtC,WAAK,kBAAkB;AACvB,WAAK,gBAAgB,OAAO;AAAA,IAC9B,GAAG,KAAK;AAAA,EACV;AACF;"}
@@ -52,6 +52,12 @@ class Controller {
52
52
  this.addCleanup(unsubscribe);
53
53
  return unsubscribe;
54
54
  }
55
+ /** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */
56
+ listenTo(source, event, handler) {
57
+ const unsubscribe = source.on(event, handler);
58
+ this.addCleanup(unsubscribe);
59
+ return unsubscribe;
60
+ }
55
61
  }
56
62
  exports.Controller = Controller;
57
63
  //# sourceMappingURL=Controller.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"Controller.cjs","sources":["../src/Controller.ts"],"sourcesContent":["import type { Disposable, Subscribable, Listener } from './types';\n\n/**\n * Base class for stateless orchestrators.\n * Controllers coordinate between ViewModels, Models, and Services.\n */\nexport abstract class Controller implements Disposable {\n private _disposed = false;\n private _initialized = false;\n private _abortController: AbortController | null = null;\n private _cleanups: (() => void)[] | null = null;\n\n /** Whether this instance has been disposed. */\n get disposed(): boolean {\n return this._disposed;\n }\n\n /** Whether init() has been called. */\n get initialized(): boolean {\n return this._initialized;\n }\n\n /** AbortSignal that fires when this instance is disposed. Lazily created. */\n get disposeSignal(): AbortSignal {\n if (!this._abortController) {\n this._abortController = new AbortController();\n }\n return this._abortController.signal;\n }\n\n /** Initializes the instance. Called automatically by React hooks after mount. */\n init(): void | Promise<void> {\n if (this._initialized || this._disposed) return;\n this._initialized = true;\n return this.onInit?.();\n }\n\n /** Tears down the instance, releasing all subscriptions and resources. */\n dispose(): void {\n if (this._disposed) {\n return;\n }\n\n this._disposed = true;\n this._abortController?.abort();\n if (this._cleanups) {\n for (const fn of this._cleanups) fn();\n this._cleanups = null;\n }\n this.onDispose?.();\n }\n\n /** Registers a cleanup function to be called on dispose. @protected */\n protected addCleanup(fn: () => void): void {\n if (!this._cleanups) {\n this._cleanups = [];\n }\n this._cleanups.push(fn);\n }\n\n /** Subscribes to an external Subscribable with automatic cleanup on dispose. @protected */\n protected subscribeTo<T>(source: Subscribable<T>, listener: Listener<T>): () => void {\n const unsubscribe = source.subscribe(listener);\n this.addCleanup(unsubscribe);\n return unsubscribe;\n }\n\n /** Lifecycle hook called at the end of init(). Override to load initial data. @protected */\n protected onInit?(): void | Promise<void>;\n /** Lifecycle hook called during dispose(). Override for custom teardown. @protected */\n protected onDispose?(): void;\n}\n"],"names":[],"mappings":";;AAMO,MAAe,WAAiC;AAAA,EAC7C,YAAY;AAAA,EACZ,eAAe;AAAA,EACf,mBAA2C;AAAA,EAC3C,YAAmC;AAAA;AAAA,EAG3C,IAAI,WAAoB;AACtB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,cAAuB;AACzB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,gBAA6B;AAC/B,QAAI,CAAC,KAAK,kBAAkB;AAC1B,WAAK,mBAAmB,IAAI,gBAAA;AAAA,IAC9B;AACA,WAAO,KAAK,iBAAiB;AAAA,EAC/B;AAAA;AAAA,EAGA,OAA6B;AAC3B,QAAI,KAAK,gBAAgB,KAAK,UAAW;AACzC,SAAK,eAAe;AACpB,WAAO,KAAK,SAAA;AAAA,EACd;AAAA;AAAA,EAGA,UAAgB;AACd,QAAI,KAAK,WAAW;AAClB;AAAA,IACF;AAEA,SAAK,YAAY;AACjB,SAAK,kBAAkB,MAAA;AACvB,QAAI,KAAK,WAAW;AAClB,iBAAW,MAAM,KAAK,UAAW,IAAA;AACjC,WAAK,YAAY;AAAA,IACnB;AACA,SAAK,YAAA;AAAA,EACP;AAAA;AAAA,EAGU,WAAW,IAAsB;AACzC,QAAI,CAAC,KAAK,WAAW;AACnB,WAAK,YAAY,CAAA;AAAA,IACnB;AACA,SAAK,UAAU,KAAK,EAAE;AAAA,EACxB;AAAA;AAAA,EAGU,YAAe,QAAyB,UAAmC;AACnF,UAAM,cAAc,OAAO,UAAU,QAAQ;AAC7C,SAAK,WAAW,WAAW;AAC3B,WAAO;AAAA,EACT;AAMF;;"}
1
+ {"version":3,"file":"Controller.cjs","sources":["../src/Controller.ts"],"sourcesContent":["import type { Disposable, Subscribable, Listener, EventSource } from './types';\n\n/**\n * Base class for stateless orchestrators.\n * Controllers coordinate between ViewModels, Models, and Services.\n */\nexport abstract class Controller implements Disposable {\n private _disposed = false;\n private _initialized = false;\n private _abortController: AbortController | null = null;\n private _cleanups: (() => void)[] | null = null;\n\n /** Whether this instance has been disposed. */\n get disposed(): boolean {\n return this._disposed;\n }\n\n /** Whether init() has been called. */\n get initialized(): boolean {\n return this._initialized;\n }\n\n /** AbortSignal that fires when this instance is disposed. Lazily created. */\n get disposeSignal(): AbortSignal {\n if (!this._abortController) {\n this._abortController = new AbortController();\n }\n return this._abortController.signal;\n }\n\n /** Initializes the instance. Called automatically by React hooks after mount. */\n init(): void | Promise<void> {\n if (this._initialized || this._disposed) return;\n this._initialized = true;\n return this.onInit?.();\n }\n\n /** Tears down the instance, releasing all subscriptions and resources. */\n dispose(): void {\n if (this._disposed) {\n return;\n }\n\n this._disposed = true;\n this._abortController?.abort();\n if (this._cleanups) {\n for (const fn of this._cleanups) fn();\n this._cleanups = null;\n }\n this.onDispose?.();\n }\n\n /** Registers a cleanup function to be called on dispose. @protected */\n protected addCleanup(fn: () => void): void {\n if (!this._cleanups) {\n this._cleanups = [];\n }\n this._cleanups.push(fn);\n }\n\n /** Subscribes to an external Subscribable with automatic cleanup on dispose. @protected */\n protected subscribeTo<T>(source: Subscribable<T>, listener: Listener<T>): () => void {\n const unsubscribe = source.subscribe(listener);\n this.addCleanup(unsubscribe);\n return unsubscribe;\n }\n\n /** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */\n protected listenTo<E extends Record<string, any>, K extends keyof E>(\n source: EventSource<E>,\n event: K,\n handler: (payload: E[K]) => void,\n ): () => void {\n const unsubscribe = source.on(event, handler);\n this.addCleanup(unsubscribe);\n return unsubscribe;\n }\n\n /** Lifecycle hook called at the end of init(). Override to load initial data. @protected */\n protected onInit?(): void | Promise<void>;\n /** Lifecycle hook called during dispose(). Override for custom teardown. @protected */\n protected onDispose?(): void;\n}\n"],"names":[],"mappings":";;AAMO,MAAe,WAAiC;AAAA,EAC7C,YAAY;AAAA,EACZ,eAAe;AAAA,EACf,mBAA2C;AAAA,EAC3C,YAAmC;AAAA;AAAA,EAG3C,IAAI,WAAoB;AACtB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,cAAuB;AACzB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,gBAA6B;AAC/B,QAAI,CAAC,KAAK,kBAAkB;AAC1B,WAAK,mBAAmB,IAAI,gBAAA;AAAA,IAC9B;AACA,WAAO,KAAK,iBAAiB;AAAA,EAC/B;AAAA;AAAA,EAGA,OAA6B;AAC3B,QAAI,KAAK,gBAAgB,KAAK,UAAW;AACzC,SAAK,eAAe;AACpB,WAAO,KAAK,SAAA;AAAA,EACd;AAAA;AAAA,EAGA,UAAgB;AACd,QAAI,KAAK,WAAW;AAClB;AAAA,IACF;AAEA,SAAK,YAAY;AACjB,SAAK,kBAAkB,MAAA;AACvB,QAAI,KAAK,WAAW;AAClB,iBAAW,MAAM,KAAK,UAAW,IAAA;AACjC,WAAK,YAAY;AAAA,IACnB;AACA,SAAK,YAAA;AAAA,EACP;AAAA;AAAA,EAGU,WAAW,IAAsB;AACzC,QAAI,CAAC,KAAK,WAAW;AACnB,WAAK,YAAY,CAAA;AAAA,IACnB;AACA,SAAK,UAAU,KAAK,EAAE;AAAA,EACxB;AAAA;AAAA,EAGU,YAAe,QAAyB,UAAmC;AACnF,UAAM,cAAc,OAAO,UAAU,QAAQ;AAC7C,SAAK,WAAW,WAAW;AAC3B,WAAO;AAAA,EACT;AAAA;AAAA,EAGU,SACR,QACA,OACA,SACY;AACZ,UAAM,cAAc,OAAO,GAAG,OAAO,OAAO;AAC5C,SAAK,WAAW,WAAW;AAC3B,WAAO;AAAA,EACT;AAMF;;"}
@@ -1,4 +1,4 @@
1
- import type { Disposable, Subscribable, Listener } from './types';
1
+ import type { Disposable, Subscribable, Listener, EventSource } from './types';
2
2
  /**
3
3
  * Base class for stateless orchestrators.
4
4
  * Controllers coordinate between ViewModels, Models, and Services.
@@ -22,6 +22,8 @@ export declare abstract class Controller implements Disposable {
22
22
  protected addCleanup(fn: () => void): void;
23
23
  /** Subscribes to an external Subscribable with automatic cleanup on dispose. @protected */
24
24
  protected subscribeTo<T>(source: Subscribable<T>, listener: Listener<T>): () => void;
25
+ /** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */
26
+ protected listenTo<E extends Record<string, any>, K extends keyof E>(source: EventSource<E>, event: K, handler: (payload: E[K]) => void): () => void;
25
27
  /** Lifecycle hook called at the end of init(). Override to load initial data. @protected */
26
28
  protected onInit?(): void | Promise<void>;
27
29
  /** Lifecycle hook called during dispose(). Override for custom teardown. @protected */
@@ -1 +1 @@
1
- {"version":3,"file":"Controller.d.ts","sourceRoot":"","sources":["../src/Controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAElE;;;GAGG;AACH,8BAAsB,UAAW,YAAW,UAAU;IACpD,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,gBAAgB,CAAgC;IACxD,OAAO,CAAC,SAAS,CAA+B;IAEhD,+CAA+C;IAC/C,IAAI,QAAQ,IAAI,OAAO,CAEtB;IAED,sCAAsC;IACtC,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED,6EAA6E;IAC7E,IAAI,aAAa,IAAI,WAAW,CAK/B;IAED,iFAAiF;IACjF,IAAI,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAM5B,0EAA0E;IAC1E,OAAO,IAAI,IAAI;IAcf,uEAAuE;IACvE,SAAS,CAAC,UAAU,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,IAAI;IAO1C,2FAA2F;IAC3F,SAAS,CAAC,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI;IAMpF,4FAA4F;IAC5F,SAAS,CAAC,MAAM,CAAC,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IACzC,uFAAuF;IACvF,SAAS,CAAC,SAAS,CAAC,IAAI,IAAI;CAC7B"}
1
+ {"version":3,"file":"Controller.d.ts","sourceRoot":"","sources":["../src/Controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE/E;;;GAGG;AACH,8BAAsB,UAAW,YAAW,UAAU;IACpD,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,gBAAgB,CAAgC;IACxD,OAAO,CAAC,SAAS,CAA+B;IAEhD,+CAA+C;IAC/C,IAAI,QAAQ,IAAI,OAAO,CAEtB;IAED,sCAAsC;IACtC,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED,6EAA6E;IAC7E,IAAI,aAAa,IAAI,WAAW,CAK/B;IAED,iFAAiF;IACjF,IAAI,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAM5B,0EAA0E;IAC1E,OAAO,IAAI,IAAI;IAcf,uEAAuE;IACvE,SAAS,CAAC,UAAU,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,IAAI;IAO1C,2FAA2F;IAC3F,SAAS,CAAC,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI;IAMpF,yGAAyG;IACzG,SAAS,CAAC,QAAQ,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EACjE,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,EACtB,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,GAC/B,MAAM,IAAI;IAMb,4FAA4F;IAC5F,SAAS,CAAC,MAAM,CAAC,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IACzC,uFAAuF;IACvF,SAAS,CAAC,SAAS,CAAC,IAAI,IAAI;CAC7B"}
@@ -50,6 +50,12 @@ class Controller {
50
50
  this.addCleanup(unsubscribe);
51
51
  return unsubscribe;
52
52
  }
53
+ /** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */
54
+ listenTo(source, event, handler) {
55
+ const unsubscribe = source.on(event, handler);
56
+ this.addCleanup(unsubscribe);
57
+ return unsubscribe;
58
+ }
53
59
  }
54
60
  export {
55
61
  Controller
@@ -1 +1 @@
1
- {"version":3,"file":"Controller.js","sources":["../src/Controller.ts"],"sourcesContent":["import type { Disposable, Subscribable, Listener } from './types';\n\n/**\n * Base class for stateless orchestrators.\n * Controllers coordinate between ViewModels, Models, and Services.\n */\nexport abstract class Controller implements Disposable {\n private _disposed = false;\n private _initialized = false;\n private _abortController: AbortController | null = null;\n private _cleanups: (() => void)[] | null = null;\n\n /** Whether this instance has been disposed. */\n get disposed(): boolean {\n return this._disposed;\n }\n\n /** Whether init() has been called. */\n get initialized(): boolean {\n return this._initialized;\n }\n\n /** AbortSignal that fires when this instance is disposed. Lazily created. */\n get disposeSignal(): AbortSignal {\n if (!this._abortController) {\n this._abortController = new AbortController();\n }\n return this._abortController.signal;\n }\n\n /** Initializes the instance. Called automatically by React hooks after mount. */\n init(): void | Promise<void> {\n if (this._initialized || this._disposed) return;\n this._initialized = true;\n return this.onInit?.();\n }\n\n /** Tears down the instance, releasing all subscriptions and resources. */\n dispose(): void {\n if (this._disposed) {\n return;\n }\n\n this._disposed = true;\n this._abortController?.abort();\n if (this._cleanups) {\n for (const fn of this._cleanups) fn();\n this._cleanups = null;\n }\n this.onDispose?.();\n }\n\n /** Registers a cleanup function to be called on dispose. @protected */\n protected addCleanup(fn: () => void): void {\n if (!this._cleanups) {\n this._cleanups = [];\n }\n this._cleanups.push(fn);\n }\n\n /** Subscribes to an external Subscribable with automatic cleanup on dispose. @protected */\n protected subscribeTo<T>(source: Subscribable<T>, listener: Listener<T>): () => void {\n const unsubscribe = source.subscribe(listener);\n this.addCleanup(unsubscribe);\n return unsubscribe;\n }\n\n /** Lifecycle hook called at the end of init(). Override to load initial data. @protected */\n protected onInit?(): void | Promise<void>;\n /** Lifecycle hook called during dispose(). Override for custom teardown. @protected */\n protected onDispose?(): void;\n}\n"],"names":[],"mappings":"AAMO,MAAe,WAAiC;AAAA,EAC7C,YAAY;AAAA,EACZ,eAAe;AAAA,EACf,mBAA2C;AAAA,EAC3C,YAAmC;AAAA;AAAA,EAG3C,IAAI,WAAoB;AACtB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,cAAuB;AACzB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,gBAA6B;AAC/B,QAAI,CAAC,KAAK,kBAAkB;AAC1B,WAAK,mBAAmB,IAAI,gBAAA;AAAA,IAC9B;AACA,WAAO,KAAK,iBAAiB;AAAA,EAC/B;AAAA;AAAA,EAGA,OAA6B;AAC3B,QAAI,KAAK,gBAAgB,KAAK,UAAW;AACzC,SAAK,eAAe;AACpB,WAAO,KAAK,SAAA;AAAA,EACd;AAAA;AAAA,EAGA,UAAgB;AACd,QAAI,KAAK,WAAW;AAClB;AAAA,IACF;AAEA,SAAK,YAAY;AACjB,SAAK,kBAAkB,MAAA;AACvB,QAAI,KAAK,WAAW;AAClB,iBAAW,MAAM,KAAK,UAAW,IAAA;AACjC,WAAK,YAAY;AAAA,IACnB;AACA,SAAK,YAAA;AAAA,EACP;AAAA;AAAA,EAGU,WAAW,IAAsB;AACzC,QAAI,CAAC,KAAK,WAAW;AACnB,WAAK,YAAY,CAAA;AAAA,IACnB;AACA,SAAK,UAAU,KAAK,EAAE;AAAA,EACxB;AAAA;AAAA,EAGU,YAAe,QAAyB,UAAmC;AACnF,UAAM,cAAc,OAAO,UAAU,QAAQ;AAC7C,SAAK,WAAW,WAAW;AAC3B,WAAO;AAAA,EACT;AAMF;"}
1
+ {"version":3,"file":"Controller.js","sources":["../src/Controller.ts"],"sourcesContent":["import type { Disposable, Subscribable, Listener, EventSource } from './types';\n\n/**\n * Base class for stateless orchestrators.\n * Controllers coordinate between ViewModels, Models, and Services.\n */\nexport abstract class Controller implements Disposable {\n private _disposed = false;\n private _initialized = false;\n private _abortController: AbortController | null = null;\n private _cleanups: (() => void)[] | null = null;\n\n /** Whether this instance has been disposed. */\n get disposed(): boolean {\n return this._disposed;\n }\n\n /** Whether init() has been called. */\n get initialized(): boolean {\n return this._initialized;\n }\n\n /** AbortSignal that fires when this instance is disposed. Lazily created. */\n get disposeSignal(): AbortSignal {\n if (!this._abortController) {\n this._abortController = new AbortController();\n }\n return this._abortController.signal;\n }\n\n /** Initializes the instance. Called automatically by React hooks after mount. */\n init(): void | Promise<void> {\n if (this._initialized || this._disposed) return;\n this._initialized = true;\n return this.onInit?.();\n }\n\n /** Tears down the instance, releasing all subscriptions and resources. */\n dispose(): void {\n if (this._disposed) {\n return;\n }\n\n this._disposed = true;\n this._abortController?.abort();\n if (this._cleanups) {\n for (const fn of this._cleanups) fn();\n this._cleanups = null;\n }\n this.onDispose?.();\n }\n\n /** Registers a cleanup function to be called on dispose. @protected */\n protected addCleanup(fn: () => void): void {\n if (!this._cleanups) {\n this._cleanups = [];\n }\n this._cleanups.push(fn);\n }\n\n /** Subscribes to an external Subscribable with automatic cleanup on dispose. @protected */\n protected subscribeTo<T>(source: Subscribable<T>, listener: Listener<T>): () => void {\n const unsubscribe = source.subscribe(listener);\n this.addCleanup(unsubscribe);\n return unsubscribe;\n }\n\n /** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */\n protected listenTo<E extends Record<string, any>, K extends keyof E>(\n source: EventSource<E>,\n event: K,\n handler: (payload: E[K]) => void,\n ): () => void {\n const unsubscribe = source.on(event, handler);\n this.addCleanup(unsubscribe);\n return unsubscribe;\n }\n\n /** Lifecycle hook called at the end of init(). Override to load initial data. @protected */\n protected onInit?(): void | Promise<void>;\n /** Lifecycle hook called during dispose(). Override for custom teardown. @protected */\n protected onDispose?(): void;\n}\n"],"names":[],"mappings":"AAMO,MAAe,WAAiC;AAAA,EAC7C,YAAY;AAAA,EACZ,eAAe;AAAA,EACf,mBAA2C;AAAA,EAC3C,YAAmC;AAAA;AAAA,EAG3C,IAAI,WAAoB;AACtB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,cAAuB;AACzB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,gBAA6B;AAC/B,QAAI,CAAC,KAAK,kBAAkB;AAC1B,WAAK,mBAAmB,IAAI,gBAAA;AAAA,IAC9B;AACA,WAAO,KAAK,iBAAiB;AAAA,EAC/B;AAAA;AAAA,EAGA,OAA6B;AAC3B,QAAI,KAAK,gBAAgB,KAAK,UAAW;AACzC,SAAK,eAAe;AACpB,WAAO,KAAK,SAAA;AAAA,EACd;AAAA;AAAA,EAGA,UAAgB;AACd,QAAI,KAAK,WAAW;AAClB;AAAA,IACF;AAEA,SAAK,YAAY;AACjB,SAAK,kBAAkB,MAAA;AACvB,QAAI,KAAK,WAAW;AAClB,iBAAW,MAAM,KAAK,UAAW,IAAA;AACjC,WAAK,YAAY;AAAA,IACnB;AACA,SAAK,YAAA;AAAA,EACP;AAAA;AAAA,EAGU,WAAW,IAAsB;AACzC,QAAI,CAAC,KAAK,WAAW;AACnB,WAAK,YAAY,CAAA;AAAA,IACnB;AACA,SAAK,UAAU,KAAK,EAAE;AAAA,EACxB;AAAA;AAAA,EAGU,YAAe,QAAyB,UAAmC;AACnF,UAAM,cAAc,OAAO,UAAU,QAAQ;AAC7C,SAAK,WAAW,WAAW;AAC3B,WAAO;AAAA,EACT;AAAA;AAAA,EAGU,SACR,QACA,OACA,SACY;AACZ,UAAM,cAAc,OAAO,GAAG,OAAO,OAAO;AAC5C,SAAK,WAAW,WAAW;AAC3B,WAAO;AAAA,EACT;AAMF;"}
package/dist/Model.cjs CHANGED
@@ -157,6 +157,12 @@ class Model {
157
157
  this.addCleanup(unsubscribe);
158
158
  return unsubscribe;
159
159
  }
160
+ /** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */
161
+ listenTo(source, event, handler) {
162
+ const unsubscribe = source.on(event, handler);
163
+ this.addCleanup(unsubscribe);
164
+ return unsubscribe;
165
+ }
160
166
  shallowEqual(a, b) {
161
167
  const keysA = Object.keys(a);
162
168
  const keysB = Object.keys(b);
@@ -1 +1 @@
1
- {"version":3,"file":"Model.cjs","sources":["../src/Model.ts"],"sourcesContent":["import type { Listener, Updater, Subscribable, ValidationErrors } from './types';\n\n/**\n * Reactive entity with validation and dirty tracking.\n */\nexport abstract class Model<S extends object> implements Subscribable<S> {\n private _state: Readonly<S>;\n private _committed: Readonly<S>;\n private _disposed = false;\n private _initialized = false;\n private _listeners = new Set<Listener<S>>();\n private _abortController: AbortController | null = null;\n private _cleanups: (() => void)[] | null = null;\n\n constructor(initialState: S) {\n const frozen = Object.freeze({ ...initialState });\n this._state = frozen;\n this._committed = frozen;\n }\n\n /** Current frozen state object. */\n get state(): S {\n return this._state;\n }\n\n /**\n * The baseline state for dirty tracking.\n */\n get committed(): S {\n return this._committed;\n }\n\n /**\n * True if current state differs from committed state.\n */\n get dirty(): boolean {\n return !this.shallowEqual(this._state, this._committed);\n }\n\n /**\n * Validation errors for the current state.\n */\n get errors(): ValidationErrors<S> {\n return this.validate(this._state);\n }\n\n /**\n * True if there are no validation errors.\n */\n get valid(): boolean {\n return Object.keys(this.errors).length === 0;\n }\n\n /** Whether this instance has been disposed. */\n get disposed(): boolean {\n return this._disposed;\n }\n\n /** Whether init() has been called. */\n get initialized(): boolean {\n return this._initialized;\n }\n\n /** AbortSignal that fires when this instance is disposed. Lazily created. */\n get disposeSignal(): AbortSignal {\n if (!this._abortController) {\n this._abortController = new AbortController();\n }\n return this._abortController.signal;\n }\n\n /** Initializes the instance. Called automatically by React hooks after mount. */\n init(): void | Promise<void> {\n if (this._initialized || this._disposed) return;\n this._initialized = true;\n return this.onInit?.();\n }\n\n /**\n * Merges partial state with validation. No-op if no values changed by reference.\n * @protected\n */\n protected set(partialOrUpdater: Partial<S> | Updater<S>): void {\n if (this._disposed) {\n throw new Error('Cannot set state on disposed Model');\n }\n\n const partial =\n typeof partialOrUpdater === 'function'\n ? partialOrUpdater(this._state)\n : partialOrUpdater;\n\n // Check if any values actually changed (shallow equality)\n const keys = Object.keys(partial) as (keyof S)[];\n const hasChanges = keys.some(\n (key) => partial[key] !== this._state[key]\n );\n\n if (!hasChanges) {\n return;\n }\n\n const prev = this._state;\n const next = Object.freeze({ ...prev, ...partial });\n this._state = next;\n\n this.onSet?.(prev, next);\n\n for (const listener of this._listeners) {\n listener(next, prev);\n }\n }\n\n /**\n * Mark current state as the new baseline (not dirty).\n */\n commit(): void {\n if (this._disposed) {\n throw new Error('Cannot commit on disposed Model');\n }\n this._committed = this._state;\n }\n\n /**\n * Revert state to committed baseline.\n */\n rollback(): void {\n if (this._disposed) {\n throw new Error('Cannot rollback on disposed Model');\n }\n\n if (this.shallowEqual(this._state, this._committed)) {\n return;\n }\n\n const prev = this._state;\n this._state = this._committed;\n\n this.onSet?.(prev, this._state);\n\n for (const listener of this._listeners) {\n listener(this._state, prev);\n }\n }\n\n /** Subscribes to state changes. Returns an unsubscribe function. */\n subscribe(listener: Listener<S>): () => void {\n if (this._disposed) {\n return () => {};\n }\n\n this._listeners.add(listener);\n\n return () => {\n this._listeners.delete(listener);\n };\n }\n\n /** Tears down the instance, releasing all subscriptions and resources. */\n dispose(): void {\n if (this._disposed) {\n return;\n }\n\n this._disposed = true;\n this._abortController?.abort();\n if (this._cleanups) {\n for (const fn of this._cleanups) fn();\n this._cleanups = null;\n }\n this.onDispose?.();\n this._listeners.clear();\n }\n\n /**\n * Override to provide validation logic.\n * Return an object mapping field keys to error messages.\n */\n protected validate(_state: S): ValidationErrors<S> {\n return {};\n }\n\n /** Registers a cleanup function to be called on dispose. @protected */\n protected addCleanup(fn: () => void): void {\n if (!this._cleanups) {\n this._cleanups = [];\n }\n this._cleanups.push(fn);\n }\n\n /** Subscribes to an external Subscribable with automatic cleanup on dispose. @protected */\n protected subscribeTo<T>(source: Subscribable<T>, listener: Listener<T>): () => void {\n const unsubscribe = source.subscribe(listener);\n this.addCleanup(unsubscribe);\n return unsubscribe;\n }\n\n /** Lifecycle hook called after every set() with the previous state. @protected */\n protected onSet?(prev: S, next: S): void;\n /** Lifecycle hook called at the end of init(). Override to load initial data. @protected */\n protected onInit?(): void | Promise<void>;\n /** Lifecycle hook called during dispose(). Override for custom teardown. @protected */\n protected onDispose?(): void;\n\n private shallowEqual(a: S, b: S): boolean {\n const keysA = Object.keys(a) as (keyof S)[];\n const keysB = Object.keys(b) as (keyof S)[];\n\n if (keysA.length !== keysB.length) {\n return false;\n }\n\n for (const key of keysA) {\n if (a[key] !== b[key]) {\n return false;\n }\n }\n\n return true;\n }\n}\n"],"names":[],"mappings":";;AAKO,MAAe,MAAmD;AAAA,EAC/D;AAAA,EACA;AAAA,EACA,YAAY;AAAA,EACZ,eAAe;AAAA,EACf,iCAAiB,IAAA;AAAA,EACjB,mBAA2C;AAAA,EAC3C,YAAmC;AAAA,EAE3C,YAAY,cAAiB;AAC3B,UAAM,SAAS,OAAO,OAAO,EAAE,GAAG,cAAc;AAChD,SAAK,SAAS;AACd,SAAK,aAAa;AAAA,EACpB;AAAA;AAAA,EAGA,IAAI,QAAW;AACb,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,YAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,QAAiB;AACnB,WAAO,CAAC,KAAK,aAAa,KAAK,QAAQ,KAAK,UAAU;AAAA,EACxD;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,SAA8B;AAChC,WAAO,KAAK,SAAS,KAAK,MAAM;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,QAAiB;AACnB,WAAO,OAAO,KAAK,KAAK,MAAM,EAAE,WAAW;AAAA,EAC7C;AAAA;AAAA,EAGA,IAAI,WAAoB;AACtB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,cAAuB;AACzB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,gBAA6B;AAC/B,QAAI,CAAC,KAAK,kBAAkB;AAC1B,WAAK,mBAAmB,IAAI,gBAAA;AAAA,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AAAA,EAGA,UAAU,UAAmC;AAC3C,QAAI,KAAK,WAAW;AAClB,aAAO,MAAM;AAAA,MAAC;AAAA,IAChB;AAEA,SAAK,WAAW,IAAI,QAAQ;AAE5B,WAAO,MAAM;AACX,WAAK,WAAW,OAAO,QAAQ;AAAA,IACjC;AAAA,EACF;AAAA;AAAA,EAGA,UAAgB;AACd,QAAI,KAAK,WAAW;AAClB;AAAA,IACF;AAEA,SAAK,YAAY;AACjB,SAAK,kBAAkB,MAAA;AACvB,QAAI,KAAK,WAAW;AAClB,iBAAW,MAAM,KAAK,UAAW,IAAA;AACjC,WAAK,YAAY;AAAA,IACnB;AACA,SAAK,YAAA;AACL,SAAK,WAAW,MAAA;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMU,SAAS,QAAgC;AACjD,WAAO,CAAA;AAAA,EACT;AAAA;AAAA,EAGU,WAAW,IAAsB;AACzC,QAAI,CAAC,KAAK,WAAW;AACnB,WAAK,YAAY,CAAA;AAAA,IACnB;AACA,SAAK,UAAU,KAAK,EAAE;AAAA,EACxB;AAAA;AAAA,EAGU,YAAe,QAAyB,UAAmC;AACnF,UAAM,cAAc,OAAO,UAAU,QAAQ;AAC7C,SAAK,WAAW,WAAW;AAC3B,WAAO;AAAA,EACT;AAAA,EASQ,aAAa,GAAM,GAAe;AACxC,UAAM,QAAQ,OAAO,KAAK,CAAC;AAC3B,UAAM,QAAQ,OAAO,KAAK,CAAC;AAE3B,QAAI,MAAM,WAAW,MAAM,QAAQ;AACjC,aAAO;AAAA,IACT;AAEA,eAAW,OAAO,OAAO;AACvB,UAAI,EAAE,GAAG,MAAM,EAAE,GAAG,GAAG;AACrB,eAAO;AAAA,MACT;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AACF;;"}
1
+ {"version":3,"file":"Model.cjs","sources":["../src/Model.ts"],"sourcesContent":["import type { Listener, Updater, Subscribable, ValidationErrors, EventSource } from './types';\n\n/**\n * Reactive entity with validation and dirty tracking.\n */\nexport abstract class Model<S extends object> implements Subscribable<S> {\n private _state: Readonly<S>;\n private _committed: Readonly<S>;\n private _disposed = false;\n private _initialized = false;\n private _listeners = new Set<Listener<S>>();\n private _abortController: AbortController | null = null;\n private _cleanups: (() => void)[] | null = null;\n\n constructor(initialState: S) {\n const frozen = Object.freeze({ ...initialState });\n this._state = frozen;\n this._committed = frozen;\n }\n\n /** Current frozen state object. */\n get state(): S {\n return this._state;\n }\n\n /**\n * The baseline state for dirty tracking.\n */\n get committed(): S {\n return this._committed;\n }\n\n /**\n * True if current state differs from committed state.\n */\n get dirty(): boolean {\n return !this.shallowEqual(this._state, this._committed);\n }\n\n /**\n * Validation errors for the current state.\n */\n get errors(): ValidationErrors<S> {\n return this.validate(this._state);\n }\n\n /**\n * True if there are no validation errors.\n */\n get valid(): boolean {\n return Object.keys(this.errors).length === 0;\n }\n\n /** Whether this instance has been disposed. */\n get disposed(): boolean {\n return this._disposed;\n }\n\n /** Whether init() has been called. */\n get initialized(): boolean {\n return this._initialized;\n }\n\n /** AbortSignal that fires when this instance is disposed. Lazily created. */\n get disposeSignal(): AbortSignal {\n if (!this._abortController) {\n this._abortController = new AbortController();\n }\n return this._abortController.signal;\n }\n\n /** Initializes the instance. Called automatically by React hooks after mount. */\n init(): void | Promise<void> {\n if (this._initialized || this._disposed) return;\n this._initialized = true;\n return this.onInit?.();\n }\n\n /**\n * Merges partial state with validation. No-op if no values changed by reference.\n * @protected\n */\n protected set(partialOrUpdater: Partial<S> | Updater<S>): void {\n if (this._disposed) {\n throw new Error('Cannot set state on disposed Model');\n }\n\n const partial =\n typeof partialOrUpdater === 'function'\n ? partialOrUpdater(this._state)\n : partialOrUpdater;\n\n // Check if any values actually changed (shallow equality)\n const keys = Object.keys(partial) as (keyof S)[];\n const hasChanges = keys.some(\n (key) => partial[key] !== this._state[key]\n );\n\n if (!hasChanges) {\n return;\n }\n\n const prev = this._state;\n const next = Object.freeze({ ...prev, ...partial });\n this._state = next;\n\n this.onSet?.(prev, next);\n\n for (const listener of this._listeners) {\n listener(next, prev);\n }\n }\n\n /**\n * Mark current state as the new baseline (not dirty).\n */\n commit(): void {\n if (this._disposed) {\n throw new Error('Cannot commit on disposed Model');\n }\n this._committed = this._state;\n }\n\n /**\n * Revert state to committed baseline.\n */\n rollback(): void {\n if (this._disposed) {\n throw new Error('Cannot rollback on disposed Model');\n }\n\n if (this.shallowEqual(this._state, this._committed)) {\n return;\n }\n\n const prev = this._state;\n this._state = this._committed;\n\n this.onSet?.(prev, this._state);\n\n for (const listener of this._listeners) {\n listener(this._state, prev);\n }\n }\n\n /** Subscribes to state changes. Returns an unsubscribe function. */\n subscribe(listener: Listener<S>): () => void {\n if (this._disposed) {\n return () => {};\n }\n\n this._listeners.add(listener);\n\n return () => {\n this._listeners.delete(listener);\n };\n }\n\n /** Tears down the instance, releasing all subscriptions and resources. */\n dispose(): void {\n if (this._disposed) {\n return;\n }\n\n this._disposed = true;\n this._abortController?.abort();\n if (this._cleanups) {\n for (const fn of this._cleanups) fn();\n this._cleanups = null;\n }\n this.onDispose?.();\n this._listeners.clear();\n }\n\n /**\n * Override to provide validation logic.\n * Return an object mapping field keys to error messages.\n */\n protected validate(_state: S): ValidationErrors<S> {\n return {};\n }\n\n /** Registers a cleanup function to be called on dispose. @protected */\n protected addCleanup(fn: () => void): void {\n if (!this._cleanups) {\n this._cleanups = [];\n }\n this._cleanups.push(fn);\n }\n\n /** Subscribes to an external Subscribable with automatic cleanup on dispose. @protected */\n protected subscribeTo<T>(source: Subscribable<T>, listener: Listener<T>): () => void {\n const unsubscribe = source.subscribe(listener);\n this.addCleanup(unsubscribe);\n return unsubscribe;\n }\n\n /** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */\n protected listenTo<E extends Record<string, any>, K extends keyof E>(\n source: EventSource<E>,\n event: K,\n handler: (payload: E[K]) => void,\n ): () => void {\n const unsubscribe = source.on(event, handler);\n this.addCleanup(unsubscribe);\n return unsubscribe;\n }\n\n /** Lifecycle hook called after every set() with the previous state. @protected */\n protected onSet?(prev: S, next: S): void;\n /** Lifecycle hook called at the end of init(). Override to load initial data. @protected */\n protected onInit?(): void | Promise<void>;\n /** Lifecycle hook called during dispose(). Override for custom teardown. @protected */\n protected onDispose?(): void;\n\n private shallowEqual(a: S, b: S): boolean {\n const keysA = Object.keys(a) as (keyof S)[];\n const keysB = Object.keys(b) as (keyof S)[];\n\n if (keysA.length !== keysB.length) {\n return false;\n }\n\n for (const key of keysA) {\n if (a[key] !== b[key]) {\n return false;\n }\n }\n\n return true;\n }\n}\n"],"names":[],"mappings":";;AAKO,MAAe,MAAmD;AAAA,EAC/D;AAAA,EACA;AAAA,EACA,YAAY;AAAA,EACZ,eAAe;AAAA,EACf,iCAAiB,IAAA;AAAA,EACjB,mBAA2C;AAAA,EAC3C,YAAmC;AAAA,EAE3C,YAAY,cAAiB;AAC3B,UAAM,SAAS,OAAO,OAAO,EAAE,GAAG,cAAc;AAChD,SAAK,SAAS;AACd,SAAK,aAAa;AAAA,EACpB;AAAA;AAAA,EAGA,IAAI,QAAW;AACb,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,YAAe;AACjB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,QAAiB;AACnB,WAAO,CAAC,KAAK,aAAa,KAAK,QAAQ,KAAK,UAAU;AAAA,EACxD;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,SAA8B;AAChC,WAAO,KAAK,SAAS,KAAK,MAAM;AAAA,EAClC;AAAA;AAAA;AAAA;AAAA,EAKA,IAAI,QAAiB;AACnB,WAAO,OAAO,KAAK,KAAK,MAAM,EAAE,WAAW;AAAA,EAC7C;AAAA;AAAA,EAGA,IAAI,WAAoB;AACtB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,cAAuB;AACzB,WAAO,KAAK;AAAA,EACd;AAAA;AAAA,EAGA,IAAI,gBAA6B;AAC/B,QAAI,CAAC,K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ACT;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AACF;;"}
package/dist/Model.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Listener, Updater, Subscribable, ValidationErrors } from './types';
1
+ import type { Listener, Updater, Subscribable, ValidationErrors, EventSource } from './types';
2
2
  /**
3
3
  * Reactive entity with validation and dirty tracking.
4
4
  */
@@ -63,6 +63,8 @@ export declare abstract class Model<S extends object> implements Subscribable<S>
63
63
  protected addCleanup(fn: () => void): void;
64
64
  /** Subscribes to an external Subscribable with automatic cleanup on dispose. @protected */
65
65
  protected subscribeTo<T>(source: Subscribable<T>, listener: Listener<T>): () => void;
66
+ /** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */
67
+ protected listenTo<E extends Record<string, any>, K extends keyof E>(source: EventSource<E>, event: K, handler: (payload: E[K]) => void): () => void;
66
68
  /** Lifecycle hook called after every set() with the previous state. @protected */
67
69
  protected onSet?(prev: S, next: S): void;
68
70
  /** Lifecycle hook called at the end of init(). Override to load initial data. @protected */
@@ -1 +1 @@
1
- {"version":3,"file":"Model.d.ts","sourceRoot":"","sources":["../src/Model.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEjF;;GAEG;AACH,8BAAsB,KAAK,CAAC,CAAC,SAAS,MAAM,CAAE,YAAW,YAAY,CAAC,CAAC,CAAC;IACtE,OAAO,CAAC,MAAM,CAAc;IAC5B,OAAO,CAAC,UAAU,CAAc;IAChC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,UAAU,CAA0B;IAC5C,OAAO,CAAC,gBAAgB,CAAgC;IACxD,OAAO,CAAC,SAAS,CAA+B;gBAEpC,YAAY,EAAE,CAAC;IAM3B,mCAAmC;IACnC,IAAI,KAAK,IAAI,CAAC,CAEb;IAED;;OAEG;IACH,IAAI,SAA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/** Subscribes to a typed event on a Channel or EventBus with automatic cleanup on dispose. @protected */
159
+ listenTo(source, event, handler) {
160
+ const unsubscribe = source.on(event, handler);
161
+ this.addCleanup(unsubscribe);
162
+ return unsubscribe;
163
+ }
158
164
  shallowEqual(a, b) {
159
165
  const keysA = Object.keys(a);
160
166
  const keysB = Object.keys(b);