@asaidimu/utils-store 10.1.0 → 10.1.2

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/index.d.mts CHANGED
@@ -1,51 +1,5 @@
1
- interface SimplePersistence<T> {
2
- /**
3
- * Persists data to storage.
4
- *
5
- * @param id The **unique identifier of the *consumer instance*** making the change. This is NOT the ID of the data (`T`) itself.
6
- * Think of it as the ID of the specific browser tab, component, or module that's currently interacting with the persistence layer.
7
- * It should typically be a **UUID** generated once at the consumer instance's instantiation.
8
- * This `id` is crucial for the `subscribe` method, helping to differentiate updates originating from the current instance versus other instances/tabs, thereby preventing self-triggered notification loops.
9
- * @param state The state (of type T) to persist. This state is generally considered the **global or shared state** that all instances interact with.
10
- * @returns `true` if the operation was successful, `false` if an error occurred. For asynchronous implementations (like `IndexedDBPersistence`), this returns a `Promise<boolean>`.
11
- */
12
- set(id: string, state: T): boolean | Promise<boolean>;
13
- /**
14
- * Retrieves the global persisted data from storage.
15
- *
16
- * @returns The retrieved state of type `T`, or `null` if no data is found or if an error occurs during retrieval/parsing.
17
- * For asynchronous implementations, this returns a `Promise<T | null>`.
18
- */
19
- get(): (T | null) | Promise<T | null>;
20
- /**
21
- * Subscribes to changes in the global persisted data that originate from *other* instances of your application (e.g., other tabs or independent components using the same persistence layer).
22
- *
23
- * @param id The **unique identifier of the *consumer instance* subscribing**. This allows the persistence implementation to filter out notifications that were initiated by the subscribing instance itself.
24
- * @param callback The function to call when the global persisted data changes from *another* source. The new state (`T`) is passed as an argument to this callback.
25
- * @returns A function that, when called, will unsubscribe the provided callback from future updates. Call this when your component or instance is no longer active to prevent memory leaks.
26
- */
27
- subscribe(id: string, callback: (state: T) => void): () => void;
28
- /**
29
- * Clears (removes) the entire global persisted data from storage.
30
- *
31
- * @returns `true` if the operation was successful, `false` if an error occurred. For asynchronous implementations, this returns a `Promise<boolean>`.
32
- */
33
- clear(): boolean | Promise<boolean>;
34
- /**
35
- * Returns metadata about the persistence layer.
36
- *
37
- * This is useful for distinguishing between multiple apps running on the same host
38
- * (e.g., several apps served at `localhost:3000` that share the same storage key).
39
- *
40
- * @returns An object containing:
41
- * - `version`: The semantic version string of the persistence schema or application.
42
- * - `id`: A unique identifier for the application using this persistence instance.
43
- */
44
- stats(): {
45
- version: string;
46
- id: string;
47
- };
48
- }
1
+ import { SimplePersistence as SimplePersistence$1 } from '@core/persistence';
2
+ import { EventBus } from '@core/events';
49
3
 
50
4
  /**
51
5
  * Utility type for representing partial updates to the state, allowing deep nesting.
@@ -312,6 +266,17 @@ interface ReactiveSelector<S> {
312
266
  */
313
267
  subscribe: (callback: (state: S) => void) => () => void;
314
268
  }
269
+ interface ActionWatcher {
270
+ /** Unique identifier for the action */
271
+ name: string;
272
+ /** Function to get the current computed value of the action. */
273
+ status: () => boolean;
274
+ /**
275
+ * Subscribes a callback function to run whenever the action's status changes.
276
+ * Returns an unsubscribe function.
277
+ */
278
+ subscribe: (callback: () => void) => () => void;
279
+ }
315
280
  /**
316
281
  * Interface defining the contract for the core data state store.
317
282
  * T must be an object type.
@@ -366,6 +331,28 @@ interface DataStore<T extends object> {
366
331
  * @returns An unsubscribe function.
367
332
  */
368
333
  watch(path: string | Array<string>, callback: (state: T) => void): () => void;
334
+ /**
335
+ * Subscribes to execution‑status changes of a registered action.
336
+ *
337
+ * The provided callback is called **every time** the action transitions
338
+ * between idle and running (i.e. on `action:start`, `action:complete`, or
339
+ * `action:error` for the given name). The current status can also be read
340
+ * synchronously with `isActionRunning(name)`.
341
+ *
342
+ * **Deferred listener teardown**
343
+ * To avoid unnecessary churn when subscriptions are rapidly created and
344
+ * destroyed, the underlying event listeners are not removed immediately
345
+ * on unsubscribe. Instead, a *pending reset* is queued via `queueMicrotask`.
346
+ * If a new subscription for the same action arrives before that microtask
347
+ * executes, the reset is silently cancelled and the already‑established
348
+ * listeners are reused. This keeps the subscription infrastructure stable
349
+ * and prevents cascading notifications that could otherwise arise from
350
+ * repeated subscribe‑unsubscribe‑subscribe cycles.
351
+ *
352
+ * @param name - The name of the action to watch.
353
+ * @returns An ActionWatcher
354
+ */
355
+ watchAction(name: string): ActionWatcher;
369
356
  /**
370
357
  * Executes an operation function within a transaction block.
371
358
  * All state updates (`set` or actions) within the transaction are batched and applied atomically (all or nothing).
@@ -501,11 +488,6 @@ type StoreAction<T, R extends any[] = any[]> = {
501
488
  };
502
489
  };
503
490
 
504
- /**
505
- * Reactive Data Store Implementation - Updated for Concurrency and Performance
506
- * The public API is preserved for test compatibility.
507
- */
508
-
509
491
  /**
510
492
  * Main ReactiveDataStore - a robust, type-safe state management solution.
511
493
  * It features optimistic updates, a Serializer-based update queue for safe serial
@@ -559,7 +541,7 @@ declare class ReactiveDataStore<T extends object> implements DataStore<T> {
559
541
  /**
560
542
  * Creates a new ReactiveDataStore instance.
561
543
  */
562
- constructor(initialData: T, persistence?: SimplePersistence<T>, deleteMarker?: symbol, options?: {
544
+ constructor(initialData: T, persistence?: SimplePersistence$1<T>, deleteMarker?: symbol, options?: {
563
545
  persistenceMaxRetries?: number;
564
546
  persistenceRetryDelay?: number;
565
547
  });
@@ -624,6 +606,28 @@ declare class ReactiveDataStore<T extends object> implements DataStore<T> {
624
606
  private setupReadyLatch;
625
607
  private setupPersistenceListener;
626
608
  watch(path: string | Array<string>, callback: (state: T) => void): () => void;
609
+ /**
610
+ * Subscribes to execution‑status changes of a registered action.
611
+ *
612
+ * The provided callback is called **every time** the action transitions
613
+ * between idle and running (i.e. on `action:start`, `action:complete`, or
614
+ * `action:error` for the given name). The current status can also be read
615
+ * synchronously with `isActionRunning(name)`.
616
+ *
617
+ * **Deferred listener teardown**
618
+ * To avoid unnecessary churn when subscriptions are rapidly created and
619
+ * destroyed, the underlying event listeners are not removed immediately
620
+ * on unsubscribe. Instead, a *pending reset* is queued via `queueMicrotask`.
621
+ * If a new subscription for the same action arrives before that microtask
622
+ * executes, the reset is silently cancelled and the already‑established
623
+ * listeners are reused. This keeps the subscription infrastructure stable
624
+ * and prevents cascading notifications that could otherwise arise from
625
+ * repeated subscribe‑unsubscribe‑subscribe cycles.
626
+ *
627
+ * @param name - The name of the action to watch.
628
+ * @returns ActionWatcher
629
+ */
630
+ watchAction(name: string): ActionWatcher;
627
631
  /**
628
632
  * Creates a debounced version of `set()` using a `Debouncer`.
629
633
  *
@@ -681,6 +685,55 @@ declare class ReactiveDataStore<T extends object> implements DataStore<T> {
681
685
  private emit;
682
686
  }
683
687
 
688
+ interface SimplePersistence<T> {
689
+ /**
690
+ * Persists data to storage.
691
+ *
692
+ * @param id The **unique identifier of the *consumer instance*** making the change. This is NOT the ID of the data (`T`) itself.
693
+ * Think of it as the ID of the specific browser tab, component, or module that's currently interacting with the persistence layer.
694
+ * It should typically be a **UUID** generated once at the consumer instance's instantiation.
695
+ * This `id` is crucial for the `subscribe` method, helping to differentiate updates originating from the current instance versus other instances/tabs, thereby preventing self-triggered notification loops.
696
+ * @param state The state (of type T) to persist. This state is generally considered the **global or shared state** that all instances interact with.
697
+ * @returns `true` if the operation was successful, `false` if an error occurred. For asynchronous implementations (like `IndexedDBPersistence`), this returns a `Promise<boolean>`.
698
+ */
699
+ set(id: string, state: T): boolean | Promise<boolean>;
700
+ /**
701
+ * Retrieves the global persisted data from storage.
702
+ *
703
+ * @returns The retrieved state of type `T`, or `null` if no data is found or if an error occurs during retrieval/parsing.
704
+ * For asynchronous implementations, this returns a `Promise<T | null>`.
705
+ */
706
+ get(): (T | null) | Promise<T | null>;
707
+ /**
708
+ * Subscribes to changes in the global persisted data that originate from *other* instances of your application (e.g., other tabs or independent components using the same persistence layer).
709
+ *
710
+ * @param id The **unique identifier of the *consumer instance* subscribing**. This allows the persistence implementation to filter out notifications that were initiated by the subscribing instance itself.
711
+ * @param callback The function to call when the global persisted data changes from *another* source. The new state (`T`) is passed as an argument to this callback.
712
+ * @returns A function that, when called, will unsubscribe the provided callback from future updates. Call this when your component or instance is no longer active to prevent memory leaks.
713
+ */
714
+ subscribe(id: string, callback: (state: T) => void): () => void;
715
+ /**
716
+ * Clears (removes) the entire global persisted data from storage.
717
+ *
718
+ * @returns `true` if the operation was successful, `false` if an error occurred. For asynchronous implementations, this returns a `Promise<boolean>`.
719
+ */
720
+ clear(): boolean | Promise<boolean>;
721
+ /**
722
+ * Returns metadata about the persistence layer.
723
+ *
724
+ * This is useful for distinguishing between multiple apps running on the same host
725
+ * (e.g., several apps served at `localhost:3000` that share the same storage key).
726
+ *
727
+ * @returns An object containing:
728
+ * - `version`: The semantic version string of the persistence schema or application.
729
+ * - `id`: A unique identifier for the application using this persistence instance.
730
+ */
731
+ stats(): {
732
+ version: string;
733
+ id: string;
734
+ };
735
+ }
736
+
684
737
  /**
685
738
  * @fileoverview Store Observer Module
686
739
  * @description A comprehensive module to add advanced debugging, observability, and time-travel
@@ -976,59 +1029,6 @@ declare function createMerge(options?: {
976
1029
  declare const merge: <T extends object>(original: T, changes: DeepPartial<T> | symbol) => T;
977
1030
  type MergeFunction = ReturnType<typeof createMerge>;
978
1031
 
979
- /**
980
- * Interface defining the shape of the EventBus.
981
- * @template TEventMap - A record mapping event names to their respective payload types.
982
- */
983
- interface EventBus<TEventMap extends Record<string, any>> {
984
- /**
985
- * Subscribes to a specific event by name.
986
- * @param eventName - The name of the event to subscribe to.
987
- * @param callback - The function to call when the event is emitted.
988
- * @returns A function to unsubscribe from the event.
989
- */
990
- subscribe: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void) => () => void;
991
- /**
992
- * Subscribes to an event and automatically unsubscribes after it fires once.
993
- * @param eventName - The name of the event to subscribe to.
994
- * @param callback - The function to call when the event is emitted.
995
- * @returns A function to cancel the one-shot subscription before it fires.
996
- */
997
- once: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void) => () => void;
998
- /**
999
- * Emits an event with a payload to all subscribed listeners.
1000
- * @param event - An object containing the event name and payload.
1001
- */
1002
- emit: <TEventName extends keyof TEventMap>(event: {
1003
- name: TEventName;
1004
- payload: TEventMap[TEventName];
1005
- }) => void;
1006
- /**
1007
- * Retrieves metrics about event bus usage.
1008
- * @returns An object containing various metrics.
1009
- */
1010
- metrics: () => EventMetrics;
1011
- /**
1012
- * Clears all subscriptions and resets metrics.
1013
- * After calling clear(), the bus is fully reset and can be reused —
1014
- * cross-tab communication is re-established if it was previously enabled.
1015
- */
1016
- clear: () => void;
1017
- }
1018
- /**
1019
- * Interface defining the metrics tracked by the EventBus.
1020
- */
1021
- interface EventMetrics {
1022
- /** Total number of events emitted (both sync and deferred paths). */
1023
- totalEvents: number;
1024
- /** Number of active subscriptions across all event names. */
1025
- activeSubscriptions: number;
1026
- /** Map of event names to their emission counts. */
1027
- eventCounts: Map<string, number>;
1028
- /** Average duration of event dispatch in milliseconds. */
1029
- averageEmitDuration: number;
1030
- }
1031
-
1032
1032
  declare class ActionCancelledError extends Error {
1033
1033
  constructor();
1034
1034
  }
@@ -1055,7 +1055,11 @@ declare class ActionManager<T extends object> {
1055
1055
  }): () => void;
1056
1056
  dispatch<R extends any[]>(name: string, ...params: R): Promise<T>;
1057
1057
  private executeAction;
1058
+ running(name: string): boolean;
1059
+ subscribe(name: string, callback: () => void): () => void;
1060
+ watch(name: string): ActionWatcher;
1061
+ private notifyStatusListeners;
1058
1062
  private emit;
1059
1063
  }
1060
1064
 
1061
- export { ActionCancelledError, type ActionCompletePayload, type ActionErrorPayload, ActionManager, type ActionStartPayload, type BlockingMiddleware, DELETE_SYMBOL, type DataStore, type DeepPartial, type DiffFunction, type MergeFunction, type Middleware, type MiddlewareConfig, type MiddlewareExecution, type ObserverOptions, type PersistenceFailedPayload, type PersistenceInitErrorPayload, type PersistenceQueueClearedPayload, type PersistenceQueuedPayload, type PersistenceRetryPayload, type PersistenceSuccessPayload, ReactiveDataStore, type ReactiveSelector, type SelectorAccessedPayload, type SelectorChangedPayload, type StateDelta, type StateUpdater, type StoreAction, type StoreEvent, type StoreEvents, type StoreExecutionState, type StoreMetrics, StoreObserver, type TransformMiddleware, UnknownActionError, createDerivePaths, createDiff, createMerge, derivePaths, diff, merge, shallowClone };
1065
+ export { ActionCancelledError, type ActionCompletePayload, type ActionErrorPayload, ActionManager, type ActionStartPayload, type ActionWatcher, type BlockingMiddleware, DELETE_SYMBOL, type DataStore, type DeepPartial, type DiffFunction, type MergeFunction, type Middleware, type MiddlewareConfig, type MiddlewareExecution, type ObserverOptions, type PersistenceFailedPayload, type PersistenceInitErrorPayload, type PersistenceQueueClearedPayload, type PersistenceQueuedPayload, type PersistenceRetryPayload, type PersistenceSuccessPayload, ReactiveDataStore, type ReactiveSelector, type SelectorAccessedPayload, type SelectorChangedPayload, type StateDelta, type StateUpdater, type StoreAction, type StoreEvent, type StoreEvents, type StoreExecutionState, type StoreMetrics, StoreObserver, type TransformMiddleware, UnknownActionError, createDerivePaths, createDiff, createMerge, derivePaths, diff, merge, shallowClone };
package/index.d.ts CHANGED
@@ -1,51 +1,5 @@
1
- interface SimplePersistence<T> {
2
- /**
3
- * Persists data to storage.
4
- *
5
- * @param id The **unique identifier of the *consumer instance*** making the change. This is NOT the ID of the data (`T`) itself.
6
- * Think of it as the ID of the specific browser tab, component, or module that's currently interacting with the persistence layer.
7
- * It should typically be a **UUID** generated once at the consumer instance's instantiation.
8
- * This `id` is crucial for the `subscribe` method, helping to differentiate updates originating from the current instance versus other instances/tabs, thereby preventing self-triggered notification loops.
9
- * @param state The state (of type T) to persist. This state is generally considered the **global or shared state** that all instances interact with.
10
- * @returns `true` if the operation was successful, `false` if an error occurred. For asynchronous implementations (like `IndexedDBPersistence`), this returns a `Promise<boolean>`.
11
- */
12
- set(id: string, state: T): boolean | Promise<boolean>;
13
- /**
14
- * Retrieves the global persisted data from storage.
15
- *
16
- * @returns The retrieved state of type `T`, or `null` if no data is found or if an error occurs during retrieval/parsing.
17
- * For asynchronous implementations, this returns a `Promise<T | null>`.
18
- */
19
- get(): (T | null) | Promise<T | null>;
20
- /**
21
- * Subscribes to changes in the global persisted data that originate from *other* instances of your application (e.g., other tabs or independent components using the same persistence layer).
22
- *
23
- * @param id The **unique identifier of the *consumer instance* subscribing**. This allows the persistence implementation to filter out notifications that were initiated by the subscribing instance itself.
24
- * @param callback The function to call when the global persisted data changes from *another* source. The new state (`T`) is passed as an argument to this callback.
25
- * @returns A function that, when called, will unsubscribe the provided callback from future updates. Call this when your component or instance is no longer active to prevent memory leaks.
26
- */
27
- subscribe(id: string, callback: (state: T) => void): () => void;
28
- /**
29
- * Clears (removes) the entire global persisted data from storage.
30
- *
31
- * @returns `true` if the operation was successful, `false` if an error occurred. For asynchronous implementations, this returns a `Promise<boolean>`.
32
- */
33
- clear(): boolean | Promise<boolean>;
34
- /**
35
- * Returns metadata about the persistence layer.
36
- *
37
- * This is useful for distinguishing between multiple apps running on the same host
38
- * (e.g., several apps served at `localhost:3000` that share the same storage key).
39
- *
40
- * @returns An object containing:
41
- * - `version`: The semantic version string of the persistence schema or application.
42
- * - `id`: A unique identifier for the application using this persistence instance.
43
- */
44
- stats(): {
45
- version: string;
46
- id: string;
47
- };
48
- }
1
+ import { SimplePersistence as SimplePersistence$1 } from '@core/persistence';
2
+ import { EventBus } from '@core/events';
49
3
 
50
4
  /**
51
5
  * Utility type for representing partial updates to the state, allowing deep nesting.
@@ -312,6 +266,17 @@ interface ReactiveSelector<S> {
312
266
  */
313
267
  subscribe: (callback: (state: S) => void) => () => void;
314
268
  }
269
+ interface ActionWatcher {
270
+ /** Unique identifier for the action */
271
+ name: string;
272
+ /** Function to get the current computed value of the action. */
273
+ status: () => boolean;
274
+ /**
275
+ * Subscribes a callback function to run whenever the action's status changes.
276
+ * Returns an unsubscribe function.
277
+ */
278
+ subscribe: (callback: () => void) => () => void;
279
+ }
315
280
  /**
316
281
  * Interface defining the contract for the core data state store.
317
282
  * T must be an object type.
@@ -366,6 +331,28 @@ interface DataStore<T extends object> {
366
331
  * @returns An unsubscribe function.
367
332
  */
368
333
  watch(path: string | Array<string>, callback: (state: T) => void): () => void;
334
+ /**
335
+ * Subscribes to execution‑status changes of a registered action.
336
+ *
337
+ * The provided callback is called **every time** the action transitions
338
+ * between idle and running (i.e. on `action:start`, `action:complete`, or
339
+ * `action:error` for the given name). The current status can also be read
340
+ * synchronously with `isActionRunning(name)`.
341
+ *
342
+ * **Deferred listener teardown**
343
+ * To avoid unnecessary churn when subscriptions are rapidly created and
344
+ * destroyed, the underlying event listeners are not removed immediately
345
+ * on unsubscribe. Instead, a *pending reset* is queued via `queueMicrotask`.
346
+ * If a new subscription for the same action arrives before that microtask
347
+ * executes, the reset is silently cancelled and the already‑established
348
+ * listeners are reused. This keeps the subscription infrastructure stable
349
+ * and prevents cascading notifications that could otherwise arise from
350
+ * repeated subscribe‑unsubscribe‑subscribe cycles.
351
+ *
352
+ * @param name - The name of the action to watch.
353
+ * @returns An ActionWatcher
354
+ */
355
+ watchAction(name: string): ActionWatcher;
369
356
  /**
370
357
  * Executes an operation function within a transaction block.
371
358
  * All state updates (`set` or actions) within the transaction are batched and applied atomically (all or nothing).
@@ -501,11 +488,6 @@ type StoreAction<T, R extends any[] = any[]> = {
501
488
  };
502
489
  };
503
490
 
504
- /**
505
- * Reactive Data Store Implementation - Updated for Concurrency and Performance
506
- * The public API is preserved for test compatibility.
507
- */
508
-
509
491
  /**
510
492
  * Main ReactiveDataStore - a robust, type-safe state management solution.
511
493
  * It features optimistic updates, a Serializer-based update queue for safe serial
@@ -559,7 +541,7 @@ declare class ReactiveDataStore<T extends object> implements DataStore<T> {
559
541
  /**
560
542
  * Creates a new ReactiveDataStore instance.
561
543
  */
562
- constructor(initialData: T, persistence?: SimplePersistence<T>, deleteMarker?: symbol, options?: {
544
+ constructor(initialData: T, persistence?: SimplePersistence$1<T>, deleteMarker?: symbol, options?: {
563
545
  persistenceMaxRetries?: number;
564
546
  persistenceRetryDelay?: number;
565
547
  });
@@ -624,6 +606,28 @@ declare class ReactiveDataStore<T extends object> implements DataStore<T> {
624
606
  private setupReadyLatch;
625
607
  private setupPersistenceListener;
626
608
  watch(path: string | Array<string>, callback: (state: T) => void): () => void;
609
+ /**
610
+ * Subscribes to execution‑status changes of a registered action.
611
+ *
612
+ * The provided callback is called **every time** the action transitions
613
+ * between idle and running (i.e. on `action:start`, `action:complete`, or
614
+ * `action:error` for the given name). The current status can also be read
615
+ * synchronously with `isActionRunning(name)`.
616
+ *
617
+ * **Deferred listener teardown**
618
+ * To avoid unnecessary churn when subscriptions are rapidly created and
619
+ * destroyed, the underlying event listeners are not removed immediately
620
+ * on unsubscribe. Instead, a *pending reset* is queued via `queueMicrotask`.
621
+ * If a new subscription for the same action arrives before that microtask
622
+ * executes, the reset is silently cancelled and the already‑established
623
+ * listeners are reused. This keeps the subscription infrastructure stable
624
+ * and prevents cascading notifications that could otherwise arise from
625
+ * repeated subscribe‑unsubscribe‑subscribe cycles.
626
+ *
627
+ * @param name - The name of the action to watch.
628
+ * @returns ActionWatcher
629
+ */
630
+ watchAction(name: string): ActionWatcher;
627
631
  /**
628
632
  * Creates a debounced version of `set()` using a `Debouncer`.
629
633
  *
@@ -681,6 +685,55 @@ declare class ReactiveDataStore<T extends object> implements DataStore<T> {
681
685
  private emit;
682
686
  }
683
687
 
688
+ interface SimplePersistence<T> {
689
+ /**
690
+ * Persists data to storage.
691
+ *
692
+ * @param id The **unique identifier of the *consumer instance*** making the change. This is NOT the ID of the data (`T`) itself.
693
+ * Think of it as the ID of the specific browser tab, component, or module that's currently interacting with the persistence layer.
694
+ * It should typically be a **UUID** generated once at the consumer instance's instantiation.
695
+ * This `id` is crucial for the `subscribe` method, helping to differentiate updates originating from the current instance versus other instances/tabs, thereby preventing self-triggered notification loops.
696
+ * @param state The state (of type T) to persist. This state is generally considered the **global or shared state** that all instances interact with.
697
+ * @returns `true` if the operation was successful, `false` if an error occurred. For asynchronous implementations (like `IndexedDBPersistence`), this returns a `Promise<boolean>`.
698
+ */
699
+ set(id: string, state: T): boolean | Promise<boolean>;
700
+ /**
701
+ * Retrieves the global persisted data from storage.
702
+ *
703
+ * @returns The retrieved state of type `T`, or `null` if no data is found or if an error occurs during retrieval/parsing.
704
+ * For asynchronous implementations, this returns a `Promise<T | null>`.
705
+ */
706
+ get(): (T | null) | Promise<T | null>;
707
+ /**
708
+ * Subscribes to changes in the global persisted data that originate from *other* instances of your application (e.g., other tabs or independent components using the same persistence layer).
709
+ *
710
+ * @param id The **unique identifier of the *consumer instance* subscribing**. This allows the persistence implementation to filter out notifications that were initiated by the subscribing instance itself.
711
+ * @param callback The function to call when the global persisted data changes from *another* source. The new state (`T`) is passed as an argument to this callback.
712
+ * @returns A function that, when called, will unsubscribe the provided callback from future updates. Call this when your component or instance is no longer active to prevent memory leaks.
713
+ */
714
+ subscribe(id: string, callback: (state: T) => void): () => void;
715
+ /**
716
+ * Clears (removes) the entire global persisted data from storage.
717
+ *
718
+ * @returns `true` if the operation was successful, `false` if an error occurred. For asynchronous implementations, this returns a `Promise<boolean>`.
719
+ */
720
+ clear(): boolean | Promise<boolean>;
721
+ /**
722
+ * Returns metadata about the persistence layer.
723
+ *
724
+ * This is useful for distinguishing between multiple apps running on the same host
725
+ * (e.g., several apps served at `localhost:3000` that share the same storage key).
726
+ *
727
+ * @returns An object containing:
728
+ * - `version`: The semantic version string of the persistence schema or application.
729
+ * - `id`: A unique identifier for the application using this persistence instance.
730
+ */
731
+ stats(): {
732
+ version: string;
733
+ id: string;
734
+ };
735
+ }
736
+
684
737
  /**
685
738
  * @fileoverview Store Observer Module
686
739
  * @description A comprehensive module to add advanced debugging, observability, and time-travel
@@ -976,59 +1029,6 @@ declare function createMerge(options?: {
976
1029
  declare const merge: <T extends object>(original: T, changes: DeepPartial<T> | symbol) => T;
977
1030
  type MergeFunction = ReturnType<typeof createMerge>;
978
1031
 
979
- /**
980
- * Interface defining the shape of the EventBus.
981
- * @template TEventMap - A record mapping event names to their respective payload types.
982
- */
983
- interface EventBus<TEventMap extends Record<string, any>> {
984
- /**
985
- * Subscribes to a specific event by name.
986
- * @param eventName - The name of the event to subscribe to.
987
- * @param callback - The function to call when the event is emitted.
988
- * @returns A function to unsubscribe from the event.
989
- */
990
- subscribe: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void) => () => void;
991
- /**
992
- * Subscribes to an event and automatically unsubscribes after it fires once.
993
- * @param eventName - The name of the event to subscribe to.
994
- * @param callback - The function to call when the event is emitted.
995
- * @returns A function to cancel the one-shot subscription before it fires.
996
- */
997
- once: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void) => () => void;
998
- /**
999
- * Emits an event with a payload to all subscribed listeners.
1000
- * @param event - An object containing the event name and payload.
1001
- */
1002
- emit: <TEventName extends keyof TEventMap>(event: {
1003
- name: TEventName;
1004
- payload: TEventMap[TEventName];
1005
- }) => void;
1006
- /**
1007
- * Retrieves metrics about event bus usage.
1008
- * @returns An object containing various metrics.
1009
- */
1010
- metrics: () => EventMetrics;
1011
- /**
1012
- * Clears all subscriptions and resets metrics.
1013
- * After calling clear(), the bus is fully reset and can be reused —
1014
- * cross-tab communication is re-established if it was previously enabled.
1015
- */
1016
- clear: () => void;
1017
- }
1018
- /**
1019
- * Interface defining the metrics tracked by the EventBus.
1020
- */
1021
- interface EventMetrics {
1022
- /** Total number of events emitted (both sync and deferred paths). */
1023
- totalEvents: number;
1024
- /** Number of active subscriptions across all event names. */
1025
- activeSubscriptions: number;
1026
- /** Map of event names to their emission counts. */
1027
- eventCounts: Map<string, number>;
1028
- /** Average duration of event dispatch in milliseconds. */
1029
- averageEmitDuration: number;
1030
- }
1031
-
1032
1032
  declare class ActionCancelledError extends Error {
1033
1033
  constructor();
1034
1034
  }
