@rkrupinski/stan 1.0.0-rc10
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 +5 -0
- package/dist/atom.d.ts +13 -0
- package/dist/cache.d.ts +27 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +1 -0
- package/dist/misc.d.ts +7 -0
- package/dist/react.d.ts +15 -0
- package/dist/react.js +1 -0
- package/dist/selector.d.ts +18 -0
- package/dist/state.d.ts +12 -0
- package/dist/utils-BY8H9TPH.js +1 -0
- package/dist/utils.d.ts +2 -0
- package/package.json +56 -0
package/README.md
ADDED
package/dist/atom.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SetterOrUpdater, WritableState } from './state';
|
|
2
|
+
import { type SerializableParam } from './misc';
|
|
3
|
+
export type AtomEffect<T> = (param: {
|
|
4
|
+
init(value: T): void;
|
|
5
|
+
set: SetterOrUpdater<T>;
|
|
6
|
+
onSet(cb: (value: T) => void): void;
|
|
7
|
+
}) => void;
|
|
8
|
+
export type AtomOptions<T> = {
|
|
9
|
+
effects?: ReadonlyArray<AtomEffect<T>>;
|
|
10
|
+
};
|
|
11
|
+
export declare const atom: <T>(initialValue: T, { effects }?: AtomOptions<T>) => WritableState<T>;
|
|
12
|
+
export type ValueFromParam<T, P extends SerializableParam> = (param: P) => T;
|
|
13
|
+
export declare const atomFamily: <T, P extends SerializableParam>(initialValue: T | ValueFromParam<T, P>, options?: AtomOptions<T>) => (param: P) => WritableState<T>;
|
package/dist/cache.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface Cache<T> {
|
|
2
|
+
has(key: string): boolean;
|
|
3
|
+
get(key: string): T | undefined;
|
|
4
|
+
set(key: string, value: T): void;
|
|
5
|
+
delete(key: string): boolean;
|
|
6
|
+
clear(): void;
|
|
7
|
+
}
|
|
8
|
+
export declare class LRUCache<T> implements Cache<T> {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(maxSize: number);
|
|
11
|
+
has(key: string): boolean;
|
|
12
|
+
get(key: string): T | undefined;
|
|
13
|
+
set(key: string, value: T): void;
|
|
14
|
+
delete(key: string): boolean;
|
|
15
|
+
clear(): void;
|
|
16
|
+
}
|
|
17
|
+
export declare class SimpleCache<T> extends Map<string, T> implements Cache<T> {
|
|
18
|
+
}
|
|
19
|
+
export type CachePolicy = {
|
|
20
|
+
type: 'keep-all';
|
|
21
|
+
} | {
|
|
22
|
+
type: 'most-recent';
|
|
23
|
+
} | {
|
|
24
|
+
type: 'lru';
|
|
25
|
+
maxSize: number;
|
|
26
|
+
};
|
|
27
|
+
export declare const makeCache: <T>(policy: CachePolicy) => Cache<T>;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +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}},i=(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)})}},h=(t,s)=>{const c=n({type:"keep-all"});return a=>{const r=e(a);return c.has(r)||c.set(r,i(t instanceof Function?t(a):t,s)),c.get(r)}},l=(e,{areValuesEqual:c=s}={})=>{let a,r=!1,n=!1;const i=new Set,h=new Set,l=new Set,o=e=>{let t=e.get();if(!h.has(e)){h.add(e);const s=e.subscribe((e=>{c(t,e)||(t=e,u(),d())}));l.add(s)}return t},u=()=>{a=e({get:o})},d=()=>{i.forEach((e=>e(a)))};return{get:()=>(r||(r=!0,u()),a),subscribe:e=>(0===i.size&&(n=!0),i.add(e),function(){i.delete(e),0===i.size&&(n=!1,l.forEach((e=>e())),l.clear(),h.clear())}),[t](){n?(u(),d()):r=!1}}},o=(t,{areValuesEqual:c=s,cachePolicy:a={type:"keep-all"}}={})=>{const r=n(a);return s=>{const a=e(s);if(!r.has(a)){const e=l(t(s),{areValuesEqual:c}),n=e.get.bind(e);e.get=()=>{const e=n();return e instanceof Promise&&e.catch((()=>{r.delete(a)})),e},r.set(a,e)}return r.get(a)}};export{i as atom,h as atomFamily,l as selector,o as selectorFamily};
|
package/dist/misc.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { default as stableStringify } from 'fast-json-stable-stringify';
|
|
2
|
+
type Json = string | number | boolean | null | {
|
|
3
|
+
[property: string]: Json;
|
|
4
|
+
} | Json[];
|
|
5
|
+
export type SerializableParam = Json;
|
|
6
|
+
export declare const REFRESH_TAG: unique symbol;
|
|
7
|
+
export declare const dejaVu: <T>(a: T, b: T) => boolean;
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { State, ReadonlyState, WritableState } from './state';
|
|
2
|
+
export type AsyncValue<T> = {
|
|
3
|
+
type: 'loading';
|
|
4
|
+
} | {
|
|
5
|
+
type: 'ready';
|
|
6
|
+
value: T;
|
|
7
|
+
} | {
|
|
8
|
+
type: 'error';
|
|
9
|
+
reason: string;
|
|
10
|
+
};
|
|
11
|
+
export declare const useStan: <T>(state: WritableState<T>) => readonly [T, import("./state").SetterOrUpdater<T>];
|
|
12
|
+
export declare const useStanValue: <T>(state: State<T>) => T;
|
|
13
|
+
export declare const useStanValueAsync: <T>(state: ReadonlyState<PromiseLike<T>>) => AsyncValue<T>;
|
|
14
|
+
export declare const useSetStanValue: <T>(state: WritableState<T>) => import("./state").SetterOrUpdater<T>;
|
|
15
|
+
export declare const useStanRefresher: (state: ReadonlyState<any>) => () => void;
|
package/dist/react.js
ADDED
|
@@ -0,0 +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};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ReadonlyState, State } from './state';
|
|
2
|
+
import { type CachePolicy } from './cache';
|
|
3
|
+
import { type SerializableParam } from './misc';
|
|
4
|
+
export interface GetFn {
|
|
5
|
+
<T>(state: State<T>): T;
|
|
6
|
+
}
|
|
7
|
+
export type SelectorFn<T> = ({ get }: {
|
|
8
|
+
get: GetFn;
|
|
9
|
+
}) => T;
|
|
10
|
+
export type SelectorOptions = {
|
|
11
|
+
areValuesEqual?: <T>(a: T, b: T) => boolean;
|
|
12
|
+
};
|
|
13
|
+
export declare const selector: <T>(selectorFn: SelectorFn<T>, { areValuesEqual }?: SelectorOptions) => ReadonlyState<T>;
|
|
14
|
+
export type SelectorFamilyFn<T, P extends SerializableParam> = (param: P) => SelectorFn<T>;
|
|
15
|
+
export type SelectorFamilyOptions = SelectorOptions & {
|
|
16
|
+
cachePolicy?: CachePolicy;
|
|
17
|
+
};
|
|
18
|
+
export declare const selectorFamily: <T, P extends SerializableParam>(selectorFamilyFn: SelectorFamilyFn<T, P>, { areValuesEqual, cachePolicy, }?: SelectorFamilyOptions) => (param: P) => ReadonlyState<T>;
|
package/dist/state.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { REFRESH_TAG } from './misc';
|
|
2
|
+
export type SetterOrUpdater<T> = (valueOrUpdater: ((currentValue: T) => T) | T) => void;
|
|
3
|
+
export interface State<T> {
|
|
4
|
+
get(): T;
|
|
5
|
+
subscribe(callback: (value: T) => void): () => void;
|
|
6
|
+
}
|
|
7
|
+
export interface ReadonlyState<T> extends State<T> {
|
|
8
|
+
[REFRESH_TAG](): void;
|
|
9
|
+
}
|
|
10
|
+
export interface WritableState<T> extends State<T> {
|
|
11
|
+
set: SetterOrUpdater<T>;
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
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};
|
package/dist/utils.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rkrupinski/stan",
|
|
3
|
+
"description": "Minimal, type-safe state management",
|
|
4
|
+
"version": "1.0.0-rc10",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./dist/index.js",
|
|
7
|
+
"./react": "./dist/react.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"typecheck": "tsc",
|
|
11
|
+
"lint": "eslint --max-warnings=0",
|
|
12
|
+
"test": "jest",
|
|
13
|
+
"build": "rm -rf dist && rollup --config",
|
|
14
|
+
"prepublishOnly": "yarn typecheck && yarn lint && yarn test && yarn build"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"fast-json-stable-stringify": "2.1.0"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@eslint/js": "^9.23.0",
|
|
21
|
+
"@rkrupinski/prettierconfig": "1.0.1",
|
|
22
|
+
"@rollup/plugin-commonjs": "28.0.3",
|
|
23
|
+
"@rollup/plugin-node-resolve": "16.0.1",
|
|
24
|
+
"@rollup/plugin-terser": "0.4.4",
|
|
25
|
+
"@rollup/plugin-typescript": "12.1.2",
|
|
26
|
+
"@testing-library/dom": "10.4.0",
|
|
27
|
+
"@testing-library/react": "16.2.0",
|
|
28
|
+
"@types/jest": "29.5.14",
|
|
29
|
+
"@types/react": "^18.2.0",
|
|
30
|
+
"@types/react-dom": "^18.2.0",
|
|
31
|
+
"eslint": "^9.23.0",
|
|
32
|
+
"eslint-plugin-react": "^7.37.4",
|
|
33
|
+
"globals": "^16.0.0",
|
|
34
|
+
"jest": "29.7.0",
|
|
35
|
+
"jest-environment-jsdom": "29.7.0",
|
|
36
|
+
"react": "^18.2.0",
|
|
37
|
+
"react-dom": "^18.2.0",
|
|
38
|
+
"rollup": "4.37.0",
|
|
39
|
+
"ts-jest": "29.3.0",
|
|
40
|
+
"tslib": "2.8.1",
|
|
41
|
+
"typescript": "5.8.2",
|
|
42
|
+
"typescript-eslint": "^8.28.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
46
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
47
|
+
},
|
|
48
|
+
"author": "Rafał Krupiński <rkrupinski.npm@gmail.com> (https://github.com/rkrupinski)",
|
|
49
|
+
"homepage": "https://github.com/rkrupinski/stan#readme",
|
|
50
|
+
"repository": {
|
|
51
|
+
"type": "git",
|
|
52
|
+
"url": "git+https://github.com/rkrupinski/stan.git"
|
|
53
|
+
},
|
|
54
|
+
"license": "MIT",
|
|
55
|
+
"prettier": "@rkrupinski/prettierconfig"
|
|
56
|
+
}
|