All files store.ts

93.33% Statements 42/45
85.71% Branches 18/21
92.3% Functions 12/13
95.23% Lines 40/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134  2x 2x 2x           2x                     2x         9x 1x   8x   8x 8x       8x   8x   8x                 5x   5x             5x           5x 3x               5x         8x 4x   5x 5x 5x 5x     3x 3x       8x 8x                   2x 2x 2x 1x   1x                   2x         2x 1x   2x       1x 1x      
import { Listener, Store, StoreOptions } from "./types";
import { BroadcastStrategy } from "./enums";
import { globalMiddlewares } from "./middleware";
import { persistState, getPersistedState, resolveStorage } from "./storage";
 
/**
 * Global registry for all defined stores.
 * @internal
 */
const globalStoreRegistry = new Map<string, Store<any>>();
 
/**
 * Defines a new global store.
 * @template T
 * @param {string} name - Unique store name.
 * @param {T} initialState - Initial state.
 * @param {StoreOptions} [options={}] - Store options.
 * @returns {Store<T>} The created store instance.
 * @throws If the store name is already registered.
 */
export function defineGlobalStore<T extends object>(
  name: string,
  initialState: T,
  options: StoreOptions = {}
): Store<T> {
  if (globalStoreRegistry.has(name)) {
    throw new Error(`[stadojs] Store "${name}" is already registered.`);
  }
  const storage = resolveStorage(options.storageType);
  let state: T;
  (async () => {
    state = options.persist
      ? await getPersistedState(name, initialState, storage, options)
      : initialState;
  })();
  const listeners = new Set<Listener<T>>();
  const shouldBroadcastTab =
    options.broadcast === BroadcastStrategy.CrossTab ||
    options.broadcast === BroadcastStrategy.All;
  const channel = shouldBroadcastTab
    ? new BroadcastChannel(`store:${name}`)
    : null;
 
  /**
   * Notifies all listeners of a state change.
   */
  function notifyListeners() {
    // Execute global middlewares
    globalMiddlewares.forEach((mw) => mw({ name, state }));
    // Notify registered listeners
    listeners.forEach((listener) => listener(state));
  }
 
  /**
   * Broadcasts state changes to other tabs or locally.
   */
  function broadcastState() {
    Iif (
      options.broadcast === BroadcastStrategy.Local ||
      options.broadcast === BroadcastStrategy.All
    ) {
      window.dispatchEvent(new CustomEvent(`store:${name}`, { detail: state }));
    }
    if (channel) {
      channel.postMessage(state);
    }
  }
 
  /**
   * Persists the current state if enabled.
   */
  function persistCurrentState() {
    Iif (options.persist) {
      persistState(name, state, storage, options);
    }
  }
 
  const store: Store<T> = {
    get: () => state,
    set: (update) => {
      state = { ...state, ...update };
      notifyListeners();
      broadcastState();
      persistCurrentState();
    },
    on: (listener) => {
      listeners.add(listener);
      return () => listeners.delete(listener);
    },
  };
 
  globalStoreRegistry.set(name, store);
  return store;
}
 
/**
 * Retrieves a global store by name.
 * @template T
 * @param {string} name - Store name.
 * @returns {Store<T>} The store instance.
 * @throws If the store is not found.
 */
export function useGlobalStore<T extends object>(name: string): Store<T> {
  const store = globalStoreRegistry.get(name);
  if (!store) {
    throw new Error(`[stadojs] Store "${name}" not found.`);
  }
  return store as Store<T>;
}
 
/**
 * Emits a global store event to listeners or other tabs.
 * @template T
 * @param {string} name - Store name.
 * @param {T} data - Data to emit.
 * @param {BroadcastStrategy} [target=BroadcastStrategy.CrossTab] - Broadcast target.
 */
export function emitGlobalStoreEvent<T extends object>(
  name: string,
  data: T,
  target: BroadcastStrategy = BroadcastStrategy.CrossTab
): void {
  if (target === BroadcastStrategy.Local || target === BroadcastStrategy.All) {
    window.dispatchEvent(new CustomEvent(`store:${name}`, { detail: data }));
  }
  if (
    target === BroadcastStrategy.CrossTab ||
    target === BroadcastStrategy.All
  ) {
    const channel = new BroadcastChannel(`store:${name}`);
    channel.postMessage(data);
  }
}