@ngstato/core 0.4.1 → 0.4.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/dist/index.mjs CHANGED
@@ -23,6 +23,13 @@ function subscribeToAction(action, listener) {
23
23
  }
24
24
 
25
25
  // src/store.ts
26
+ var DEVTOOLS_DISPATCH_KEY = "__NGSTATO_DISPATCH_HOOK__";
27
+ function getDispatchHook() {
28
+ return globalThis[DEVTOOLS_DISPATCH_KEY];
29
+ }
30
+ function setDispatchHook(hook) {
31
+ globalThis[DEVTOOLS_DISPATCH_KEY] = hook;
32
+ }
26
33
  var StatoStore = class {
27
34
  // Le state interne — jamais accessible directement
28
35
  _state;
@@ -41,6 +48,7 @@ var StatoStore = class {
41
48
  _publicActions = {};
42
49
  _initialized = false;
43
50
  _timeTraveling = false;
51
+ _devToolsStoreName = null;
44
52
  _effects = [];
45
53
  _createMemoizedSelector(fn) {
46
54
  let initialized = false;
@@ -207,6 +215,9 @@ var StatoStore = class {
207
215
  if (hasChanged) {
208
216
  this._hooks.onStateChange?.(prevState, nextState);
209
217
  }
218
+ if (this._devToolsStoreName) {
219
+ getDispatchHook()?.(this._devToolsStoreName, actionName, prevState, nextState, duration, "success");
220
+ }
210
221
  if (publicAction) {
211
222
  emitActionEvent({
212
223
  action: publicAction,
@@ -219,6 +230,10 @@ var StatoStore = class {
219
230
  }
220
231
  } catch (error) {
221
232
  this._hooks.onError?.(error, actionName);
233
+ const errDuration = Date.now() - start;
234
+ if (this._devToolsStoreName) {
235
+ getDispatchHook()?.(this._devToolsStoreName, actionName, prevState, prevState, errDuration, "error", error.message);
236
+ }
222
237
  if (publicAction) {
223
238
  emitActionEvent({
224
239
  action: publicAction,
@@ -226,7 +241,7 @@ var StatoStore = class {
226
241
  args,
227
242
  store: this._publicStore,
228
243
  status: "error",
229
- duration: Date.now() - start,
244
+ duration: errDuration,
230
245
  error
231
246
  });
232
247
  }
@@ -1891,48 +1906,42 @@ function createDevTools(maxLogs = 100) {
1891
1906
  }
1892
1907
  };
1893
1908
  }
1894
- var devTools = createDevTools();
1909
+ var DEVTOOLS_KEY = "__NGSTATO_DEVTOOLS__";
1910
+ var devTools = globalThis[DEVTOOLS_KEY] ?? (globalThis[DEVTOOLS_KEY] = createDevTools());
1911
+ function ensureDispatchHook() {
1912
+ setDispatchHook((storeName, actionName, prevState, nextState, duration, status, error) => {
1913
+ devTools.logAction({
1914
+ name: `[${storeName}] ${actionName}`,
1915
+ storeName,
1916
+ args: [],
1917
+ duration,
1918
+ status,
1919
+ error,
1920
+ prevState: { ...prevState },
1921
+ nextState: { ...nextState }
1922
+ });
1923
+ });
1924
+ }
1895
1925
  function connectDevTools(store, storeName) {
1896
1926
  if (!devTools) return;
1897
- let prevState = {};
1898
1927
  const internalStore = store.__store__;
1899
1928
  if (!internalStore) return;
1929
+ internalStore._devToolsStoreName = storeName;
1900
1930
  devTools.registerStore(storeName, store, internalStore);
1901
- const existingHooks = { ...internalStore["_hooks"] };
1902
- internalStore["_hooks"] = {
1903
- ...existingHooks,
1904
- onAction(name, args) {
1905
- prevState = store.getState();
1906
- existingHooks.onAction?.(name, args);
1907
- },
1908
- onActionDone(name, duration) {
1909
- const nextState = store.getState();
1910
- devTools.logAction({
1911
- name: `[${storeName}] ${name}`,
1912
- storeName,
1913
- args: [],
1914
- duration,
1915
- status: "success",
1916
- prevState: { ...prevState },
1917
- nextState: { ...nextState }
1918
- });
1919
- existingHooks.onActionDone?.(name, duration);
1920
- },
1921
- onError(error, actionName) {
1922
- devTools.logAction({
1923
- name: `[${storeName}] ${actionName}`,
1924
- storeName,
1925
- args: [],
1926
- duration: 0,
1927
- status: "error",
1928
- error: error.message,
1929
- prevState: { ...prevState },
1930
- nextState: { ...prevState }
1931
- });
1932
- existingHooks.onError?.(error, actionName);
1933
- },
1934
- onStateChange: existingHooks.onStateChange
1935
- };
1931
+ ensureDispatchHook();
1932
+ try {
1933
+ const currentState = store.getState();
1934
+ devTools.logAction({
1935
+ name: `[${storeName}] @@INIT`,
1936
+ storeName,
1937
+ args: [],
1938
+ duration: 0,
1939
+ status: "success",
1940
+ prevState: {},
1941
+ nextState: { ...currentState }
1942
+ });
1943
+ } catch {
1944
+ }
1936
1945
  }
1937
1946
 
1938
1947
  export { StatoHttp, StatoHttpError, abortable, catchErrorStream, combineLatest, combineLatestStream, concatMapStream, configureHttp, connectDevTools, createDevTools, createEntityAdapter, createHttp, createStore, debounceStream, debounced, devTools, distinctUntilChanged, distinctUntilChangedStream, exclusive, exhaustMapStream, filterStream, forkJoin, fromStream, http, mapStream, mergeFeatures, mergeMapStream, on, optimistic, pipeStream, queued, race, retryStream, retryable, switchMapStream, throttleStream, throttled, withEntities, withPersist, withProps };