@rkrupinski/stan 1.0.0-rc9 → 1.1.0-rc1

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,5 +1,25 @@
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
- Minimalistic state management
6
+ Minimal, type-safe state management
7
+
8
+ - [Website](https://rkrupinski.github.io/stan)
9
+ - [API docs](https://rkrupinski.github.io/stan/docs/api)
10
+ - [Examples](https://github.com/rkrupinski/stan/tree/master/packages/examples)
11
+
12
+ ```tsx
13
+ import { atom, selector } from '@rkrupinski/stan';
14
+ import { useStanValue } from '@rkrupinski/stan/react';
15
+
16
+ const salary = atom(21);
17
+ const bonus = atom(37);
18
+ const total = selector(({ get }) => get(salary) + get(bonus));
19
+
20
+ const TotalSalary = () => {
21
+ const value = useStanValue(total);
22
+
23
+ return <small>{value}</small>;
24
+ };
25
+ ```
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,R as t,d as s}from"./utils-BY8H9TPH.js";export{r as refresh}from"./utils-BY8H9TPH.js";class c{#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 a extends Map{}const n=e=>{switch(e.type){case"keep-all":return new a;case"most-recent":return new c(1);case"lru":return new c(e.maxSize);default:return e}},h=(e,{effects:t}={})=>{let s=!1,c=e;const a=new Set,r=new Set,n=(e=!1)=>t=>{c=t instanceof Function?t(c):t,a.forEach((e=>e(c))),e||r.forEach((e=>e(c)))};return t?.forEach((e=>e({init(e){s||(c=e)},set:n(!0),onSet(e){r.add(e)}}))),s=!0,{get:()=>c,set:n(),subscribe:e=>(a.add(e),()=>{a.delete(e)})}},i=(t,s)=>{const c=n({type:"keep-all"});return a=>{const r=e(a);return c.has(r)||c.set(r,h(t instanceof Function?t(a):t,s)),c.get(r)}},l=(e,{areValuesEqual:c=s}={})=>{let a,r=!1,n=!1;const h=new Set,i=new Set,l=new Set,u=e=>{let t=e.get();if(!i.has(e)){i.add(e);const s=e.subscribe((e=>{c(t,e)||(t=e,o(),d())}));l.add(s)}return t},o=()=>{a=e({get:u})},d=()=>{h.forEach((e=>e(a)))};return{get:()=>(r||(r=!0,o()),a),subscribe:e=>(0===h.size&&(n=!0),h.add(e),function(){h.delete(e),0===h.size&&(n=!1,l.forEach((e=>e())),l.clear(),i.clear())}),[t](){n?(o(),d()):r=!1}}},u=(t,{areValuesEqual:c=s,cachePolicy:a={type:"keep-all"}}={})=>{const r=n(a);return s=>{const a=e(s);return r.has(a)||r.set(a,l(t(s),{areValuesEqual:c})),r.get(a)}};export{h as atom,i as atomFamily,l as selector,u 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};
package/dist/misc.d.ts CHANGED
@@ -5,3 +5,5 @@ type Json = string | number | boolean | null | {
5
5
  export type SerializableParam = Json;
6
6
  export declare const REFRESH_TAG: unique symbol;
7
7
  export declare const dejaVu: <T>(a: T, b: T) => boolean;
8
+ export declare const isFunction: (candidate: any) => candidate is (...args: any[]) => any;
9
+ export declare const isPromiseLike: (candidate: any) => candidate is PromiseLike<any>;
package/dist/react.d.ts CHANGED
@@ -8,8 +8,8 @@ export type AsyncValue<T> = {
8
8
  type: 'error';
9
9
  reason: string;
10
10
  };
11
- export declare const useStan: <T>(state: WritableState<T>) => readonly [T, import("./state").SetterOrUpdater<T>];
12
11
  export declare const useStanValue: <T>(state: State<T>) => T;
12
+ export declare const useStan: <T>(state: WritableState<T>) => readonly [T, import("./state").SetterOrUpdater<T>];
13
13
  export declare const useStanValueAsync: <T>(state: ReadonlyState<PromiseLike<T>>) => AsyncValue<T>;
14
14
  export declare const useSetStanValue: <T>(state: WritableState<T>) => import("./state").SetterOrUpdater<T>;
15
15
  export declare const useStanRefresher: (state: ReadonlyState<any>) => () => void;
package/dist/react.js CHANGED
@@ -1 +1 @@
1
- import{useSyncExternalStore as e,useCallback as t,useState as r,useEffect as s}from"react";import{r as o}from"./utils-BY8H9TPH.js";const n=t=>[e(t.subscribe,t.get),t.set],a=t=>e(t.subscribe,t.get),i=t=>{const o=e(t.subscribe,t.get),[n,a]=r({type:"loading"});return s((()=>{let e=!0;return"loading"!==n.type&&a({type:"loading"}),o.then((t=>{e&&a({type:"ready",value:t})}),(t=>{e&&a({type:"error",reason:t?.message??"unknown"})})),()=>{e=!1}}),[o]),n},p=e=>e.set,u=e=>t((()=>{o(e)}),[e]);export{p as useSetStanValue,n as useStan,u as useStanRefresher,a as useStanValue,i as useStanValueAsync};
1
+ import{useCallback as e,useRef as t,useState as r,useEffect as n}from"react";import{r as o}from"./utils-D5tiXDeQ.js";const s=e=>{const o=t(e),[s,a]=r(e.get());return n((()=>(e!==o.current&&(a(e.get()),o.current=e),e.subscribe(a))),[e]),s},a=e=>[s(e),e.set],u=e=>{const t=s(e),[o,a]=r({type:"loading"});return n((()=>{let e=!0;return"loading"!==o.type&&a({type:"loading"}),t.then((t=>{e&&a({type:"ready",value:t})}),(t=>{e&&a({type:"error",reason:t?.message??"unknown"})})),()=>{e=!1}}),[t]),o},p=e=>e.set,c=t=>e((()=>{o(t)}),[t]);export{p as useSetStanValue,a as useStan,c as useStanRefresher,s as useStanValue,u as useStanValueAsync};
@@ -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
  }
@@ -0,0 +1 @@
1
+ function t(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var r,e;var n=t(e?r:(e=1,r=function(t,r){r||(r={}),"function"==typeof r&&(r={cmp:r});var e,n="boolean"==typeof r.cycles&&r.cycles,o=r.cmp&&(e=r.cmp,function(t){return function(r,n){var o={key:r,value:t[r]},c={key:n,value:t[n]};return e(o,c)}}),c=[];return function t(r){if(r&&r.toJSON&&"function"==typeof r.toJSON&&(r=r.toJSON()),void 0!==r){if("number"==typeof r)return isFinite(r)?""+r:"null";if("object"!=typeof r)return JSON.stringify(r);var e,i;if(Array.isArray(r)){for(i="[",e=0;e<r.length;e++)e&&(i+=","),i+=t(r[e])||"null";return i+"]"}if(null===r)return"null";if(-1!==c.indexOf(r)){if(n)return JSON.stringify("__cycle__");throw new TypeError("Converting circular structure to JSON")}var u=c.push(r)-1,f=Object.keys(r).sort(o&&o(r));for(i="",e=0;e<f.length;e++){var a=f[e],l=t(r[a]);l&&(i&&(i+=","),i+=JSON.stringify(a)+":"+l)}return c.splice(u,1),"{"+i+"}"}}(t)}));const o=Symbol("__refresh__"),c=(t,r)=>t===r,i=["[object AsyncFunction]","[object AsyncGeneratorFunction]","[object GeneratorFunction]","[object Function]"],u=t=>i.includes(Object.prototype.toString.call(t)),f=t=>!!t&&u(t.then),a=t=>{t[o]()};export{o as R,f as a,c as d,u as i,a as r,n as s};
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@rkrupinski/stan",
3
- "description": "Minimalistic state management",
4
- "version": "1.0.0-rc9",
3
+ "description": "Minimal, type-safe state management",
4
+ "version": "1.1.0-rc1",
5
+ "main": "./dist/index.js",
5
6
  "exports": {
6
7
  ".": "./dist/index.js",
7
8
  "./react": "./dist/react.js"
@@ -1 +0,0 @@
1
- function r(r){return r&&r.__esModule&&Object.prototype.hasOwnProperty.call(r,"default")?r.default:r}var t,e;var n=r(e?t:(e=1,t=function(r,t){t||(t={}),"function"==typeof t&&(t={cmp:t});var e,n="boolean"==typeof t.cycles&&t.cycles,o=t.cmp&&(e=t.cmp,function(r){return function(t,n){var o={key:t,value:r[t]},u={key:n,value:r[n]};return e(o,u)}}),u=[];return function r(t){if(t&&t.toJSON&&"function"==typeof t.toJSON&&(t=t.toJSON()),void 0!==t){if("number"==typeof t)return isFinite(t)?""+t:"null";if("object"!=typeof t)return JSON.stringify(t);var e,i;if(Array.isArray(t)){for(i="[",e=0;e<t.length;e++)e&&(i+=","),i+=r(t[e])||"null";return i+"]"}if(null===t)return"null";if(-1!==u.indexOf(t)){if(n)return JSON.stringify("__cycle__");throw new TypeError("Converting circular structure to JSON")}var f=u.push(t)-1,c=Object.keys(t).sort(o&&o(t));for(i="",e=0;e<c.length;e++){var l=c[e],a=r(t[l]);a&&(i&&(i+=","),i+=JSON.stringify(l)+":"+a)}return u.splice(f,1),"{"+i+"}"}}(r)}));const o=Symbol("__refresh__"),u=(r,t)=>r===t,i=r=>{r[o]()};export{o as R,u as d,i as r,n as s};