@codeleap/store 6.3.0 → 6.8.0

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.
@@ -0,0 +1,16 @@
1
+ import { WritableAtom } from "nanostores";
2
+ /**
3
+ * Returns a `Proxy` that mirrors every property access on the atom's current array value.
4
+ *
5
+ * When a function property (e.g. `push`, `splice`, `sort`) is accessed, the proxy wraps the call so that after the native method executes it writes the mutated array back to the store with `store.set`.
6
+ * This makes mutating array methods reactive: callers interact with a familiar `Array` API while nanostores' subscriber notifications fire automatically.
7
+ *
8
+ * Non-function properties (indices, `length`, etc.) are forwarded as-is.
9
+ */
10
+ export declare function arrayHandler<T extends any[]>(store: WritableAtom<T>): never[];
11
+ /**
12
+ * Complete list of own property names on `Array.prototype`.
13
+ * Used by the `globalState` proxy to detect when a caller is accessing an array method so it can delegate to `arrayHandler` instead of the raw atom.
14
+ */
15
+ export declare const arrayOps: string[];
16
+ //# sourceMappingURL=array.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../src/array.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAEzC;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,GAAG,EAAE,EAAE,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,WAoBnE;AAED;;;GAGG;AACH,eAAO,MAAM,QAAQ,UAA8C,CAAA"}
@@ -0,0 +1,22 @@
1
+ import { setPersistentEngine } from '@nanostores/persistent';
2
+ import { GlobalState, GlobalStateConfig } from './types';
3
+ /**
4
+ * Registers the storage engine used by all persistent atoms in this process.
5
+ * Must be called once at app startup — before any `globalState` with a `persistKey` is created — otherwise the persistent atom falls back to an in-memory store.
6
+ * Thin re-export of `@nanostores/persistent`'s `setPersistentEngine`.
7
+ */
8
+ export declare const setGlobalStatePersistor: typeof setPersistentEngine;
9
+ /**
10
+ * Creates a `GlobalState<T>` atom backed by nanostores.
11
+ *
12
+ * The returned value is a `Proxy` over the raw atom that intercepts property access to:
13
+ * - Route `use` / `get` through optional selector functions so components subscribe only to the slice they need.
14
+ * - Merge partial updates on `set` for object atoms (primitives and arrays are replaced outright).
15
+ * - Expose `reset` as an alias for the raw `WritableAtom.set`, bypassing merge logic.
16
+ * - Forward any `Array.prototype` property access to `arrayHandler`, which commits mutations back to the store.
17
+ *
18
+ * When `config.persistKey` is provided, the atom is created with `persistentAtom` and serialised via `JSON.stringify` / `JSON.parse`.
19
+ * Call `setGlobalStatePersistor` before using persistence so the engine is ready.
20
+ */
21
+ export declare function globalState<T>(value: T, config?: GlobalStateConfig): GlobalState<T>;
22
+ //# sourceMappingURL=globalState.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"globalState.d.ts","sourceRoot":"","sources":["../src/globalState.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAkB,MAAM,wBAAwB,CAAA;AAE5E,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAA8B,MAAM,SAAS,CAAA;AAQpF;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,4BAAsB,CAAA;AAE1D;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,GAAE,iBAAiC,GAAG,WAAW,CAAC,CAAC,CAAC,CAoDlG"}
@@ -0,0 +1,4 @@
1
+ export * from './globalState';
2
+ export * from './types';
3
+ export * from './utils';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,SAAS,CAAA;AACvB,cAAc,SAAS,CAAA"}
@@ -0,0 +1,40 @@
1
+ import { WritableAtom } from 'nanostores';
2
+ import { PersistentStore, PersistentEvents, PersistentEvent } from '@nanostores/persistent';
3
+ /** Derives a value `R` from a state snapshot `S`. Used by `get` and `use` to avoid subscribing to the full atom when only a slice is needed. */
4
+ export type StateSelector<S, R> = (state: S) => R;
5
+ /** Functional form of a setter — receives the current value and returns the next value. Enables relative updates without reading state externally. */
6
+ export type StateSetterFunction<In, Out = In> = (current: In) => Out;
7
+ /** Accepts either a direct value or a function setter. `set` on object atoms only requires the changed keys (`Partial<T>`), so `TOut` defaults to `TIn` but can differ. */
8
+ export type StateSetter<TIn, TOut = TIn> = TOut | StateSetterFunction<TIn, TOut>;
9
+ /**
10
+ * A nanostores `WritableAtom` extended with a richer API surface.
11
+ *
12
+ * - `use` — React hook that subscribes to the atom (or a selector slice) and re-renders only when the selected value changes.
13
+ * - `set` — Merges partial updates for object atoms; replaces the value for primitives and arrays.
14
+ * - `get` — Non-reactive snapshot read; accepts an optional selector to avoid allocating an intermediate slice.
15
+ * - `reset` — Bypasses the merge logic and replaces the entire state, equivalent to calling the underlying `WritableAtom.set` directly.
16
+ *
17
+ * When `T` is an array the atom also exposes every `Array.prototype` method directly; mutating methods commit the result back to the store automatically.
18
+ */
19
+ export type GlobalState<T> = Omit<WritableAtom<T>, 'set' | 'get'> & {
20
+ use: <Selected = T>(selector?: StateSelector<T, Selected>) => Selected;
21
+ set: (newValue: T extends Record<string, any> ? StateSetter<T, Partial<T>> : StateSetter<T>) => void;
22
+ get: <Selected = T>(selector?: StateSelector<T, Selected>) => Selected extends undefined ? T : Selected;
23
+ reset: WritableAtom<T>['set'];
24
+ } & (T extends any[] ? Array<T[number]> : {});
25
+ /**
26
+ * Creation options for `globalState`.
27
+ *
28
+ * When `persistKey` is provided the atom is backed by a `persistentAtom`, serialised with `JSON.stringify`/`JSON.parse`.
29
+ * The storage engine must be registered first via `setGlobalStatePersistor`.
30
+ */
31
+ export type GlobalStateConfig = {
32
+ persistKey?: string;
33
+ };
34
+ /** The storage engine contract for persistent atoms. Swap implementations (localStorage, AsyncStorage, MMKV, …) via `setGlobalStatePersistor`. */
35
+ export type GlobalStatePersistor = PersistentStore;
36
+ /** Map of lifecycle events emitted by the persistent engine (e.g. `storage` events in a browser environment). */
37
+ export type GlobalStatePersistorEvents = PersistentEvents;
38
+ /** A single event emitted by the persistent engine, carrying the key and updated value. */
39
+ export type GlobalStatePersistorEvent = PersistentEvent;
40
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AACzC,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AAE3F,gJAAgJ;AAChJ,MAAM,MAAM,aAAa,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAA;AAEjD,sJAAsJ;AACtJ,MAAM,MAAM,mBAAmB,CAAC,EAAE,EAAE,GAAG,GAAG,EAAE,IAAI,CAAC,OAAO,EAAE,EAAE,KAAK,GAAG,CAAA;AAEpE,2KAA2K;AAC3K,MAAM,MAAM,WAAW,CAAC,GAAG,EAAE,IAAI,GAAG,GAAG,IAAI,IAAI,GAAG,mBAAmB,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;AAEhF;;;;;;;;;GASG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,GAAG;IAClE,GAAG,EAAE,CAAC,QAAQ,GAAG,CAAC,EAAE,QAAQ,CAAC,EAAE,aAAa,CAAC,CAAC,EAAE,QAAQ,CAAC,KAAK,QAAQ,CAAA;IAEtE,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,WAAW,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;IAEpG,GAAG,EAAE,CAAC,QAAQ,GAAG,CAAC,EAAE,QAAQ,CAAC,EAAE,aAAa,CAAC,CAAC,EAAE,QAAQ,CAAC,KAAK,QAAQ,SAAS,SAAS,GAAG,CAAC,GAAG,QAAQ,CAAA;IAEvG,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAA;CAC9B,GAAG,CACA,CAAC,SAAS,GAAG,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CACxC,CAAA;AAEH;;;;;GAKG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB,CAAA;AAED,kJAAkJ;AAClJ,MAAM,MAAM,oBAAoB,GAAG,eAAe,CAAA;AAElD,iHAAiH;AACjH,MAAM,MAAM,0BAA0B,GAAG,gBAAgB,CAAA;AAEzD,2FAA2F;AAC3F,MAAM,MAAM,yBAAyB,GAAG,eAAe,CAAA"}
@@ -0,0 +1,29 @@
1
+ import { WritableAtom } from 'nanostores';
2
+ import { StateSetter } from './types';
3
+ /**
4
+ * Resolves `newValue` against `stateValue` and returns the next state.
5
+ *
6
+ * For object atoms the resolved value is shallow-merged onto the existing state, so callers only need to supply changed keys.
7
+ * For primitives and arrays the resolved value replaces the current state entirely.
8
+ */
9
+ export declare function stateAssign<T>(newValue: StateSetter<Partial<T>>, stateValue: T): T;
10
+ /**
11
+ * Derives a virtual `WritableAtom<R>` that projects a slice of a parent atom through `selector`.
12
+ *
13
+ * - `get` — applies `selector` to the parent snapshot without subscribing.
14
+ * - `listen` — subscribes to the parent atom and calls `listener` with the projected value on every change.
15
+ * - `set` — requires a `deselector` to map the slice value back to a `Partial<T>`, then merges it into the parent atom via `stateAssign`. Throws if `deselector` is absent.
16
+ *
17
+ * The returned object satisfies the `WritableAtom<R>` interface so it can be passed anywhere a nanostores atom is expected, including `useStore`.
18
+ */
19
+ export declare const createStateSlice: <T, R, S extends WritableAtom<T>>(store: S, selector: (state: T) => R, deselector?: (result: R) => Partial<T>) => WritableAtom<R>;
20
+ /**
21
+ * React hook that subscribes a component to a derived slice of an atom.
22
+ *
23
+ * Internally it memoises a `createStateSlice` projection keyed on `selector` identity, then passes the virtual atom to `useStore`.
24
+ * Components re-render only when the selected value changes — not on every atom update — which avoids the cost of subscribing to the full atom shape.
25
+ *
26
+ * Caution: because the slice is memoised on `selector` reference, prefer stable (module-level or `useCallback`) selector functions to avoid unnecessary slice recreation.
27
+ */
28
+ export declare function useStateSelector<T, R, S extends WritableAtom<T>>(store: S, selector: (state: T) => R): R;
29
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAEzC,OAAO,EAAE,WAAW,EAAuB,MAAM,SAAS,CAAA;AAgB1D;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,QAAQ,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,GAAG,CAAC,CAYlF;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,gBAAgB,GAAI,CAAC,EAAE,CAAC,EAAE,CAAC,SAAS,YAAY,CAAC,CAAC,CAAC,EAC9D,OAAO,CAAC,EACR,UAAU,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,EACzB,aAAa,CAAC,MAAM,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,KAsBjC,YAAY,CAAC,CAAC,CAAE,CAAA;AAEvB;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,SAAS,YAAY,CAAC,CAAC,CAAC,EAC9D,KAAK,EAAE,CAAC,EACR,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GACxB,CAAC,CAIH"}
package/package.json CHANGED
@@ -1,7 +1,20 @@
1
1
  {
2
2
  "name": "@codeleap/store",
3
- "version": "6.3.0",
3
+ "version": "6.8.0",
4
4
  "main": "src/index.ts",
5
+ "types": "dist/index.d.ts",
6
+ "exports": {
7
+ ".": {
8
+ "source": "./src/index.ts",
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "src"
17
+ ],
5
18
  "license": "UNLICENSED",
6
19
  "repository": {
7
20
  "url": "https://github.com/codeleap-uk/internal-libs-monorepo.git",
@@ -9,17 +22,18 @@
9
22
  "directory": "packages/store"
10
23
  },
11
24
  "devDependencies": {
12
- "@codeleap/config": "6.3.0",
25
+ "@codeleap/config": "6.8.0",
13
26
  "ts-node-dev": "1.1.8"
14
27
  },
15
28
  "scripts": {
16
- "build": "echo 'No build needed'"
29
+ "build": "tsc --build tsconfig.build.json",
30
+ "typecheck": "bun tsc --noEmit -p ./tsconfig.json"
17
31
  },
18
32
  "peerDependencies": {
19
- "typescript": "5.5.2",
33
+ "typescript": "6.0.3",
20
34
  "react": "19.1.0",
21
35
  "nanostores": "*",
22
36
  "@nanostores/react": "*",
23
37
  "@nanostores/persistent": "*"
24
38
  }
25
- }
39
+ }
package/src/array.ts CHANGED
@@ -1,18 +1,26 @@
1
1
  import { WritableAtom } from "nanostores"
