@player-devtools/utils 0.13.1--canary.13.753 → 0.13.1--canary.16.915

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.
@@ -20,9 +20,32 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/utils/core/src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
- dsetAssign: () => dsetAssign
23
+ dsetAssign: () => dsetAssign,
24
+ useStateReducer: () => useStateReducer
24
25
  });
25
26
  module.exports = __toCommonJS(src_exports);
27
+ var useStateReducer = (reducer, initialState) => {
28
+ let state = initialState;
29
+ const subscribers = /* @__PURE__ */ new Set();
30
+ return {
31
+ getState: () => state,
32
+ /** Subscribe to state changes; returns an unsubscribe function. */
33
+ subscribe(subscriber) {
34
+ subscribers.add(subscriber);
35
+ subscriber(state);
36
+ return () => subscribers.delete(subscriber);
37
+ },
38
+ /** Dispatch an action through the reducer, then run side-effects. */
39
+ dispatch(action) {
40
+ const prevState = state;
41
+ const nextState = reducer(prevState, action);
42
+ if (nextState !== prevState) {
43
+ state = nextState;
44
+ for (const sub of subscribers) sub(state);
45
+ }
46
+ }
47
+ };
48
+ };
26
49
  function deepAssign(target, source, merge = false) {
27
50
  if (Array.isArray(target) && Array.isArray(source)) {
28
51
  while (!merge && target.length > source.length) target.pop();
@@ -55,6 +78,7 @@ function dsetAssign(obj, keys, value, merge = false) {
55
78
  }
56
79
  // Annotate the CommonJS export names for ESM import in node:
57
80
  0 && (module.exports = {
58
- dsetAssign
81
+ dsetAssign,
82
+ useStateReducer
59
83
  });
60
84
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/utils/core/src/index.ts"],"sourcesContent":["/**\n * Recursively assigns the contents of `source` into `target`, preserving\n * existing object and array references to maintain key insertion order.\n *\n * - **Objects**: keys present in `target` but absent in `source` are deleted\n * (unless `merge` is true), then each source entry is recursed into.\n * - **Arrays**: target is truncated or extended to match source length\n * (unless `merge` is true), then each element is recursed into.\n * - **Primitives / type mismatch**: source is returned directly; the caller\n * is responsible for assigning the return value.\n *\n * @param target - The existing value to assign into.\n * @param source - The new value to assign from.\n * @param merge - When true, stale keys and excess array elements are preserved\n * rather than deleted. Defaults to false.\n * @returns The mutated `target`, or `source` if types are incompatible.\n */\nfunction deepAssign<T, V>(target: T, source: V, merge: boolean = false) {\n if (Array.isArray(target) && Array.isArray(source)) {\n while (!merge && target.length > source.length) target.pop();\n // recurse and assign new values\n for (const [index, item] of source.entries()) {\n target[index] = deepAssign(target[index], item, merge);\n }\n } else if (\n target &&\n typeof target === \"object\" &&\n !Array.isArray(target) &&\n source &&\n typeof source === \"object\" &&\n !Array.isArray(source)\n ) {\n const record = target as Record<string, unknown>;\n\n // clear stale keys\n if (!merge)\n for (const key of Object.keys(target)) {\n if (!(key in source)) delete record[key];\n }\n // recurse and assign new values\n for (const [key, item] of Object.entries(source)) {\n record[key] = deepAssign(record[key], item, merge);\n }\n } else {\n // new data doesn't match the types, so there is no referential equality to retain\n return source;\n }\n\n return target;\n}\n\ntype Index = string | number;\n\n/**\n * Deep-sets a value at a key path on an object, preserving existing nested\n * object and array references to maintain key insertion order.\n *\n * Intermediate objects are auto-vivified as needed. The final assignment is\n * handled by {@link deepAssign}, which mutates the existing value in place\n * rather than replacing it wholesale.\n *\n * @param obj - The root object to assign into.\n * @param keys - Path of keys leading to the target location.\n * @param value - The value to assign at the path.\n * @param merge - When true, stale keys and excess array elements are preserved\n * rather than deleted. Defaults to false.\n * @throws If `keys` is empty or contains an undefined key.\n *\n * @see {@link https://github.com/lukeed/dset} for the original dset implementation that inspired this API\n */\nexport function dsetAssign<V>(\n obj: Record<Index, unknown>,\n keys: Array<Index>,\n value: V,\n merge: boolean = false,\n): void {\n const key = keys[keys.length - 1];\n if (key === undefined)\n throw Error(\"Unable to assign at path containing undefined keys\");\n\n // Walk the path, auto-vivifying intermediate objects when necessary\n const target = keys\n .slice(0, -1)\n .reduce(\n (acc, key) => (acc[key] ??= {}) as Record<string | number, unknown>,\n obj,\n );\n\n // Deep assign the value into the target object for key\n target[key] = deepAssign(target[key], value, merge);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAiBA,SAAS,WAAiB,QAAW,QAAW,QAAiB,OAAO;AACtE,MAAI,MAAM,QAAQ,MAAM,KAAK,MAAM,QAAQ,MAAM,GAAG;AAClD,WAAO,CAAC,SAAS,OAAO,SAAS,OAAO,OAAQ,QAAO,IAAI;AAE3D,eAAW,CAAC,OAAO,IAAI,KAAK,OAAO,QAAQ,GAAG;AAC5C,aAAO,KAAK,IAAI,WAAW,OAAO,KAAK,GAAG,MAAM,KAAK;AAAA,IACvD;AAAA,EACF,WACE,UACA,OAAO,WAAW,YAClB,CAAC,MAAM,QAAQ,MAAM,KACrB,UACA,OAAO,WAAW,YAClB,CAAC,MAAM,QAAQ,MAAM,GACrB;AACA,UAAM,SAAS;AAGf,QAAI,CAAC;AACH,iBAAW,OAAO,OAAO,KAAK,MAAM,GAAG;AACrC,YAAI,EAAE,OAAO,QAAS,QAAO,OAAO,GAAG;AAAA,MACzC;AAEF,eAAW,CAAC,KAAK,IAAI,KAAK,OAAO,QAAQ,MAAM,GAAG;AAChD,aAAO,GAAG,IAAI,WAAW,OAAO,GAAG,GAAG,MAAM,KAAK;AAAA,IACnD;AAAA,EACF,OAAO;AAEL,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAqBO,SAAS,WACd,KACA,MACA,OACA,QAAiB,OACX;AACN,QAAM,MAAM,KAAK,KAAK,SAAS,CAAC;AAChC,MAAI,QAAQ;AACV,UAAM,MAAM,oDAAoD;AAGlE,QAAM,SAAS,KACZ,MAAM,GAAG,EAAE,EACX;AAAA,IACC,CAAC,KAAKA,SAAS,IAAAA,UAAA,IAAAA,QAAa,CAAC;AAAA,IAC7B;AAAA,EACF;AAGF,SAAO,GAAG,IAAI,WAAW,OAAO,GAAG,GAAG,OAAO,KAAK;AACpD;","names":["key"]}
1
+ {"version":3,"sources":["../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/utils/core/src/index.ts"],"sourcesContent":["export type Reducer<State, Action> = (state: State, action: Action) => State;\nexport type Dispatch<Action> = (action: Action) => void;\nexport type Subscriber<State> = (state: State) => void;\nexport type Subscribe<State> = (subscriber: Subscriber<State>) => Unsubscribe;\nexport type Unsubscribe = () => void;\n\nexport interface Store<State, Action> {\n getState: () => State;\n subscribe: Subscribe<State>;\n dispatch: Dispatch<Action>;\n}\n\nexport const useStateReducer = <State, Action>(\n reducer: Reducer<State, Action>,\n initialState: State,\n): Store<State, Action> => {\n let state = initialState;\n const subscribers = new Set<Subscriber<State>>();\n return {\n getState: () => state,\n\n /** Subscribe to state changes; returns an unsubscribe function. */\n subscribe(subscriber: Subscriber<State>): Unsubscribe {\n subscribers.add(subscriber);\n subscriber(state);\n return () => subscribers.delete(subscriber);\n },\n\n /** Dispatch an action through the reducer, then run side-effects. */\n dispatch(action: Action): void {\n const prevState = state;\n const nextState = reducer(prevState, action);\n\n if (nextState !== prevState) {\n state = nextState;\n for (const sub of subscribers) sub(state);\n }\n },\n };\n};\n\n/**\n * Recursively assigns the contents of `source` into `target`, preserving\n * existing object and array references to maintain key insertion order.\n *\n * - **Objects**: keys present in `target` but absent in `source` are deleted\n * (unless `merge` is true), then each source entry is recursed into.\n * - **Arrays**: target is truncated or extended to match source length\n * (unless `merge` is true), then each element is recursed into.\n * - **Primitives / type mismatch**: source is returned directly; the caller\n * is responsible for assigning the return value.\n *\n * @param target - The existing value to assign into.\n * @param source - The new value to assign from.\n * @param merge - When true, stale keys and excess array elements are preserved\n * rather than deleted. Defaults to false.\n * @returns The mutated `target`, or `source` if types are incompatible.\n */\nfunction deepAssign<T, V>(target: T, source: V, merge: boolean = false) {\n if (Array.isArray(target) && Array.isArray(source)) {\n while (!merge && target.length > source.length) target.pop();\n // recurse and assign new values\n for (const [index, item] of source.entries()) {\n target[index] = deepAssign(target[index], item, merge);\n }\n } else if (\n target &&\n typeof target === \"object\" &&\n !Array.isArray(target) &&\n source &&\n typeof source === \"object\" &&\n !Array.isArray(source)\n ) {\n const record = target as Record<string, unknown>;\n\n // clear stale keys\n if (!merge)\n for (const key of Object.keys(target)) {\n if (!(key in source)) delete record[key];\n }\n // recurse and assign new values\n for (const [key, item] of Object.entries(source)) {\n record[key] = deepAssign(record[key], item, merge);\n }\n } else {\n // new data doesn't match the types, so there is no referential equality to retain\n return source;\n }\n\n return target;\n}\n\ntype Index = string | number;\n\n/**\n * Deep-sets a value at a key path on an object, preserving existing nested\n * object and array references to maintain key insertion order.\n *\n * Intermediate objects are auto-vivified as needed. The final assignment is\n * handled by {@link deepAssign}, which mutates the existing value in place\n * rather than replacing it wholesale.\n *\n * @param obj - The root object to assign into.\n * @param keys - Path of keys leading to the target location.\n * @param value - The value to assign at the path.\n * @param merge - When true, stale keys and excess array elements are preserved\n * rather than deleted. Defaults to false.\n * @throws If `keys` is empty or contains an undefined key.\n *\n * @see {@link https://github.com/lukeed/dset} for the original dset implementation that inspired this API\n */\nexport function dsetAssign<V>(\n obj: Record<Index, unknown>,\n keys: Array<Index>,\n value: V,\n merge: boolean = false,\n): void {\n const key = keys[keys.length - 1];\n if (key === undefined)\n throw Error(\"Unable to assign at path containing undefined keys\");\n\n // Walk the path, auto-vivifying intermediate objects when necessary\n const target = keys\n .slice(0, -1)\n .reduce(\n (acc, key) => (acc[key] ??= {}) as Record<string | number, unknown>,\n obj,\n );\n\n // Deep assign the value into the target object for key\n target[key] = deepAssign(target[key], value, merge);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAYO,IAAM,kBAAkB,CAC7B,SACA,iBACyB;AACzB,MAAI,QAAQ;AACZ,QAAM,cAAc,oBAAI,IAAuB;AAC/C,SAAO;AAAA,IACL,UAAU,MAAM;AAAA;AAAA,IAGhB,UAAU,YAA4C;AACpD,kBAAY,IAAI,UAAU;AAC1B,iBAAW,KAAK;AAChB,aAAO,MAAM,YAAY,OAAO,UAAU;AAAA,IAC5C;AAAA;AAAA,IAGA,SAAS,QAAsB;AAC7B,YAAM,YAAY;AAClB,YAAM,YAAY,QAAQ,WAAW,MAAM;AAE3C,UAAI,cAAc,WAAW;AAC3B,gBAAQ;AACR,mBAAW,OAAO,YAAa,KAAI,KAAK;AAAA,MAC1C;AAAA,IACF;AAAA,EACF;AACF;AAmBA,SAAS,WAAiB,QAAW,QAAW,QAAiB,OAAO;AACtE,MAAI,MAAM,QAAQ,MAAM,KAAK,MAAM,QAAQ,MAAM,GAAG;AAClD,WAAO,CAAC,SAAS,OAAO,SAAS,OAAO,OAAQ,QAAO,IAAI;AAE3D,eAAW,CAAC,OAAO,IAAI,KAAK,OAAO,QAAQ,GAAG;AAC5C,aAAO,KAAK,IAAI,WAAW,OAAO,KAAK,GAAG,MAAM,KAAK;AAAA,IACvD;AAAA,EACF,WACE,UACA,OAAO,WAAW,YAClB,CAAC,MAAM,QAAQ,MAAM,KACrB,UACA,OAAO,WAAW,YAClB,CAAC,MAAM,QAAQ,MAAM,GACrB;AACA,UAAM,SAAS;AAGf,QAAI,CAAC;AACH,iBAAW,OAAO,OAAO,KAAK,MAAM,GAAG;AACrC,YAAI,EAAE,OAAO,QAAS,QAAO,OAAO,GAAG;AAAA,MACzC;AAEF,eAAW,CAAC,KAAK,IAAI,KAAK,OAAO,QAAQ,MAAM,GAAG;AAChD,aAAO,GAAG,IAAI,WAAW,OAAO,GAAG,GAAG,MAAM,KAAK;AAAA,IACnD;AAAA,EACF,OAAO;AAEL,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAqBO,SAAS,WACd,KACA,MACA,OACA,QAAiB,OACX;AACN,QAAM,MAAM,KAAK,KAAK,SAAS,CAAC;AAChC,MAAI,QAAQ;AACV,UAAM,MAAM,oDAAoD;AAGlE,QAAM,SAAS,KACZ,MAAM,GAAG,EAAE,EACX;AAAA,IACC,CAAC,KAAKA,SAAS,IAAAA,UAAA,IAAAA,QAAa,CAAC;AAAA,IAC7B;AAAA,EACF;AAGF,SAAO,GAAG,IAAI,WAAW,OAAO,GAAG,GAAG,OAAO,KAAK;AACpD;","names":["key"]}
@@ -1,4 +1,26 @@
1
1
  // ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/utils/core/src/index.ts
2
+ var useStateReducer = (reducer, initialState) => {
3
+ let state = initialState;
4
+ const subscribers = /* @__PURE__ */ new Set();
5
+ return {
6
+ getState: () => state,
7
+ /** Subscribe to state changes; returns an unsubscribe function. */
8
+ subscribe(subscriber) {
9
+ subscribers.add(subscriber);
10
+ subscriber(state);
11
+ return () => subscribers.delete(subscriber);
12
+ },
13
+ /** Dispatch an action through the reducer, then run side-effects. */
14
+ dispatch(action) {
15
+ const prevState = state;
16
+ const nextState = reducer(prevState, action);
17
+ if (nextState !== prevState) {
18
+ state = nextState;
19
+ for (const sub of subscribers) sub(state);
20
+ }
21
+ }
22
+ };
23
+ };
2
24
  function deepAssign(target, source, merge = false) {
3
25
  if (Array.isArray(target) && Array.isArray(source)) {
4
26
  while (!merge && target.length > source.length) target.pop();
@@ -30,6 +52,7 @@ function dsetAssign(obj, keys, value, merge = false) {
30
52
  target[key] = deepAssign(target[key], value, merge);
31
53
  }
32
54
  export {
33
- dsetAssign
55
+ dsetAssign,
56
+ useStateReducer
34
57
  };
35
58
  //# sourceMappingURL=index.mjs.map
package/dist/index.mjs CHANGED
@@ -1,4 +1,26 @@
1
1
  // ../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/utils/core/src/index.ts
2
+ var useStateReducer = (reducer, initialState) => {
3
+ let state = initialState;
4
+ const subscribers = /* @__PURE__ */ new Set();
5
+ return {
6
+ getState: () => state,
7
+ /** Subscribe to state changes; returns an unsubscribe function. */
8
+ subscribe(subscriber) {
9
+ subscribers.add(subscriber);
10
+ subscriber(state);
11
+ return () => subscribers.delete(subscriber);
12
+ },
13
+ /** Dispatch an action through the reducer, then run side-effects. */
14
+ dispatch(action) {
15
+ const prevState = state;
16
+ const nextState = reducer(prevState, action);
17
+ if (nextState !== prevState) {
18
+ state = nextState;
19
+ for (const sub of subscribers) sub(state);
20
+ }
21
+ }
22
+ };
23
+ };
2
24
  function deepAssign(target, source, merge = false) {
3
25
  if (Array.isArray(target) && Array.isArray(source)) {
4
26
  while (!merge && target.length > source.length) target.pop();
@@ -30,6 +52,7 @@ function dsetAssign(obj, keys, value, merge = false) {
30
52
  target[key] = deepAssign(target[key], value, merge);
31
53
  }
32
54
  export {
33
- dsetAssign
55
+ dsetAssign,
56
+ useStateReducer
34
57
  };
35
58
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/utils/core/src/index.ts"],"sourcesContent":["/**\n * Recursively assigns the contents of `source` into `target`, preserving\n * existing object and array references to maintain key insertion order.\n *\n * - **Objects**: keys present in `target` but absent in `source` are deleted\n * (unless `merge` is true), then each source entry is recursed into.\n * - **Arrays**: target is truncated or extended to match source length\n * (unless `merge` is true), then each element is recursed into.\n * - **Primitives / type mismatch**: source is returned directly; the caller\n * is responsible for assigning the return value.\n *\n * @param target - The existing value to assign into.\n * @param source - The new value to assign from.\n * @param merge - When true, stale keys and excess array elements are preserved\n * rather than deleted. Defaults to false.\n * @returns The mutated `target`, or `source` if types are incompatible.\n */\nfunction deepAssign<T, V>(target: T, source: V, merge: boolean = false) {\n if (Array.isArray(target) && Array.isArray(source)) {\n while (!merge && target.length > source.length) target.pop();\n // recurse and assign new values\n for (const [index, item] of source.entries()) {\n target[index] = deepAssign(target[index], item, merge);\n }\n } else if (\n target &&\n typeof target === \"object\" &&\n !Array.isArray(target) &&\n source &&\n typeof source === \"object\" &&\n !Array.isArray(source)\n ) {\n const record = target as Record<string, unknown>;\n\n // clear stale keys\n if (!merge)\n for (const key of Object.keys(target)) {\n if (!(key in source)) delete record[key];\n }\n // recurse and assign new values\n for (const [key, item] of Object.entries(source)) {\n record[key] = deepAssign(record[key], item, merge);\n }\n } else {\n // new data doesn't match the types, so there is no referential equality to retain\n return source;\n }\n\n return target;\n}\n\ntype Index = string | number;\n\n/**\n * Deep-sets a value at a key path on an object, preserving existing nested\n * object and array references to maintain key insertion order.\n *\n * Intermediate objects are auto-vivified as needed. The final assignment is\n * handled by {@link deepAssign}, which mutates the existing value in place\n * rather than replacing it wholesale.\n *\n * @param obj - The root object to assign into.\n * @param keys - Path of keys leading to the target location.\n * @param value - The value to assign at the path.\n * @param merge - When true, stale keys and excess array elements are preserved\n * rather than deleted. Defaults to false.\n * @throws If `keys` is empty or contains an undefined key.\n *\n * @see {@link https://github.com/lukeed/dset} for the original dset implementation that inspired this API\n */\nexport function dsetAssign<V>(\n obj: Record<Index, unknown>,\n keys: Array<Index>,\n value: V,\n merge: boolean = false,\n): void {\n const key = keys[keys.length - 1];\n if (key === undefined)\n throw Error(\"Unable to assign at path containing undefined keys\");\n\n // Walk the path, auto-vivifying intermediate objects when necessary\n const target = keys\n .slice(0, -1)\n .reduce(\n (acc, key) => (acc[key] ??= {}) as Record<string | number, unknown>,\n obj,\n );\n\n // Deep assign the value into the target object for key\n target[key] = deepAssign(target[key], value, merge);\n}\n"],"mappings":";AAiBA,SAAS,WAAiB,QAAW,QAAW,QAAiB,OAAO;AACtE,MAAI,MAAM,QAAQ,MAAM,KAAK,MAAM,QAAQ,MAAM,GAAG;AAClD,WAAO,CAAC,SAAS,OAAO,SAAS,OAAO,OAAQ,QAAO,IAAI;AAE3D,eAAW,CAAC,OAAO,IAAI,KAAK,OAAO,QAAQ,GAAG;AAC5C,aAAO,KAAK,IAAI,WAAW,OAAO,KAAK,GAAG,MAAM,KAAK;AAAA,IACvD;AAAA,EACF,WACE,UACA,OAAO,WAAW,YAClB,CAAC,MAAM,QAAQ,MAAM,KACrB,UACA,OAAO,WAAW,YAClB,CAAC,MAAM,QAAQ,MAAM,GACrB;AACA,UAAM,SAAS;AAGf,QAAI,CAAC;AACH,iBAAW,OAAO,OAAO,KAAK,MAAM,GAAG;AACrC,YAAI,EAAE,OAAO,QAAS,QAAO,OAAO,GAAG;AAAA,MACzC;AAEF,eAAW,CAAC,KAAK,IAAI,KAAK,OAAO,QAAQ,MAAM,GAAG;AAChD,aAAO,GAAG,IAAI,WAAW,OAAO,GAAG,GAAG,MAAM,KAAK;AAAA,IACnD;AAAA,EACF,OAAO;AAEL,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAqBO,SAAS,WACd,KACA,MACA,OACA,QAAiB,OACX;AACN,QAAM,MAAM,KAAK,KAAK,SAAS,CAAC;AAChC,MAAI,QAAQ;AACV,UAAM,MAAM,oDAAoD;AAGlE,QAAM,SAAS,KACZ,MAAM,GAAG,EAAE,EACX;AAAA,IACC,CAAC,KAAKA,SAAS,IAAAA,UAAA,IAAAA,QAAa,CAAC;AAAA,IAC7B;AAAA,EACF;AAGF,SAAO,GAAG,IAAI,WAAW,OAAO,GAAG,GAAG,OAAO,KAAK;AACpD;","names":["key"]}
1
+ {"version":3,"sources":["../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/utils/core/src/index.ts"],"sourcesContent":["export type Reducer<State, Action> = (state: State, action: Action) => State;\nexport type Dispatch<Action> = (action: Action) => void;\nexport type Subscriber<State> = (state: State) => void;\nexport type Subscribe<State> = (subscriber: Subscriber<State>) => Unsubscribe;\nexport type Unsubscribe = () => void;\n\nexport interface Store<State, Action> {\n getState: () => State;\n subscribe: Subscribe<State>;\n dispatch: Dispatch<Action>;\n}\n\nexport const useStateReducer = <State, Action>(\n reducer: Reducer<State, Action>,\n initialState: State,\n): Store<State, Action> => {\n let state = initialState;\n const subscribers = new Set<Subscriber<State>>();\n return {\n getState: () => state,\n\n /** Subscribe to state changes; returns an unsubscribe function. */\n subscribe(subscriber: Subscriber<State>): Unsubscribe {\n subscribers.add(subscriber);\n subscriber(state);\n return () => subscribers.delete(subscriber);\n },\n\n /** Dispatch an action through the reducer, then run side-effects. */\n dispatch(action: Action): void {\n const prevState = state;\n const nextState = reducer(prevState, action);\n\n if (nextState !== prevState) {\n state = nextState;\n for (const sub of subscribers) sub(state);\n }\n },\n };\n};\n\n/**\n * Recursively assigns the contents of `source` into `target`, preserving\n * existing object and array references to maintain key insertion order.\n *\n * - **Objects**: keys present in `target` but absent in `source` are deleted\n * (unless `merge` is true), then each source entry is recursed into.\n * - **Arrays**: target is truncated or extended to match source length\n * (unless `merge` is true), then each element is recursed into.\n * - **Primitives / type mismatch**: source is returned directly; the caller\n * is responsible for assigning the return value.\n *\n * @param target - The existing value to assign into.\n * @param source - The new value to assign from.\n * @param merge - When true, stale keys and excess array elements are preserved\n * rather than deleted. Defaults to false.\n * @returns The mutated `target`, or `source` if types are incompatible.\n */\nfunction deepAssign<T, V>(target: T, source: V, merge: boolean = false) {\n if (Array.isArray(target) && Array.isArray(source)) {\n while (!merge && target.length > source.length) target.pop();\n // recurse and assign new values\n for (const [index, item] of source.entries()) {\n target[index] = deepAssign(target[index], item, merge);\n }\n } else if (\n target &&\n typeof target === \"object\" &&\n !Array.isArray(target) &&\n source &&\n typeof source === \"object\" &&\n !Array.isArray(source)\n ) {\n const record = target as Record<string, unknown>;\n\n // clear stale keys\n if (!merge)\n for (const key of Object.keys(target)) {\n if (!(key in source)) delete record[key];\n }\n // recurse and assign new values\n for (const [key, item] of Object.entries(source)) {\n record[key] = deepAssign(record[key], item, merge);\n }\n } else {\n // new data doesn't match the types, so there is no referential equality to retain\n return source;\n }\n\n return target;\n}\n\ntype Index = string | number;\n\n/**\n * Deep-sets a value at a key path on an object, preserving existing nested\n * object and array references to maintain key insertion order.\n *\n * Intermediate objects are auto-vivified as needed. The final assignment is\n * handled by {@link deepAssign}, which mutates the existing value in place\n * rather than replacing it wholesale.\n *\n * @param obj - The root object to assign into.\n * @param keys - Path of keys leading to the target location.\n * @param value - The value to assign at the path.\n * @param merge - When true, stale keys and excess array elements are preserved\n * rather than deleted. Defaults to false.\n * @throws If `keys` is empty or contains an undefined key.\n *\n * @see {@link https://github.com/lukeed/dset} for the original dset implementation that inspired this API\n */\nexport function dsetAssign<V>(\n obj: Record<Index, unknown>,\n keys: Array<Index>,\n value: V,\n merge: boolean = false,\n): void {\n const key = keys[keys.length - 1];\n if (key === undefined)\n throw Error(\"Unable to assign at path containing undefined keys\");\n\n // Walk the path, auto-vivifying intermediate objects when necessary\n const target = keys\n .slice(0, -1)\n .reduce(\n (acc, key) => (acc[key] ??= {}) as Record<string | number, unknown>,\n obj,\n );\n\n // Deep assign the value into the target object for key\n target[key] = deepAssign(target[key], value, merge);\n}\n"],"mappings":";AAYO,IAAM,kBAAkB,CAC7B,SACA,iBACyB;AACzB,MAAI,QAAQ;AACZ,QAAM,cAAc,oBAAI,IAAuB;AAC/C,SAAO;AAAA,IACL,UAAU,MAAM;AAAA;AAAA,IAGhB,UAAU,YAA4C;AACpD,kBAAY,IAAI,UAAU;AAC1B,iBAAW,KAAK;AAChB,aAAO,MAAM,YAAY,OAAO,UAAU;AAAA,IAC5C;AAAA;AAAA,IAGA,SAAS,QAAsB;AAC7B,YAAM,YAAY;AAClB,YAAM,YAAY,QAAQ,WAAW,MAAM;AAE3C,UAAI,cAAc,WAAW;AAC3B,gBAAQ;AACR,mBAAW,OAAO,YAAa,KAAI,KAAK;AAAA,MAC1C;AAAA,IACF;AAAA,EACF;AACF;AAmBA,SAAS,WAAiB,QAAW,QAAW,QAAiB,OAAO;AACtE,MAAI,MAAM,QAAQ,MAAM,KAAK,MAAM,QAAQ,MAAM,GAAG;AAClD,WAAO,CAAC,SAAS,OAAO,SAAS,OAAO,OAAQ,QAAO,IAAI;AAE3D,eAAW,CAAC,OAAO,IAAI,KAAK,OAAO,QAAQ,GAAG;AAC5C,aAAO,KAAK,IAAI,WAAW,OAAO,KAAK,GAAG,MAAM,KAAK;AAAA,IACvD;AAAA,EACF,WACE,UACA,OAAO,WAAW,YAClB,CAAC,MAAM,QAAQ,MAAM,KACrB,UACA,OAAO,WAAW,YAClB,CAAC,MAAM,QAAQ,MAAM,GACrB;AACA,UAAM,SAAS;AAGf,QAAI,CAAC;AACH,iBAAW,OAAO,OAAO,KAAK,MAAM,GAAG;AACrC,YAAI,EAAE,OAAO,QAAS,QAAO,OAAO,GAAG;AAAA,MACzC;AAEF,eAAW,CAAC,KAAK,IAAI,KAAK,OAAO,QAAQ,MAAM,GAAG;AAChD,aAAO,GAAG,IAAI,WAAW,OAAO,GAAG,GAAG,MAAM,KAAK;AAAA,IACnD;AAAA,EACF,OAAO;AAEL,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAqBO,SAAS,WACd,KACA,MACA,OACA,QAAiB,OACX;AACN,QAAM,MAAM,KAAK,KAAK,SAAS,CAAC;AAChC,MAAI,QAAQ;AACV,UAAM,MAAM,oDAAoD;AAGlE,QAAM,SAAS,KACZ,MAAM,GAAG,EAAE,EACX;AAAA,IACC,CAAC,KAAKA,SAAS,IAAAA,UAAA,IAAAA,QAAa,CAAC;AAAA,IAC7B;AAAA,EACF;AAGF,SAAO,GAAG,IAAI,WAAW,OAAO,GAAG,GAAG,OAAO,KAAK;AACpD;","names":["key"]}
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "types"
7
7
  ],
8
8
  "name": "@player-devtools/utils",
9
- "version": "0.13.1--canary.13.753",
9
+ "version": "0.13.1--canary.16.915",
10
10
  "main": "dist/cjs/index.cjs",
11
11
  "module": "dist/index.legacy-esm.js",
12
12
  "types": "types/index.d.ts",
package/src/index.ts CHANGED
@@ -1,3 +1,44 @@
1
+ export type Reducer<State, Action> = (state: State, action: Action) => State;
2
+ export type Dispatch<Action> = (action: Action) => void;
3
+ export type Subscriber<State> = (state: State) => void;
4
+ export type Subscribe<State> = (subscriber: Subscriber<State>) => Unsubscribe;
5
+ export type Unsubscribe = () => void;
6
+
7
+ export interface Store<State, Action> {
8
+ getState: () => State;
9
+ subscribe: Subscribe<State>;
10
+ dispatch: Dispatch<Action>;
11
+ }
12
+
13
+ export const useStateReducer = <State, Action>(
14
+ reducer: Reducer<State, Action>,
15
+ initialState: State,
16
+ ): Store<State, Action> => {
17
+ let state = initialState;
18
+ const subscribers = new Set<Subscriber<State>>();
19
+ return {
20
+ getState: () => state,
21
+
22
+ /** Subscribe to state changes; returns an unsubscribe function. */
23
+ subscribe(subscriber: Subscriber<State>): Unsubscribe {
24
+ subscribers.add(subscriber);
25
+ subscriber(state);
26
+ return () => subscribers.delete(subscriber);
27
+ },
28
+
29
+ /** Dispatch an action through the reducer, then run side-effects. */
30
+ dispatch(action: Action): void {
31
+ const prevState = state;
32
+ const nextState = reducer(prevState, action);
33
+
34
+ if (nextState !== prevState) {
35
+ state = nextState;
36
+ for (const sub of subscribers) sub(state);
37
+ }
38
+ },
39
+ };
40
+ };
41
+
1
42
  /**
2
43
  * Recursively assigns the contents of `source` into `target`, preserving
3
44
  * existing object and array references to maintain key insertion order.
package/types/index.d.ts CHANGED
@@ -1,3 +1,14 @@
1
+ export type Reducer<State, Action> = (state: State, action: Action) => State;
2
+ export type Dispatch<Action> = (action: Action) => void;
3
+ export type Subscriber<State> = (state: State) => void;
4
+ export type Subscribe<State> = (subscriber: Subscriber<State>) => Unsubscribe;
5
+ export type Unsubscribe = () => void;
6
+ export interface Store<State, Action> {
7
+ getState: () => State;
8
+ subscribe: Subscribe<State>;
9
+ dispatch: Dispatch<Action>;
10
+ }
11
+ export declare const useStateReducer: <State, Action>(reducer: Reducer<State, Action>, initialState: State) => Store<State, Action>;
1
12
  type Index = string | number;
2
13
  /**
3
14
  * Deep-sets a value at a key path on an object, preserving existing nested