@rkrupinski/stan 1.0.0 → 1.1.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.
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Stan
2
2
 
3
3
  [![CI](https://github.com/rkrupinski/stan/actions/workflows/ci.yml/badge.svg)](https://github.com/rkrupinski/stan/actions/workflows/ci.yml)
4
+ [![Bundle size](https://badgen.net/bundlephobia/minzip/@rkrupinski/stan)](https://bundlephobia.com/package/@rkrupinski/stan)
4
5
 
5
6
  Minimal, type-safe state management
6
7
 
@@ -9,7 +10,7 @@ Minimal, type-safe state management
9
10
  - [Examples](https://github.com/rkrupinski/stan/tree/master/packages/examples)
10
11
 
11
12
  ```tsx
12
- import { atom } from '@rkrupinski/stan';
13
+ import { atom, selector } from '@rkrupinski/stan';
13
14
  import { useStanValue } from '@rkrupinski/stan/react';
14
15
 
15
16
  const salary = atom(21);
package/dist/atom.d.ts CHANGED
@@ -6,8 +6,9 @@ export type AtomEffect<T> = (param: {
6
6
  onSet(cb: (value: T) => void): void;
7
7
  }) => void;
8
8
  export type AtomOptions<T> = {
9
+ tag?: string;
9
10
  effects?: ReadonlyArray<AtomEffect<T>>;
10
11
  };
11
- export declare const atom: <T>(initialValue: T, { effects }?: AtomOptions<T>) => WritableState<T>;
12
+ export declare const atom: <T>(initialValue: T, { effects, tag }?: AtomOptions<T>) => WritableState<T>;
12
13
  export type ValueFromParam<T, P extends SerializableParam> = (param: P) => T;
13
14
  export declare const atomFamily: <T, P extends SerializableParam>(initialValue: T | ValueFromParam<T, P>, options?: AtomOptions<T>) => (param: P) => WritableState<T>;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{s as e,i as t,R as s,d as a,a as c}from"./utils-D5tiXDeQ.js";export{r as refresh}from"./utils-D5tiXDeQ.js";class h{#e;#t;constructor(e){this.#e=new Map,this.#t=e}#s(){const e=this.#e.keys().next().value;void 0!==e&&this.#e.delete(e)}has(e){return this.#e.has(e)}get(e){if(!this.has(e))return;const t=this.#e.get(e);return this.#e.delete(e),this.#e.set(e,t),t}set(e,t){this.#e.has(e)?this.#e.delete(e):this.#e.size>=this.#t&&this.#s(),this.#e.set(e,t)}delete(e){return this.#e.delete(e)}clear(){this.#e.clear()}}class n extends Map{}const i=e=>{switch(e.type){case"keep-all":return new n;case"most-recent":return new h(1);case"lru":return new h(e.maxSize);default:return e}},l=(e,{effects:s}={})=>{let a=!1,c=e;const r=new Set,h=new Set,n=(e=!1)=>s=>{c=t(s)?s(c):s,r.forEach((e=>e(c))),e||h.forEach((e=>e(c)))};return s?.forEach((e=>e({init(e){a||(c=e)},set:n(!0),onSet(e){h.add(e)}}))),a=!0,{get:()=>c,set:n(),subscribe:e=>(r.add(e),()=>{r.delete(e)})}},u=(s,a)=>{const c=i({type:"keep-all"});return r=>{const h=e(r);return c.has(h)||c.set(h,l(t(s)?s(r):s,a)),c.get(h)}},o=(e,{areValuesEqual:t=a}={})=>{let r,h=!1,n=!1;const i=new Set,l=new Set,u=new Set,o=e=>{let s=e.get();if(!l.has(e)){l.add(e);const a=e.subscribe((e=>{t(s,e)||(s=e,d(),f())}));u.add(a)}return s},d=()=>{r=e({get:o}),c(r)&&r.then(void 0,(()=>{h=!1}))},f=()=>{i.forEach((e=>e(r)))};return{get:()=>(h||(h=!0,d()),r),subscribe:e=>(0===i.size&&(n=!0),i.add(e),function(){i.delete(e),0===i.size&&(n=!1,u.forEach((e=>e())),u.clear(),l.clear())}),[s](){n?(d(),f()):h=!1}}},d=(t,{areValuesEqual:s=a,cachePolicy:c={type:"keep-all"}}={})=>{const r=i(c);return a=>{const c=e(a);return r.has(c)||r.set(c,o(t(a),{areValuesEqual:s})),r.get(c)}};export{l as atom,u as atomFamily,o as selector,d as selectorFamily};
1
+ import{s as e,i as t,R as s,d as a,a as c}from"./utils-D5tiXDeQ.js";export{r as refresh}from"./utils-D5tiXDeQ.js";class h{#e;#t;constructor(e){this.#e=new Map,this.#t=e}#s(){const e=this.#e.keys().next().value;void 0!==e&&this.#e.delete(e)}has(e){return this.#e.has(e)}get(e){if(!this.has(e))return;const t=this.#e.get(e);return this.#e.delete(e),this.#e.set(e,t),t}set(e,t){this.#e.has(e)?this.#e.delete(e):this.#e.size>=this.#t&&this.#s(),this.#e.set(e,t)}delete(e){return this.#e.delete(e)}clear(){this.#e.clear()}}class n extends Map{}const i=e=>{switch(e.type){case"keep-all":return new n;case"most-recent":return new h(1);case"lru":return new h(e.maxSize);default:return e}},l=(e,{effects:s,tag:a}={})=>{let c=!1,r=e;const h=new Set,n=new Set,i=(e=!1)=>s=>{r=t(s)?s(r):s,h.forEach((e=>e(r))),e||n.forEach((e=>e(r)))};return s?.forEach((e=>e({init(e){c||(r=e)},set:i(!0),onSet(e){n.add(e)}}))),c=!0,{tag:a,get:()=>r,set:i(),subscribe:e=>(h.add(e),()=>{h.delete(e)})}},u=(s,a)=>{const c=i({type:"keep-all"});return r=>{const h=e(r);return c.has(h)||c.set(h,l(t(s)?s(r):s,a)),c.get(h)}},o=(e,{tag:t,areValuesEqual:r=a}={})=>{let h,n=!1,i=!1;const l=new Set,u=new Set,o=new Set,d=e=>{let t=e.get();if(!u.has(e)){u.add(e);const s=e.subscribe((e=>{r(t,e)||(t=e,f(),g())}));o.add(s)}return t},f=()=>{o.forEach((e=>e())),o.clear(),u.clear(),h=e({get:d}),c(h)&&h.then(void 0,(()=>{n=!1}))},g=()=>{l.forEach((e=>e(h)))};return{tag:t,get:()=>(n||(n=!0,f()),h),subscribe:e=>(0===l.size&&(i=!0),l.add(e),function(){l.delete(e),0===l.size&&(i=!1)}),[s](){i?(f(),g()):n=!1}}},d=(t,{tag:s,areValuesEqual:c=a,cachePolicy:r={type:"keep-all"}}={})=>{const h=i(r);return a=>{const r=e(a);return h.has(r)||h.set(r,o(t(a),{tag:s,areValuesEqual:c})),h.get(r)}};export{l as atom,u as atomFamily,o as selector,d as selectorFamily};
@@ -8,11 +8,12 @@ export type SelectorFn<T> = ({ get }: {
8
8
  get: GetFn;
9
9
  }) => T;
10
10
  export type SelectorOptions = {
11
+ tag?: string;
11
12
  areValuesEqual?: <T>(a: T, b: T) => boolean;
12
13
  };
13
- export declare const selector: <T>(selectorFn: SelectorFn<T>, { areValuesEqual }?: SelectorOptions) => ReadonlyState<T>;
14
+ export declare const selector: <T>(selectorFn: SelectorFn<T>, { tag, areValuesEqual }?: SelectorOptions) => ReadonlyState<T>;
14
15
  export type SelectorFamilyFn<T, P extends SerializableParam> = (param: P) => SelectorFn<T>;
15
16
  export type SelectorFamilyOptions = SelectorOptions & {
16
17
  cachePolicy?: CachePolicy;
17
18
  };
18
- export declare const selectorFamily: <T, P extends SerializableParam>(selectorFamilyFn: SelectorFamilyFn<T, P>, { areValuesEqual, cachePolicy, }?: SelectorFamilyOptions) => (param: P) => ReadonlyState<T>;
19
+ export declare const selectorFamily: <T, P extends SerializableParam>(selectorFamilyFn: SelectorFamilyFn<T, P>, { tag, areValuesEqual, cachePolicy, }?: SelectorFamilyOptions) => (param: P) => ReadonlyState<T>;
package/dist/state.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { REFRESH_TAG } from './misc';
2
2
  export type SetterOrUpdater<T> = (valueOrUpdater: ((currentValue: T) => T) | T) => void;
3
3
  export interface State<T> {
4
+ tag?: string;
4
5
  get(): T;
5
6
  subscribe(callback: (value: T) => void): () => void;
6
7
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rkrupinski/stan",
3
3
  "description": "Minimal, type-safe state management",
4
- "version": "1.0.0",
4
+ "version": "1.1.0",
5
5
  "main": "./dist/index.js",
6
6
  "exports": {
7
7
  ".": "./dist/index.js",