2
2
 
3
+ /**
4
+ * Returns a `Proxy` that mirrors every property access on the atom's current array value.
5
+ *
6
+ * When a function property (e.g. `push`, `splice`, `sort`) is accessed, the proxy wraps the call so that after the native method executes it writes the mutated array back to the store with `store.set`.
7
+ * This makes mutating array methods reactive: callers interact with a familiar `Array` API while nanostores' subscriber notifications fire automatically.
8
+ *
9
+ * Non-function properties (indices, `length`, etc.) are forwarded as-is.
10
+ */
3
11
  export function arrayHandler<T extends any[]>(store: WritableAtom<T>) {
4
12
  return new Proxy([], {
5
13
  get(target, p, receiver){
6
- const val = store.get()
14
+ const val = store.get() as Record<string | symbol, any>
7
15
 
8
16
  const property = val[p]
9
17
 
10
18
  if(typeof property == 'function') {
11
- return (...args) => {
19
+ return (...args: unknown[]) => {
12
20
  const r = val[p](...args)
13
21
 
14
- store.set(val)
15
-
22
+ store.set(val as T)
23
+
16
24
  return r
17
25
  }
18
26
  }
@@ -22,5 +30,8 @@ export function arrayHandler<T extends any[]>(store: WritableAtom<T>) {
22
30
  })
23
31
  }
24
32
 
25
-
33
+ /**
34
+ * Complete list of own property names on `Array.prototype`.
35
+ * Used by the `globalState` proxy to detect when a caller is accessing an array method so it can delegate to `arrayHandler` instead of the raw atom.
36
+ */
26
37
  export const arrayOps = Object.getOwnPropertyNames(Array.prototype)
@@ -6,11 +6,28 @@ import { stateAssign, useStateSelector } from './utils'
6
6
  import { arrayHandler, arrayOps } from './array'
7
7
 
8
8
  const defaultConfig: GlobalStateConfig = {
9
- persistKey: null,
9
+ persistKey: undefined,
10
10
  }
11
11
 
12
+ /**
13
+ * Registers the storage engine used by all persistent atoms in this process.
14
+ * Must be called once at app startup — before any `globalState` with a `persistKey` is created — otherwise the persistent atom falls back to an in-memory store.
15
+ * Thin re-export of `@nanostores/persistent`'s `setPersistentEngine`.
16
+ */
12
17
  export const setGlobalStatePersistor = setPersistentEngine
13
18
 
19
+ /**
20
+ * Creates a `GlobalState<T>` atom backed by nanostores.
21
+ *
22
+ * The returned value is a `Proxy` over the raw atom that intercepts property access to:
23
+ * - Route `use` / `get` through optional selector functions so components subscribe only to the slice they need.
24
+ * - Merge partial updates on `set` for object atoms (primitives and arrays are replaced outright).
25
+ * - Expose `reset` as an alias for the raw `WritableAtom.set`, bypassing merge logic.
26
+ * - Forward any `Array.prototype` property access to `arrayHandler`, which commits mutations back to the store.
27
+ *
28
+ * When `config.persistKey` is provided, the atom is created with `persistentAtom` and serialised via `JSON.stringify` / `JSON.parse`.
29
+ * Call `setGlobalStatePersistor` before using persistence so the engine is ready.
30
+ */
14
31
  export function globalState<T>(value: T, config: GlobalStateConfig = defaultConfig): GlobalState<T> {
15
32
  const { persistKey } = config
16
33
 
package/src/types.ts CHANGED
@@ -1,11 +1,25 @@
1
1
  import { WritableAtom } from 'nanostores'
2
2
  import { PersistentStore, PersistentEvents, PersistentEvent } from '@nanostores/persistent'
3
3
 
4
+ /** Derives a value `R` from a state snapshot `S`. Used by `get` and `use` to avoid subscribing to the full atom when only a slice is needed. */
4
5
  export type StateSelector<S, R> = (state: S) => R
5
6
 
7
+ /** Functional form of a setter — receives the current value and returns the next value. Enables relative updates without reading state externally. */
6
8
  export type StateSetterFunction<In, Out = In> = (current: In) => Out
9
+
10
+ /** Accepts either a direct value or a function setter. `set` on object atoms only requires the changed keys (`Partial<T>`), so `TOut` defaults to `TIn` but can differ. */
7
11
  export type StateSetter<TIn, TOut = TIn> = TOut | StateSetterFunction<TIn, TOut>
8
12
 
13
+ /**
14
+ * A nanostores `WritableAtom` extended with a richer API surface.
15
+ *
16
+ * - `use` — React hook that subscribes to the atom (or a selector slice) and re-renders only when the selected value changes.
17
+ * - `set` — Merges partial updates for object atoms; replaces the value for primitives and arrays.
18
+ * - `get` — Non-reactive snapshot read; accepts an optional selector to avoid allocating an intermediate slice.
19
+ * - `reset` — Bypasses the merge logic and replaces the entire state, equivalent to calling the underlying `WritableAtom.set` directly.
20
+ *
21
+ * When `T` is an array the atom also exposes every `Array.prototype` method directly; mutating methods commit the result back to the store automatically.
22
+ */
9
23
  export type GlobalState<T> = Omit<WritableAtom<T>, 'set' | 'get'> & {
10
24
  use: <Selected = T>(selector?: StateSelector<T, Selected>) => Selected
11
25
 
@@ -18,12 +32,21 @@ export type GlobalState<T> = Omit<WritableAtom<T>, 'set' | 'get'> & {
18
32
  T extends any[] ? Array<T[number]> : {}
19
33
  )
20
34
 
35
+ /**
36
+ * Creation options for `globalState`.
37
+ *
38
+ * When `persistKey` is provided the atom is backed by a `persistentAtom`, serialised with `JSON.stringify`/`JSON.parse`.
39
+ * The storage engine must be registered first via `setGlobalStatePersistor`.
40
+ */
21
41
  export type GlobalStateConfig = {
22
42
  persistKey?: string
23
43
  }
24
44
 
45
+ /** The storage engine contract for persistent atoms. Swap implementations (localStorage, AsyncStorage, MMKV, …) via `setGlobalStatePersistor`. */
25
46
  export type GlobalStatePersistor = PersistentStore
26
47
 
48
+ /** Map of lifecycle events emitted by the persistent engine (e.g. `storage` events in a browser environment). */
27
49
  export type GlobalStatePersistorEvents = PersistentEvents
28
50
 
51
+ /** A single event emitted by the persistent engine, carrying the key and updated value. */
29
52
  export type GlobalStatePersistorEvent = PersistentEvent
package/src/utils.ts CHANGED
@@ -3,18 +3,26 @@ import { WritableAtom } from 'nanostores'
3
3
  import { useMemo } from 'react'
4
4
  import { StateSetter, StateSetterFunction } from './types'
5
5
 
6
+ /** Type guard that distinguishes a functional setter from a plain value so `resolveSetter` can call it correctly. */
6
7
  function isFunctionSetter<T>(x: any): x is StateSetterFunction<T> {
7
8
  return typeof x === 'function'
8
9
  }
9
10
 
11
+ /** Evaluates a `StateSetter`: calls it with `currentValue` if it is a function, otherwise returns it as-is. */
10
12
  function resolveSetter<T>(setter: StateSetter<T>, currentValue:T):T {
11
- if (isFunctionSetter(setter)) {
13
+ if (isFunctionSetter<T>(setter)) {
12
14
  return setter(currentValue)
13
15
  }
14
16
 
15
- return setter
17
+ return setter as T
16
18
  }
17
19
 
20
+ /**
21
+ * Resolves `newValue` against `stateValue` and returns the next state.
22
+ *
23
+ * For object atoms the resolved value is shallow-merged onto the existing state, so callers only need to supply changed keys.
24
+ * For primitives and arrays the resolved value replaces the current state entirely.
25
+ */
18
26
  export function stateAssign<T>(newValue: StateSetter<Partial<T>>, stateValue: T): T {
19
27
  const resolvedValue = resolveSetter(newValue, stateValue)
20
28
  if (
@@ -29,6 +37,15 @@ export function stateAssign<T>(newValue: StateSetter<Partial<T>>, stateValue: T)
29
37
  return resolvedValue as T
30
38
  }
31
39
 
40
+ /**
41
+ * Derives a virtual `WritableAtom<R>` that projects a slice of a parent atom through `selector`.
42
+ *
43
+ * - `get` — applies `selector` to the parent snapshot without subscribing.
44
+ * - `listen` — subscribes to the parent atom and calls `listener` with the projected value on every change.
45
+ * - `set` — requires a `deselector` to map the slice value back to a `Partial<T>`, then merges it into the parent atom via `stateAssign`. Throws if `deselector` is absent.
46
+ *
47
+ * The returned object satisfies the `WritableAtom<R>` interface so it can be passed anywhere a nanostores atom is expected, including `useStore`.
48
+ */
32
49
  export const createStateSlice = <T, R, S extends WritableAtom<T>>(
33
50
  store: S,
34
51
  selector: (state: T) => R,
@@ -51,8 +68,19 @@ export const createStateSlice = <T, R, S extends WritableAtom<T>>(
51
68
 
52
69
  store.set(newValue)
53
70
  },
71
+ get value(){
72
+ return store.value
73
+ }
54
74
  } as WritableAtom<R>)
55
75
 
76
+ /**
77
+ * React hook that subscribes a component to a derived slice of an atom.
78
+ *
79
+ * Internally it memoises a `createStateSlice` projection keyed on `selector` identity, then passes the virtual atom to `useStore`.
80
+ * Components re-render only when the selected value changes — not on every atom update — which avoids the cost of subscribing to the full atom shape.
81
+ *
82
+ * Caution: because the slice is memoised on `selector` reference, prefer stable (module-level or `useCallback`) selector functions to avoid unnecessary slice recreation.
83
+ */
56
84
  export function useStateSelector<T, R, S extends WritableAtom<T>>(
57
85
  store: S,
58
86
  selector: (state: T) => R,
package/package.json.bak DELETED
@@ -1,25 +0,0 @@
1
- {
2
- "name": "@codeleap/store",
3
- "version": "6.3.0",
4
- "main": "src/index.ts",
5
- "license": "UNLICENSED",
6
- "repository": {
7
- "url": "https://github.com/codeleap-uk/internal-libs-monorepo.git",
8
- "type": "git",
9
- "directory": "packages/store"
10
- },
11
- "devDependencies": {
12
- "@codeleap/config": "workspace:*",
13
- "ts-node-dev": "1.1.8"
14
- },
15
- "scripts": {
16
- "build": "echo 'No build needed'"
17
- },
18
- "peerDependencies": {
19
- "typescript": "5.5.2",
20
- "react": "19.1.0",
21
- "nanostores": "*",
22
- "@nanostores/react": "*",
23
- "@nanostores/persistent": "*"
24
- }
25
- }