valdres 0.1.0-alpha.10

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.
Files changed (41) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +1 -0
  3. package/dist/index.d.ts +14 -0
  4. package/dist/index.js +1 -0
  5. package/dist/src/atom.d.ts +9 -0
  6. package/dist/src/atomFamily.d.ts +2 -0
  7. package/dist/src/createStore.d.ts +2 -0
  8. package/dist/src/getDefaultStore.d.ts +2 -0
  9. package/dist/src/lib/getState.d.ts +3 -0
  10. package/dist/src/lib/initAtom.d.ts +4 -0
  11. package/dist/src/lib/initSelector.d.ts +4 -0
  12. package/dist/src/lib/propagateUpdatedAtoms.d.ts +3 -0
  13. package/dist/src/lib/resetAtom.d.ts +3 -0
  14. package/dist/src/lib/setAtom.d.ts +3 -0
  15. package/dist/src/lib/setAtoms.d.ts +3 -0
  16. package/dist/src/lib/stableStringify.d.ts +1 -0
  17. package/dist/src/lib/subscribe.d.ts +3 -0
  18. package/dist/src/lib/transaction.d.ts +8 -0
  19. package/dist/src/lib/unsubscribe.d.ts +3 -0
  20. package/dist/src/lib/updateSelectorSubscribers.d.ts +3 -0
  21. package/dist/src/lib/updateStateSubscribers.d.ts +3 -0
  22. package/dist/src/selector.d.ts +5 -0
  23. package/dist/src/selectorFamily.d.ts +4 -0
  24. package/dist/src/types/Atom.d.ts +7 -0
  25. package/dist/src/types/AtomFamily.d.ts +5 -0
  26. package/dist/src/types/Family.d.ts +3 -0
  27. package/dist/src/types/GetValue.d.ts +2 -0
  28. package/dist/src/types/ResetAtom.d.ts +2 -0
  29. package/dist/src/types/Selector.d.ts +4 -0
  30. package/dist/src/types/SelectorFamily.d.ts +1 -0
  31. package/dist/src/types/SetAtom.d.ts +2 -0
  32. package/dist/src/types/State.d.ts +3 -0
  33. package/dist/src/types/Store.d.ts +14 -0
  34. package/dist/src/types/StoreData.d.ts +7 -0
  35. package/dist/src/types/SubscribeFn.d.ts +2 -0
  36. package/dist/src/types/TransactionFn.d.ts +4 -0
  37. package/dist/src/utils/isAtom.d.ts +2 -0
  38. package/dist/src/utils/isFamily.d.ts +1 -0
  39. package/dist/src/utils/isPromiseLike.d.ts +1 -0
  40. package/dist/src/utils/isSelector.d.ts +3 -0
  41. package/package.json +30 -0
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Eigil Sagafos
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ Se [valdres.dev](https://valdres.dev) for docs
@@ -0,0 +1,14 @@
1
+ export { atom } from "./src/atom";
2
+ export { atomFamily } from "./src/atomFamily";
3
+ export { createStore } from "./src/createStore";
4
+ export { getDefaultStore, resetDefaultStore } from "./src/getDefaultStore";
5
+ export { selector } from "./src/selector";
6
+ export { selectorFamily } from "./src/selectorFamily";
7
+ export { isAtom } from "./src/utils/isAtom";
8
+ export { isSelector } from "./src/utils/isSelector";
9
+ export { isFamily } from "./src/utils/isFamily";
10
+ export { isPromiseLike } from "./src/utils/isPromiseLike";
11
+ export type { Atom } from "./src/types/Atom";
12
+ export type { Selector } from "./src/types/Selector";
13
+ export type { State } from "./src/types/State";
14
+ export type { Store } from "./src/types/Store";
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ var H=(o,p)=>{if(!p)return{defaultValue:o};return{defaultValue:o,...p}};var D=(o)=>o&&o.then&&typeof o.then==="function";var M=(o,p)=>{if(typeof o==="string"&&!o.includes('"')&&!o.includes("\\"))return`"${o}"`;switch(typeof o){case"undefined":return"";case"boolean":return o?"true":"false";case"number":case"symbol":return String(o);case"string":return JSON.stringify(o);case"function":return`__FUNCTION(${o.toString()})__`}if(o===null)return"null";if(typeof o!=="object")return JSON.stringify(o)??"";if(D(o))return"__PROMISE__";if(Array.isArray(o))return`[${o.map((m,f)=>M(m,f.toString()))}]`;if(typeof o.toJSON==="function")return M(o.toJSON(p),p);if(o instanceof Map){const m={};for(let[f,n]of o)m[typeof f==="string"?f:stringify(f,opt)]=n;return M(m,p)}if(o instanceof Set)return M(Array.from(o).sort((m,f)=>M(m).localeCompare(M(f))),p);if(Symbol!==void 0&&o[Symbol.iterator]!=null&&typeof o[Symbol.iterator]==="function")return M(Array.from(o),p);return`{${Object.keys(o).filter((m)=>o[m]!==void 0).sort().map((m)=>`${M(m)}:${M(o[m],m)}`).join(",")}}`},I=(o)=>{if(typeof o==="string"||typeof o==="boolean"||typeof o==="number")return o;return M(o)};var E=(o,p,m)=>{if(p)return typeof p==="function"?()=>typeof o==="function"?p(o(m)):p(o):p;else return typeof o==="function"?()=>o(m):o},W=(o,p)=>{const m=new Map,f=(n,t)=>{const i=I(n);if(m.has(i)){if(t)throw new Error("defaultOverride is only allowed first time an atom is initiaizlied");return m.get(i)}const r=p&&p+"_"+i,y=H(E(o,t,n),{label:r});return y.family=f,y.familyKey=Object.freeze(n),m.set(i,y),y};return f._map=m,f};var N=(o,p)=>{const m=p.subscriptions.get(o);if(m?.size)for(let f of m)f.callback();if(o.family){const f=p.subscriptions.get(o.family);if(f?.size)for(let n of f)n.callback(o.familyKey)}};import L from"fast-deep-equal";import no from"fast-deep-equal";class j extends Error{promise;constructor(o){super();this.promise=o}}var X=(o,p)=>{const m=p.stateConsumers.get(o);if(m)return m;const f=new Set;return p.stateConsumers.set(o,f),f},g=(o,p)=>{const m=p.stateDependencies.get(o)??new Set,f=new Set;let n;try{n=o.get((r)=>{const y=A(r,p);if(f.add(r),D(y))throw new j(y);return y})}catch(r){if(r instanceof j)n=r;else throw r}const t=f?.difference(m),i=m?.difference(f);for(let r of t)X(r,p).add(o);for(let r of i)X(r,p).delete(o);return p.stateDependencies.set(o,f),n};var z=(o,p)=>{const m=g(o,p);if(m instanceof j)return m.promise.then(()=>z(o,p)),p.values.set(o,m.promise),m.promise;else if(D(m))return m.then((f)=>{p.values.set(o,f),N(o,p),console.log("TODO: Should we check if other selectors are using this?")}),p.values.set(o,m),m;else return p.values.set(o,m),m};var Y=(o,p,m)=>{const f=p.subscriptions.get(o),n=o.family&&p.subscriptions.get(o.family);if(!f?.size&&!n?.size)return;const t=z(o,p);if(L(t,m))return;if(f?.size)for(let i of f)i.callback();if(n?.size)for(let i of n)i.callback(state.familyKey)};var J=(o,p,m,f)=>{for(let n of o)if(!m.has(n)){m.add(n),f.set(n,p.values.get(n)),p.values.delete(n);const t=p.stateConsumers.get(n);if(t?.size)J(t,p,m,f)}},_=(o,p)=>{const m=new Set,f=new Map;for(let n of o){const t=p.stateConsumers.get(n);if(t&&t.size)J(t,p,m,f);if(n.family){const i=p.stateConsumers.get(n.family);if(i?.size)J(i,p,m,f)}}for(let n of m)Y(n,p,f.get(n));for(let n of o)N(n,p)};import c from"fast-deep-equal";var U=(o,p,m)=>{const f=A(o,m);if(typeof p==="function"){if(p=p(f),D(p)||D(f))throw new Error("Todo, how should we handle this?")}if(c(f,p))return;if(m.values.set(o,p),f?.__isEmptyAtomPromise__)f.__resolveEmptyAtomPromise__(p);_([o],m)};var P=(o,p)=>{if(o.defaultValue===void 0){let m;const f=new Promise((n)=>{m=n});return f.__isEmptyAtomPromise__=!0,f.__resolveEmptyAtomPromise__=m,f}else if(typeof o.defaultValue==="function"){const m=o.defaultValue();if(D(m))m.then((f)=>{p.values.set(o,f),_([o],p)});return m}else return o.defaultValue},w=(o,p)=>{const m=P(o,p);if(p.values.set(o,m),o.onInit)o.onInit((f)=>{U(o,f,p)});return m};var G=(o)=>Object.hasOwn(o,"defaultValue");var K=(o)=>Object.hasOwn(o,"get");var F=(o)=>Object.hasOwn(o,"_map");var A=(o,p)=>{if(p.values.has(o))return p.values.get(o);if(G(o))return w(o,p);if(K(o))return z(o,p);if(F(o)){const m=[];for(let f of o._map.values())m.push([f.familyKey,A(f,p)]);return m}throw new Error("Invalid object passed to get")};var Z=(o,p,m,f)=>{const n=m.subscriptions.get(o);if(n){if(n.delete(p),n.size===0){if(o.onUnmount)o.onUnmount(f)}}};var R=(o,p)=>{const m=new Set;return p.subscriptions.set(o,m),m},$=(o,p,m)=>{const f=m.subscriptions.get(o)||R(o,m);if(K(o)&&!m.values.has(o))z(o,m);let n;if(F(o))n={callback:p,state:o};else n={callback:p};let t;if(f.size===0&&o.onMount)t=o.onMount((i)=>{U(o,i,m)});return f.add(n),()=>Z(o,n,m,t)};import s from"fast-deep-equal";var C=(o,p)=>{const m=[];for(let[f,n]of o){const t=A(f,p);if(!s(t,n))m.push(f),p.values.set(f,n)}_(m,p)};var O=(o,p,m=new Set)=>{const f=p.stateConsumers.get(o);if(f?.size){for(let n of f)if(!m.has(n))m.add(n),O(n,p,m)}return m},T=(o,p)=>{let m=new Map,f=new Set;const n=(S)=>{const V=m.get(S);if(V)return V;else if(f.has(S)){const h=S.get(n);return m.set(S,h),h}else return A(S,p)},t=(S,V)=>{if(!G(S))throw new Error("Not an atom");if(typeof V==="function"){const h=n(S);V=V(h)}for(let h of O(S,p))f.add(h),m.delete(h);m.set(S,V)},i=(S)=>{const V=P(S,p);return m.set(S,V),V},r=()=>{C(m,p),f.clear()},y=o(t,n,i,r);return r(),y};var q=(o,p)=>{const m=w(o,p);if(!D(m))_([o],p);return m};var b=()=>(Math.random()+1).toString(36).substring(7),B=(o)=>{const p={id:o??b(),values:new WeakMap,subscriptions:new WeakMap,stateConsumers:new WeakMap,stateDependencies:new WeakMap},m=(r)=>A(r,p),f=(r,y)=>{if(G(r))return U(r,y,p);else{if(K(r))if(r.set){i((S,V)=>r.set({get:V,set:S},y));return}else throw new Error("set on selector is not supported");throw new Error("Invalid state object passed to set")}},n=(r)=>q(r,p),t=(r,y)=>$(r,y,p),i=(r)=>T(r,p);return{get:m,set:f,sub:t,txn:i,reset:n,data:p}};if(!globalThis._valdresStore)globalThis._valdresStore=B("default");var x=()=>globalThis._valdresStore,k=()=>globalThis._valdresStore=B();var Q=(o,p)=>({get:o,debugLabel:p});var v=(o,p)=>{const m=new Map,f=(n)=>{let t;try{t=I(n)}catch(y){throw console.log("errro",{key:n,debugLabel:p,e:y}),y}if(m.has(t))return m.get(t);const i=p?p+"_"+t:void 0,r=Q((y)=>o(n)(y),i);return r.family=f,m.set(t,r),r};return f._map=m,f};export{v as selectorFamily,Q as selector,k as resetDefaultStore,K as isSelector,D as isPromiseLike,F as isFamily,G as isAtom,x as getDefaultStore,B as createStore,W as atomFamily,H as atom};
@@ -0,0 +1,9 @@
1
+ import type { Atom } from "./types/Atom";
2
+ type AtomOptions<M = any> = {
3
+ label?: string;
4
+ onInit?: () => void;
5
+ onMount?: () => M;
6
+ onUnmount?: (mountRes?: M) => void;
7
+ };
8
+ export declare const atom: <Value, FamilyKey = undefined, MountReturnValue = undefined>(defaultValue?: Value | (() => Value | Promise<Value>), options?: AtomOptions<MountReturnValue>) => Atom<Value, FamilyKey>;
9
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { AtomFamily } from "./types/AtomFamily";
2
+ export declare const atomFamily: <Value, Key>(defaultValue?: Value | ((arg: Key) => Value | Promise<Value>), debugLabel?: string) => AtomFamily<Value, Key>;
@@ -0,0 +1,2 @@
1
+ import type { Store } from "./types/Store";
2
+ export declare const createStore: (id?: string) => Store;
@@ -0,0 +1,2 @@
1
+ export declare const getDefaultStore: () => any;
2
+ export declare const resetDefaultStore: () => import("..").Store;
@@ -0,0 +1,3 @@
1
+ import type { State } from "../types/State";
2
+ import type { StoreData } from "../types/StoreData";
3
+ export declare const getState: <V>(state: State<V>, data: StoreData) => V | Promise<V>;
@@ -0,0 +1,4 @@
1
+ import type { Atom } from "../types/Atom";
2
+ import type { StoreData } from "../types/StoreData";
3
+ export declare const getAtomInitValue: <V>(atom: Atom<V>, data: StoreData) => any;
4
+ export declare const initAtom: <V>(atom: Atom<V>, data: StoreData) => any;
@@ -0,0 +1,4 @@
1
+ import type { StoreData } from "../types/StoreData";
2
+ import type { Selector } from "../types/Selector";
3
+ export declare const reEvaluateSelector: <V>(selector: Selector<V>, data: StoreData) => void;
4
+ export declare const initSelector: <V>(selector: Selector<V>, data: StoreData) => V;
@@ -0,0 +1,3 @@
1
+ import type { Atom } from "../types/Atom";
2
+ import type { StoreData } from "../types/StoreData";
3
+ export declare const propagateUpdatedAtoms: (atoms: Atom[], data: StoreData) => void;
@@ -0,0 +1,3 @@
1
+ import type { Atom } from "../types/Atom";
2
+ import type { StoreData } from "../types/StoreData";
3
+ export declare const resetAtom: <V>(atom: Atom<V>, data: StoreData) => V | Promise<V>;
@@ -0,0 +1,3 @@
1
+ import type { Atom } from "../types/Atom";
2
+ import type { StoreData } from "../types/StoreData";
3
+ export declare const setAtom: <V>(atom: Atom<V>, newValue: V, data: StoreData) => void;
@@ -0,0 +1,3 @@
1
+ import type { Atom } from "../types/Atom";
2
+ import type { StoreData } from "../types/StoreData";
3
+ export declare const setAtoms: (pairs: Map<Atom<any>, any>, data: StoreData) => void;
@@ -0,0 +1 @@
1
+ export declare const stableStringify: (x: any) => string | number | boolean;
@@ -0,0 +1,3 @@
1
+ import type { State } from "../types/State";
2
+ import type { StoreData } from "../types/StoreData";
3
+ export declare const subscribe: <V>(state: State<V>, callback: any, data: StoreData) => () => void;
@@ -0,0 +1,8 @@
1
+ import type { State } from "../types/State";
2
+ import type { StoreData } from "../types/StoreData";
3
+ type GetValdresValue = <V>(state: State<V>) => V;
4
+ type SetValdresValue = <V>(state: State<V>, value: V) => void;
5
+ type ResetValdresValue = <V>(state: State<V>) => V;
6
+ type TransactionInterface = (set: SetValdresValue, get: GetValdresValue, reset: ResetValdresValue, commit: () => void) => void;
7
+ export declare const transaction: (callback: TransactionInterface, data: StoreData) => void;
8
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { State } from "../types/State";
2
+ import type { StoreData } from "../types/StoreData";
3
+ export declare const unsubscribe: <V>(state: State<V>, subscription: any, store: StoreData, mountRes?: any) => void;
@@ -0,0 +1,3 @@
1
+ import type { Selector } from "../types/Selector";
2
+ import type { StoreData } from "../types/StoreData";
3
+ export declare const updateSelectorSubscribers: (selector: Selector, data: StoreData, oldValue: any) => void;
@@ -0,0 +1,3 @@
1
+ import type { State } from "../types/State";
2
+ import type { StoreData } from "../types/StoreData";
3
+ export declare const updateStateSubscribers: (state: State, data: StoreData) => void;
@@ -0,0 +1,5 @@
1
+ import type { GetValue } from "./types/GetValue";
2
+ export declare const selector: (get: GetValue, debugLabel?: string) => {
3
+ get: GetValue;
4
+ debugLabel: string;
5
+ };
@@ -0,0 +1,4 @@
1
+ export declare const selectorFamily: <V, A>(get: any, debugLabel?: string) => {
2
+ (key: A): any;
3
+ _map: Map<any, any>;
4
+ };
@@ -0,0 +1,7 @@
1
+ import type { AtomFamily } from "./AtomFamily";
2
+ export type Atom<Value = unknown, FamilyKey = undefined> = {
3
+ defaultValue?: Value | (() => Value | Promise<Value>);
4
+ label?: string;
5
+ family?: AtomFamily<Value, FamilyKey>;
6
+ familyKey?: FamilyKey;
7
+ };
@@ -0,0 +1,5 @@
1
+ import type { Atom } from "./Atom";
2
+ export type AtomFamily<Value, Key> = {
3
+ (key: Key, defaultOverride?: any): Atom<Value, Key>;
4
+ _map: Map<Key, Atom<Value, Key>>;
5
+ };
@@ -0,0 +1,3 @@
1
+ import type { SelectorFamily } from "./SelectorFamily";
2
+ import type { AtomFamily } from "./AtomFamily";
3
+ export type Family<V, K> = AtomFamily<V, K> | SelectorFamily<V, K>;
@@ -0,0 +1,2 @@
1
+ import type { State } from "./State";
2
+ export type GetValue = <V>(state: State<V>) => V;
@@ -0,0 +1,2 @@
1
+ import type { Atom } from "./Atom";
2
+ export type ResetAtom = <V>(state: Atom<V>) => V;
@@ -0,0 +1,4 @@
1
+ export type Selector<V = any> = {
2
+ get: () => {};
3
+ debugLabel?: string;
4
+ };
@@ -0,0 +1 @@
1
+ export type SelectorFamily<A, V> = {};
@@ -0,0 +1,2 @@
1
+ import type { Atom } from "./Atom";
2
+ export type SetAtom = <V>(atom: Atom<V>, value: V) => void;
@@ -0,0 +1,3 @@
1
+ import type { Atom } from "./Atom";
2
+ import type { Selector } from "./Selector";
3
+ export type State<V = any> = Atom<V> | Selector<V>;
@@ -0,0 +1,14 @@
1
+ import type { GetValue } from "./GetValue";
2
+ import type { ResetAtom } from "./ResetAtom";
3
+ import type { SetAtom } from "./SetAtom";
4
+ import type { StoreData } from "./StoreData";
5
+ import type { SubscribeFn } from "./SubscribeFn";
6
+ import type { TransactionFn } from "./TransactionFn";
7
+ export type Store = {
8
+ data: StoreData;
9
+ get: GetValue;
10
+ set: SetAtom;
11
+ sub: SubscribeFn;
12
+ reset: ResetAtom;
13
+ txn: (callback: TransactionFn) => void;
14
+ };
@@ -0,0 +1,7 @@
1
+ export type StoreData = {
2
+ id: string;
3
+ values: WeakMap<WeakKey, any>;
4
+ subscriptions: WeakMap<WeakKey, any>;
5
+ stateConsumers: WeakMap<WeakKey, any>;
6
+ stateDependencies: WeakMap<WeakKey, any>;
7
+ };
@@ -0,0 +1,2 @@
1
+ import type { State } from "./State";
2
+ export type SubscribeFn = <V>(state: State<V>, callback: () => void) => () => void;
@@ -0,0 +1,4 @@
1
+ import type { GetValue } from "./GetValue";
2
+ import type { ResetAtom } from "./ResetAtom";
3
+ import type { SetAtom } from "./SetAtom";
4
+ export type TransactionFn = (set: SetAtom, get: GetValue, reset: ResetAtom, commit: () => void) => void;
@@ -0,0 +1,2 @@
1
+ import type { State } from "../types/State";
2
+ export declare const isAtom: (state: State) => boolean;
@@ -0,0 +1 @@
1
+ export declare const isFamily: (state: any) => boolean;
@@ -0,0 +1 @@
1
+ export declare const isPromiseLike: (object: any) => boolean;
@@ -0,0 +1,3 @@
1
+ import type { Selector } from "../types/Selector";
2
+ import type { State } from "../types/State";
3
+ export declare const isSelector: (state: State) => state is Selector;
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "valdres",
3
+ "version": "0.1.0-alpha.10",
4
+ "module": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "type": "module",
10
+ "scripts": {
11
+ "build": "bun scripts/build.ts",
12
+ "build:types": "tsc index.ts --declaration --emitDeclarationOnly --outDir dist",
13
+ "test": "bun test"
14
+ },
15
+ "dependencies": {
16
+ "fast-deep-equal": ">=3"
17
+ },
18
+ "devDependencies": {
19
+ "@testing-library/react-hooks": "8.0.1",
20
+ "react": ">=18",
21
+ "react-dom": ">=18",
22
+ "recoil": "0.7.7",
23
+ "typescript": "5.5.4"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public",
27
+ "registry": "https://registry.npmjs.org/"
28
+ },
29
+ "gitHead": "ad00a46c27d9cc4f6233233558dac8e63caf4c76"
30
+ }