@@ -1055,7 +1055,11 @@ declare class ActionManager<T extends object> {
1055
1055
  }): () => void;
1056
1056
  dispatch<R extends any[]>(name: string, ...params: R): Promise<T>;
1057
1057
  private executeAction;
1058
+ running(name: string): boolean;
1059
+ subscribe(name: string, callback: () => void): () => void;
1060
+ watch(name: string): ActionWatcher;
1061
+ private notifyStatusListeners;
1058
1062
  private emit;
1059
1063
  }
1060
1064
 
1061
- export { ActionCancelledError, type ActionCompletePayload, type ActionErrorPayload, ActionManager, type ActionStartPayload, type BlockingMiddleware, DELETE_SYMBOL, type DataStore, type DeepPartial, type DiffFunction, type MergeFunction, type Middleware, type MiddlewareConfig, type MiddlewareExecution, type ObserverOptions, type PersistenceFailedPayload, type PersistenceInitErrorPayload, type PersistenceQueueClearedPayload, type PersistenceQueuedPayload, type PersistenceRetryPayload, type PersistenceSuccessPayload, ReactiveDataStore, type ReactiveSelector, type SelectorAccessedPayload, type SelectorChangedPayload, type StateDelta, type StateUpdater, type StoreAction, type StoreEvent, type StoreEvents, type StoreExecutionState, type StoreMetrics, StoreObserver, type TransformMiddleware, UnknownActionError, createDerivePaths, createDiff, createMerge, derivePaths, diff, merge, shallowClone };
1065
+ export { ActionCancelledError, type ActionCompletePayload, type ActionErrorPayload, ActionManager, type ActionStartPayload, type ActionWatcher, type BlockingMiddleware, DELETE_SYMBOL, type DataStore, type DeepPartial, type DiffFunction, type MergeFunction, type Middleware, type MiddlewareConfig, type MiddlewareExecution, type ObserverOptions, type PersistenceFailedPayload, type PersistenceInitErrorPayload, type PersistenceQueueClearedPayload, type PersistenceQueuedPayload, type PersistenceRetryPayload, type PersistenceSuccessPayload, ReactiveDataStore, type ReactiveSelector, type SelectorAccessedPayload, type SelectorChangedPayload, type StateDelta, type StateUpdater, type StoreAction, type StoreEvent, type StoreEvents, type StoreExecutionState, type StoreMetrics, StoreObserver, type TransformMiddleware, UnknownActionError, createDerivePaths, createDiff, createMerge, derivePaths, diff, merge, shallowClone };
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("uuid"),t=class{_delay;_leading;_timer;_pendingFn;_pendingResolvers=[];_leadingFired=!1;constructor(e){this._delay=e?.delay??300,this._leading=e?.leading??!1}do(e){return new Promise((t=>{this._enqueue(e,t)}))}fire(e){this._enqueue(e,void 0)}_enqueue(e,t){if(this._pendingFn=e,t&&this._pendingResolvers.push(t),this._leading&&!this._leadingFired)return this._leadingFired=!0,this._fire(),clearTimeout(this._timer),void(this._timer=setTimeout((()=>{this._leadingFired=!1,void 0!==this._pendingFn&&this._fire()}),this._delay));clearTimeout(this._timer),this._timer=setTimeout((()=>{this._leadingFired=!1,this._fire()}),this._delay)}cancel(){clearTimeout(this._timer),this._timer=void 0,this._pendingFn=void 0,this._leadingFired=!1;const e=this._pendingResolvers.splice(0);for(const t of e)t({status:"cancelled"})}async flush(){return this._pendingFn?(clearTimeout(this._timer),this._timer=void 0,this._leadingFired=!1,this._fire()):null}pending(){return void 0!==this._pendingFn}async _fire(){const e=this._pendingFn,t=this._pendingResolvers.splice(0);let s;this._pendingFn=void 0;try{s={status:"ok",value:await e()}}catch(e){s={status:"error",error:e}}for(const e of t)e(s);return s}},s=class e extends Error{constructor(t,s){super(t,{cause:s}),this.name="SyncError",Object.setPrototypeOf(this,e.prototype)}},i=class extends s{constructor(e){super(`[ArtifactContainer] Operation timed out: ${e}`)}},r=class extends s{constructor(e){super("[Serializer] The serializer has been marked as done!",e)}},n=class{_open=!1;_resolve;_promise;constructor(){this._promise=new Promise((e=>{this._resolve=e}))}open(){this._open||(this._open=!0,this._resolve())}async wait(e){if(this._open)return;if(null==e)return this._promise;let t;await Promise.race([this._promise.then((()=>clearTimeout(t))),new Promise(((s,r)=>{t=setTimeout((()=>r(new i("Latch timed out"))),e)}))])}isOpen(){return this._open}},a=class{_locked=!1;_capacity;_yieldMode;waiters=[];constructor(e){this._capacity=e?.capacity??1/0,this._yieldMode=e?.yieldMode??"macrotask"}async lock(e){if(!this._locked)return void(this._locked=!0);if(this.waiters.length>=this._capacity)throw new Error(`Mutex queue is full (capacity: ${this._capacity})`);let t;const s=new Promise((e=>t=e));if(this.waiters.push(t),null==e)return void await s;let r;await Promise.race([s.then((()=>clearTimeout(r))),new Promise(((s,n)=>{r=setTimeout((()=>{const e=this.waiters.indexOf(t);-1!==e&&this.waiters.splice(e,1),n(new i("Mutex lock timed out"))}),e)}))])}tryLock(){return!this._locked&&(this._locked=!0,!0)}unlock(){if(!this._locked)throw new Error("Mutex is not locked");const e=this.waiters.shift();e?"microtask"===this._yieldMode?queueMicrotask(e):setTimeout(e,0):this._locked=!1}locked(){return this._locked}pending(){return this.waiters.length}},o=class{mutex;_done=!1;_lastValue=null;_lastError=void 0;_hasRun=!1;constructor(e){this.mutex=new a({capacity:e?.capacity??1e3,yieldMode:e?.yieldMode??"macrotask"})}async do(e,t){if(this._done)return{value:null,error:new r};try{await this.mutex.lock(t)}catch(e){return{value:null,error:e}}let s,i=null;try{if(this._done)throw new r;i=await e(),this._lastValue=i,this._lastError=void 0,this._hasRun=!0}catch(e){s=e,this._lastError=e,this._hasRun=!0}finally{this.mutex.unlock()}return{value:i,error:s}}peek(){return{value:this._lastValue,error:this._lastError}}hasRun(){return this._hasRun}close(){this._done=!0}pending(){return this.mutex.pending()}running(){return this.mutex.locked()}};function c(e){const s={},i=s.errorHandler??(e=>console.error("EventBus Error:",e)),r=null!=s.batch?.size,n=s.batch?.size,a=s.batch?.delay??16;if(r&&(n<=0||!Number.isFinite(n)))throw new Error(`EventBus: batch.size must be a positive finite number, got ${n}.`);if(r&&(a<0||!Number.isFinite(a)))throw new Error(`EventBus: batch.delay must be a non-negative finite number, got ${a}.`);const o=s.broadcast?.channel,c=null!=o&&o.length>0,d=new Map;let l=[];const h=r?new t({delay:a}):null;let u=0,p=0;const m=new Map,g=()=>{if(!c)return null;if("undefined"==typeof BroadcastChannel)return console.warn("EventBus: BroadcastChannel is not supported in this environment. Cross-tab notifications are disabled."),null;const e=new BroadcastChannel(o);return e.onmessage=e=>{const{name:t,payload:s}=e.data;w(t,s)},e};let y=g();const f=(e,t)=>{u++,p+=t,m.set(e,(m.get(e)??0)+1)},w=(e,t)=>{const s=d.get(e);if(s&&0!==s.size)for(const r of Array.from(s))try{r(t)}catch(s){const r=s instanceof Error?s:Object.assign(new Error(String(s)),{cause:s}),n=Object.assign(r,{eventName:e,payload:t});i(n)}},v=(e,t)=>{d.has(e)||d.set(e,new Set);const s=d.get(e);return s.add(t),()=>{s.delete(t),0===s.size&&d.delete(e)}},b=()=>{const e=l;l=[];for(const{name:t,payload:s}of e){const e=performance.now();w(t,s),f(t,performance.now()-e)}};return{subscribe:(e,t)=>v(e,t),once:(e,t)=>{let s;return s=v(e,(e=>{s(),t(e)})),s},emit:({name:e,payload:t})=>{if(r)return l.push({name:e,payload:t}),l.length>=n?(h.cancel(),void b()):(h.fire((()=>b())),void y?.postMessage({name:e,payload:t}));const s=performance.now();w(e,t),f(e,performance.now()-s),y?.postMessage({name:e,payload:t})},metrics:()=>({totalEvents:u,activeSubscriptions:Array.from(d.values()).reduce(((e,t)=>e+t.size),0),eventCounts:new Map(m),averageEmitDuration:u>0?p/u:0}),clear:()=>{h?.cancel(),l=[],d.clear(),u=0,p=0,m.clear(),y?.close(),y=g()}}}var d=Symbol.for("delete"),l=e=>Array.isArray(e)?[...e]:{...e};function h(e){const t=e?.deleteMarker||d;function s(e){if(null==e)return e;if(Array.isArray(e))return e.filter((e=>e!==t)).map((e=>"object"!=typeof e||null===e||Array.isArray(e)?e:s(e)));if("object"==typeof e){const i={};for(const[r,n]of Object.entries(e))if(n!==t)if("object"==typeof n&&null!==n){const e=s(n);void 0!==e&&(i[r]=e)}else i[r]=n;return i}return e===t?void 0:e}return function(e,i){if("object"!=typeof e||null===e)return"object"==typeof i&&null!==i?s(i):i===t?{}:i;if("object"!=typeof i||null===i)return e;const r=l(e),n=[{target:r,source:i}];for(;n.length>0;){const{target:e,source:s}=n.pop();for(const i of Object.keys(s)){const r=s[i];if(r!==t)if(Array.isArray(r))e[i]=r;else if("object"==typeof r&&null!==r){const t=i in e&&"object"==typeof e[i]&&null!==e[i]?e[i]:{};e[i]=l(t),n.push({target:e[i],source:r})}else e[i]=r;else delete e[i]}}return r}}var u=h(),p=["map","filter","reduce","forEach","find","findIndex","some","every","includes","flatMap","flat","slice","splice"],m=class{reactiveSelectors=new Map;pathBasedCache=new Map;dependencyMap=new Map;getState;eventBus;unsubscribeFromStore;constructor(e,t){this.getState=e,this.eventBus=t,this.unsubscribeFromStore=this.eventBus.subscribe("update:complete",this.handleStoreUpdate)}handleStoreUpdate=e=>{const t=new Set;for(const s of e.deltas){const e=s.path;for(const[s,i]of this.dependencyMap)if(s===e||s.startsWith(e+".")||e.startsWith(s+"."))for(const e of i)t.add(e)}for(const e of t){const t=this.reactiveSelectors.get(e);t&&this.evaluateEntry(t)}};evaluateEntry(e){let t;try{t=e.selector(this.getState())}catch{t=void 0}if(t!==e.lastResult){e.lastResult=t;for(const s of e.subscribers)s(t);this.eventBus.emit({name:"selector:changed",payload:{selectorId:e.id,newResult:t,timestamp:performance.now()}})}}createReactiveSelector(e){const t=function(e,t="."){const s=new Set,i=new Map,r=(e="")=>{if(i.has(e))return i.get(e);const n=new Proxy((()=>{}),{get:(i,n)=>{if("symbol"==typeof n||"then"===n)return;if("valueOf"===n||"toString"===n)throw new Error("Cannot perform logic, arithmetic, or string operations inside a selector.");if(p.includes(n))throw new Error(`Array method .${n}() is not allowed in selectors.`);const a=e?`${e}${t}${n}`:n;return e&&s.delete(e),s.add(a),r(a)},has:()=>{throw new Error("The 'in' operator is not allowed in selectors.")},apply:()=>{throw new Error("Selectors cannot call functions or methods.")}});return i.set(e,n),n};try{e(r())}catch(e){throw new Error(`Selector failed during path analysis. Selectors must be simple property accessors only. Error: ${e instanceof Error?e.message:String(e)}`)}return Array.from(s)}(e),s=[...t].sort().join("|"),i=this.pathBasedCache.get(s);if(i)return void 0!==i.cleanupTimer&&(clearTimeout(i.cleanupTimer),i.cleanupTimer=void 0),i.reactiveSelectorInstance;const r=`sel-${Math.random().toString(36).slice(2,9)}`,n={id:r,selector:e,lastResult:e(this.getState()),accessedPaths:t,subscribers:new Set,count:0,cleanupTimer:void 0,pathCacheKey:s,reactiveSelectorInstance:null};for(const e of t)this.dependencyMap.has(e)||this.dependencyMap.set(e,new Set),this.dependencyMap.get(e).add(r);const a={id:r,get:()=>{try{return n.selector(this.getState())}catch{return}},subscribe:e=>(void 0!==n.cleanupTimer&&(clearTimeout(n.cleanupTimer),n.cleanupTimer=void 0),n.subscribers.add(e),n.count++,()=>{n.subscribers.delete(e),n.count--,0===n.count&&(n.cleanupTimer=setTimeout((()=>{0===n.count&&this.evictEntry(n)}),0))})};return n.reactiveSelectorInstance=a,this.reactiveSelectors.set(r,n),this.pathBasedCache.set(s,n),this.eventBus.emit({name:"selector:accessed",payload:{selectorId:r,accessedPaths:t,duration:0,timestamp:performance.now()}}),a}evictEntry(e){for(const t of e.accessedPaths){const s=this.dependencyMap.get(t);s&&(s.delete(e.id),0===s.size&&this.dependencyMap.delete(t))}this.reactiveSelectors.delete(e.id),this.pathBasedCache.delete(e.pathCacheKey)}dispose(){this.unsubscribeFromStore(),this.reactiveSelectors.clear(),this.dependencyMap.clear(),this.pathBasedCache.clear()}};function g(e,t){if(e===t)return!0;if(e&&t&&"object"==typeof e&&"object"==typeof t){if(e.constructor!==t.constructor)return!1;let s,i;if(Array.isArray(e)){if(s=e.length,s!=t.length)return!1;for(i=s;i-- >0;)if(!g(e[i],t[i]))return!1;return!0}const[r,n]=[Object.keys(e),Object.keys(t)];if(s=r.length,s!==n.length)return!1;for(i=s;i-- >0;){const s=r[i];if(!Object.prototype.hasOwnProperty.call(t,s)||!g(e[s],t[s]))return!1}return!0}return e!=e&&t!=t}function y(e){const t=e?.deleteMarker||d;return function(e,s){const i=[],r=[{pathStr:"",orig:e||{},part:s||{}}];for(;r.length>0;){const{pathStr:e,orig:s,part:n}=r.pop();if(null!=n&&!g(s,n))if("object"!=typeof n||Array.isArray(n))e&&i.push({path:e,oldValue:s,newValue:n});else for(const a of Object.keys(n)){const o=e?e+"."+a:a,c=n[a],d=s&&"object"==typeof s?s[a]:void 0;c!==t?"object"==typeof c&&null!==c?r.push({pathStr:o,orig:d,part:c}):g(d,c)||i.push({path:o,oldValue:d,newValue:c}):void 0!==d&&i.push({path:o,oldValue:d,newValue:void 0})}}return i}}function f(e){const t=e?.deleteMarker||d;return function(e){const s=new Set,i=[{obj:e,currentPath:""}];for(;i.length>0;){const{obj:e,currentPath:r}=i.pop();if(null!=e&&"object"==typeof e&&!Array.isArray(e))for(const n of Object.keys(e)){const a=r?`${r}.${n}`:n;s.add(a);const o=e[n];"object"!=typeof o||null===o||Array.isArray(o)||o===t||i.push({obj:o,currentPath:a})}}return Array.from(s)}}var w=y(),v=f(),b=class{constructor(e,t,s){this.updateBus=t,this.diff=s,this.cache=structuredClone(e)}cache;get(e){return e?structuredClone(this.cache):this.cache}applyChanges(e,t=!1,s=!1,i=[]){if(t)return this.cache=s?structuredClone(e):e,this.notifyListeners([]),[];0===i.length&&(i=[e]);const r=this.get(!1),n=new Map;for(let e=0;e<i.length;e++){const t=i[e],s=this.diff(r,t);for(let e=0;e<s.length;e++){const t=s[e];n.set(t.path,t)}}const a=n.size?[...n.values()]:[];if(a.length>0){this.cache=s?structuredClone(e):e;const t=new Set;for(let e=0;e<a.length;e++){let s=a[e].path;for(;s&&!t.has(s);){t.add(s);const e=s.lastIndexOf(".");if(e<0)break;s=s.slice(0,e)}}this.notifyListeners(t)}return a}notifyListeners(e){for(const t of e)this.updateBus.emit({name:"update",payload:t})}},S=class{constructor(e,t,s){this.eventBus=e,this.executionState=t,this.merge=s}middleware=[];blockingMiddleware=[];async executeBlocking(e,t){for(const{fn:s,name:i,id:r}of this.blockingMiddleware){const n={id:r,name:i,startTime:performance.now()};this.executionState.runningMiddleware={id:r,name:i,startTime:performance.now()},this.emitMiddlewareLifecycle("start",{id:r,name:i,type:"blocking"});try{const a=await Promise.resolve(s(e,t));if(n.endTime=performance.now(),n.duration=n.endTime-n.startTime,!1===a)return n.blocked=!0,this.emitMiddlewareLifecycle("blocked",{id:r,name:i,duration:n.duration}),this.emit(this.eventBus,{name:"middleware:executed",payload:n}),{blocked:!0};this.emitMiddlewareLifecycle("complete",{id:r,name:i,type:"blocking",duration:n.duration}),this.emit(this.eventBus,{name:"middleware:executed",payload:{...n,blocked:!1}})}catch(e){return n.endTime=performance.now(),n.duration=n.endTime-n.startTime,n.error=e instanceof Error?e:new Error(String(e)),n.blocked=!0,this.emitMiddlewareError(r,i,n.error,n.duration),this.emit(this.eventBus,{name:"middleware:executed",payload:n}),{blocked:!0,error:n.error}}finally{this.executionState.runningMiddleware=null}}return{blocked:!1}}async executeTransform(e,t){let s=e,i=t;for(const{fn:e,name:r,id:n}of this.middleware){const a={id:n,name:r,startTime:performance.now()};this.executionState.runningMiddleware={id:n,name:r,startTime:performance.now()},this.emitMiddlewareLifecycle("start",{id:n,name:r,type:"transform"});try{const o=await Promise.resolve(e(s,t));a.endTime=performance.now(),a.duration=a.endTime-a.startTime,a.blocked=!1,o&&"object"==typeof o&&(s=this.merge(s,o),i=this.merge(i,o)),this.emit(this.eventBus,{name:"middleware:executed",payload:a}),this.emitMiddlewareLifecycle("complete",{id:n,name:r,type:"transform",duration:a.duration})}catch(e){a.endTime=performance.now(),a.duration=a.endTime-a.startTime,a.error=e instanceof Error?e:new Error(String(e)),a.blocked=!1,this.emit(this.eventBus,{name:"middleware:executed",payload:a}),this.emitMiddlewareError(n,r,a.error,a.duration),console.error(`Middleware ${r} error:`,e)}finally{this.executionState.runningMiddleware=null}}return i}addMiddleware(e,t="unnamed-middleware"){const s=this.generateId();return this.middleware.push({fn:e,name:t,id:s}),this.updateExecutionState(),s}addBlockingMiddleware(e,t="unnamed-blocking-middleware"){const s=this.generateId();return this.blockingMiddleware.push({fn:e,name:t,id:s}),this.updateExecutionState(),s}removeMiddleware(e){const t=this.middleware.length+this.blockingMiddleware.length;return this.middleware=this.middleware.filter((t=>t.id!==e)),this.blockingMiddleware=this.blockingMiddleware.filter((t=>t.id!==e)),this.updateExecutionState(),this.middleware.length+this.blockingMiddleware.length<t}updateExecutionState(){this.executionState.middlewares=[...this.middleware.map((e=>e.name)),...this.blockingMiddleware.map((e=>e.name))]}emitMiddlewareLifecycle(e,t){this.emit(this.eventBus,{name:`middleware:${e}`,payload:{...t,timestamp:Date.now()}})}emitMiddlewareError(e,t,s,i){this.emit(this.eventBus,{name:"middleware:error",payload:{id:e,name:t,error:s,duration:i,timestamp:Date.now()}})}generateId(){return crypto.randomUUID?crypto.randomUUID():`${Date.now()}-${Math.random().toString(36).substring(2,15)}`}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},x=class{constructor(e,t,s,i){this.eventBus=e,this.coreState=t,this.instanceID=s,this.maxRetries=i?.maxRetries??3,this.retryDelay=i?.retryDelay??1e3}persistence;instanceID;persistenceReady=!1;backgroundQueue=[];isProcessingQueue=!1;maxRetries=3;retryDelay=1e3;queueProcessor;pendingRetries=new Set;async initialize(e){e?await this.setPersistence(e):this.setPersistenceReady()}isReady(){return this.persistenceReady}handleStateChange(e,t){if(!this.persistence||0===e.length)return;const s={id:`${Date.now()}-${Math.random().toString(36).slice(2,11)}`,state:structuredClone(t),changedPaths:[...e],timestamp:Date.now(),retries:0};this.backgroundQueue.push(s),this.scheduleQueueProcessing(),this.emit(this.eventBus,{name:"persistence:queued",payload:{taskId:s.id,changedPaths:e,queueSize:this.backgroundQueue.length,timestamp:s.timestamp}})}getQueueStatus(){return{queueSize:this.backgroundQueue.length,isProcessing:this.isProcessingQueue,pendingRetries:this.pendingRetries.size,oldestTask:this.backgroundQueue[0]?.timestamp}}async flush(){this.isProcessingQueue&&await new Promise((e=>{const t=()=>{this.isProcessingQueue?setTimeout(t,10):e()};t()})),await this.processQueue()}discardQueue(){const e=this.backgroundQueue.length+this.pendingRetries.size;this.backgroundQueue=[],this.pendingRetries.clear(),this.queueProcessor&&(clearTimeout(this.queueProcessor),this.queueProcessor=void 0),this.emit(this.eventBus,{name:"persistence:queue_cleared",payload:{clearedTasks:e,timestamp:Date.now()}})}scheduleQueueProcessing(){this.queueProcessor||this.isProcessingQueue||(this.queueProcessor=setTimeout((()=>{this.processQueue().catch((e=>{console.error("Queue processing failed:",e)}))}),10))}async processQueue(){if(!this.isProcessingQueue&&0!==this.backgroundQueue.length){this.isProcessingQueue=!0,this.queueProcessor=void 0;try{for(;this.backgroundQueue.length>0;){const e=this.backgroundQueue.shift();await this.processTask(e)}}finally{this.isProcessingQueue=!1}}}async processTask(e){try{await this.persistence.set(this.instanceID,e.state)?this.emit(this.eventBus,{name:"persistence:success",payload:{taskId:e.id,changedPaths:e.changedPaths,duration:Date.now()-e.timestamp,timestamp:Date.now()}}):await this.handleTaskFailure(e,new Error("Persistence returned false"))}catch(t){await this.handleTaskFailure(e,t)}}async handleTaskFailure(e,t){if(e.retries++,e.retries<=this.maxRetries){const s=this.retryDelay*Math.pow(2,e.retries-1);this.emit(this.eventBus,{name:"persistence:retry",payload:{taskId:e.id,attempt:e.retries,maxRetries:this.maxRetries,nextRetryIn:s,error:t,timestamp:Date.now()}}),this.pendingRetries.add(e.id),setTimeout((()=>{this.pendingRetries.has(e.id)&&(this.pendingRetries.delete(e.id),this.backgroundQueue.unshift(e),this.scheduleQueueProcessing())}),s)}else this.emit(this.eventBus,{name:"persistence:failed",payload:{taskId:e.id,changedPaths:e.changedPaths,attempts:e.retries,error:t,timestamp:Date.now()}})}setPersistenceReady(){this.persistenceReady=!0,this.emit(this.eventBus,{name:"persistence:ready",payload:{timestamp:Date.now()}})}async setPersistence(e){this.persistence=e;try{const e=await this.persistence.get();e&&this.coreState.applyChanges(e)}catch(e){console.error("Failed to initialize persistence:",e),this.emit(this.eventBus,{name:"persistence:init_error",payload:{error:e,timestamp:Date.now()}})}finally{this.setPersistenceReady()}this.persistence.subscribe(this.instanceID,(async e=>{const t=this.coreState.applyChanges(e);t.length>0&&this.emit(this.eventBus,{name:"update:complete",payload:{changedPaths:t,source:"external",timestamp:Date.now()}})}))}dispose(){this.discardQueue(),this.isProcessingQueue=!1,this.persistenceReady=!1}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},T=class{constructor(e,t,s){this.eventBus=e,this.coreState=t,this.executionState=s}async execute(e){const t=this.coreState.get(!0);this.executionState.transactionActive=!0,this.emit(this.eventBus,{name:"transaction:start",payload:{timestamp:Date.now()}});try{const t=await Promise.resolve(e());return this.emit(this.eventBus,{name:"transaction:complete",payload:{timestamp:Date.now()}}),this.executionState.transactionActive=!1,t}catch(e){throw this.coreState.applyChanges(t,!0,!1),this.emit(this.eventBus,{name:"transaction:error",payload:{error:e instanceof Error?e:new Error(String(e)),timestamp:Date.now()}}),this.executionState.transactionActive=!1,e}}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},E=class{updateCount=0;listenerExecutions=0;averageUpdateTime=0;largestUpdateSize=0;mostActiveListenerPaths=[];totalUpdates=0;blockedUpdates=0;averageUpdateDuration=0;middlewareExecutions=0;transactionCount=0;totalEventsFired=0;totalActionsDispatched=0;totalActionsSucceeded=0;totalActionsFailed=0;averageActionDuration=0;updateTimes=[];actionTimes=[];pathExecutionCounts=new Map;constructor(e){this.setupEventListeners(e)}getMetrics(){return{updateCount:this.updateCount,listenerExecutions:this.listenerExecutions,averageUpdateTime:this.averageUpdateTime,largestUpdateSize:this.largestUpdateSize,mostActiveListenerPaths:[...this.mostActiveListenerPaths],totalUpdates:this.totalUpdates,blockedUpdates:this.blockedUpdates,averageUpdateDuration:this.averageUpdateDuration,middlewareExecutions:this.middlewareExecutions,transactionCount:this.transactionCount,totalEventsFired:this.totalEventsFired,totalActionsDispatched:this.totalActionsDispatched,totalActionsSucceeded:this.totalActionsSucceeded,totalActionsFailed:this.totalActionsFailed,averageActionDuration:this.averageActionDuration}}setupEventListeners(e){const t=e.emit;e.emit=s=>(this.totalEventsFired++,t.call(e,s)),e.subscribe("update:complete",(e=>{if(this.totalUpdates++,e.blocked)this.blockedUpdates++;else{if(e.duration){this.updateTimes.push(e.duration),this.updateTimes.length>100&&this.updateTimes.shift();const t=this.updateTimes.reduce(((e,t)=>e+t),0)/this.updateTimes.length;this.averageUpdateTime=t,this.averageUpdateDuration=t}e.deltas?.length&&(this.updateCount++,this.largestUpdateSize=Math.max(this.largestUpdateSize,e.deltas.length),e.deltas.forEach((e=>{const t=this.pathExecutionCounts.get(e.path)||0;this.pathExecutionCounts.set(e.path,t+1)})),this.mostActiveListenerPaths=Array.from(this.pathExecutionCounts.entries()).sort((([,e],[,t])=>t-e)).slice(0,5).map((([e])=>e)))}})),e.subscribe("middleware:start",(()=>{this.middlewareExecutions++})),e.subscribe("transaction:start",(()=>{this.transactionCount++})),e.subscribe("action:start",(()=>{this.totalActionsDispatched++})),e.subscribe("action:complete",(e=>{this.totalActionsSucceeded++,e.duration&&(this.actionTimes.push(e.duration),this.actionTimes.length>100&&this.actionTimes.shift(),this.averageActionDuration=this.actionTimes.reduce(((e,t)=>e+t),0)/this.actionTimes.length)})),e.subscribe("action:error",(()=>{this.totalActionsFailed++}))}reset(){this.updateCount=0,this.listenerExecutions=0,this.averageUpdateTime=0,this.largestUpdateSize=0,this.mostActiveListenerPaths=[],this.totalUpdates=0,this.blockedUpdates=0,this.averageUpdateDuration=0,this.middlewareExecutions=0,this.transactionCount=0,this.totalEventsFired=0,this.totalActionsDispatched=0,this.totalActionsSucceeded=0,this.totalActionsFailed=0,this.averageActionDuration=0,this.updateTimes=[],this.actionTimes=[],this.pathExecutionCounts.clear()}getDetailedMetrics(){return{pathExecutionCounts:new Map(this.pathExecutionCounts),recentUpdateTimes:[...this.updateTimes],successRate:this.totalUpdates>0?(this.totalUpdates-this.blockedUpdates)/this.totalUpdates:1,averagePathsPerUpdate:this.updateCount>0?Array.from(this.pathExecutionCounts.values()).reduce(((e,t)=>e+t),0)/this.updateCount:0}}dispose(){this.reset()}},_=class extends Error{constructor(){super("Action Cancelled by Debounce"),this.name="ActionCancelledError"}},k=class extends Error{constructor({action:e}){super(`Unknown action: "${e}"`),this.name="UnknownActionError"}},M=class{constructor(e,t){this.eventBus=e,this.set=t}registrations=new Map;register(s){const i={action:{name:s.name,id:e.v4(),action:s.fn,debounce:s.debounce?{...s.debounce,condition:s.debounce.condition??(()=>!0)}:void 0},debouncer:s.debounce&&s.debounce.delay>0?new t({delay:s.debounce.delay}):void 0,previousArgs:void 0};return this.registrations.set(s.name,i),()=>{const e=this.registrations.get(s.name);e&&(e.debouncer?.cancel(),this.registrations.delete(s.name))}}async dispatch(e,...t){const s=this.registrations.get(e);if(!s)throw new k({action:e});const{action:i,debouncer:r}=s,{id:n,action:a,debounce:o}=i;if(!r||!o)return this.executeAction(e,n,a,t);const c=o.condition(s.previousArgs,t);if(s.previousArgs=t,!c)return this.executeAction(e,n,a,t);const d=await r.do((()=>this.executeAction(e,n,a,t)));if("cancelled"===d.status)throw new _;if("error"===d.status&&d.error)throw d.error;return d.value}async executeAction(e,t,s,i){const r=performance.now();this.emit(this.eventBus,{name:"action:start",payload:{actionId:t,name:e,params:i||[],timestamp:r}});try{const n=await this.set((e=>s(e,...i)),{actionId:t}),a=performance.now();return this.emit(this.eventBus,{name:"action:complete",payload:{actionId:t,name:e,params:i,startTime:r,endTime:a,duration:a-r,result:n}}),n}catch(s){const n=performance.now();throw this.emit(this.eventBus,{name:"action:error",payload:{actionId:t,name:e,params:i,startTime:r,endTime:n,duration:n-r,error:s}}),s}}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}};exports.ActionCancelledError=_,exports.ActionManager=M,exports.DELETE_SYMBOL=d,exports.ReactiveDataStore=class{coreState;middlewareEngine;persistenceHandler;transactionManager;metricsCollector;selectorManager;actions;updateSerializer=new o({yieldMode:"macrotask",capacity:1e3});readyLatch=new n;updateBus=c();eventBus;executionState;instanceID=e.v4();merge;diff;constructor(e,t,s=d,i){this.eventBus=c(),this.executionState={executing:!1,changes:null,pendingChanges:[],middlewares:[],runningMiddleware:null,transactionActive:!1},this.merge=h({deleteMarker:s}),this.diff=y({deleteMarker:s}),this.coreState=new b(e,this.updateBus,this.diff),this.middlewareEngine=new S(this.eventBus,this.executionState,this.merge),this.persistenceHandler=new x(this.eventBus,this.coreState,this.instanceID,{maxRetries:i?.persistenceMaxRetries,retryDelay:i?.persistenceRetryDelay}),this.transactionManager=new T(this.eventBus,this.coreState,this.executionState),this.metricsCollector=new E(this.eventBus),this.actions=new M(this.eventBus,this.set.bind(this)),this.persistenceHandler.initialize(t),this.setupPersistenceListener(),this.setupReadyLatch(),this.selectorManager=new m(this.get.bind(this),this.eventBus)}isReady(){return this.readyLatch.isOpen()}async ready(e){return this.readyLatch.wait(e)}state(){return this.executionState.executing=this.updateSerializer.running(),this.executionState}get(e){return this.coreState.get(e??!1)}select(e){return this.selectorManager.createReactiveSelector(e)}register(e){return this.actions.register(e)}async dispatch(e,...t){return this.actions.dispatch(e,...t)}async set(e,t={}){const s=await this.updateSerializer.do((()=>this._performUpdate(e,t)));if(s.error)throw s.error;return s.value}async _performUpdate(e,t){const s=performance.now();this.emit(this.eventBus,{name:"update:start",payload:{timestamp:s,actionId:t.actionId}});try{if(t.force){const i=this.get(!1),r="function"==typeof e?e(i):e;this.coreState.applyChanges(r,!0);const n=performance.now();return this.emit(this.eventBus,{name:"update:complete",payload:{deltas:[],duration:n-s,timestamp:Date.now(),actionId:t.actionId,newState:r}}),r}let i;const r=this.get(!1);if("function"==typeof e){const t=e(r);i=t instanceof Promise?await t:t}else i=e;const n=await this.middlewareEngine.executeBlocking(r,i);if(n.blocked)throw n.error||new Error("Update blocked by middleware");const a=this.merge(r,i),o=await this.middlewareEngine.executeTransform(a,i),c=this.merge(a,o),d=this.coreState.applyChanges(c,!1,!1,[i,o]),l=performance.now(),h=this.get(!1);return this.emit(this.eventBus,{name:"update:complete",payload:{deltas:d,duration:l-s,timestamp:Date.now(),actionId:t.actionId,newState:h}}),h}catch(e){throw this.emit(this.eventBus,{name:"update:complete",payload:{blocked:!0,error:e,timestamp:Date.now(),actionId:t.actionId,newState:this.get(!1)}}),e}finally{this.executionState.executing=!1,this.executionState.changes=null,this.executionState.runningMiddleware=null,this.executionState.pendingChanges=[]}}setupReadyLatch(){this.readyLatch.open(),this.persistenceHandler.isReady()?this.readyLatch.open():queueMicrotask((()=>{this.readyLatch.isOpen()||this.readyLatch.open()}))}setupPersistenceListener(){this.updateBus.subscribe("update",(e=>{e&&this.persistenceHandler.isReady()&&this.persistenceHandler.handleStateChange([e],this.get(!1))}))}watch(e,t){const s=Array.isArray(e)?e:[e],i=""===e||0===s.length;return this.updateBus.subscribe("update",(e=>{(i||s.includes(e))&&(t(this.get(!1)),this.metricsCollector.listenerExecutions++)}))}debouncedSetter(e){const s=new t({delay:e.delay,leading:e.leading});return(e,t={})=>{s.fire((()=>this.set(e,t)))}}subscribe(e,t){return this.watch(e,t)}id(){return this.instanceID}async transaction(e){return this.transactionManager.execute(e)}use(e){const t=(e.block?this.middlewareEngine.addBlockingMiddleware:this.middlewareEngine.addMiddleware).bind(this.middlewareEngine)(e.action,e.name);return()=>this.middlewareEngine.removeMiddleware(t)}metrics(){return this.metricsCollector.getMetrics()}on(e,t){return this.eventBus.subscribe(e,t)}onStoreEvent(e,t){return this.eventBus.subscribe(e,t)}getPersistenceStatus(){return this.persistenceHandler.getQueueStatus()}async flushPersistence(){return this.persistenceHandler.flush()}discardPersistenceQueue(){this.persistenceHandler.discardQueue()}dispose(){this.updateSerializer.close(),this.persistenceHandler.dispose(),this.metricsCollector.dispose?.(),this.selectorManager.dispose?.()}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},exports.StoreObserver=class{store;eventHistory=[];stateHistory=[];unsubscribers=[];isTimeTraveling=!1;devTools=null;middlewareExecutions=[];activeTransactionCount=0;activeBatches=new Set;maxEvents;maxStateHistory;enableConsoleLogging;isSilent;logEvents;performanceThresholds;constructor(e,t={}){this.store=e,this.maxEvents=t.maxEvents??500,this.maxStateHistory=t.maxStateHistory??20,this.enableConsoleLogging=t.enableConsoleLogging??!1,this.isSilent=t.silent??!1,this.logEvents={updates:t.logEvents?.updates??!0,middleware:t.logEvents?.middleware??!0,transactions:t.logEvents?.transactions??!0,actions:t.logEvents?.actions??!0,selectors:t.logEvents?.selectors??!0},this.performanceThresholds={updateTime:t.performanceThresholds?.updateTime??50,middlewareTime:t.performanceThresholds?.middlewareTime??20},this.recordStateSnapshot([]),this.setupEventListeners()}_consoleLog(e,...t){this.isSilent||"function"==typeof console[e]&&console[e](...t)}setupEventListeners(){const e=["update:start","update:complete","middleware:start","middleware:complete","middleware:error","middleware:blocked","transaction:start","transaction:complete","transaction:error","middleware:executed","action:start","action:complete","action:error","selector:accessed"];for(const t of e){const e=t.startsWith("update")&&this.logEvents.updates||t.startsWith("middleware")&&this.logEvents.middleware||t.startsWith("transaction")&&this.logEvents.transactions||t.startsWith("action")&&this.logEvents.actions||t.startsWith("selector")&&this.logEvents.selectors;this.unsubscribers.push(this.store.on(t,(s=>{"update:complete"!==t||s.blocked||this.isTimeTraveling||this.recordStateSnapshot(s.deltas),"middleware:executed"===t?this.middlewareExecutions.push(s):"transaction:start"===t?this.activeTransactionCount++:"transaction:complete"!==t&&"transaction:error"!==t||(this.activeTransactionCount=Math.max(0,this.activeTransactionCount-1)),s.batchId&&(t.endsWith("start")?this.activeBatches.add(s.batchId):(t.endsWith("complete")||t.endsWith("error"))&&this.activeBatches.delete(s.batchId)),this.recordEvent(t,s),this.enableConsoleLogging&&e&&this._log(t,s),this._checkPerformance(t,s)})))}}recordStateSnapshot(e){const t={state:this.store.get(!0),timestamp:Date.now(),deltas:e};this.stateHistory.unshift(t),this.stateHistory.length>this.maxStateHistory&&this.stateHistory.pop()}recordEvent(e,t){const s={type:e,timestamp:Date.now(),data:structuredClone(t)};this.eventHistory.unshift(s),this.eventHistory.length>this.maxEvents&&this.eventHistory.pop()}getEventHistory(){return structuredClone(this.eventHistory)}getStateHistory(){return structuredClone(this.stateHistory)}getMiddlewareExecutions(){return this.middlewareExecutions}getTransactionStatus(){return{activeTransactions:this.activeTransactionCount,activeBatches:Array.from(this.activeBatches)}}createLoggingMiddleware(e={}){const{logLevel:t="debug",logUpdates:s=!0}=e;return(e,i)=>{if(s){(console[t]||console.log)("State Update:",i)}return i}}createValidationMiddleware(e){return(t,s)=>{const i=e(t,s);return"boolean"==typeof i?i:(!i.valid&&i.reason&&this._consoleLog("warn","Validation failed:",i.reason),i.valid)}}getRecentChanges(e=5){const t=[],s=Math.min(e,this.stateHistory.length);for(let e=0;e<s;e++){const s=this.stateHistory[e];if(!s.deltas||0===s.deltas.length)continue;const i={},r={},n=(e,t,s)=>{t.reduce(((e,i,r)=>(r===t.length-1?e[i]=s:e[i]=e[i]??{},e[i])),e)};for(const e of s.deltas){const t=e.path.split(".");n(i,t,e.oldValue),n(r,t,e.newValue)}t.push({timestamp:s.timestamp,changedPaths:s.deltas.map((e=>e.path)),from:i,to:r})}return t}clearHistory(){this.eventHistory=[],this.stateHistory.length>0&&(this.stateHistory=[this.stateHistory[0]])}getHistoryForAction(e){return this.eventHistory.filter((t=>t.data?.actionId===e))}async replay(e){const t=this.eventHistory.filter((e=>"update:start"===e.type))[e];t?.data.update?(this._consoleLog("log",`Replaying event at index ${e}:`,t),await this.store.set(t.data.update,{force:!0})):this._consoleLog("warn",`No replayable event found at index ${e}.`)}createTimeTravel(){let e=0,t=[];const s=this.store.on("update:complete",(s=>{this.isTimeTraveling||s.blocked||(t=[],e=0)}));this.unsubscribers.push(s);const i=()=>this.stateHistory.length,r=()=>e<i()-1,n=()=>t.length>0;return{canUndo:r,canRedo:n,undo:async()=>{if(!r())return;t.unshift(this.stateHistory[e]),e++;const s=this.stateHistory[e].state;this.isTimeTraveling=!0,await this.store.set({...s},{force:!0}),this.isTimeTraveling=!1},redo:async()=>{if(!n())return;const s=t.shift();e--,this.isTimeTraveling=!0,await this.store.set({...s.state},{force:!0}),this.isTimeTraveling=!1},length:i,clear:()=>{t=[],e=0}}}async saveSession(e){const t=this.store.id(),s={eventHistory:this.eventHistory,stateHistory:this.stateHistory};return Promise.resolve(e.set(t,s))}async loadSession(e){const t=await Promise.resolve(e.get());return!!t&&(this.eventHistory=t.eventHistory||[],this.stateHistory=t.stateHistory||[],this.stateHistory.length>0&&await this.store.set(this.stateHistory[0].state,{force:!0}),!0)}exportSession(){const e={eventHistory:this.eventHistory,stateHistory:this.stateHistory},t=new Blob([JSON.stringify(e,null,2)],{type:"application/json"}),s=URL.createObjectURL(t),i=document.createElement("a");i.href=s,i.download=`store-observer-session-${(new Date).toISOString()}.json`,i.click(),URL.revokeObjectURL(s)}importSession(e){return new Promise(((t,s)=>{const i=new FileReader;i.onload=async e=>{try{const s=JSON.parse(e.target?.result);this.eventHistory=s.eventHistory||[],this.stateHistory=s.stateHistory||[],this.stateHistory.length>0&&await this.store.set(this.stateHistory[0].state,{force:!0}),t()}catch(e){s(e)}},i.onerror=e=>s(e),i.readAsText(e)}))}disconnect(){this.unsubscribers.forEach((e=>e())),this.unsubscribers=[],this.devTools?.disconnect(),this.clearHistory()}_log(e,t){const s=new Date(t.timestamp||Date.now()).toISOString().split("T")[1].replace("Z","");if("update:start"===e)this._consoleLog("group",`%c⚡ Store Update Started [${s}]`,"color: #4a6da7");else if("update:complete"===e){if(t.blocked)this._consoleLog("warn",`%c✋ Update Blocked [${s}]`,"color: #bf8c0a",t.error);else{const e=t.deltas||[];e.length>0&&(this._consoleLog("log",`%c✅ Update Complete [${s}] - ${e.length} paths changed in ${t.duration?.toFixed(2)}ms`,"color: #2a9d8f"),this._consoleLog("table",e.map((e=>({path:e.path,oldValue:e.oldValue,newValue:e.newValue})))))}this._consoleLog("groupEnd")}else"middleware:start"===e?this._consoleLog("debug",`%c◀ Middleware "${t.name}" started [${s}] (${t.type})`,"color: #8c8c8c"):"middleware:complete"===e?this._consoleLog("debug",`%c▶ Middleware "${t.name}" completed [${s}] in ${t.duration?.toFixed(2)}ms`,"color: #7c9c7c"):"middleware:error"===e?this._consoleLog("error",`%c❌ Middleware "${t.name}" error [${s}]:`,"color: #e63946",t.error):"middleware:blocked"===e?this._consoleLog("warn",`%c🛑 Middleware "${t.name}" blocked update [${s}]`,"color: #e76f51"):"transaction:start"===e?this._consoleLog("group",`%c📦 Transaction Started [${s}]`,"color: #6d597a"):"transaction:complete"===e?(this._consoleLog("log",`%c📦 Transaction Complete [${s}]`,"color: #355070"),this._consoleLog("groupEnd")):"transaction:error"===e?(this._consoleLog("error",`%c📦 Transaction Error [${s}]:`,"color: #e56b6f",t.error),this._consoleLog("groupEnd")):"action:start"===e?this._consoleLog("group",`%c🚀 Action "${t.name}" Started [${s}]`,"color: #9b59b6",{params:t.params}):"action:complete"===e?(this._consoleLog("log",`%c✔️ Action "${t.name}" Complete [${s}] in ${t.duration?.toFixed(2)}ms`,"color: #2ecc71"),this._consoleLog("groupEnd")):"action:error"===e?(this._consoleLog("error",`%c🔥 Action "${t.name}" Error [${s}]:`,"color: #e74c3c",t.error),this._consoleLog("groupEnd")):"selector:accessed"===e&&this._consoleLog("debug",`%c👀 Selector Accessed [${s}] in ${t.duration?.toFixed(2)}ms`,"color: #f1c40f",{accessedPaths:t.accessedPaths,selectorId:t.selectorId})}_checkPerformance(e,t){this.enableConsoleLogging&&("update:complete"===e&&!t.blocked&&t.duration>this.performanceThresholds.updateTime&&this._consoleLog("warn",`%c⚠️ Slow update detected [${t.duration.toFixed(2)}ms]`,"color: #ff9f1c",{deltas:t.deltas,threshold:this.performanceThresholds.updateTime}),"middleware:complete"===e&&t.duration>this.performanceThresholds.middlewareTime&&this._consoleLog("warn",`%c⚠️ Slow middleware "${t.name}" [${t.duration.toFixed(2)}ms]`,"color: #ff9f1c",{threshold:this.performanceThresholds.middlewareTime}))}},exports.UnknownActionError=k,exports.createDerivePaths=f,exports.createDiff=y,exports.createMerge=h,exports.derivePaths=v,exports.diff=w,exports.merge=u,exports.shallowClone=l;
1
+ "use strict";var e=require("@core/events"),t=require("uuid"),s=class{_delay;_leading;_timer;_pendingFn;_pendingResolvers=[];_leadingFired=!1;constructor(e){this._delay=e?.delay??300,this._leading=e?.leading??!1}do(e){return new Promise((t=>{this._enqueue(e,t)}))}fire(e){this._enqueue(e,void 0)}_enqueue(e,t){if(this._pendingFn=e,t&&this._pendingResolvers.push(t),this._leading&&!this._leadingFired)return this._leadingFired=!0,this._fire(),clearTimeout(this._timer),void(this._timer=setTimeout((()=>{this._leadingFired=!1,void 0!==this._pendingFn&&this._fire()}),this._delay));clearTimeout(this._timer),this._timer=setTimeout((()=>{this._leadingFired=!1,this._fire()}),this._delay)}cancel(){clearTimeout(this._timer),this._timer=void 0,this._pendingFn=void 0,this._leadingFired=!1;const e=this._pendingResolvers.splice(0);for(const t of e)t({status:"cancelled"})}async flush(){return this._pendingFn?(clearTimeout(this._timer),this._timer=void 0,this._leadingFired=!1,this._fire()):null}pending(){return void 0!==this._pendingFn}async _fire(){const e=this._pendingFn,t=this._pendingResolvers.splice(0);let s;this._pendingFn=void 0;try{s={status:"ok",value:await e()}}catch(e){s={status:"error",error:e}}for(const e of t)e(s);return s}},i=class e extends Error{constructor(t,s){super(t,{cause:s}),this.name="SyncError",Object.setPrototypeOf(this,e.prototype)}},r=class extends i{constructor(e){super(`[ArtifactContainer] Operation timed out: ${e}`)}},n=class extends i{constructor(e){super("[Serializer] The serializer has been marked as done!",e)}},a=class{_open=!1;_resolve;_promise;constructor(){this._promise=new Promise((e=>{this._resolve=e}))}open(){this._open||(this._open=!0,this._resolve())}async wait(e){if(this._open)return;if(null==e)return this._promise;let t;await Promise.race([this._promise.then((()=>clearTimeout(t))),new Promise(((s,i)=>{t=setTimeout((()=>i(new r("Latch timed out"))),e)}))])}isOpen(){return this._open}},o=class{_locked=!1;_capacity;_yieldMode;waiters=[];constructor(e){this._capacity=e?.capacity??1/0,this._yieldMode=e?.yieldMode??"macrotask"}async lock(e){if(!this._locked)return void(this._locked=!0);if(this.waiters.length>=this._capacity)throw new Error(`Mutex queue is full (capacity: ${this._capacity})`);let t;const s=new Promise((e=>t=e));if(this.waiters.push(t),null==e)return void await s;let i;await Promise.race([s.then((()=>clearTimeout(i))),new Promise(((s,n)=>{i=setTimeout((()=>{const e=this.waiters.indexOf(t);-1!==e&&this.waiters.splice(e,1),n(new r("Mutex lock timed out"))}),e)}))])}tryLock(){return!this._locked&&(this._locked=!0,!0)}unlock(){if(!this._locked)throw new Error("Mutex is not locked");const e=this.waiters.shift();e?"microtask"===this._yieldMode?queueMicrotask(e):setTimeout(e,0):this._locked=!1}locked(){return this._locked}pending(){return this.waiters.length}},c=class{mutex;_done=!1;_lastValue=null;_lastError=void 0;_hasRun=!1;constructor(e){this.mutex=new o({capacity:e?.capacity??1e3,yieldMode:e?.yieldMode??"macrotask"})}async do(e,t){if(this._done)return{value:null,error:new n};try{await this.mutex.lock(t)}catch(e){return{value:null,error:e}}let s,i=null;try{if(this._done)throw new n;i=await e(),this._lastValue=i,this._lastError=void 0,this._hasRun=!0}catch(e){s=e,this._lastError=e,this._hasRun=!0}finally{this.mutex.unlock()}return{value:i,error:s}}peek(){return{value:this._lastValue,error:this._lastError}}hasRun(){return this._hasRun}close(){this._done=!0}pending(){return this.mutex.pending()}running(){return this.mutex.locked()}},d=Symbol.for("delete"),h=e=>Array.isArray(e)?[...e]:{...e};function l(e){const t=e?.deleteMarker||d;function s(e){if(null==e)return e;if(Array.isArray(e))return e.filter((e=>e!==t)).map((e=>"object"!=typeof e||null===e||Array.isArray(e)?e:s(e)));if("object"==typeof e){const i={};for(const[r,n]of Object.entries(e))if(n!==t)if("object"==typeof n&&null!==n){const e=s(n);void 0!==e&&(i[r]=e)}else i[r]=n;return i}return e===t?void 0:e}return function(e,i){if("object"!=typeof e||null===e)return"object"==typeof i&&null!==i?s(i):i===t?{}:i;if("object"!=typeof i||null===i)return e;const r=h(e),n=[{target:r,source:i}];for(;n.length>0;){const{target:e,source:s}=n.pop();for(const i of Object.keys(s)){const r=s[i];if(r!==t)if(Array.isArray(r))e[i]=r;else if("object"==typeof r&&null!==r){const t=i in e&&"object"==typeof e[i]&&null!==e[i]?e[i]:{};e[i]=h(t),n.push({target:e[i],source:r})}else e[i]=r;else delete e[i]}}return r}}var u=l(),p=["map","filter","reduce","forEach","find","findIndex","some","every","includes","flatMap","flat","slice","splice"],m=class{reactiveSelectors=new Map;pathBasedCache=new Map;dependencyMap=new Map;getState;eventBus;unsubscribeFromStore;constructor(e,t){this.getState=e,this.eventBus=t,this.unsubscribeFromStore=this.eventBus.subscribe("update:complete",this.handleStoreUpdate)}handleStoreUpdate=e=>{const t=new Set;for(const s of e.deltas){const e=s.path;for(const[s,i]of this.dependencyMap)if(s===e||s.startsWith(e+".")||e.startsWith(s+"."))for(const e of i)t.add(e)}for(const e of t){const t=this.reactiveSelectors.get(e);t&&this.evaluateEntry(t)}};evaluateEntry(e){let t;try{t=e.selector(this.getState())}catch{t=void 0}if(t!==e.lastResult){e.lastResult=t;for(const s of e.subscribers)s(t);this.eventBus.emit({name:"selector:changed",payload:{selectorId:e.id,newResult:t,timestamp:performance.now()}})}}createReactiveSelector(e){const t=function(e,t="."){const s=new Set,i=new Map,r=(e="")=>{if(i.has(e))return i.get(e);const n=new Proxy((()=>{}),{get:(i,n)=>{if("symbol"==typeof n||"then"===n)return;if("valueOf"===n||"toString"===n)throw new Error("Cannot perform logic, arithmetic, or string operations inside a selector.");if(p.includes(n))throw new Error(`Array method .${n}() is not allowed in selectors.`);const a=e?`${e}${t}${n}`:n;return e&&s.delete(e),s.add(a),r(a)},has:()=>{throw new Error("The 'in' operator is not allowed in selectors.")},apply:()=>{throw new Error("Selectors cannot call functions or methods.")}});return i.set(e,n),n};try{e(r())}catch(e){throw new Error(`Selector failed during path analysis. Selectors must be simple property accessors only. Error: ${e instanceof Error?e.message:String(e)}`)}return Array.from(s)}(e),s=[...t].sort().join("|"),i=this.pathBasedCache.get(s);if(i)return void 0!==i.cleanupTimer&&(clearTimeout(i.cleanupTimer),i.cleanupTimer=void 0),i.reactiveSelectorInstance;const r=`sel-${Math.random().toString(36).slice(2,9)}`,n={id:r,selector:e,lastResult:e(this.getState()),accessedPaths:t,subscribers:new Set,count:0,cleanupTimer:void 0,pathCacheKey:s,reactiveSelectorInstance:null};for(const e of t)this.dependencyMap.has(e)||this.dependencyMap.set(e,new Set),this.dependencyMap.get(e).add(r);const a={id:r,get:()=>{try{return n.selector(this.getState())}catch{return}},subscribe:e=>(void 0!==n.cleanupTimer&&(clearTimeout(n.cleanupTimer),n.cleanupTimer=void 0),n.subscribers.add(e),n.count++,()=>{n.subscribers.delete(e),n.count--,0===n.count&&(n.cleanupTimer=setTimeout((()=>{0===n.count&&this.evictEntry(n)}),0))})};return n.reactiveSelectorInstance=a,this.reactiveSelectors.set(r,n),this.pathBasedCache.set(s,n),this.eventBus.emit({name:"selector:accessed",payload:{selectorId:r,accessedPaths:t,duration:0,timestamp:performance.now()}}),a}evictEntry(e){for(const t of e.accessedPaths){const s=this.dependencyMap.get(t);s&&(s.delete(e.id),0===s.size&&this.dependencyMap.delete(t))}this.reactiveSelectors.delete(e.id),this.pathBasedCache.delete(e.pathCacheKey)}dispose(){this.unsubscribeFromStore(),this.reactiveSelectors.clear(),this.dependencyMap.clear(),this.pathBasedCache.clear()}};function g(e,t){if(e===t)return!0;if(e&&t&&"object"==typeof e&&"object"==typeof t){if(e.constructor!==t.constructor)return!1;let s,i;if(Array.isArray(e)){if(s=e.length,s!=t.length)return!1;for(i=s;i-- >0;)if(!g(e[i],t[i]))return!1;return!0}const[r,n]=[Object.keys(e),Object.keys(t)];if(s=r.length,s!==n.length)return!1;for(i=s;i-- >0;){const s=r[i];if(!Object.prototype.hasOwnProperty.call(t,s)||!g(e[s],t[s]))return!1}return!0}return e!=e&&t!=t}function y(e){const t=e?.deleteMarker||d;return function(e,s){const i=[],r=[{pathStr:"",orig:e||{},part:s||{}}];for(;r.length>0;){const{pathStr:e,orig:s,part:n}=r.pop();if(null!=n&&!g(s,n))if("object"!=typeof n||Array.isArray(n))e&&i.push({path:e,oldValue:s,newValue:n});else for(const a of Object.keys(n)){const o=e?e+"."+a:a,c=n[a],d=s&&"object"==typeof s?s[a]:void 0;c!==t?"object"==typeof c&&null!==c?r.push({pathStr:o,orig:d,part:c}):g(d,c)||i.push({path:o,oldValue:d,newValue:c}):void 0!==d&&i.push({path:o,oldValue:d,newValue:void 0})}}return i}}function f(e){const t=e?.deleteMarker||d;return function(e){const s=new Set,i=[{obj:e,currentPath:""}];for(;i.length>0;){const{obj:e,currentPath:r}=i.pop();if(null!=e&&"object"==typeof e&&!Array.isArray(e))for(const n of Object.keys(e)){const a=r?`${r}.${n}`:n;s.add(a);const o=e[n];"object"!=typeof o||null===o||Array.isArray(o)||o===t||i.push({obj:o,currentPath:a})}}return Array.from(s)}}var w=y(),v=f(),b=class{constructor(e,t,s){this.updateBus=t,this.diff=s,this.cache=structuredClone(e)}cache;get(e){return e?structuredClone(this.cache):this.cache}applyChanges(e,t=!1,s=!1,i=[]){if(t)return this.cache=s?structuredClone(e):e,this.notifyListeners([]),[];0===i.length&&(i=[e]);const r=this.get(!1),n=new Map;for(let e=0;e<i.length;e++){const t=i[e],s=this.diff(r,t);for(let e=0;e<s.length;e++){const t=s[e];n.set(t.path,t)}}const a=n.size?[...n.values()]:[];if(a.length>0){this.cache=s?structuredClone(e):e;const t=new Set;for(let e=0;e<a.length;e++){let s=a[e].path;for(;s&&!t.has(s);){t.add(s);const e=s.lastIndexOf(".");if(e<0)break;s=s.slice(0,e)}}this.notifyListeners(t)}return a}notifyListeners(e){for(const t of e)this.updateBus.emit({name:"update",payload:t})}},S=class{constructor(e,t,s){this.eventBus=e,this.executionState=t,this.merge=s}middleware=[];blockingMiddleware=[];async executeBlocking(e,t){for(const{fn:s,name:i,id:r}of this.blockingMiddleware){const n={id:r,name:i,startTime:performance.now()};this.executionState.runningMiddleware={id:r,name:i,startTime:performance.now()},this.emitMiddlewareLifecycle("start",{id:r,name:i,type:"blocking"});try{const a=await Promise.resolve(s(e,t));if(n.endTime=performance.now(),n.duration=n.endTime-n.startTime,!1===a)return n.blocked=!0,this.emitMiddlewareLifecycle("blocked",{id:r,name:i,duration:n.duration}),this.emit(this.eventBus,{name:"middleware:executed",payload:n}),{blocked:!0};this.emitMiddlewareLifecycle("complete",{id:r,name:i,type:"blocking",duration:n.duration}),this.emit(this.eventBus,{name:"middleware:executed",payload:{...n,blocked:!1}})}catch(e){return n.endTime=performance.now(),n.duration=n.endTime-n.startTime,n.error=e instanceof Error?e:new Error(String(e)),n.blocked=!0,this.emitMiddlewareError(r,i,n.error,n.duration),this.emit(this.eventBus,{name:"middleware:executed",payload:n}),{blocked:!0,error:n.error}}finally{this.executionState.runningMiddleware=null}}return{blocked:!1}}async executeTransform(e,t){let s=e,i=t;for(const{fn:e,name:r,id:n}of this.middleware){const a={id:n,name:r,startTime:performance.now()};this.executionState.runningMiddleware={id:n,name:r,startTime:performance.now()},this.emitMiddlewareLifecycle("start",{id:n,name:r,type:"transform"});try{const o=await Promise.resolve(e(s,t));a.endTime=performance.now(),a.duration=a.endTime-a.startTime,a.blocked=!1,o&&"object"==typeof o&&(s=this.merge(s,o),i=this.merge(i,o)),this.emit(this.eventBus,{name:"middleware:executed",payload:a}),this.emitMiddlewareLifecycle("complete",{id:n,name:r,type:"transform",duration:a.duration})}catch(e){a.endTime=performance.now(),a.duration=a.endTime-a.startTime,a.error=e instanceof Error?e:new Error(String(e)),a.blocked=!1,this.emit(this.eventBus,{name:"middleware:executed",payload:a}),this.emitMiddlewareError(n,r,a.error,a.duration),console.error(`Middleware ${r} error:`,e)}finally{this.executionState.runningMiddleware=null}}return i}addMiddleware(e,t="unnamed-middleware"){const s=this.generateId();return this.middleware.push({fn:e,name:t,id:s}),this.updateExecutionState(),s}addBlockingMiddleware(e,t="unnamed-blocking-middleware"){const s=this.generateId();return this.blockingMiddleware.push({fn:e,name:t,id:s}),this.updateExecutionState(),s}removeMiddleware(e){const t=this.middleware.length+this.blockingMiddleware.length;return this.middleware=this.middleware.filter((t=>t.id!==e)),this.blockingMiddleware=this.blockingMiddleware.filter((t=>t.id!==e)),this.updateExecutionState(),this.middleware.length+this.blockingMiddleware.length<t}updateExecutionState(){this.executionState.middlewares=[...this.middleware.map((e=>e.name)),...this.blockingMiddleware.map((e=>e.name))]}emitMiddlewareLifecycle(e,t){this.emit(this.eventBus,{name:`middleware:${e}`,payload:{...t,timestamp:Date.now()}})}emitMiddlewareError(e,t,s,i){this.emit(this.eventBus,{name:"middleware:error",payload:{id:e,name:t,error:s,duration:i,timestamp:Date.now()}})}generateId(){return crypto.randomUUID?crypto.randomUUID():`${Date.now()}-${Math.random().toString(36).substring(2,15)}`}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},x=class{constructor(e,t,s,i){this.eventBus=e,this.coreState=t,this.instanceID=s,this.maxRetries=i?.maxRetries??3,this.retryDelay=i?.retryDelay??1e3}persistence;instanceID;persistenceReady=!1;backgroundQueue=[];isProcessingQueue=!1;maxRetries=3;retryDelay=1e3;queueProcessor;pendingRetries=new Set;async initialize(e){e?await this.setPersistence(e):this.setPersistenceReady()}isReady(){return this.persistenceReady}handleStateChange(e,t){if(!this.persistence||0===e.length)return;const s={id:`${Date.now()}-${Math.random().toString(36).slice(2,11)}`,state:structuredClone(t),changedPaths:[...e],timestamp:Date.now(),retries:0};this.backgroundQueue.push(s),this.scheduleQueueProcessing(),this.emit(this.eventBus,{name:"persistence:queued",payload:{taskId:s.id,changedPaths:e,queueSize:this.backgroundQueue.length,timestamp:s.timestamp}})}getQueueStatus(){return{queueSize:this.backgroundQueue.length,isProcessing:this.isProcessingQueue,pendingRetries:this.pendingRetries.size,oldestTask:this.backgroundQueue[0]?.timestamp}}async flush(){this.isProcessingQueue&&await new Promise((e=>{const t=()=>{this.isProcessingQueue?setTimeout(t,10):e()};t()})),await this.processQueue()}discardQueue(){const e=this.backgroundQueue.length+this.pendingRetries.size;this.backgroundQueue=[],this.pendingRetries.clear(),this.queueProcessor&&(clearTimeout(this.queueProcessor),this.queueProcessor=void 0),this.emit(this.eventBus,{name:"persistence:queue_cleared",payload:{clearedTasks:e,timestamp:Date.now()}})}scheduleQueueProcessing(){this.queueProcessor||this.isProcessingQueue||(this.queueProcessor=setTimeout((()=>{this.processQueue().catch((e=>{console.error("Queue processing failed:",e)}))}),10))}async processQueue(){if(!this.isProcessingQueue&&0!==this.backgroundQueue.length){this.isProcessingQueue=!0,this.queueProcessor=void 0;try{for(;this.backgroundQueue.length>0;){const e=this.backgroundQueue.shift();await this.processTask(e)}}finally{this.isProcessingQueue=!1}}}async processTask(e){try{await this.persistence.set(this.instanceID,e.state)?this.emit(this.eventBus,{name:"persistence:success",payload:{taskId:e.id,changedPaths:e.changedPaths,duration:Date.now()-e.timestamp,timestamp:Date.now()}}):await this.handleTaskFailure(e,new Error("Persistence returned false"))}catch(t){await this.handleTaskFailure(e,t)}}async handleTaskFailure(e,t){if(e.retries++,e.retries<=this.maxRetries){const s=this.retryDelay*Math.pow(2,e.retries-1);this.emit(this.eventBus,{name:"persistence:retry",payload:{taskId:e.id,attempt:e.retries,maxRetries:this.maxRetries,nextRetryIn:s,error:t,timestamp:Date.now()}}),this.pendingRetries.add(e.id),setTimeout((()=>{this.pendingRetries.has(e.id)&&(this.pendingRetries.delete(e.id),this.backgroundQueue.unshift(e),this.scheduleQueueProcessing())}),s)}else this.emit(this.eventBus,{name:"persistence:failed",payload:{taskId:e.id,changedPaths:e.changedPaths,attempts:e.retries,error:t,timestamp:Date.now()}})}setPersistenceReady(){this.persistenceReady=!0,this.emit(this.eventBus,{name:"persistence:ready",payload:{timestamp:Date.now()}})}async setPersistence(e){this.persistence=e;try{const e=await this.persistence.get();e&&this.coreState.applyChanges(e)}catch(e){console.error("Failed to initialize persistence:",e),this.emit(this.eventBus,{name:"persistence:init_error",payload:{error:e,timestamp:Date.now()}})}finally{this.setPersistenceReady()}this.persistence.subscribe(this.instanceID,(async e=>{const t=this.coreState.applyChanges(e);t.length>0&&this.emit(this.eventBus,{name:"update:complete",payload:{changedPaths:t,source:"external",timestamp:Date.now()}})}))}dispose(){this.discardQueue(),this.isProcessingQueue=!1,this.persistenceReady=!1}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},T=class{constructor(e,t,s){this.eventBus=e,this.coreState=t,this.executionState=s}async execute(e){const t=this.coreState.get(!0);this.executionState.transactionActive=!0,this.emit(this.eventBus,{name:"transaction:start",payload:{timestamp:Date.now()}});try{const t=await Promise.resolve(e());return this.emit(this.eventBus,{name:"transaction:complete",payload:{timestamp:Date.now()}}),this.executionState.transactionActive=!1,t}catch(e){throw this.coreState.applyChanges(t,!0,!1),this.emit(this.eventBus,{name:"transaction:error",payload:{error:e instanceof Error?e:new Error(String(e)),timestamp:Date.now()}}),this.executionState.transactionActive=!1,e}}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},_=class{updateCount=0;listenerExecutions=0;averageUpdateTime=0;largestUpdateSize=0;mostActiveListenerPaths=[];totalUpdates=0;blockedUpdates=0;averageUpdateDuration=0;middlewareExecutions=0;transactionCount=0;totalEventsFired=0;totalActionsDispatched=0;totalActionsSucceeded=0;totalActionsFailed=0;averageActionDuration=0;updateTimes=[];actionTimes=[];pathExecutionCounts=new Map;constructor(e){this.setupEventListeners(e)}getMetrics(){return{updateCount:this.updateCount,listenerExecutions:this.listenerExecutions,averageUpdateTime:this.averageUpdateTime,largestUpdateSize:this.largestUpdateSize,mostActiveListenerPaths:[...this.mostActiveListenerPaths],totalUpdates:this.totalUpdates,blockedUpdates:this.blockedUpdates,averageUpdateDuration:this.averageUpdateDuration,middlewareExecutions:this.middlewareExecutions,transactionCount:this.transactionCount,totalEventsFired:this.totalEventsFired,totalActionsDispatched:this.totalActionsDispatched,totalActionsSucceeded:this.totalActionsSucceeded,totalActionsFailed:this.totalActionsFailed,averageActionDuration:this.averageActionDuration}}setupEventListeners(e){const t=e.emit;e.emit=s=>(this.totalEventsFired++,t.call(e,s)),e.subscribe("update:complete",(e=>{if(this.totalUpdates++,e.blocked)this.blockedUpdates++;else{if(e.duration){this.updateTimes.push(e.duration),this.updateTimes.length>100&&this.updateTimes.shift();const t=this.updateTimes.reduce(((e,t)=>e+t),0)/this.updateTimes.length;this.averageUpdateTime=t,this.averageUpdateDuration=t}e.deltas?.length&&(this.updateCount++,this.largestUpdateSize=Math.max(this.largestUpdateSize,e.deltas.length),e.deltas.forEach((e=>{const t=this.pathExecutionCounts.get(e.path)||0;this.pathExecutionCounts.set(e.path,t+1)})),this.mostActiveListenerPaths=Array.from(this.pathExecutionCounts.entries()).sort((([,e],[,t])=>t-e)).slice(0,5).map((([e])=>e)))}})),e.subscribe("middleware:start",(()=>{this.middlewareExecutions++})),e.subscribe("transaction:start",(()=>{this.transactionCount++})),e.subscribe("action:start",(()=>{this.totalActionsDispatched++})),e.subscribe("action:complete",(e=>{this.totalActionsSucceeded++,e.duration&&(this.actionTimes.push(e.duration),this.actionTimes.length>100&&this.actionTimes.shift(),this.averageActionDuration=this.actionTimes.reduce(((e,t)=>e+t),0)/this.actionTimes.length)})),e.subscribe("action:error",(()=>{this.totalActionsFailed++}))}reset(){this.updateCount=0,this.listenerExecutions=0,this.averageUpdateTime=0,this.largestUpdateSize=0,this.mostActiveListenerPaths=[],this.totalUpdates=0,this.blockedUpdates=0,this.averageUpdateDuration=0,this.middlewareExecutions=0,this.transactionCount=0,this.totalEventsFired=0,this.totalActionsDispatched=0,this.totalActionsSucceeded=0,this.totalActionsFailed=0,this.averageActionDuration=0,this.updateTimes=[],this.actionTimes=[],this.pathExecutionCounts.clear()}getDetailedMetrics(){return{pathExecutionCounts:new Map(this.pathExecutionCounts),recentUpdateTimes:[...this.updateTimes],successRate:this.totalUpdates>0?(this.totalUpdates-this.blockedUpdates)/this.totalUpdates:1,averagePathsPerUpdate:this.updateCount>0?Array.from(this.pathExecutionCounts.values()).reduce(((e,t)=>e+t),0)/this.updateCount:0}}dispose(){this.reset()}},E=class extends Error{constructor(){super("Action Cancelled by Debounce"),this.name="ActionCancelledError"}},k=class extends Error{constructor({action:e}){super(`Unknown action: "${e}"`),this.name="UnknownActionError"}},M=()=>{},P={name:"UNDEFINED ACTION",status:()=>!1,subscribe:e=>()=>{}},A=class{constructor(e,t){this.eventBus=e,this.set=t}registrations=new Map;register(e){const i={action:{name:e.name,id:t.v4(),action:e.fn,debounce:e.debounce?{...e.debounce,condition:e.debounce.condition??(()=>!0)}:void 0},debouncer:e.debounce&&e.debounce.delay>0?new s({delay:e.debounce.delay}):void 0,previousArgs:void 0,running:!1,subscription:{count:0,listeners:new Set,unsubscribers:[],watcher:null}};return this.registrations.set(e.name,i),()=>{const t=this.registrations.get(e.name);t&&(t.debouncer?.cancel(),this.registrations.delete(e.name))}}async dispatch(e,...t){const s=this.registrations.get(e);if(!s)throw new k({action:e});const{action:i,debouncer:r}=s,{debounce:n}=i;if(!r||!n)return this.executeAction(s,t);const a=n.condition(s.previousArgs,t);if(s.previousArgs=t,!a)return this.executeAction(s,t);const o=await r.do((()=>this.executeAction(s,t)));if("cancelled"===o.status)throw new E;if("error"===o.status&&o.error)throw o.error;return o.value}async executeAction(e,t){const s=performance.now();e.running=!0,this.emit(this.eventBus,{name:"action:start",payload:{actionId:e.action.id,name:e.action.name,params:t||[],timestamp:s}});try{const i=await this.set((s=>e.action.action(s,...t)),{actionId:e.action.id}),r=performance.now();return e.running=!1,this.emit(this.eventBus,{name:"action:complete",payload:{actionId:e.action.id,name:e.action.name,params:t,startTime:s,endTime:r,duration:r-s,result:i}}),i}catch(i){const r=performance.now();throw e.running=!1,this.emit(this.eventBus,{name:"action:error",payload:{actionId:e.action.id,name:e.action.name,params:t,startTime:s,endTime:r,duration:r-s,error:i}}),i}}running(e){const t=this.registrations.get(e);return!!t&&t.running}subscribe(e,t){const s=this.registrations.get(e);return s?(s.subscription.reset=!1,s.subscription.count++,s.subscription.listeners.add(t),s.subscription.unsubscribers||(s.subscription.unsubscribers=[this.eventBus.subscribe("action:start",(t=>{t.name===e&&this.notifyStatusListeners(e)})),this.eventBus.subscribe("action:complete",(t=>{t.name===e&&this.notifyStatusListeners(e)})),this.eventBus.subscribe("action:error",(t=>{t.name===e&&this.notifyStatusListeners(e)}))]),()=>{s.subscription.listeners.delete(t),s.subscription.count--,0===s.subscription.count&&(s.subscription.reset=!0,queueMicrotask((()=>{s.subscription.reset&&(s.subscription.reset=!1,s.subscription.unsubscribers?.forEach((e=>e())),s.subscription.unsubscribers=[])})))}):M}watch(e){const t=this.registrations.get(e);return t?(t.subscription.watcher||(t.subscription.watcher={name:e,status:()=>this.running(e),subscribe:t=>this.subscribe(e,t)}),t.subscription.watcher):P}notifyStatusListeners(e){const t=this.registrations.get(e).subscription.listeners;t&&t.forEach((e=>e()))}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}};exports.ActionCancelledError=E,exports.ActionManager=A,exports.DELETE_SYMBOL=d,exports.ReactiveDataStore=class{coreState;middlewareEngine;persistenceHandler;transactionManager;metricsCollector;selectorManager;actions;updateSerializer=new c({yieldMode:"macrotask",capacity:1e3});readyLatch=new a;updateBus=e.createEventBus();eventBus;executionState;instanceID=t.v4();merge;diff;constructor(t,s,i=d,r){this.eventBus=e.createEventBus(),this.executionState={executing:!1,changes:null,pendingChanges:[],middlewares:[],runningMiddleware:null,transactionActive:!1},this.merge=l({deleteMarker:i}),this.diff=y({deleteMarker:i}),this.coreState=new b(t,this.updateBus,this.diff),this.middlewareEngine=new S(this.eventBus,this.executionState,this.merge),this.persistenceHandler=new x(this.eventBus,this.coreState,this.instanceID,{maxRetries:r?.persistenceMaxRetries,retryDelay:r?.persistenceRetryDelay}),this.transactionManager=new T(this.eventBus,this.coreState,this.executionState),this.metricsCollector=new _(this.eventBus),this.actions=new A(this.eventBus,this.set.bind(this)),this.persistenceHandler.initialize(s),this.setupPersistenceListener(),this.setupReadyLatch(),this.selectorManager=new m(this.get.bind(this),this.eventBus)}isReady(){return this.readyLatch.isOpen()}async ready(e){return this.readyLatch.wait(e)}state(){return this.executionState.executing=this.updateSerializer.running(),this.executionState}get(e){return this.coreState.get(e??!1)}select(e){return this.selectorManager.createReactiveSelector(e)}register(e){return this.actions.register(e)}async dispatch(e,...t){return this.actions.dispatch(e,...t)}async set(e,t={}){const s=await this.updateSerializer.do((()=>this._performUpdate(e,t)));if(s.error)throw s.error;return s.value}async _performUpdate(e,t){const s=performance.now();this.emit(this.eventBus,{name:"update:start",payload:{timestamp:s,actionId:t.actionId}});try{if(t.force){const i=this.get(!1),r="function"==typeof e?e(i):e;this.coreState.applyChanges(r,!0);const n=performance.now();return this.emit(this.eventBus,{name:"update:complete",payload:{deltas:[],duration:n-s,timestamp:Date.now(),actionId:t.actionId,newState:r}}),r}let i;const r=this.get(!1);if("function"==typeof e){const t=e(r);i=t instanceof Promise?await t:t}else i=e;const n=await this.middlewareEngine.executeBlocking(r,i);if(n.blocked)throw n.error||new Error("Update blocked by middleware");const a=this.merge(r,i),o=await this.middlewareEngine.executeTransform(a,i),c=this.merge(a,o),d=this.coreState.applyChanges(c,!1,!1,[i,o]),h=performance.now(),l=this.get(!1);return this.emit(this.eventBus,{name:"update:complete",payload:{deltas:d,duration:h-s,timestamp:Date.now(),actionId:t.actionId,newState:l}}),l}catch(e){throw this.emit(this.eventBus,{name:"update:complete",payload:{blocked:!0,error:e,timestamp:Date.now(),actionId:t.actionId,newState:this.get(!1)}}),e}finally{this.executionState.executing=!1,this.executionState.changes=null,this.executionState.runningMiddleware=null,this.executionState.pendingChanges=[]}}setupReadyLatch(){this.readyLatch.open(),this.persistenceHandler.isReady()?this.readyLatch.open():queueMicrotask((()=>{this.readyLatch.isOpen()||this.readyLatch.open()}))}setupPersistenceListener(){this.updateBus.subscribe("update",(e=>{e&&this.persistenceHandler.isReady()&&this.persistenceHandler.handleStateChange([e],this.get(!1))}))}watch(e,t){const s=Array.isArray(e)?e:[e],i=""===e||0===s.length;return this.updateBus.subscribe("update",(e=>{(i||s.includes(e))&&(t(this.get(!1)),this.metricsCollector.listenerExecutions++)}))}watchAction(e){return this.actions.watch(e)}debouncedSetter(e){const t=new s({delay:e.delay,leading:e.leading});return(e,s={})=>{t.fire((()=>this.set(e,s)))}}subscribe(e,t){return this.watch(e,t)}id(){return this.instanceID}async transaction(e){return this.transactionManager.execute(e)}use(e){const t=(e.block?this.middlewareEngine.addBlockingMiddleware:this.middlewareEngine.addMiddleware).bind(this.middlewareEngine)(e.action,e.name);return()=>this.middlewareEngine.removeMiddleware(t)}metrics(){return this.metricsCollector.getMetrics()}on(e,t){return this.eventBus.subscribe(e,t)}onStoreEvent(e,t){return this.eventBus.subscribe(e,t)}getPersistenceStatus(){return this.persistenceHandler.getQueueStatus()}async flushPersistence(){return this.persistenceHandler.flush()}discardPersistenceQueue(){this.persistenceHandler.discardQueue()}dispose(){this.updateSerializer.close(),this.persistenceHandler.dispose(),this.metricsCollector.dispose?.(),this.selectorManager.dispose?.()}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},exports.StoreObserver=class{store;eventHistory=[];stateHistory=[];unsubscribers=[];isTimeTraveling=!1;devTools=null;middlewareExecutions=[];activeTransactionCount=0;activeBatches=new Set;maxEvents;maxStateHistory;enableConsoleLogging;isSilent;logEvents;performanceThresholds;constructor(e,t={}){this.store=e,this.maxEvents=t.maxEvents??500,this.maxStateHistory=t.maxStateHistory??20,this.enableConsoleLogging=t.enableConsoleLogging??!1,this.isSilent=t.silent??!1,this.logEvents={updates:t.logEvents?.updates??!0,middleware:t.logEvents?.middleware??!0,transactions:t.logEvents?.transactions??!0,actions:t.logEvents?.actions??!0,selectors:t.logEvents?.selectors??!0},this.performanceThresholds={updateTime:t.performanceThresholds?.updateTime??50,middlewareTime:t.performanceThresholds?.middlewareTime??20},this.recordStateSnapshot([]),this.setupEventListeners()}_consoleLog(e,...t){this.isSilent||"function"==typeof console[e]&&console[e](...t)}setupEventListeners(){const e=["update:start","update:complete","middleware:start","middleware:complete","middleware:error","middleware:blocked","transaction:start","transaction:complete","transaction:error","middleware:executed","action:start","action:complete","action:error","selector:accessed"];for(const t of e){const e=t.startsWith("update")&&this.logEvents.updates||t.startsWith("middleware")&&this.logEvents.middleware||t.startsWith("transaction")&&this.logEvents.transactions||t.startsWith("action")&&this.logEvents.actions||t.startsWith("selector")&&this.logEvents.selectors;this.unsubscribers.push(this.store.on(t,(s=>{"update:complete"!==t||s.blocked||this.isTimeTraveling||this.recordStateSnapshot(s.deltas),"middleware:executed"===t?this.middlewareExecutions.push(s):"transaction:start"===t?this.activeTransactionCount++:"transaction:complete"!==t&&"transaction:error"!==t||(this.activeTransactionCount=Math.max(0,this.activeTransactionCount-1)),s.batchId&&(t.endsWith("start")?this.activeBatches.add(s.batchId):(t.endsWith("complete")||t.endsWith("error"))&&this.activeBatches.delete(s.batchId)),this.recordEvent(t,s),this.enableConsoleLogging&&e&&this._log(t,s),this._checkPerformance(t,s)})))}}recordStateSnapshot(e){const t={state:this.store.get(!0),timestamp:Date.now(),deltas:e};this.stateHistory.unshift(t),this.stateHistory.length>this.maxStateHistory&&this.stateHistory.pop()}recordEvent(e,t){const s={type:e,timestamp:Date.now(),data:structuredClone(t)};this.eventHistory.unshift(s),this.eventHistory.length>this.maxEvents&&this.eventHistory.pop()}getEventHistory(){return structuredClone(this.eventHistory)}getStateHistory(){return structuredClone(this.stateHistory)}getMiddlewareExecutions(){return this.middlewareExecutions}getTransactionStatus(){return{activeTransactions:this.activeTransactionCount,activeBatches:Array.from(this.activeBatches)}}createLoggingMiddleware(e={}){const{logLevel:t="debug",logUpdates:s=!0}=e;return(e,i)=>{if(s){(console[t]||console.log)("State Update:",i)}return i}}createValidationMiddleware(e){return(t,s)=>{const i=e(t,s);return"boolean"==typeof i?i:(!i.valid&&i.reason&&this._consoleLog("warn","Validation failed:",i.reason),i.valid)}}getRecentChanges(e=5){const t=[],s=Math.min(e,this.stateHistory.length);for(let e=0;e<s;e++){const s=this.stateHistory[e];if(!s.deltas||0===s.deltas.length)continue;const i={},r={},n=(e,t,s)=>{t.reduce(((e,i,r)=>(r===t.length-1?e[i]=s:e[i]=e[i]??{},e[i])),e)};for(const e of s.deltas){const t=e.path.split(".");n(i,t,e.oldValue),n(r,t,e.newValue)}t.push({timestamp:s.timestamp,changedPaths:s.deltas.map((e=>e.path)),from:i,to:r})}return t}clearHistory(){this.eventHistory=[],this.stateHistory.length>0&&(this.stateHistory=[this.stateHistory[0]])}getHistoryForAction(e){return this.eventHistory.filter((t=>t.data?.actionId===e))}async replay(e){const t=this.eventHistory.filter((e=>"update:start"===e.type))[e];t?.data.update?(this._consoleLog("log",`Replaying event at index ${e}:`,t),await this.store.set(t.data.update,{force:!0})):this._consoleLog("warn",`No replayable event found at index ${e}.`)}createTimeTravel(){let e=0,t=[];const s=this.store.on("update:complete",(s=>{this.isTimeTraveling||s.blocked||(t=[],e=0)}));this.unsubscribers.push(s);const i=()=>this.stateHistory.length,r=()=>e<i()-1,n=()=>t.length>0;return{canUndo:r,canRedo:n,undo:async()=>{if(!r())return;t.unshift(this.stateHistory[e]),e++;const s=this.stateHistory[e].state;this.isTimeTraveling=!0,await this.store.set({...s},{force:!0}),this.isTimeTraveling=!1},redo:async()=>{if(!n())return;const s=t.shift();e--,this.isTimeTraveling=!0,await this.store.set({...s.state},{force:!0}),this.isTimeTraveling=!1},length:i,clear:()=>{t=[],e=0}}}async saveSession(e){const t=this.store.id(),s={eventHistory:this.eventHistory,stateHistory:this.stateHistory};return Promise.resolve(e.set(t,s))}async loadSession(e){const t=await Promise.resolve(e.get());return!!t&&(this.eventHistory=t.eventHistory||[],this.stateHistory=t.stateHistory||[],this.stateHistory.length>0&&await this.store.set(this.stateHistory[0].state,{force:!0}),!0)}exportSession(){const e={eventHistory:this.eventHistory,stateHistory:this.stateHistory},t=new Blob([JSON.stringify(e,null,2)],{type:"application/json"}),s=URL.createObjectURL(t),i=document.createElement("a");i.href=s,i.download=`store-observer-session-${(new Date).toISOString()}.json`,i.click(),URL.revokeObjectURL(s)}importSession(e){return new Promise(((t,s)=>{const i=new FileReader;i.onload=async e=>{try{const s=JSON.parse(e.target?.result);this.eventHistory=s.eventHistory||[],this.stateHistory=s.stateHistory||[],this.stateHistory.length>0&&await this.store.set(this.stateHistory[0].state,{force:!0}),t()}catch(e){s(e)}},i.onerror=e=>s(e),i.readAsText(e)}))}disconnect(){this.unsubscribers.forEach((e=>e())),this.unsubscribers=[],this.devTools?.disconnect(),this.clearHistory()}_log(e,t){const s=new Date(t.timestamp||Date.now()).toISOString().split("T")[1].replace("Z","");if("update:start"===e)this._consoleLog("group",`%c⚡ Store Update Started [${s}]`,"color: #4a6da7");else if("update:complete"===e){if(t.blocked)this._consoleLog("warn",`%c✋ Update Blocked [${s}]`,"color: #bf8c0a",t.error);else{const e=t.deltas||[];e.length>0&&(this._consoleLog("log",`%c✅ Update Complete [${s}] - ${e.length} paths changed in ${t.duration?.toFixed(2)}ms`,"color: #2a9d8f"),this._consoleLog("table",e.map((e=>({path:e.path,oldValue:e.oldValue,newValue:e.newValue})))))}this._consoleLog("groupEnd")}else"middleware:start"===e?this._consoleLog("debug",`%c◀ Middleware "${t.name}" started [${s}] (${t.type})`,"color: #8c8c8c"):"middleware:complete"===e?this._consoleLog("debug",`%c▶ Middleware "${t.name}" completed [${s}] in ${t.duration?.toFixed(2)}ms`,"color: #7c9c7c"):"middleware:error"===e?this._consoleLog("error",`%c❌ Middleware "${t.name}" error [${s}]:`,"color: #e63946",t.error):"middleware:blocked"===e?this._consoleLog("warn",`%c🛑 Middleware "${t.name}" blocked update [${s}]`,"color: #e76f51"):"transaction:start"===e?this._consoleLog("group",`%c📦 Transaction Started [${s}]`,"color: #6d597a"):"transaction:complete"===e?(this._consoleLog("log",`%c📦 Transaction Complete [${s}]`,"color: #355070"),this._consoleLog("groupEnd")):"transaction:error"===e?(this._consoleLog("error",`%c📦 Transaction Error [${s}]:`,"color: #e56b6f",t.error),this._consoleLog("groupEnd")):"action:start"===e?this._consoleLog("group",`%c🚀 Action "${t.name}" Started [${s}]`,"color: #9b59b6",{params:t.params}):"action:complete"===e?(this._consoleLog("log",`%c✔️ Action "${t.name}" Complete [${s}] in ${t.duration?.toFixed(2)}ms`,"color: #2ecc71"),this._consoleLog("groupEnd")):"action:error"===e?(this._consoleLog("error",`%c🔥 Action "${t.name}" Error [${s}]:`,"color: #e74c3c",t.error),this._consoleLog("groupEnd")):"selector:accessed"===e&&this._consoleLog("debug",`%c👀 Selector Accessed [${s}] in ${t.duration?.toFixed(2)}ms`,"color: #f1c40f",{accessedPaths:t.accessedPaths,selectorId:t.selectorId})}_checkPerformance(e,t){this.enableConsoleLogging&&("update:complete"===e&&!t.blocked&&t.duration>this.performanceThresholds.updateTime&&this._consoleLog("warn",`%c⚠️ Slow update detected [${t.duration.toFixed(2)}ms]`,"color: #ff9f1c",{deltas:t.deltas,threshold:this.performanceThresholds.updateTime}),"middleware:complete"===e&&t.duration>this.performanceThresholds.middlewareTime&&this._consoleLog("warn",`%c⚠️ Slow middleware "${t.name}" [${t.duration.toFixed(2)}ms]`,"color: #ff9f1c",{threshold:this.performanceThresholds.middlewareTime}))}},exports.UnknownActionError=k,exports.createDerivePaths=f,exports.createDiff=y,exports.createMerge=l,exports.derivePaths=v,exports.diff=w,exports.merge=u,exports.shallowClone=h;
package/index.mjs CHANGED
@@ -1 +1 @@
1
- import{v4 as e}from"uuid";var t=class{_delay;_leading;_timer;_pendingFn;_pendingResolvers=[];_leadingFired=!1;constructor(e){this._delay=e?.delay??300,this._leading=e?.leading??!1}do(e){return new Promise((t=>{this._enqueue(e,t)}))}fire(e){this._enqueue(e,void 0)}_enqueue(e,t){if(this._pendingFn=e,t&&this._pendingResolvers.push(t),this._leading&&!this._leadingFired)return this._leadingFired=!0,this._fire(),clearTimeout(this._timer),void(this._timer=setTimeout((()=>{this._leadingFired=!1,void 0!==this._pendingFn&&this._fire()}),this._delay));clearTimeout(this._timer),this._timer=setTimeout((()=>{this._leadingFired=!1,this._fire()}),this._delay)}cancel(){clearTimeout(this._timer),this._timer=void 0,this._pendingFn=void 0,this._leadingFired=!1;const e=this._pendingResolvers.splice(0);for(const t of e)t({status:"cancelled"})}async flush(){return this._pendingFn?(clearTimeout(this._timer),this._timer=void 0,this._leadingFired=!1,this._fire()):null}pending(){return void 0!==this._pendingFn}async _fire(){const e=this._pendingFn,t=this._pendingResolvers.splice(0);let s;this._pendingFn=void 0;try{s={status:"ok",value:await e()}}catch(e){s={status:"error",error:e}}for(const e of t)e(s);return s}},s=class e extends Error{constructor(t,s){super(t,{cause:s}),this.name="SyncError",Object.setPrototypeOf(this,e.prototype)}},i=class extends s{constructor(e){super(`[ArtifactContainer] Operation timed out: ${e}`)}},r=class extends s{constructor(e){super("[Serializer] The serializer has been marked as done!",e)}},n=class{_open=!1;_resolve;_promise;constructor(){this._promise=new Promise((e=>{this._resolve=e}))}open(){this._open||(this._open=!0,this._resolve())}async wait(e){if(this._open)return;if(null==e)return this._promise;let t;await Promise.race([this._promise.then((()=>clearTimeout(t))),new Promise(((s,r)=>{t=setTimeout((()=>r(new i("Latch timed out"))),e)}))])}isOpen(){return this._open}},a=class{_locked=!1;_capacity;_yieldMode;waiters=[];constructor(e){this._capacity=e?.capacity??1/0,this._yieldMode=e?.yieldMode??"macrotask"}async lock(e){if(!this._locked)return void(this._locked=!0);if(this.waiters.length>=this._capacity)throw new Error(`Mutex queue is full (capacity: ${this._capacity})`);let t;const s=new Promise((e=>t=e));if(this.waiters.push(t),null==e)return void await s;let r;await Promise.race([s.then((()=>clearTimeout(r))),new Promise(((s,n)=>{r=setTimeout((()=>{const e=this.waiters.indexOf(t);-1!==e&&this.waiters.splice(e,1),n(new i("Mutex lock timed out"))}),e)}))])}tryLock(){return!this._locked&&(this._locked=!0,!0)}unlock(){if(!this._locked)throw new Error("Mutex is not locked");const e=this.waiters.shift();e?"microtask"===this._yieldMode?queueMicrotask(e):setTimeout(e,0):this._locked=!1}locked(){return this._locked}pending(){return this.waiters.length}},o=class{mutex;_done=!1;_lastValue=null;_lastError=void 0;_hasRun=!1;constructor(e){this.mutex=new a({capacity:e?.capacity??1e3,yieldMode:e?.yieldMode??"macrotask"})}async do(e,t){if(this._done)return{value:null,error:new r};try{await this.mutex.lock(t)}catch(e){return{value:null,error:e}}let s,i=null;try{if(this._done)throw new r;i=await e(),this._lastValue=i,this._lastError=void 0,this._hasRun=!0}catch(e){s=e,this._lastError=e,this._hasRun=!0}finally{this.mutex.unlock()}return{value:i,error:s}}peek(){return{value:this._lastValue,error:this._lastError}}hasRun(){return this._hasRun}close(){this._done=!0}pending(){return this.mutex.pending()}running(){return this.mutex.locked()}};function c(e){const s={},i=s.errorHandler??(e=>console.error("EventBus Error:",e)),r=null!=s.batch?.size,n=s.batch?.size,a=s.batch?.delay??16;if(r&&(n<=0||!Number.isFinite(n)))throw new Error(`EventBus: batch.size must be a positive finite number, got ${n}.`);if(r&&(a<0||!Number.isFinite(a)))throw new Error(`EventBus: batch.delay must be a non-negative finite number, got ${a}.`);const o=s.broadcast?.channel,c=null!=o&&o.length>0,d=new Map;let l=[];const h=r?new t({delay:a}):null;let u=0,p=0;const m=new Map,g=()=>{if(!c)return null;if("undefined"==typeof BroadcastChannel)return console.warn("EventBus: BroadcastChannel is not supported in this environment. Cross-tab notifications are disabled."),null;const e=new BroadcastChannel(o);return e.onmessage=e=>{const{name:t,payload:s}=e.data;w(t,s)},e};let y=g();const f=(e,t)=>{u++,p+=t,m.set(e,(m.get(e)??0)+1)},w=(e,t)=>{const s=d.get(e);if(s&&0!==s.size)for(const r of Array.from(s))try{r(t)}catch(s){const r=s instanceof Error?s:Object.assign(new Error(String(s)),{cause:s}),n=Object.assign(r,{eventName:e,payload:t});i(n)}},v=(e,t)=>{d.has(e)||d.set(e,new Set);const s=d.get(e);return s.add(t),()=>{s.delete(t),0===s.size&&d.delete(e)}},b=()=>{const e=l;l=[];for(const{name:t,payload:s}of e){const e=performance.now();w(t,s),f(t,performance.now()-e)}};return{subscribe:(e,t)=>v(e,t),once:(e,t)=>{let s;return s=v(e,(e=>{s(),t(e)})),s},emit:({name:e,payload:t})=>{if(r)return l.push({name:e,payload:t}),l.length>=n?(h.cancel(),void b()):(h.fire((()=>b())),void y?.postMessage({name:e,payload:t}));const s=performance.now();w(e,t),f(e,performance.now()-s),y?.postMessage({name:e,payload:t})},metrics:()=>({totalEvents:u,activeSubscriptions:Array.from(d.values()).reduce(((e,t)=>e+t.size),0),eventCounts:new Map(m),averageEmitDuration:u>0?p/u:0}),clear:()=>{h?.cancel(),l=[],d.clear(),u=0,p=0,m.clear(),y?.close(),y=g()}}}var d=Symbol.for("delete"),l=e=>Array.isArray(e)?[...e]:{...e};function h(e){const t=e?.deleteMarker||d;function s(e){if(null==e)return e;if(Array.isArray(e))return e.filter((e=>e!==t)).map((e=>"object"!=typeof e||null===e||Array.isArray(e)?e:s(e)));if("object"==typeof e){const i={};for(const[r,n]of Object.entries(e))if(n!==t)if("object"==typeof n&&null!==n){const e=s(n);void 0!==e&&(i[r]=e)}else i[r]=n;return i}return e===t?void 0:e}return function(e,i){if("object"!=typeof e||null===e)return"object"==typeof i&&null!==i?s(i):i===t?{}:i;if("object"!=typeof i||null===i)return e;const r=l(e),n=[{target:r,source:i}];for(;n.length>0;){const{target:e,source:s}=n.pop();for(const i of Object.keys(s)){const r=s[i];if(r!==t)if(Array.isArray(r))e[i]=r;else if("object"==typeof r&&null!==r){const t=i in e&&"object"==typeof e[i]&&null!==e[i]?e[i]:{};e[i]=l(t),n.push({target:e[i],source:r})}else e[i]=r;else delete e[i]}}return r}}var u=h(),p=["map","filter","reduce","forEach","find","findIndex","some","every","includes","flatMap","flat","slice","splice"],m=class{reactiveSelectors=new Map;pathBasedCache=new Map;dependencyMap=new Map;getState;eventBus;unsubscribeFromStore;constructor(e,t){this.getState=e,this.eventBus=t,this.unsubscribeFromStore=this.eventBus.subscribe("update:complete",this.handleStoreUpdate)}handleStoreUpdate=e=>{const t=new Set;for(const s of e.deltas){const e=s.path;for(const[s,i]of this.dependencyMap)if(s===e||s.startsWith(e+".")||e.startsWith(s+"."))for(const e of i)t.add(e)}for(const e of t){const t=this.reactiveSelectors.get(e);t&&this.evaluateEntry(t)}};evaluateEntry(e){let t;try{t=e.selector(this.getState())}catch{t=void 0}if(t!==e.lastResult){e.lastResult=t;for(const s of e.subscribers)s(t);this.eventBus.emit({name:"selector:changed",payload:{selectorId:e.id,newResult:t,timestamp:performance.now()}})}}createReactiveSelector(e){const t=function(e,t="."){const s=new Set,i=new Map,r=(e="")=>{if(i.has(e))return i.get(e);const n=new Proxy((()=>{}),{get:(i,n)=>{if("symbol"==typeof n||"then"===n)return;if("valueOf"===n||"toString"===n)throw new Error("Cannot perform logic, arithmetic, or string operations inside a selector.");if(p.includes(n))throw new Error(`Array method .${n}() is not allowed in selectors.`);const a=e?`${e}${t}${n}`:n;return e&&s.delete(e),s.add(a),r(a)},has:()=>{throw new Error("The 'in' operator is not allowed in selectors.")},apply:()=>{throw new Error("Selectors cannot call functions or methods.")}});return i.set(e,n),n};try{e(r())}catch(e){throw new Error(`Selector failed during path analysis. Selectors must be simple property accessors only. Error: ${e instanceof Error?e.message:String(e)}`)}return Array.from(s)}(e),s=[...t].sort().join("|"),i=this.pathBasedCache.get(s);if(i)return void 0!==i.cleanupTimer&&(clearTimeout(i.cleanupTimer),i.cleanupTimer=void 0),i.reactiveSelectorInstance;const r=`sel-${Math.random().toString(36).slice(2,9)}`,n={id:r,selector:e,lastResult:e(this.getState()),accessedPaths:t,subscribers:new Set,count:0,cleanupTimer:void 0,pathCacheKey:s,reactiveSelectorInstance:null};for(const e of t)this.dependencyMap.has(e)||this.dependencyMap.set(e,new Set),this.dependencyMap.get(e).add(r);const a={id:r,get:()=>{try{return n.selector(this.getState())}catch{return}},subscribe:e=>(void 0!==n.cleanupTimer&&(clearTimeout(n.cleanupTimer),n.cleanupTimer=void 0),n.subscribers.add(e),n.count++,()=>{n.subscribers.delete(e),n.count--,0===n.count&&(n.cleanupTimer=setTimeout((()=>{0===n.count&&this.evictEntry(n)}),0))})};return n.reactiveSelectorInstance=a,this.reactiveSelectors.set(r,n),this.pathBasedCache.set(s,n),this.eventBus.emit({name:"selector:accessed",payload:{selectorId:r,accessedPaths:t,duration:0,timestamp:performance.now()}}),a}evictEntry(e){for(const t of e.accessedPaths){const s=this.dependencyMap.get(t);s&&(s.delete(e.id),0===s.size&&this.dependencyMap.delete(t))}this.reactiveSelectors.delete(e.id),this.pathBasedCache.delete(e.pathCacheKey)}dispose(){this.unsubscribeFromStore(),this.reactiveSelectors.clear(),this.dependencyMap.clear(),this.pathBasedCache.clear()}};function g(e,t){if(e===t)return!0;if(e&&t&&"object"==typeof e&&"object"==typeof t){if(e.constructor!==t.constructor)return!1;let s,i;if(Array.isArray(e)){if(s=e.length,s!=t.length)return!1;for(i=s;i-- >0;)if(!g(e[i],t[i]))return!1;return!0}const[r,n]=[Object.keys(e),Object.keys(t)];if(s=r.length,s!==n.length)return!1;for(i=s;i-- >0;){const s=r[i];if(!Object.prototype.hasOwnProperty.call(t,s)||!g(e[s],t[s]))return!1}return!0}return e!=e&&t!=t}function y(e){const t=e?.deleteMarker||d;return function(e,s){const i=[],r=[{pathStr:"",orig:e||{},part:s||{}}];for(;r.length>0;){const{pathStr:e,orig:s,part:n}=r.pop();if(null!=n&&!g(s,n))if("object"!=typeof n||Array.isArray(n))e&&i.push({path:e,oldValue:s,newValue:n});else for(const a of Object.keys(n)){const o=e?e+"."+a:a,c=n[a],d=s&&"object"==typeof s?s[a]:void 0;c!==t?"object"==typeof c&&null!==c?r.push({pathStr:o,orig:d,part:c}):g(d,c)||i.push({path:o,oldValue:d,newValue:c}):void 0!==d&&i.push({path:o,oldValue:d,newValue:void 0})}}return i}}function f(e){const t=e?.deleteMarker||d;return function(e){const s=new Set,i=[{obj:e,currentPath:""}];for(;i.length>0;){const{obj:e,currentPath:r}=i.pop();if(null!=e&&"object"==typeof e&&!Array.isArray(e))for(const n of Object.keys(e)){const a=r?`${r}.${n}`:n;s.add(a);const o=e[n];"object"!=typeof o||null===o||Array.isArray(o)||o===t||i.push({obj:o,currentPath:a})}}return Array.from(s)}}var w=y(),v=f(),b=class{constructor(e,t,s){this.updateBus=t,this.diff=s,this.cache=structuredClone(e)}cache;get(e){return e?structuredClone(this.cache):this.cache}applyChanges(e,t=!1,s=!1,i=[]){if(t)return this.cache=s?structuredClone(e):e,this.notifyListeners([]),[];0===i.length&&(i=[e]);const r=this.get(!1),n=new Map;for(let e=0;e<i.length;e++){const t=i[e],s=this.diff(r,t);for(let e=0;e<s.length;e++){const t=s[e];n.set(t.path,t)}}const a=n.size?[...n.values()]:[];if(a.length>0){this.cache=s?structuredClone(e):e;const t=new Set;for(let e=0;e<a.length;e++){let s=a[e].path;for(;s&&!t.has(s);){t.add(s);const e=s.lastIndexOf(".");if(e<0)break;s=s.slice(0,e)}}this.notifyListeners(t)}return a}notifyListeners(e){for(const t of e)this.updateBus.emit({name:"update",payload:t})}},S=class{constructor(e,t,s){this.eventBus=e,this.executionState=t,this.merge=s}middleware=[];blockingMiddleware=[];async executeBlocking(e,t){for(const{fn:s,name:i,id:r}of this.blockingMiddleware){const n={id:r,name:i,startTime:performance.now()};this.executionState.runningMiddleware={id:r,name:i,startTime:performance.now()},this.emitMiddlewareLifecycle("start",{id:r,name:i,type:"blocking"});try{const a=await Promise.resolve(s(e,t));if(n.endTime=performance.now(),n.duration=n.endTime-n.startTime,!1===a)return n.blocked=!0,this.emitMiddlewareLifecycle("blocked",{id:r,name:i,duration:n.duration}),this.emit(this.eventBus,{name:"middleware:executed",payload:n}),{blocked:!0};this.emitMiddlewareLifecycle("complete",{id:r,name:i,type:"blocking",duration:n.duration}),this.emit(this.eventBus,{name:"middleware:executed",payload:{...n,blocked:!1}})}catch(e){return n.endTime=performance.now(),n.duration=n.endTime-n.startTime,n.error=e instanceof Error?e:new Error(String(e)),n.blocked=!0,this.emitMiddlewareError(r,i,n.error,n.duration),this.emit(this.eventBus,{name:"middleware:executed",payload:n}),{blocked:!0,error:n.error}}finally{this.executionState.runningMiddleware=null}}return{blocked:!1}}async executeTransform(e,t){let s=e,i=t;for(const{fn:e,name:r,id:n}of this.middleware){const a={id:n,name:r,startTime:performance.now()};this.executionState.runningMiddleware={id:n,name:r,startTime:performance.now()},this.emitMiddlewareLifecycle("start",{id:n,name:r,type:"transform"});try{const o=await Promise.resolve(e(s,t));a.endTime=performance.now(),a.duration=a.endTime-a.startTime,a.blocked=!1,o&&"object"==typeof o&&(s=this.merge(s,o),i=this.merge(i,o)),this.emit(this.eventBus,{name:"middleware:executed",payload:a}),this.emitMiddlewareLifecycle("complete",{id:n,name:r,type:"transform",duration:a.duration})}catch(e){a.endTime=performance.now(),a.duration=a.endTime-a.startTime,a.error=e instanceof Error?e:new Error(String(e)),a.blocked=!1,this.emit(this.eventBus,{name:"middleware:executed",payload:a}),this.emitMiddlewareError(n,r,a.error,a.duration),console.error(`Middleware ${r} error:`,e)}finally{this.executionState.runningMiddleware=null}}return i}addMiddleware(e,t="unnamed-middleware"){const s=this.generateId();return this.middleware.push({fn:e,name:t,id:s}),this.updateExecutionState(),s}addBlockingMiddleware(e,t="unnamed-blocking-middleware"){const s=this.generateId();return this.blockingMiddleware.push({fn:e,name:t,id:s}),this.updateExecutionState(),s}removeMiddleware(e){const t=this.middleware.length+this.blockingMiddleware.length;return this.middleware=this.middleware.filter((t=>t.id!==e)),this.blockingMiddleware=this.blockingMiddleware.filter((t=>t.id!==e)),this.updateExecutionState(),this.middleware.length+this.blockingMiddleware.length<t}updateExecutionState(){this.executionState.middlewares=[...this.middleware.map((e=>e.name)),...this.blockingMiddleware.map((e=>e.name))]}emitMiddlewareLifecycle(e,t){this.emit(this.eventBus,{name:`middleware:${e}`,payload:{...t,timestamp:Date.now()}})}emitMiddlewareError(e,t,s,i){this.emit(this.eventBus,{name:"middleware:error",payload:{id:e,name:t,error:s,duration:i,timestamp:Date.now()}})}generateId(){return crypto.randomUUID?crypto.randomUUID():`${Date.now()}-${Math.random().toString(36).substring(2,15)}`}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},T=class{constructor(e,t,s,i){this.eventBus=e,this.coreState=t,this.instanceID=s,this.maxRetries=i?.maxRetries??3,this.retryDelay=i?.retryDelay??1e3}persistence;instanceID;persistenceReady=!1;backgroundQueue=[];isProcessingQueue=!1;maxRetries=3;retryDelay=1e3;queueProcessor;pendingRetries=new Set;async initialize(e){e?await this.setPersistence(e):this.setPersistenceReady()}isReady(){return this.persistenceReady}handleStateChange(e,t){if(!this.persistence||0===e.length)return;const s={id:`${Date.now()}-${Math.random().toString(36).slice(2,11)}`,state:structuredClone(t),changedPaths:[...e],timestamp:Date.now(),retries:0};this.backgroundQueue.push(s),this.scheduleQueueProcessing(),this.emit(this.eventBus,{name:"persistence:queued",payload:{taskId:s.id,changedPaths:e,queueSize:this.backgroundQueue.length,timestamp:s.timestamp}})}getQueueStatus(){return{queueSize:this.backgroundQueue.length,isProcessing:this.isProcessingQueue,pendingRetries:this.pendingRetries.size,oldestTask:this.backgroundQueue[0]?.timestamp}}async flush(){this.isProcessingQueue&&await new Promise((e=>{const t=()=>{this.isProcessingQueue?setTimeout(t,10):e()};t()})),await this.processQueue()}discardQueue(){const e=this.backgroundQueue.length+this.pendingRetries.size;this.backgroundQueue=[],this.pendingRetries.clear(),this.queueProcessor&&(clearTimeout(this.queueProcessor),this.queueProcessor=void 0),this.emit(this.eventBus,{name:"persistence:queue_cleared",payload:{clearedTasks:e,timestamp:Date.now()}})}scheduleQueueProcessing(){this.queueProcessor||this.isProcessingQueue||(this.queueProcessor=setTimeout((()=>{this.processQueue().catch((e=>{console.error("Queue processing failed:",e)}))}),10))}async processQueue(){if(!this.isProcessingQueue&&0!==this.backgroundQueue.length){this.isProcessingQueue=!0,this.queueProcessor=void 0;try{for(;this.backgroundQueue.length>0;){const e=this.backgroundQueue.shift();await this.processTask(e)}}finally{this.isProcessingQueue=!1}}}async processTask(e){try{await this.persistence.set(this.instanceID,e.state)?this.emit(this.eventBus,{name:"persistence:success",payload:{taskId:e.id,changedPaths:e.changedPaths,duration:Date.now()-e.timestamp,timestamp:Date.now()}}):await this.handleTaskFailure(e,new Error("Persistence returned false"))}catch(t){await this.handleTaskFailure(e,t)}}async handleTaskFailure(e,t){if(e.retries++,e.retries<=this.maxRetries){const s=this.retryDelay*Math.pow(2,e.retries-1);this.emit(this.eventBus,{name:"persistence:retry",payload:{taskId:e.id,attempt:e.retries,maxRetries:this.maxRetries,nextRetryIn:s,error:t,timestamp:Date.now()}}),this.pendingRetries.add(e.id),setTimeout((()=>{this.pendingRetries.has(e.id)&&(this.pendingRetries.delete(e.id),this.backgroundQueue.unshift(e),this.scheduleQueueProcessing())}),s)}else this.emit(this.eventBus,{name:"persistence:failed",payload:{taskId:e.id,changedPaths:e.changedPaths,attempts:e.retries,error:t,timestamp:Date.now()}})}setPersistenceReady(){this.persistenceReady=!0,this.emit(this.eventBus,{name:"persistence:ready",payload:{timestamp:Date.now()}})}async setPersistence(e){this.persistence=e;try{const e=await this.persistence.get();e&&this.coreState.applyChanges(e)}catch(e){console.error("Failed to initialize persistence:",e),this.emit(this.eventBus,{name:"persistence:init_error",payload:{error:e,timestamp:Date.now()}})}finally{this.setPersistenceReady()}this.persistence.subscribe(this.instanceID,(async e=>{const t=this.coreState.applyChanges(e);t.length>0&&this.emit(this.eventBus,{name:"update:complete",payload:{changedPaths:t,source:"external",timestamp:Date.now()}})}))}dispose(){this.discardQueue(),this.isProcessingQueue=!1,this.persistenceReady=!1}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},E=class{constructor(e,t,s){this.eventBus=e,this.coreState=t,this.executionState=s}async execute(e){const t=this.coreState.get(!0);this.executionState.transactionActive=!0,this.emit(this.eventBus,{name:"transaction:start",payload:{timestamp:Date.now()}});try{const t=await Promise.resolve(e());return this.emit(this.eventBus,{name:"transaction:complete",payload:{timestamp:Date.now()}}),this.executionState.transactionActive=!1,t}catch(e){throw this.coreState.applyChanges(t,!0,!1),this.emit(this.eventBus,{name:"transaction:error",payload:{error:e instanceof Error?e:new Error(String(e)),timestamp:Date.now()}}),this.executionState.transactionActive=!1,e}}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},_=class{updateCount=0;listenerExecutions=0;averageUpdateTime=0;largestUpdateSize=0;mostActiveListenerPaths=[];totalUpdates=0;blockedUpdates=0;averageUpdateDuration=0;middlewareExecutions=0;transactionCount=0;totalEventsFired=0;totalActionsDispatched=0;totalActionsSucceeded=0;totalActionsFailed=0;averageActionDuration=0;updateTimes=[];actionTimes=[];pathExecutionCounts=new Map;constructor(e){this.setupEventListeners(e)}getMetrics(){return{updateCount:this.updateCount,listenerExecutions:this.listenerExecutions,averageUpdateTime:this.averageUpdateTime,largestUpdateSize:this.largestUpdateSize,mostActiveListenerPaths:[...this.mostActiveListenerPaths],totalUpdates:this.totalUpdates,blockedUpdates:this.blockedUpdates,averageUpdateDuration:this.averageUpdateDuration,middlewareExecutions:this.middlewareExecutions,transactionCount:this.transactionCount,totalEventsFired:this.totalEventsFired,totalActionsDispatched:this.totalActionsDispatched,totalActionsSucceeded:this.totalActionsSucceeded,totalActionsFailed:this.totalActionsFailed,averageActionDuration:this.averageActionDuration}}setupEventListeners(e){const t=e.emit;e.emit=s=>(this.totalEventsFired++,t.call(e,s)),e.subscribe("update:complete",(e=>{if(this.totalUpdates++,e.blocked)this.blockedUpdates++;else{if(e.duration){this.updateTimes.push(e.duration),this.updateTimes.length>100&&this.updateTimes.shift();const t=this.updateTimes.reduce(((e,t)=>e+t),0)/this.updateTimes.length;this.averageUpdateTime=t,this.averageUpdateDuration=t}e.deltas?.length&&(this.updateCount++,this.largestUpdateSize=Math.max(this.largestUpdateSize,e.deltas.length),e.deltas.forEach((e=>{const t=this.pathExecutionCounts.get(e.path)||0;this.pathExecutionCounts.set(e.path,t+1)})),this.mostActiveListenerPaths=Array.from(this.pathExecutionCounts.entries()).sort((([,e],[,t])=>t-e)).slice(0,5).map((([e])=>e)))}})),e.subscribe("middleware:start",(()=>{this.middlewareExecutions++})),e.subscribe("transaction:start",(()=>{this.transactionCount++})),e.subscribe("action:start",(()=>{this.totalActionsDispatched++})),e.subscribe("action:complete",(e=>{this.totalActionsSucceeded++,e.duration&&(this.actionTimes.push(e.duration),this.actionTimes.length>100&&this.actionTimes.shift(),this.averageActionDuration=this.actionTimes.reduce(((e,t)=>e+t),0)/this.actionTimes.length)})),e.subscribe("action:error",(()=>{this.totalActionsFailed++}))}reset(){this.updateCount=0,this.listenerExecutions=0,this.averageUpdateTime=0,this.largestUpdateSize=0,this.mostActiveListenerPaths=[],this.totalUpdates=0,this.blockedUpdates=0,this.averageUpdateDuration=0,this.middlewareExecutions=0,this.transactionCount=0,this.totalEventsFired=0,this.totalActionsDispatched=0,this.totalActionsSucceeded=0,this.totalActionsFailed=0,this.averageActionDuration=0,this.updateTimes=[],this.actionTimes=[],this.pathExecutionCounts.clear()}getDetailedMetrics(){return{pathExecutionCounts:new Map(this.pathExecutionCounts),recentUpdateTimes:[...this.updateTimes],successRate:this.totalUpdates>0?(this.totalUpdates-this.blockedUpdates)/this.totalUpdates:1,averagePathsPerUpdate:this.updateCount>0?Array.from(this.pathExecutionCounts.values()).reduce(((e,t)=>e+t),0)/this.updateCount:0}}dispose(){this.reset()}},x=class extends Error{constructor(){super("Action Cancelled by Debounce"),this.name="ActionCancelledError"}},k=class extends Error{constructor({action:e}){super(`Unknown action: "${e}"`),this.name="UnknownActionError"}},M=class{constructor(e,t){this.eventBus=e,this.set=t}registrations=new Map;register(s){const i={action:{name:s.name,id:e(),action:s.fn,debounce:s.debounce?{...s.debounce,condition:s.debounce.condition??(()=>!0)}:void 0},debouncer:s.debounce&&s.debounce.delay>0?new t({delay:s.debounce.delay}):void 0,previousArgs:void 0};return this.registrations.set(s.name,i),()=>{const e=this.registrations.get(s.name);e&&(e.debouncer?.cancel(),this.registrations.delete(s.name))}}async dispatch(e,...t){const s=this.registrations.get(e);if(!s)throw new k({action:e});const{action:i,debouncer:r}=s,{id:n,action:a,debounce:o}=i;if(!r||!o)return this.executeAction(e,n,a,t);const c=o.condition(s.previousArgs,t);if(s.previousArgs=t,!c)return this.executeAction(e,n,a,t);const d=await r.do((()=>this.executeAction(e,n,a,t)));if("cancelled"===d.status)throw new x;if("error"===d.status&&d.error)throw d.error;return d.value}async executeAction(e,t,s,i){const r=performance.now();this.emit(this.eventBus,{name:"action:start",payload:{actionId:t,name:e,params:i||[],timestamp:r}});try{const n=await this.set((e=>s(e,...i)),{actionId:t}),a=performance.now();return this.emit(this.eventBus,{name:"action:complete",payload:{actionId:t,name:e,params:i,startTime:r,endTime:a,duration:a-r,result:n}}),n}catch(s){const n=performance.now();throw this.emit(this.eventBus,{name:"action:error",payload:{actionId:t,name:e,params:i,startTime:r,endTime:n,duration:n-r,error:s}}),s}}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},P=class{coreState;middlewareEngine;persistenceHandler;transactionManager;metricsCollector;selectorManager;actions;updateSerializer=new o({yieldMode:"macrotask",capacity:1e3});readyLatch=new n;updateBus=c();eventBus;executionState;instanceID=e();merge;diff;constructor(e,t,s=d,i){this.eventBus=c(),this.executionState={executing:!1,changes:null,pendingChanges:[],middlewares:[],runningMiddleware:null,transactionActive:!1},this.merge=h({deleteMarker:s}),this.diff=y({deleteMarker:s}),this.coreState=new b(e,this.updateBus,this.diff),this.middlewareEngine=new S(this.eventBus,this.executionState,this.merge),this.persistenceHandler=new T(this.eventBus,this.coreState,this.instanceID,{maxRetries:i?.persistenceMaxRetries,retryDelay:i?.persistenceRetryDelay}),this.transactionManager=new E(this.eventBus,this.coreState,this.executionState),this.metricsCollector=new _(this.eventBus),this.actions=new M(this.eventBus,this.set.bind(this)),this.persistenceHandler.initialize(t),this.setupPersistenceListener(),this.setupReadyLatch(),this.selectorManager=new m(this.get.bind(this),this.eventBus)}isReady(){return this.readyLatch.isOpen()}async ready(e){return this.readyLatch.wait(e)}state(){return this.executionState.executing=this.updateSerializer.running(),this.executionState}get(e){return this.coreState.get(e??!1)}select(e){return this.selectorManager.createReactiveSelector(e)}register(e){return this.actions.register(e)}async dispatch(e,...t){return this.actions.dispatch(e,...t)}async set(e,t={}){const s=await this.updateSerializer.do((()=>this._performUpdate(e,t)));if(s.error)throw s.error;return s.value}async _performUpdate(e,t){const s=performance.now();this.emit(this.eventBus,{name:"update:start",payload:{timestamp:s,actionId:t.actionId}});try{if(t.force){const i=this.get(!1),r="function"==typeof e?e(i):e;this.coreState.applyChanges(r,!0);const n=performance.now();return this.emit(this.eventBus,{name:"update:complete",payload:{deltas:[],duration:n-s,timestamp:Date.now(),actionId:t.actionId,newState:r}}),r}let i;const r=this.get(!1);if("function"==typeof e){const t=e(r);i=t instanceof Promise?await t:t}else i=e;const n=await this.middlewareEngine.executeBlocking(r,i);if(n.blocked)throw n.error||new Error("Update blocked by middleware");const a=this.merge(r,i),o=await this.middlewareEngine.executeTransform(a,i),c=this.merge(a,o),d=this.coreState.applyChanges(c,!1,!1,[i,o]),l=performance.now(),h=this.get(!1);return this.emit(this.eventBus,{name:"update:complete",payload:{deltas:d,duration:l-s,timestamp:Date.now(),actionId:t.actionId,newState:h}}),h}catch(e){throw this.emit(this.eventBus,{name:"update:complete",payload:{blocked:!0,error:e,timestamp:Date.now(),actionId:t.actionId,newState:this.get(!1)}}),e}finally{this.executionState.executing=!1,this.executionState.changes=null,this.executionState.runningMiddleware=null,this.executionState.pendingChanges=[]}}setupReadyLatch(){this.readyLatch.open(),this.persistenceHandler.isReady()?this.readyLatch.open():queueMicrotask((()=>{this.readyLatch.isOpen()||this.readyLatch.open()}))}setupPersistenceListener(){this.updateBus.subscribe("update",(e=>{e&&this.persistenceHandler.isReady()&&this.persistenceHandler.handleStateChange([e],this.get(!1))}))}watch(e,t){const s=Array.isArray(e)?e:[e],i=""===e||0===s.length;return this.updateBus.subscribe("update",(e=>{(i||s.includes(e))&&(t(this.get(!1)),this.metricsCollector.listenerExecutions++)}))}debouncedSetter(e){const s=new t({delay:e.delay,leading:e.leading});return(e,t={})=>{s.fire((()=>this.set(e,t)))}}subscribe(e,t){return this.watch(e,t)}id(){return this.instanceID}async transaction(e){return this.transactionManager.execute(e)}use(e){const t=(e.block?this.middlewareEngine.addBlockingMiddleware:this.middlewareEngine.addMiddleware).bind(this.middlewareEngine)(e.action,e.name);return()=>this.middlewareEngine.removeMiddleware(t)}metrics(){return this.metricsCollector.getMetrics()}on(e,t){return this.eventBus.subscribe(e,t)}onStoreEvent(e,t){return this.eventBus.subscribe(e,t)}getPersistenceStatus(){return this.persistenceHandler.getQueueStatus()}async flushPersistence(){return this.persistenceHandler.flush()}discardPersistenceQueue(){this.persistenceHandler.discardQueue()}dispose(){this.updateSerializer.close(),this.persistenceHandler.dispose(),this.metricsCollector.dispose?.(),this.selectorManager.dispose?.()}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},B=class{store;eventHistory=[];stateHistory=[];unsubscribers=[];isTimeTraveling=!1;devTools=null;middlewareExecutions=[];activeTransactionCount=0;activeBatches=new Set;maxEvents;maxStateHistory;enableConsoleLogging;isSilent;logEvents;performanceThresholds;constructor(e,t={}){this.store=e,this.maxEvents=t.maxEvents??500,this.maxStateHistory=t.maxStateHistory??20,this.enableConsoleLogging=t.enableConsoleLogging??!1,this.isSilent=t.silent??!1,this.logEvents={updates:t.logEvents?.updates??!0,middleware:t.logEvents?.middleware??!0,transactions:t.logEvents?.transactions??!0,actions:t.logEvents?.actions??!0,selectors:t.logEvents?.selectors??!0},this.performanceThresholds={updateTime:t.performanceThresholds?.updateTime??50,middlewareTime:t.performanceThresholds?.middlewareTime??20},this.recordStateSnapshot([]),this.setupEventListeners()}_consoleLog(e,...t){this.isSilent||"function"==typeof console[e]&&console[e](...t)}setupEventListeners(){const e=["update:start","update:complete","middleware:start","middleware:complete","middleware:error","middleware:blocked","transaction:start","transaction:complete","transaction:error","middleware:executed","action:start","action:complete","action:error","selector:accessed"];for(const t of e){const e=t.startsWith("update")&&this.logEvents.updates||t.startsWith("middleware")&&this.logEvents.middleware||t.startsWith("transaction")&&this.logEvents.transactions||t.startsWith("action")&&this.logEvents.actions||t.startsWith("selector")&&this.logEvents.selectors;this.unsubscribers.push(this.store.on(t,(s=>{"update:complete"!==t||s.blocked||this.isTimeTraveling||this.recordStateSnapshot(s.deltas),"middleware:executed"===t?this.middlewareExecutions.push(s):"transaction:start"===t?this.activeTransactionCount++:"transaction:complete"!==t&&"transaction:error"!==t||(this.activeTransactionCount=Math.max(0,this.activeTransactionCount-1)),s.batchId&&(t.endsWith("start")?this.activeBatches.add(s.batchId):(t.endsWith("complete")||t.endsWith("error"))&&this.activeBatches.delete(s.batchId)),this.recordEvent(t,s),this.enableConsoleLogging&&e&&this._log(t,s),this._checkPerformance(t,s)})))}}recordStateSnapshot(e){const t={state:this.store.get(!0),timestamp:Date.now(),deltas:e};this.stateHistory.unshift(t),this.stateHistory.length>this.maxStateHistory&&this.stateHistory.pop()}recordEvent(e,t){const s={type:e,timestamp:Date.now(),data:structuredClone(t)};this.eventHistory.unshift(s),this.eventHistory.length>this.maxEvents&&this.eventHistory.pop()}getEventHistory(){return structuredClone(this.eventHistory)}getStateHistory(){return structuredClone(this.stateHistory)}getMiddlewareExecutions(){return this.middlewareExecutions}getTransactionStatus(){return{activeTransactions:this.activeTransactionCount,activeBatches:Array.from(this.activeBatches)}}createLoggingMiddleware(e={}){const{logLevel:t="debug",logUpdates:s=!0}=e;return(e,i)=>{if(s){(console[t]||console.log)("State Update:",i)}return i}}createValidationMiddleware(e){return(t,s)=>{const i=e(t,s);return"boolean"==typeof i?i:(!i.valid&&i.reason&&this._consoleLog("warn","Validation failed:",i.reason),i.valid)}}getRecentChanges(e=5){const t=[],s=Math.min(e,this.stateHistory.length);for(let e=0;e<s;e++){const s=this.stateHistory[e];if(!s.deltas||0===s.deltas.length)continue;const i={},r={},n=(e,t,s)=>{t.reduce(((e,i,r)=>(r===t.length-1?e[i]=s:e[i]=e[i]??{},e[i])),e)};for(const e of s.deltas){const t=e.path.split(".");n(i,t,e.oldValue),n(r,t,e.newValue)}t.push({timestamp:s.timestamp,changedPaths:s.deltas.map((e=>e.path)),from:i,to:r})}return t}clearHistory(){this.eventHistory=[],this.stateHistory.length>0&&(this.stateHistory=[this.stateHistory[0]])}getHistoryForAction(e){return this.eventHistory.filter((t=>t.data?.actionId===e))}async replay(e){const t=this.eventHistory.filter((e=>"update:start"===e.type))[e];t?.data.update?(this._consoleLog("log",`Replaying event at index ${e}:`,t),await this.store.set(t.data.update,{force:!0})):this._consoleLog("warn",`No replayable event found at index ${e}.`)}createTimeTravel(){let e=0,t=[];const s=this.store.on("update:complete",(s=>{this.isTimeTraveling||s.blocked||(t=[],e=0)}));this.unsubscribers.push(s);const i=()=>this.stateHistory.length,r=()=>e<i()-1,n=()=>t.length>0;return{canUndo:r,canRedo:n,undo:async()=>{if(!r())return;t.unshift(this.stateHistory[e]),e++;const s=this.stateHistory[e].state;this.isTimeTraveling=!0,await this.store.set({...s},{force:!0}),this.isTimeTraveling=!1},redo:async()=>{if(!n())return;const s=t.shift();e--,this.isTimeTraveling=!0,await this.store.set({...s.state},{force:!0}),this.isTimeTraveling=!1},length:i,clear:()=>{t=[],e=0}}}async saveSession(e){const t=this.store.id(),s={eventHistory:this.eventHistory,stateHistory:this.stateHistory};return Promise.resolve(e.set(t,s))}async loadSession(e){const t=await Promise.resolve(e.get());return!!t&&(this.eventHistory=t.eventHistory||[],this.stateHistory=t.stateHistory||[],this.stateHistory.length>0&&await this.store.set(this.stateHistory[0].state,{force:!0}),!0)}exportSession(){const e={eventHistory:this.eventHistory,stateHistory:this.stateHistory},t=new Blob([JSON.stringify(e,null,2)],{type:"application/json"}),s=URL.createObjectURL(t),i=document.createElement("a");i.href=s,i.download=`store-observer-session-${(new Date).toISOString()}.json`,i.click(),URL.revokeObjectURL(s)}importSession(e){return new Promise(((t,s)=>{const i=new FileReader;i.onload=async e=>{try{const s=JSON.parse(e.target?.result);this.eventHistory=s.eventHistory||[],this.stateHistory=s.stateHistory||[],this.stateHistory.length>0&&await this.store.set(this.stateHistory[0].state,{force:!0}),t()}catch(e){s(e)}},i.onerror=e=>s(e),i.readAsText(e)}))}disconnect(){this.unsubscribers.forEach((e=>e())),this.unsubscribers=[],this.devTools?.disconnect(),this.clearHistory()}_log(e,t){const s=new Date(t.timestamp||Date.now()).toISOString().split("T")[1].replace("Z","");if("update:start"===e)this._consoleLog("group",`%c⚡ Store Update Started [${s}]`,"color: #4a6da7");else if("update:complete"===e){if(t.blocked)this._consoleLog("warn",`%c✋ Update Blocked [${s}]`,"color: #bf8c0a",t.error);else{const e=t.deltas||[];e.length>0&&(this._consoleLog("log",`%c✅ Update Complete [${s}] - ${e.length} paths changed in ${t.duration?.toFixed(2)}ms`,"color: #2a9d8f"),this._consoleLog("table",e.map((e=>({path:e.path,oldValue:e.oldValue,newValue:e.newValue})))))}this._consoleLog("groupEnd")}else"middleware:start"===e?this._consoleLog("debug",`%c◀ Middleware "${t.name}" started [${s}] (${t.type})`,"color: #8c8c8c"):"middleware:complete"===e?this._consoleLog("debug",`%c▶ Middleware "${t.name}" completed [${s}] in ${t.duration?.toFixed(2)}ms`,"color: #7c9c7c"):"middleware:error"===e?this._consoleLog("error",`%c❌ Middleware "${t.name}" error [${s}]:`,"color: #e63946",t.error):"middleware:blocked"===e?this._consoleLog("warn",`%c🛑 Middleware "${t.name}" blocked update [${s}]`,"color: #e76f51"):"transaction:start"===e?this._consoleLog("group",`%c📦 Transaction Started [${s}]`,"color: #6d597a"):"transaction:complete"===e?(this._consoleLog("log",`%c📦 Transaction Complete [${s}]`,"color: #355070"),this._consoleLog("groupEnd")):"transaction:error"===e?(this._consoleLog("error",`%c📦 Transaction Error [${s}]:`,"color: #e56b6f",t.error),this._consoleLog("groupEnd")):"action:start"===e?this._consoleLog("group",`%c🚀 Action "${t.name}" Started [${s}]`,"color: #9b59b6",{params:t.params}):"action:complete"===e?(this._consoleLog("log",`%c✔️ Action "${t.name}" Complete [${s}] in ${t.duration?.toFixed(2)}ms`,"color: #2ecc71"),this._consoleLog("groupEnd")):"action:error"===e?(this._consoleLog("error",`%c🔥 Action "${t.name}" Error [${s}]:`,"color: #e74c3c",t.error),this._consoleLog("groupEnd")):"selector:accessed"===e&&this._consoleLog("debug",`%c👀 Selector Accessed [${s}] in ${t.duration?.toFixed(2)}ms`,"color: #f1c40f",{accessedPaths:t.accessedPaths,selectorId:t.selectorId})}_checkPerformance(e,t){this.enableConsoleLogging&&("update:complete"===e&&!t.blocked&&t.duration>this.performanceThresholds.updateTime&&this._consoleLog("warn",`%c⚠️ Slow update detected [${t.duration.toFixed(2)}ms]`,"color: #ff9f1c",{deltas:t.deltas,threshold:this.performanceThresholds.updateTime}),"middleware:complete"===e&&t.duration>this.performanceThresholds.middlewareTime&&this._consoleLog("warn",`%c⚠️ Slow middleware "${t.name}" [${t.duration.toFixed(2)}ms]`,"color: #ff9f1c",{threshold:this.performanceThresholds.middlewareTime}))}};export{x as ActionCancelledError,M as ActionManager,d as DELETE_SYMBOL,P as ReactiveDataStore,B as StoreObserver,k as UnknownActionError,f as createDerivePaths,y as createDiff,h as createMerge,v as derivePaths,w as diff,u as merge,l as shallowClone};
1
+ import{createEventBus as e}from"@core/events";import{v4 as t}from"uuid";var s=class{_delay;_leading;_timer;_pendingFn;_pendingResolvers=[];_leadingFired=!1;constructor(e){this._delay=e?.delay??300,this._leading=e?.leading??!1}do(e){return new Promise((t=>{this._enqueue(e,t)}))}fire(e){this._enqueue(e,void 0)}_enqueue(e,t){if(this._pendingFn=e,t&&this._pendingResolvers.push(t),this._leading&&!this._leadingFired)return this._leadingFired=!0,this._fire(),clearTimeout(this._timer),void(this._timer=setTimeout((()=>{this._leadingFired=!1,void 0!==this._pendingFn&&this._fire()}),this._delay));clearTimeout(this._timer),this._timer=setTimeout((()=>{this._leadingFired=!1,this._fire()}),this._delay)}cancel(){clearTimeout(this._timer),this._timer=void 0,this._pendingFn=void 0,this._leadingFired=!1;const e=this._pendingResolvers.splice(0);for(const t of e)t({status:"cancelled"})}async flush(){return this._pendingFn?(clearTimeout(this._timer),this._timer=void 0,this._leadingFired=!1,this._fire()):null}pending(){return void 0!==this._pendingFn}async _fire(){const e=this._pendingFn,t=this._pendingResolvers.splice(0);let s;this._pendingFn=void 0;try{s={status:"ok",value:await e()}}catch(e){s={status:"error",error:e}}for(const e of t)e(s);return s}},i=class e extends Error{constructor(t,s){super(t,{cause:s}),this.name="SyncError",Object.setPrototypeOf(this,e.prototype)}},r=class extends i{constructor(e){super(`[ArtifactContainer] Operation timed out: ${e}`)}},n=class extends i{constructor(e){super("[Serializer] The serializer has been marked as done!",e)}},a=class{_open=!1;_resolve;_promise;constructor(){this._promise=new Promise((e=>{this._resolve=e}))}open(){this._open||(this._open=!0,this._resolve())}async wait(e){if(this._open)return;if(null==e)return this._promise;let t;await Promise.race([this._promise.then((()=>clearTimeout(t))),new Promise(((s,i)=>{t=setTimeout((()=>i(new r("Latch timed out"))),e)}))])}isOpen(){return this._open}},o=class{_locked=!1;_capacity;_yieldMode;waiters=[];constructor(e){this._capacity=e?.capacity??1/0,this._yieldMode=e?.yieldMode??"macrotask"}async lock(e){if(!this._locked)return void(this._locked=!0);if(this.waiters.length>=this._capacity)throw new Error(`Mutex queue is full (capacity: ${this._capacity})`);let t;const s=new Promise((e=>t=e));if(this.waiters.push(t),null==e)return void await s;let i;await Promise.race([s.then((()=>clearTimeout(i))),new Promise(((s,n)=>{i=setTimeout((()=>{const e=this.waiters.indexOf(t);-1!==e&&this.waiters.splice(e,1),n(new r("Mutex lock timed out"))}),e)}))])}tryLock(){return!this._locked&&(this._locked=!0,!0)}unlock(){if(!this._locked)throw new Error("Mutex is not locked");const e=this.waiters.shift();e?"microtask"===this._yieldMode?queueMicrotask(e):setTimeout(e,0):this._locked=!1}locked(){return this._locked}pending(){return this.waiters.length}},c=class{mutex;_done=!1;_lastValue=null;_lastError=void 0;_hasRun=!1;constructor(e){this.mutex=new o({capacity:e?.capacity??1e3,yieldMode:e?.yieldMode??"macrotask"})}async do(e,t){if(this._done)return{value:null,error:new n};try{await this.mutex.lock(t)}catch(e){return{value:null,error:e}}let s,i=null;try{if(this._done)throw new n;i=await e(),this._lastValue=i,this._lastError=void 0,this._hasRun=!0}catch(e){s=e,this._lastError=e,this._hasRun=!0}finally{this.mutex.unlock()}return{value:i,error:s}}peek(){return{value:this._lastValue,error:this._lastError}}hasRun(){return this._hasRun}close(){this._done=!0}pending(){return this.mutex.pending()}running(){return this.mutex.locked()}},d=Symbol.for("delete"),h=e=>Array.isArray(e)?[...e]:{...e};function l(e){const t=e?.deleteMarker||d;function s(e){if(null==e)return e;if(Array.isArray(e))return e.filter((e=>e!==t)).map((e=>"object"!=typeof e||null===e||Array.isArray(e)?e:s(e)));if("object"==typeof e){const i={};for(const[r,n]of Object.entries(e))if(n!==t)if("object"==typeof n&&null!==n){const e=s(n);void 0!==e&&(i[r]=e)}else i[r]=n;return i}return e===t?void 0:e}return function(e,i){if("object"!=typeof e||null===e)return"object"==typeof i&&null!==i?s(i):i===t?{}:i;if("object"!=typeof i||null===i)return e;const r=h(e),n=[{target:r,source:i}];for(;n.length>0;){const{target:e,source:s}=n.pop();for(const i of Object.keys(s)){const r=s[i];if(r!==t)if(Array.isArray(r))e[i]=r;else if("object"==typeof r&&null!==r){const t=i in e&&"object"==typeof e[i]&&null!==e[i]?e[i]:{};e[i]=h(t),n.push({target:e[i],source:r})}else e[i]=r;else delete e[i]}}return r}}var u=l(),p=["map","filter","reduce","forEach","find","findIndex","some","every","includes","flatMap","flat","slice","splice"],m=class{reactiveSelectors=new Map;pathBasedCache=new Map;dependencyMap=new Map;getState;eventBus;unsubscribeFromStore;constructor(e,t){this.getState=e,this.eventBus=t,this.unsubscribeFromStore=this.eventBus.subscribe("update:complete",this.handleStoreUpdate)}handleStoreUpdate=e=>{const t=new Set;for(const s of e.deltas){const e=s.path;for(const[s,i]of this.dependencyMap)if(s===e||s.startsWith(e+".")||e.startsWith(s+"."))for(const e of i)t.add(e)}for(const e of t){const t=this.reactiveSelectors.get(e);t&&this.evaluateEntry(t)}};evaluateEntry(e){let t;try{t=e.selector(this.getState())}catch{t=void 0}if(t!==e.lastResult){e.lastResult=t;for(const s of e.subscribers)s(t);this.eventBus.emit({name:"selector:changed",payload:{selectorId:e.id,newResult:t,timestamp:performance.now()}})}}createReactiveSelector(e){const t=function(e,t="."){const s=new Set,i=new Map,r=(e="")=>{if(i.has(e))return i.get(e);const n=new Proxy((()=>{}),{get:(i,n)=>{if("symbol"==typeof n||"then"===n)return;if("valueOf"===n||"toString"===n)throw new Error("Cannot perform logic, arithmetic, or string operations inside a selector.");if(p.includes(n))throw new Error(`Array method .${n}() is not allowed in selectors.`);const a=e?`${e}${t}${n}`:n;return e&&s.delete(e),s.add(a),r(a)},has:()=>{throw new Error("The 'in' operator is not allowed in selectors.")},apply:()=>{throw new Error("Selectors cannot call functions or methods.")}});return i.set(e,n),n};try{e(r())}catch(e){throw new Error(`Selector failed during path analysis. Selectors must be simple property accessors only. Error: ${e instanceof Error?e.message:String(e)}`)}return Array.from(s)}(e),s=[...t].sort().join("|"),i=this.pathBasedCache.get(s);if(i)return void 0!==i.cleanupTimer&&(clearTimeout(i.cleanupTimer),i.cleanupTimer=void 0),i.reactiveSelectorInstance;const r=`sel-${Math.random().toString(36).slice(2,9)}`,n={id:r,selector:e,lastResult:e(this.getState()),accessedPaths:t,subscribers:new Set,count:0,cleanupTimer:void 0,pathCacheKey:s,reactiveSelectorInstance:null};for(const e of t)this.dependencyMap.has(e)||this.dependencyMap.set(e,new Set),this.dependencyMap.get(e).add(r);const a={id:r,get:()=>{try{return n.selector(this.getState())}catch{return}},subscribe:e=>(void 0!==n.cleanupTimer&&(clearTimeout(n.cleanupTimer),n.cleanupTimer=void 0),n.subscribers.add(e),n.count++,()=>{n.subscribers.delete(e),n.count--,0===n.count&&(n.cleanupTimer=setTimeout((()=>{0===n.count&&this.evictEntry(n)}),0))})};return n.reactiveSelectorInstance=a,this.reactiveSelectors.set(r,n),this.pathBasedCache.set(s,n),this.eventBus.emit({name:"selector:accessed",payload:{selectorId:r,accessedPaths:t,duration:0,timestamp:performance.now()}}),a}evictEntry(e){for(const t of e.accessedPaths){const s=this.dependencyMap.get(t);s&&(s.delete(e.id),0===s.size&&this.dependencyMap.delete(t))}this.reactiveSelectors.delete(e.id),this.pathBasedCache.delete(e.pathCacheKey)}dispose(){this.unsubscribeFromStore(),this.reactiveSelectors.clear(),this.dependencyMap.clear(),this.pathBasedCache.clear()}};function g(e,t){if(e===t)return!0;if(e&&t&&"object"==typeof e&&"object"==typeof t){if(e.constructor!==t.constructor)return!1;let s,i;if(Array.isArray(e)){if(s=e.length,s!=t.length)return!1;for(i=s;i-- >0;)if(!g(e[i],t[i]))return!1;return!0}const[r,n]=[Object.keys(e),Object.keys(t)];if(s=r.length,s!==n.length)return!1;for(i=s;i-- >0;){const s=r[i];if(!Object.prototype.hasOwnProperty.call(t,s)||!g(e[s],t[s]))return!1}return!0}return e!=e&&t!=t}function y(e){const t=e?.deleteMarker||d;return function(e,s){const i=[],r=[{pathStr:"",orig:e||{},part:s||{}}];for(;r.length>0;){const{pathStr:e,orig:s,part:n}=r.pop();if(null!=n&&!g(s,n))if("object"!=typeof n||Array.isArray(n))e&&i.push({path:e,oldValue:s,newValue:n});else for(const a of Object.keys(n)){const o=e?e+"."+a:a,c=n[a],d=s&&"object"==typeof s?s[a]:void 0;c!==t?"object"==typeof c&&null!==c?r.push({pathStr:o,orig:d,part:c}):g(d,c)||i.push({path:o,oldValue:d,newValue:c}):void 0!==d&&i.push({path:o,oldValue:d,newValue:void 0})}}return i}}function f(e){const t=e?.deleteMarker||d;return function(e){const s=new Set,i=[{obj:e,currentPath:""}];for(;i.length>0;){const{obj:e,currentPath:r}=i.pop();if(null!=e&&"object"==typeof e&&!Array.isArray(e))for(const n of Object.keys(e)){const a=r?`${r}.${n}`:n;s.add(a);const o=e[n];"object"!=typeof o||null===o||Array.isArray(o)||o===t||i.push({obj:o,currentPath:a})}}return Array.from(s)}}var w=y(),v=f(),b=class{constructor(e,t,s){this.updateBus=t,this.diff=s,this.cache=structuredClone(e)}cache;get(e){return e?structuredClone(this.cache):this.cache}applyChanges(e,t=!1,s=!1,i=[]){if(t)return this.cache=s?structuredClone(e):e,this.notifyListeners([]),[];0===i.length&&(i=[e]);const r=this.get(!1),n=new Map;for(let e=0;e<i.length;e++){const t=i[e],s=this.diff(r,t);for(let e=0;e<s.length;e++){const t=s[e];n.set(t.path,t)}}const a=n.size?[...n.values()]:[];if(a.length>0){this.cache=s?structuredClone(e):e;const t=new Set;for(let e=0;e<a.length;e++){let s=a[e].path;for(;s&&!t.has(s);){t.add(s);const e=s.lastIndexOf(".");if(e<0)break;s=s.slice(0,e)}}this.notifyListeners(t)}return a}notifyListeners(e){for(const t of e)this.updateBus.emit({name:"update",payload:t})}},S=class{constructor(e,t,s){this.eventBus=e,this.executionState=t,this.merge=s}middleware=[];blockingMiddleware=[];async executeBlocking(e,t){for(const{fn:s,name:i,id:r}of this.blockingMiddleware){const n={id:r,name:i,startTime:performance.now()};this.executionState.runningMiddleware={id:r,name:i,startTime:performance.now()},this.emitMiddlewareLifecycle("start",{id:r,name:i,type:"blocking"});try{const a=await Promise.resolve(s(e,t));if(n.endTime=performance.now(),n.duration=n.endTime-n.startTime,!1===a)return n.blocked=!0,this.emitMiddlewareLifecycle("blocked",{id:r,name:i,duration:n.duration}),this.emit(this.eventBus,{name:"middleware:executed",payload:n}),{blocked:!0};this.emitMiddlewareLifecycle("complete",{id:r,name:i,type:"blocking",duration:n.duration}),this.emit(this.eventBus,{name:"middleware:executed",payload:{...n,blocked:!1}})}catch(e){return n.endTime=performance.now(),n.duration=n.endTime-n.startTime,n.error=e instanceof Error?e:new Error(String(e)),n.blocked=!0,this.emitMiddlewareError(r,i,n.error,n.duration),this.emit(this.eventBus,{name:"middleware:executed",payload:n}),{blocked:!0,error:n.error}}finally{this.executionState.runningMiddleware=null}}return{blocked:!1}}async executeTransform(e,t){let s=e,i=t;for(const{fn:e,name:r,id:n}of this.middleware){const a={id:n,name:r,startTime:performance.now()};this.executionState.runningMiddleware={id:n,name:r,startTime:performance.now()},this.emitMiddlewareLifecycle("start",{id:n,name:r,type:"transform"});try{const o=await Promise.resolve(e(s,t));a.endTime=performance.now(),a.duration=a.endTime-a.startTime,a.blocked=!1,o&&"object"==typeof o&&(s=this.merge(s,o),i=this.merge(i,o)),this.emit(this.eventBus,{name:"middleware:executed",payload:a}),this.emitMiddlewareLifecycle("complete",{id:n,name:r,type:"transform",duration:a.duration})}catch(e){a.endTime=performance.now(),a.duration=a.endTime-a.startTime,a.error=e instanceof Error?e:new Error(String(e)),a.blocked=!1,this.emit(this.eventBus,{name:"middleware:executed",payload:a}),this.emitMiddlewareError(n,r,a.error,a.duration),console.error(`Middleware ${r} error:`,e)}finally{this.executionState.runningMiddleware=null}}return i}addMiddleware(e,t="unnamed-middleware"){const s=this.generateId();return this.middleware.push({fn:e,name:t,id:s}),this.updateExecutionState(),s}addBlockingMiddleware(e,t="unnamed-blocking-middleware"){const s=this.generateId();return this.blockingMiddleware.push({fn:e,name:t,id:s}),this.updateExecutionState(),s}removeMiddleware(e){const t=this.middleware.length+this.blockingMiddleware.length;return this.middleware=this.middleware.filter((t=>t.id!==e)),this.blockingMiddleware=this.blockingMiddleware.filter((t=>t.id!==e)),this.updateExecutionState(),this.middleware.length+this.blockingMiddleware.length<t}updateExecutionState(){this.executionState.middlewares=[...this.middleware.map((e=>e.name)),...this.blockingMiddleware.map((e=>e.name))]}emitMiddlewareLifecycle(e,t){this.emit(this.eventBus,{name:`middleware:${e}`,payload:{...t,timestamp:Date.now()}})}emitMiddlewareError(e,t,s,i){this.emit(this.eventBus,{name:"middleware:error",payload:{id:e,name:t,error:s,duration:i,timestamp:Date.now()}})}generateId(){return crypto.randomUUID?crypto.randomUUID():`${Date.now()}-${Math.random().toString(36).substring(2,15)}`}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},T=class{constructor(e,t,s,i){this.eventBus=e,this.coreState=t,this.instanceID=s,this.maxRetries=i?.maxRetries??3,this.retryDelay=i?.retryDelay??1e3}persistence;instanceID;persistenceReady=!1;backgroundQueue=[];isProcessingQueue=!1;maxRetries=3;retryDelay=1e3;queueProcessor;pendingRetries=new Set;async initialize(e){e?await this.setPersistence(e):this.setPersistenceReady()}isReady(){return this.persistenceReady}handleStateChange(e,t){if(!this.persistence||0===e.length)return;const s={id:`${Date.now()}-${Math.random().toString(36).slice(2,11)}`,state:structuredClone(t),changedPaths:[...e],timestamp:Date.now(),retries:0};this.backgroundQueue.push(s),this.scheduleQueueProcessing(),this.emit(this.eventBus,{name:"persistence:queued",payload:{taskId:s.id,changedPaths:e,queueSize:this.backgroundQueue.length,timestamp:s.timestamp}})}getQueueStatus(){return{queueSize:this.backgroundQueue.length,isProcessing:this.isProcessingQueue,pendingRetries:this.pendingRetries.size,oldestTask:this.backgroundQueue[0]?.timestamp}}async flush(){this.isProcessingQueue&&await new Promise((e=>{const t=()=>{this.isProcessingQueue?setTimeout(t,10):e()};t()})),await this.processQueue()}discardQueue(){const e=this.backgroundQueue.length+this.pendingRetries.size;this.backgroundQueue=[],this.pendingRetries.clear(),this.queueProcessor&&(clearTimeout(this.queueProcessor),this.queueProcessor=void 0),this.emit(this.eventBus,{name:"persistence:queue_cleared",payload:{clearedTasks:e,timestamp:Date.now()}})}scheduleQueueProcessing(){this.queueProcessor||this.isProcessingQueue||(this.queueProcessor=setTimeout((()=>{this.processQueue().catch((e=>{console.error("Queue processing failed:",e)}))}),10))}async processQueue(){if(!this.isProcessingQueue&&0!==this.backgroundQueue.length){this.isProcessingQueue=!0,this.queueProcessor=void 0;try{for(;this.backgroundQueue.length>0;){const e=this.backgroundQueue.shift();await this.processTask(e)}}finally{this.isProcessingQueue=!1}}}async processTask(e){try{await this.persistence.set(this.instanceID,e.state)?this.emit(this.eventBus,{name:"persistence:success",payload:{taskId:e.id,changedPaths:e.changedPaths,duration:Date.now()-e.timestamp,timestamp:Date.now()}}):await this.handleTaskFailure(e,new Error("Persistence returned false"))}catch(t){await this.handleTaskFailure(e,t)}}async handleTaskFailure(e,t){if(e.retries++,e.retries<=this.maxRetries){const s=this.retryDelay*Math.pow(2,e.retries-1);this.emit(this.eventBus,{name:"persistence:retry",payload:{taskId:e.id,attempt:e.retries,maxRetries:this.maxRetries,nextRetryIn:s,error:t,timestamp:Date.now()}}),this.pendingRetries.add(e.id),setTimeout((()=>{this.pendingRetries.has(e.id)&&(this.pendingRetries.delete(e.id),this.backgroundQueue.unshift(e),this.scheduleQueueProcessing())}),s)}else this.emit(this.eventBus,{name:"persistence:failed",payload:{taskId:e.id,changedPaths:e.changedPaths,attempts:e.retries,error:t,timestamp:Date.now()}})}setPersistenceReady(){this.persistenceReady=!0,this.emit(this.eventBus,{name:"persistence:ready",payload:{timestamp:Date.now()}})}async setPersistence(e){this.persistence=e;try{const e=await this.persistence.get();e&&this.coreState.applyChanges(e)}catch(e){console.error("Failed to initialize persistence:",e),this.emit(this.eventBus,{name:"persistence:init_error",payload:{error:e,timestamp:Date.now()}})}finally{this.setPersistenceReady()}this.persistence.subscribe(this.instanceID,(async e=>{const t=this.coreState.applyChanges(e);t.length>0&&this.emit(this.eventBus,{name:"update:complete",payload:{changedPaths:t,source:"external",timestamp:Date.now()}})}))}dispose(){this.discardQueue(),this.isProcessingQueue=!1,this.persistenceReady=!1}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},_=class{constructor(e,t,s){this.eventBus=e,this.coreState=t,this.executionState=s}async execute(e){const t=this.coreState.get(!0);this.executionState.transactionActive=!0,this.emit(this.eventBus,{name:"transaction:start",payload:{timestamp:Date.now()}});try{const t=await Promise.resolve(e());return this.emit(this.eventBus,{name:"transaction:complete",payload:{timestamp:Date.now()}}),this.executionState.transactionActive=!1,t}catch(e){throw this.coreState.applyChanges(t,!0,!1),this.emit(this.eventBus,{name:"transaction:error",payload:{error:e instanceof Error?e:new Error(String(e)),timestamp:Date.now()}}),this.executionState.transactionActive=!1,e}}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},x=class{updateCount=0;listenerExecutions=0;averageUpdateTime=0;largestUpdateSize=0;mostActiveListenerPaths=[];totalUpdates=0;blockedUpdates=0;averageUpdateDuration=0;middlewareExecutions=0;transactionCount=0;totalEventsFired=0;totalActionsDispatched=0;totalActionsSucceeded=0;totalActionsFailed=0;averageActionDuration=0;updateTimes=[];actionTimes=[];pathExecutionCounts=new Map;constructor(e){this.setupEventListeners(e)}getMetrics(){return{updateCount:this.updateCount,listenerExecutions:this.listenerExecutions,averageUpdateTime:this.averageUpdateTime,largestUpdateSize:this.largestUpdateSize,mostActiveListenerPaths:[...this.mostActiveListenerPaths],totalUpdates:this.totalUpdates,blockedUpdates:this.blockedUpdates,averageUpdateDuration:this.averageUpdateDuration,middlewareExecutions:this.middlewareExecutions,transactionCount:this.transactionCount,totalEventsFired:this.totalEventsFired,totalActionsDispatched:this.totalActionsDispatched,totalActionsSucceeded:this.totalActionsSucceeded,totalActionsFailed:this.totalActionsFailed,averageActionDuration:this.averageActionDuration}}setupEventListeners(e){const t=e.emit;e.emit=s=>(this.totalEventsFired++,t.call(e,s)),e.subscribe("update:complete",(e=>{if(this.totalUpdates++,e.blocked)this.blockedUpdates++;else{if(e.duration){this.updateTimes.push(e.duration),this.updateTimes.length>100&&this.updateTimes.shift();const t=this.updateTimes.reduce(((e,t)=>e+t),0)/this.updateTimes.length;this.averageUpdateTime=t,this.averageUpdateDuration=t}e.deltas?.length&&(this.updateCount++,this.largestUpdateSize=Math.max(this.largestUpdateSize,e.deltas.length),e.deltas.forEach((e=>{const t=this.pathExecutionCounts.get(e.path)||0;this.pathExecutionCounts.set(e.path,t+1)})),this.mostActiveListenerPaths=Array.from(this.pathExecutionCounts.entries()).sort((([,e],[,t])=>t-e)).slice(0,5).map((([e])=>e)))}})),e.subscribe("middleware:start",(()=>{this.middlewareExecutions++})),e.subscribe("transaction:start",(()=>{this.transactionCount++})),e.subscribe("action:start",(()=>{this.totalActionsDispatched++})),e.subscribe("action:complete",(e=>{this.totalActionsSucceeded++,e.duration&&(this.actionTimes.push(e.duration),this.actionTimes.length>100&&this.actionTimes.shift(),this.averageActionDuration=this.actionTimes.reduce(((e,t)=>e+t),0)/this.actionTimes.length)})),e.subscribe("action:error",(()=>{this.totalActionsFailed++}))}reset(){this.updateCount=0,this.listenerExecutions=0,this.averageUpdateTime=0,this.largestUpdateSize=0,this.mostActiveListenerPaths=[],this.totalUpdates=0,this.blockedUpdates=0,this.averageUpdateDuration=0,this.middlewareExecutions=0,this.transactionCount=0,this.totalEventsFired=0,this.totalActionsDispatched=0,this.totalActionsSucceeded=0,this.totalActionsFailed=0,this.averageActionDuration=0,this.updateTimes=[],this.actionTimes=[],this.pathExecutionCounts.clear()}getDetailedMetrics(){return{pathExecutionCounts:new Map(this.pathExecutionCounts),recentUpdateTimes:[...this.updateTimes],successRate:this.totalUpdates>0?(this.totalUpdates-this.blockedUpdates)/this.totalUpdates:1,averagePathsPerUpdate:this.updateCount>0?Array.from(this.pathExecutionCounts.values()).reduce(((e,t)=>e+t),0)/this.updateCount:0}}dispose(){this.reset()}},k=class extends Error{constructor(){super("Action Cancelled by Debounce"),this.name="ActionCancelledError"}},E=class extends Error{constructor({action:e}){super(`Unknown action: "${e}"`),this.name="UnknownActionError"}},M=()=>{},P={name:"UNDEFINED ACTION",status:()=>!1,subscribe:e=>()=>{}},A=class{constructor(e,t){this.eventBus=e,this.set=t}registrations=new Map;register(e){const i={action:{name:e.name,id:t(),action:e.fn,debounce:e.debounce?{...e.debounce,condition:e.debounce.condition??(()=>!0)}:void 0},debouncer:e.debounce&&e.debounce.delay>0?new s({delay:e.debounce.delay}):void 0,previousArgs:void 0,running:!1,subscription:{count:0,listeners:new Set,unsubscribers:[],watcher:null}};return this.registrations.set(e.name,i),()=>{const t=this.registrations.get(e.name);t&&(t.debouncer?.cancel(),this.registrations.delete(e.name))}}async dispatch(e,...t){const s=this.registrations.get(e);if(!s)throw new E({action:e});const{action:i,debouncer:r}=s,{debounce:n}=i;if(!r||!n)return this.executeAction(s,t);const a=n.condition(s.previousArgs,t);if(s.previousArgs=t,!a)return this.executeAction(s,t);const o=await r.do((()=>this.executeAction(s,t)));if("cancelled"===o.status)throw new k;if("error"===o.status&&o.error)throw o.error;return o.value}async executeAction(e,t){const s=performance.now();e.running=!0,this.emit(this.eventBus,{name:"action:start",payload:{actionId:e.action.id,name:e.action.name,params:t||[],timestamp:s}});try{const i=await this.set((s=>e.action.action(s,...t)),{actionId:e.action.id}),r=performance.now();return e.running=!1,this.emit(this.eventBus,{name:"action:complete",payload:{actionId:e.action.id,name:e.action.name,params:t,startTime:s,endTime:r,duration:r-s,result:i}}),i}catch(i){const r=performance.now();throw e.running=!1,this.emit(this.eventBus,{name:"action:error",payload:{actionId:e.action.id,name:e.action.name,params:t,startTime:s,endTime:r,duration:r-s,error:i}}),i}}running(e){const t=this.registrations.get(e);return!!t&&t.running}subscribe(e,t){const s=this.registrations.get(e);return s?(s.subscription.reset=!1,s.subscription.count++,s.subscription.listeners.add(t),s.subscription.unsubscribers||(s.subscription.unsubscribers=[this.eventBus.subscribe("action:start",(t=>{t.name===e&&this.notifyStatusListeners(e)})),this.eventBus.subscribe("action:complete",(t=>{t.name===e&&this.notifyStatusListeners(e)})),this.eventBus.subscribe("action:error",(t=>{t.name===e&&this.notifyStatusListeners(e)}))]),()=>{s.subscription.listeners.delete(t),s.subscription.count--,0===s.subscription.count&&(s.subscription.reset=!0,queueMicrotask((()=>{s.subscription.reset&&(s.subscription.reset=!1,s.subscription.unsubscribers?.forEach((e=>e())),s.subscription.unsubscribers=[])})))}):M}watch(e){const t=this.registrations.get(e);return t?(t.subscription.watcher||(t.subscription.watcher={name:e,status:()=>this.running(e),subscribe:t=>this.subscribe(e,t)}),t.subscription.watcher):P}notifyStatusListeners(e){const t=this.registrations.get(e).subscription.listeners;t&&t.forEach((e=>e()))}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},L=class{coreState;middlewareEngine;persistenceHandler;transactionManager;metricsCollector;selectorManager;actions;updateSerializer=new c({yieldMode:"macrotask",capacity:1e3});readyLatch=new a;updateBus=e();eventBus;executionState;instanceID=t();merge;diff;constructor(t,s,i=d,r){this.eventBus=e(),this.executionState={executing:!1,changes:null,pendingChanges:[],middlewares:[],runningMiddleware:null,transactionActive:!1},this.merge=l({deleteMarker:i}),this.diff=y({deleteMarker:i}),this.coreState=new b(t,this.updateBus,this.diff),this.middlewareEngine=new S(this.eventBus,this.executionState,this.merge),this.persistenceHandler=new T(this.eventBus,this.coreState,this.instanceID,{maxRetries:r?.persistenceMaxRetries,retryDelay:r?.persistenceRetryDelay}),this.transactionManager=new _(this.eventBus,this.coreState,this.executionState),this.metricsCollector=new x(this.eventBus),this.actions=new A(this.eventBus,this.set.bind(this)),this.persistenceHandler.initialize(s),this.setupPersistenceListener(),this.setupReadyLatch(),this.selectorManager=new m(this.get.bind(this),this.eventBus)}isReady(){return this.readyLatch.isOpen()}async ready(e){return this.readyLatch.wait(e)}state(){return this.executionState.executing=this.updateSerializer.running(),this.executionState}get(e){return this.coreState.get(e??!1)}select(e){return this.selectorManager.createReactiveSelector(e)}register(e){return this.actions.register(e)}async dispatch(e,...t){return this.actions.dispatch(e,...t)}async set(e,t={}){const s=await this.updateSerializer.do((()=>this._performUpdate(e,t)));if(s.error)throw s.error;return s.value}async _performUpdate(e,t){const s=performance.now();this.emit(this.eventBus,{name:"update:start",payload:{timestamp:s,actionId:t.actionId}});try{if(t.force){const i=this.get(!1),r="function"==typeof e?e(i):e;this.coreState.applyChanges(r,!0);const n=performance.now();return this.emit(this.eventBus,{name:"update:complete",payload:{deltas:[],duration:n-s,timestamp:Date.now(),actionId:t.actionId,newState:r}}),r}let i;const r=this.get(!1);if("function"==typeof e){const t=e(r);i=t instanceof Promise?await t:t}else i=e;const n=await this.middlewareEngine.executeBlocking(r,i);if(n.blocked)throw n.error||new Error("Update blocked by middleware");const a=this.merge(r,i),o=await this.middlewareEngine.executeTransform(a,i),c=this.merge(a,o),d=this.coreState.applyChanges(c,!1,!1,[i,o]),h=performance.now(),l=this.get(!1);return this.emit(this.eventBus,{name:"update:complete",payload:{deltas:d,duration:h-s,timestamp:Date.now(),actionId:t.actionId,newState:l}}),l}catch(e){throw this.emit(this.eventBus,{name:"update:complete",payload:{blocked:!0,error:e,timestamp:Date.now(),actionId:t.actionId,newState:this.get(!1)}}),e}finally{this.executionState.executing=!1,this.executionState.changes=null,this.executionState.runningMiddleware=null,this.executionState.pendingChanges=[]}}setupReadyLatch(){this.readyLatch.open(),this.persistenceHandler.isReady()?this.readyLatch.open():queueMicrotask((()=>{this.readyLatch.isOpen()||this.readyLatch.open()}))}setupPersistenceListener(){this.updateBus.subscribe("update",(e=>{e&&this.persistenceHandler.isReady()&&this.persistenceHandler.handleStateChange([e],this.get(!1))}))}watch(e,t){const s=Array.isArray(e)?e:[e],i=""===e||0===s.length;return this.updateBus.subscribe("update",(e=>{(i||s.includes(e))&&(t(this.get(!1)),this.metricsCollector.listenerExecutions++)}))}watchAction(e){return this.actions.watch(e)}debouncedSetter(e){const t=new s({delay:e.delay,leading:e.leading});return(e,s={})=>{t.fire((()=>this.set(e,s)))}}subscribe(e,t){return this.watch(e,t)}id(){return this.instanceID}async transaction(e){return this.transactionManager.execute(e)}use(e){const t=(e.block?this.middlewareEngine.addBlockingMiddleware:this.middlewareEngine.addMiddleware).bind(this.middlewareEngine)(e.action,e.name);return()=>this.middlewareEngine.removeMiddleware(t)}metrics(){return this.metricsCollector.getMetrics()}on(e,t){return this.eventBus.subscribe(e,t)}onStoreEvent(e,t){return this.eventBus.subscribe(e,t)}getPersistenceStatus(){return this.persistenceHandler.getQueueStatus()}async flushPersistence(){return this.persistenceHandler.flush()}discardPersistenceQueue(){this.persistenceHandler.discardQueue()}dispose(){this.updateSerializer.close(),this.persistenceHandler.dispose(),this.metricsCollector.dispose?.(),this.selectorManager.dispose?.()}emit(e,t){queueMicrotask((()=>{e.emit(t)}))}},B=class{store;eventHistory=[];stateHistory=[];unsubscribers=[];isTimeTraveling=!1;devTools=null;middlewareExecutions=[];activeTransactionCount=0;activeBatches=new Set;maxEvents;maxStateHistory;enableConsoleLogging;isSilent;logEvents;performanceThresholds;constructor(e,t={}){this.store=e,this.maxEvents=t.maxEvents??500,this.maxStateHistory=t.maxStateHistory??20,this.enableConsoleLogging=t.enableConsoleLogging??!1,this.isSilent=t.silent??!1,this.logEvents={updates:t.logEvents?.updates??!0,middleware:t.logEvents?.middleware??!0,transactions:t.logEvents?.transactions??!0,actions:t.logEvents?.actions??!0,selectors:t.logEvents?.selectors??!0},this.performanceThresholds={updateTime:t.performanceThresholds?.updateTime??50,middlewareTime:t.performanceThresholds?.middlewareTime??20},this.recordStateSnapshot([]),this.setupEventListeners()}_consoleLog(e,...t){this.isSilent||"function"==typeof console[e]&&console[e](...t)}setupEventListeners(){const e=["update:start","update:complete","middleware:start","middleware:complete","middleware:error","middleware:blocked","transaction:start","transaction:complete","transaction:error","middleware:executed","action:start","action:complete","action:error","selector:accessed"];for(const t of e){const e=t.startsWith("update")&&this.logEvents.updates||t.startsWith("middleware")&&this.logEvents.middleware||t.startsWith("transaction")&&this.logEvents.transactions||t.startsWith("action")&&this.logEvents.actions||t.startsWith("selector")&&this.logEvents.selectors;this.unsubscribers.push(this.store.on(t,(s=>{"update:complete"!==t||s.blocked||this.isTimeTraveling||this.recordStateSnapshot(s.deltas),"middleware:executed"===t?this.middlewareExecutions.push(s):"transaction:start"===t?this.activeTransactionCount++:"transaction:complete"!==t&&"transaction:error"!==t||(this.activeTransactionCount=Math.max(0,this.activeTransactionCount-1)),s.batchId&&(t.endsWith("start")?this.activeBatches.add(s.batchId):(t.endsWith("complete")||t.endsWith("error"))&&this.activeBatches.delete(s.batchId)),this.recordEvent(t,s),this.enableConsoleLogging&&e&&this._log(t,s),this._checkPerformance(t,s)})))}}recordStateSnapshot(e){const t={state:this.store.get(!0),timestamp:Date.now(),deltas:e};this.stateHistory.unshift(t),this.stateHistory.length>this.maxStateHistory&&this.stateHistory.pop()}recordEvent(e,t){const s={type:e,timestamp:Date.now(),data:structuredClone(t)};this.eventHistory.unshift(s),this.eventHistory.length>this.maxEvents&&this.eventHistory.pop()}getEventHistory(){return structuredClone(this.eventHistory)}getStateHistory(){return structuredClone(this.stateHistory)}getMiddlewareExecutions(){return this.middlewareExecutions}getTransactionStatus(){return{activeTransactions:this.activeTransactionCount,activeBatches:Array.from(this.activeBatches)}}createLoggingMiddleware(e={}){const{logLevel:t="debug",logUpdates:s=!0}=e;return(e,i)=>{if(s){(console[t]||console.log)("State Update:",i)}return i}}createValidationMiddleware(e){return(t,s)=>{const i=e(t,s);return"boolean"==typeof i?i:(!i.valid&&i.reason&&this._consoleLog("warn","Validation failed:",i.reason),i.valid)}}getRecentChanges(e=5){const t=[],s=Math.min(e,this.stateHistory.length);for(let e=0;e<s;e++){const s=this.stateHistory[e];if(!s.deltas||0===s.deltas.length)continue;const i={},r={},n=(e,t,s)=>{t.reduce(((e,i,r)=>(r===t.length-1?e[i]=s:e[i]=e[i]??{},e[i])),e)};for(const e of s.deltas){const t=e.path.split(".");n(i,t,e.oldValue),n(r,t,e.newValue)}t.push({timestamp:s.timestamp,changedPaths:s.deltas.map((e=>e.path)),from:i,to:r})}return t}clearHistory(){this.eventHistory=[],this.stateHistory.length>0&&(this.stateHistory=[this.stateHistory[0]])}getHistoryForAction(e){return this.eventHistory.filter((t=>t.data?.actionId===e))}async replay(e){const t=this.eventHistory.filter((e=>"update:start"===e.type))[e];t?.data.update?(this._consoleLog("log",`Replaying event at index ${e}:`,t),await this.store.set(t.data.update,{force:!0})):this._consoleLog("warn",`No replayable event found at index ${e}.`)}createTimeTravel(){let e=0,t=[];const s=this.store.on("update:complete",(s=>{this.isTimeTraveling||s.blocked||(t=[],e=0)}));this.unsubscribers.push(s);const i=()=>this.stateHistory.length,r=()=>e<i()-1,n=()=>t.length>0;return{canUndo:r,canRedo:n,undo:async()=>{if(!r())return;t.unshift(this.stateHistory[e]),e++;const s=this.stateHistory[e].state;this.isTimeTraveling=!0,await this.store.set({...s},{force:!0}),this.isTimeTraveling=!1},redo:async()=>{if(!n())return;const s=t.shift();e--,this.isTimeTraveling=!0,await this.store.set({...s.state},{force:!0}),this.isTimeTraveling=!1},length:i,clear:()=>{t=[],e=0}}}async saveSession(e){const t=this.store.id(),s={eventHistory:this.eventHistory,stateHistory:this.stateHistory};return Promise.resolve(e.set(t,s))}async loadSession(e){const t=await Promise.resolve(e.get());return!!t&&(this.eventHistory=t.eventHistory||[],this.stateHistory=t.stateHistory||[],this.stateHistory.length>0&&await this.store.set(this.stateHistory[0].state,{force:!0}),!0)}exportSession(){const e={eventHistory:this.eventHistory,stateHistory:this.stateHistory},t=new Blob([JSON.stringify(e,null,2)],{type:"application/json"}),s=URL.createObjectURL(t),i=document.createElement("a");i.href=s,i.download=`store-observer-session-${(new Date).toISOString()}.json`,i.click(),URL.revokeObjectURL(s)}importSession(e){return new Promise(((t,s)=>{const i=new FileReader;i.onload=async e=>{try{const s=JSON.parse(e.target?.result);this.eventHistory=s.eventHistory||[],this.stateHistory=s.stateHistory||[],this.stateHistory.length>0&&await this.store.set(this.stateHistory[0].state,{force:!0}),t()}catch(e){s(e)}},i.onerror=e=>s(e),i.readAsText(e)}))}disconnect(){this.unsubscribers.forEach((e=>e())),this.unsubscribers=[],this.devTools?.disconnect(),this.clearHistory()}_log(e,t){const s=new Date(t.timestamp||Date.now()).toISOString().split("T")[1].replace("Z","");if("update:start"===e)this._consoleLog("group",`%c⚡ Store Update Started [${s}]`,"color: #4a6da7");else if("update:complete"===e){if(t.blocked)this._consoleLog("warn",`%c✋ Update Blocked [${s}]`,"color: #bf8c0a",t.error);else{const e=t.deltas||[];e.length>0&&(this._consoleLog("log",`%c✅ Update Complete [${s}] - ${e.length} paths changed in ${t.duration?.toFixed(2)}ms`,"color: #2a9d8f"),this._consoleLog("table",e.map((e=>({path:e.path,oldValue:e.oldValue,newValue:e.newValue})))))}this._consoleLog("groupEnd")}else"middleware:start"===e?this._consoleLog("debug",`%c◀ Middleware "${t.name}" started [${s}] (${t.type})`,"color: #8c8c8c"):"middleware:complete"===e?this._consoleLog("debug",`%c▶ Middleware "${t.name}" completed [${s}] in ${t.duration?.toFixed(2)}ms`,"color: #7c9c7c"):"middleware:error"===e?this._consoleLog("error",`%c❌ Middleware "${t.name}" error [${s}]:`,"color: #e63946",t.error):"middleware:blocked"===e?this._consoleLog("warn",`%c🛑 Middleware "${t.name}" blocked update [${s}]`,"color: #e76f51"):"transaction:start"===e?this._consoleLog("group",`%c📦 Transaction Started [${s}]`,"color: #6d597a"):"transaction:complete"===e?(this._consoleLog("log",`%c📦 Transaction Complete [${s}]`,"color: #355070"),this._consoleLog("groupEnd")):"transaction:error"===e?(this._consoleLog("error",`%c📦 Transaction Error [${s}]:`,"color: #e56b6f",t.error),this._consoleLog("groupEnd")):"action:start"===e?this._consoleLog("group",`%c🚀 Action "${t.name}" Started [${s}]`,"color: #9b59b6",{params:t.params}):"action:complete"===e?(this._consoleLog("log",`%c✔️ Action "${t.name}" Complete [${s}] in ${t.duration?.toFixed(2)}ms`,"color: #2ecc71"),this._consoleLog("groupEnd")):"action:error"===e?(this._consoleLog("error",`%c🔥 Action "${t.name}" Error [${s}]:`,"color: #e74c3c",t.error),this._consoleLog("groupEnd")):"selector:accessed"===e&&this._consoleLog("debug",`%c👀 Selector Accessed [${s}] in ${t.duration?.toFixed(2)}ms`,"color: #f1c40f",{accessedPaths:t.accessedPaths,selectorId:t.selectorId})}_checkPerformance(e,t){this.enableConsoleLogging&&("update:complete"===e&&!t.blocked&&t.duration>this.performanceThresholds.updateTime&&this._consoleLog("warn",`%c⚠️ Slow update detected [${t.duration.toFixed(2)}ms]`,"color: #ff9f1c",{deltas:t.deltas,threshold:this.performanceThresholds.updateTime}),"middleware:complete"===e&&t.duration>this.performanceThresholds.middlewareTime&&this._consoleLog("warn",`%c⚠️ Slow middleware "${t.name}" [${t.duration.toFixed(2)}ms]`,"color: #ff9f1c",{threshold:this.performanceThresholds.middlewareTime}))}};export{k as ActionCancelledError,A as ActionManager,d as DELETE_SYMBOL,L as ReactiveDataStore,B as StoreObserver,E as UnknownActionError,f as createDerivePaths,y as createDiff,l as createMerge,v as derivePaths,w as diff,u as merge,h as shallowClone};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asaidimu/utils-store",
3
- "version": "10.1.0",
3
+ "version": "10.1.2",
4
4
  "description": "A reactive data store",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -29,8 +29,11 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@asaidimu/utils-persistence": "6.0.0",
33
- "uuid": "^11.1.0"
32
+ "uuid": "^11.1.0",
33
+ "@asaidimu/utils-events": "1.1.0"
34
+ },
35
+ "peerDependencies": {
36
+ "@asaidimu/utils-persistence": "6.1.1"
34
37
  },
35
38
  "exports": {
36
39
  ".": {