@ngstato/core 0.4.0 → 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.js CHANGED
@@ -25,6 +25,13 @@ function subscribeToAction(action, listener) {
25
25
  }
26
26
 
27
27
  // src/store.ts
28
+ var DEVTOOLS_DISPATCH_KEY = "__NGSTATO_DISPATCH_HOOK__";
29
+ function getDispatchHook() {
30
+ return globalThis[DEVTOOLS_DISPATCH_KEY];
31
+ }
32
+ function setDispatchHook(hook) {
33
+ globalThis[DEVTOOLS_DISPATCH_KEY] = hook;
34
+ }
28
35
  var StatoStore = class {
29
36
  // Le state interne — jamais accessible directement
30
37
  _state;
@@ -43,6 +50,7 @@ var StatoStore = class {
43
50
  _publicActions = {};
44
51
  _initialized = false;
45
52
  _timeTraveling = false;
53
+ _devToolsStoreName = null;
46
54
  _effects = [];
47
55
  _createMemoizedSelector(fn) {
48
56
  let initialized = false;
@@ -209,6 +217,9 @@ var StatoStore = class {
209
217
  if (hasChanged) {
210
218
  this._hooks.onStateChange?.(prevState, nextState);
211
219
  }
220
+ if (this._devToolsStoreName) {
221
+ getDispatchHook()?.(this._devToolsStoreName, actionName, prevState, nextState, duration, "success");
222
+ }
212
223
  if (publicAction) {
213
224
  emitActionEvent({
214
225
  action: publicAction,
@@ -221,6 +232,10 @@ var StatoStore = class {
221
232
  }
222
233
  } catch (error) {
223
234
  this._hooks.onError?.(error, actionName);
235
+ const errDuration = Date.now() - start;
236
+ if (this._devToolsStoreName) {
237
+ getDispatchHook()?.(this._devToolsStoreName, actionName, prevState, prevState, errDuration, "error", error.message);
238
+ }
224
239
  if (publicAction) {
225
240
  emitActionEvent({
226
241
  action: publicAction,
@@ -228,7 +243,7 @@ var StatoStore = class {
228
243
  args,
229
244
  store: this._publicStore,
230
245
  status: "error",
231
- duration: Date.now() - start,
246
+ duration: errDuration,
232
247
  error
233
248
  });
234
249
  }
@@ -585,7 +600,7 @@ function fromStream(setupFn, updateFn, options) {
585
600
  // src/helpers/optimistic.ts
586
601
  function optimistic(immediate, confirm) {
587
602
  return async (state, ...args) => {
588
- const snapshot = structuredClone(state);
603
+ const snapshot = JSON.parse(JSON.stringify(state));
589
604
  immediate(state, ...args);
590
605
  try {
591
606
  await confirm(state, ...args);
@@ -1893,48 +1908,42 @@ function createDevTools(maxLogs = 100) {
1893
1908
  }
1894
1909
  };
1895
1910
  }
1896
- var devTools = createDevTools();
1911
+ var DEVTOOLS_KEY = "__NGSTATO_DEVTOOLS__";
1912
+ var devTools = globalThis[DEVTOOLS_KEY] ?? (globalThis[DEVTOOLS_KEY] = createDevTools());
1913
+ function ensureDispatchHook() {
1914
+ setDispatchHook((storeName, actionName, prevState, nextState, duration, status, error) => {
1915
+ devTools.logAction({
1916
+ name: `[${storeName}] ${actionName}`,
1917
+ storeName,
1918
+ args: [],
1919
+ duration,
1920
+ status,
1921
+ error,
1922
+ prevState: { ...prevState },
1923
+ nextState: { ...nextState }
1924
+ });
1925
+ });
1926
+ }
1897
1927
  function connectDevTools(store, storeName) {
1898
1928
  if (!devTools) return;
1899
- let prevState = {};
1900
1929
  const internalStore = store.__store__;
1901
1930
  if (!internalStore) return;
1931
+ internalStore._devToolsStoreName = storeName;
1902
1932
  devTools.registerStore(storeName, store, internalStore);
1903
- const existingHooks = { ...internalStore["_hooks"] };
1904
- internalStore["_hooks"] = {
1905
- ...existingHooks,
1906
- onAction(name, args) {
1907
- prevState = store.getState();
1908
- existingHooks.onAction?.(name, args);
1909
- },
1910
- onActionDone(name, duration) {
1911
- const nextState = store.getState();
1912
- devTools.logAction({
1913
- name: `[${storeName}] ${name}`,
1914
- storeName,
1915
- args: [],
1916
- duration,
1917
- status: "success",
1918
- prevState: { ...prevState },
1919
- nextState: { ...nextState }
1920
- });
1921
- existingHooks.onActionDone?.(name, duration);
1922
- },
1923
- onError(error, actionName) {
1924
- devTools.logAction({
1925
- name: `[${storeName}] ${actionName}`,
1926
- storeName,
1927
- args: [],
1928
- duration: 0,
1929
- status: "error",
1930
- error: error.message,
1931
- prevState: { ...prevState },
1932
- nextState: { ...prevState }
1933
- });
1934
- existingHooks.onError?.(error, actionName);
1935
- },
1936
- onStateChange: existingHooks.onStateChange
1937
- };
1933
+ ensureDispatchHook();
1934
+ try {
1935
+ const currentState = store.getState();
1936
+ devTools.logAction({
1937
+ name: `[${storeName}] @@INIT`,
1938
+ storeName,
1939
+ args: [],
1940
+ duration: 0,
1941
+ status: "success",
1942
+ prevState: {},
1943
+ nextState: { ...currentState }
1944
+ });
1945
+ } catch {
1946
+ }
1938
1947
  }
1939
1948
 
1940
1949
  exports.StatoHttp = StatoHttp;