statedrive 0.1.1 → 0.5.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/dist/_hook_factory-Du0yg9Jl.js +90 -0
- package/dist/_hook_factory-Du0yg9Jl.js.map +1 -0
- package/dist/index.d.ts +12 -4
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/react-index.d.ts +27 -0
- package/dist/react-index.d.ts.map +1 -0
- package/dist/react-index.js +19 -0
- package/dist/react-index.js.map +1 -0
- package/dist/subscribe-DZeYBamT.d.ts +36 -0
- package/dist/subscribe-DZeYBamT.d.ts.map +1 -0
- package/package.json +30 -22
- package/dist/_hook_factory.d.ts +0 -12
- package/dist/hooks.d.ts +0 -4
- package/dist/react/src/hooks.d.ts +0 -12
- package/dist/react/src/index.d.ts +0 -4
- package/dist/react.modern.js +0 -2
- package/dist/react.modern.js.map +0 -1
- package/dist/src/_hook_factory.d.ts +0 -12
- package/dist/src/state.d.ts +0 -4
- package/dist/src/subscribe.d.ts +0 -8
- package/dist/src/types.d.ts +0 -18
- package/dist/src/util.d.ts +0 -2
- package/dist/state.d.ts +0 -4
- package/dist/statedrive.modern.js +0 -2
- package/dist/statedrive.modern.js.map +0 -1
- package/dist/subscribe.d.ts +0 -8
- package/dist/types.d.ts +0 -18
- package/dist/util.d.ts +0 -2
- package/react/package.json +0 -17
- package/react/src/hooks.ts +0 -27
- package/react/src/index.ts +0 -4
- package/react/yarn.lock +0 -52
- package/src/_hook_factory.ts +0 -77
- package/src/hooks.ts +0 -26
- package/src/index.ts +0 -4
- package/src/state.ts +0 -27
- package/src/subscribe.ts +0 -26
- package/src/types.ts +0 -20
- package/src/util.ts +0 -6
- package/tsconfig.json +0 -10
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
//#region src/subscribe.ts
|
|
2
|
+
const listenerMap = new WeakMap();
|
|
3
|
+
function notify(state, oldValue, newValue) {
|
|
4
|
+
const listeners = listenerMap.get(state);
|
|
5
|
+
if (listeners) listeners.forEach((fn) => fn(oldValue, newValue));
|
|
6
|
+
}
|
|
7
|
+
function subscribe(state, callback) {
|
|
8
|
+
let listeners = listenerMap.get(state);
|
|
9
|
+
if (listeners == null) {
|
|
10
|
+
listeners = new Set();
|
|
11
|
+
listenerMap.set(state, listeners);
|
|
12
|
+
}
|
|
13
|
+
listeners.add(callback);
|
|
14
|
+
}
|
|
15
|
+
function unsubscribe(state, callback) {
|
|
16
|
+
const listeners = listenerMap.get(state);
|
|
17
|
+
if (listeners) listeners.delete(callback);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/util.ts
|
|
22
|
+
function consumeUpdater(u, oldValue) {
|
|
23
|
+
if (typeof u === "function") return u(oldValue);
|
|
24
|
+
return u;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/state.ts
|
|
29
|
+
const valueMap = new WeakMap();
|
|
30
|
+
function createState(options) {
|
|
31
|
+
const state = _state(options || {});
|
|
32
|
+
valueMap.set(state, options.initialValue);
|
|
33
|
+
return state;
|
|
34
|
+
}
|
|
35
|
+
function _state(options) {
|
|
36
|
+
return { name: options.name };
|
|
37
|
+
}
|
|
38
|
+
function get(state) {
|
|
39
|
+
return valueMap.get(state);
|
|
40
|
+
}
|
|
41
|
+
function set(state, newValue) {
|
|
42
|
+
const oldValue = get(state);
|
|
43
|
+
const next = consumeUpdater(newValue, oldValue);
|
|
44
|
+
valueMap.set(state, next);
|
|
45
|
+
notify(state, oldValue, next);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/_hook_factory.ts
|
|
50
|
+
function createUseSharedState(useEffect, useState) {
|
|
51
|
+
return function useSharedState(state) {
|
|
52
|
+
const [value, setValue] = useState(() => get(state));
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
const listener = (_, newVal) => setValue(newVal);
|
|
55
|
+
subscribe(state, listener);
|
|
56
|
+
return () => unsubscribe(state, listener);
|
|
57
|
+
}, [state]);
|
|
58
|
+
return [value, (nextValue) => set(state, nextValue)];
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function createUseSelector(useEffect, useState, useMemo, useCallback) {
|
|
62
|
+
return function useSelector(func) {
|
|
63
|
+
const hasSubscribed = useMemo(() => new Set(), []);
|
|
64
|
+
const [, setState] = useState(null);
|
|
65
|
+
const fn = useCallback(() => setState({}), []);
|
|
66
|
+
const _get = useCallback((s) => {
|
|
67
|
+
if (!hasSubscribed.has(s)) {
|
|
68
|
+
hasSubscribed.add(s);
|
|
69
|
+
subscribe(s, fn);
|
|
70
|
+
}
|
|
71
|
+
return get(s);
|
|
72
|
+
}, [hasSubscribed]);
|
|
73
|
+
useEffect(() => () => hasSubscribed.forEach((x) => unsubscribe(x, fn)), [hasSubscribed, fn]);
|
|
74
|
+
return func({ get: _get });
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function createUseSharedStateValue(useSharedState) {
|
|
78
|
+
return function useSharedStateValue(s) {
|
|
79
|
+
return useSharedState(s)[0];
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function createUseSetSharedState(useSharedState) {
|
|
83
|
+
return function useSetSharedState(s) {
|
|
84
|
+
return useSharedState(s)[1];
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
export { createState, createUseSelector, createUseSetSharedState, createUseSharedState, createUseSharedStateValue, get, notify, set, subscribe, unsubscribe };
|
|
90
|
+
//# sourceMappingURL=_hook_factory-Du0yg9Jl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_hook_factory-Du0yg9Jl.js","names":["state: State<T>","oldValue: T","newValue: T","callback: Listener<T>","u: StateUpdater<T>","oldValue: T","options: StateOptions<T>","options: StateOptions<T> | State<T>","state: State<T>","newValue: StateUpdater<T>","useEffect: Hooks.useEffect","useState: Hooks.useState","state: State<T>","_: T","newVal: T","nextValue: StateUpdater<T>","useMemo: Hooks.useMemo","useCallback: Hooks.useCallback","func: (options: SelectorOptions<R>) => R","s: State<S>","useSharedState: ReturnType<typeof createUseSharedState>","s: State<T>"],"sources":["../src/subscribe.ts","../src/util.ts","../src/state.ts","../src/_hook_factory.ts"],"sourcesContent":["import type {State} from \"./types\";\n\ninterface Listener<T> {\n (oldValue: T, newValue: T): void;\n}\n\nconst listenerMap = new WeakMap<State<any>, Set<Listener<any>>>();\n\nexport function notify<T>(state: State<T>, oldValue: T, newValue: T) {\n const listeners = listenerMap.get(state) as Set<Listener<T>> | undefined;\n if (listeners) listeners.forEach((fn) => fn(oldValue, newValue));\n}\n\nexport function subscribe<T>(state: State<T>, callback: Listener<T>) {\n let listeners = listenerMap.get(state) as Set<Listener<T>> | undefined;\n if (listeners == null) {\n listeners = new Set();\n listenerMap.set(state, listeners);\n }\n listeners.add(callback);\n}\n\nexport function unsubscribe<T>(state: State<T>, callback: Listener<T>) {\n const listeners = listenerMap.get(state) as Set<Listener<T>> | undefined;\n if (listeners) listeners.delete(callback);\n}\n","import type { StateUpdater, FunctionUpdater } from \"./types\";\n\nexport function consumeUpdater<T>(u: StateUpdater<T>, oldValue: T) {\n if (typeof u === \"function\") return (u as FunctionUpdater<T>)(oldValue);\n return u;\n}\n","import { notify } from \"./subscribe\";\nimport type { State, StateOptions, StateUpdater } from \"./types\";\nimport { consumeUpdater } from \"./util\";\n\nconst valueMap = new WeakMap<State<unknown>, unknown>();\n\nexport function createState<T>(options: StateOptions<T>): State<T> {\n const state = _state(options || {});\n valueMap.set(state, options.initialValue);\n return state;\n}\n\nfunction _state<T>(options: StateOptions<T> | State<T>) {\n return { name: options.name };\n}\n\nexport function get<T>(state: State<T>): T {\n return valueMap.get(state) as T;\n}\n\nexport function set<T>(state: State<T>, newValue: StateUpdater<T>) {\n const oldValue = get(state);\n const next = consumeUpdater(newValue, oldValue);\n valueMap.set(state, next);\n notify(state, oldValue, next);\n}\n","import {get, set} from \"./state\";\nimport {subscribe, unsubscribe} from \"./subscribe\";\nimport type {\n SelectorOptions,\n SetSharedState,\n State,\n StateUpdater,\n} from \"./types\";\n\n// Generic hook type signatures compatible with React, Preact, and similar frameworks.\n// Uses `any` in places where framework hook signatures are inherently polymorphic.\nexport namespace Hooks {\n export type useEffect = (\n effect: () => void | (() => void),\n deps?: any[],\n ) => void;\n export type useState = <S>(\n initialState: S | (() => S),\n ) => [S, (value: any) => void];\n export type useCallback = (callback: any, deps: any[]) => any;\n export type useMemo = <T>(factory: () => T, deps: any[]) => T;\n}\n\nexport function createUseSharedState(\n useEffect: Hooks.useEffect,\n useState: Hooks.useState,\n) {\n return function useSharedState<T>(state: State<T>): [T, SetSharedState<T>] {\n const [value, setValue] = useState(() => get(state));\n\n useEffect(() => {\n const listener = (_: T, newVal: T) => setValue(newVal);\n subscribe(state, listener);\n return () => unsubscribe(state, listener);\n }, [state]);\n\n return [value, (nextValue: StateUpdater<T>) => set(state, nextValue)];\n };\n}\n\nexport function createUseSelector(\n useEffect: Hooks.useEffect,\n useState: Hooks.useState,\n useMemo: Hooks.useMemo,\n useCallback: Hooks.useCallback,\n) {\n return function useSelector<R>(func: (options: SelectorOptions<R>) => R): R {\n const hasSubscribed = useMemo(() => new Set<State<unknown>>(), []);\n const [, setState] = useState<object | null>(null);\n const fn = useCallback(() => setState({}), []);\n const _get = useCallback(\n <S>(s: State<S>): S => {\n if (!hasSubscribed.has(s as State<unknown>)) {\n hasSubscribed.add(s as State<unknown>);\n subscribe(s, fn);\n }\n return get(s);\n },\n [hasSubscribed],\n );\n useEffect(\n () => () => hasSubscribed.forEach((x) => unsubscribe(x, fn)),\n [hasSubscribed, fn],\n );\n return func({get: _get});\n };\n}\n\nexport function createUseSharedStateValue(\n useSharedState: ReturnType<typeof createUseSharedState>,\n) {\n return function useSharedStateValue<T>(s: State<T>): T {\n return useSharedState(s)[0];\n };\n}\nexport function createUseSetSharedState(\n useSharedState: ReturnType<typeof createUseSharedState>,\n) {\n return function useSetSharedState<T>(s: State<T>): SetSharedState<T> {\n return useSharedState(s)[1];\n };\n}\n"],"mappings":";AAMA,MAAM,cAAc,IAAI;AAExB,SAAgB,OAAUA,OAAiBC,UAAaC,UAAa;CACnE,MAAM,YAAY,YAAY,IAAI,MAAM;AACxC,KAAI,UAAW,WAAU,QAAQ,CAAC,OAAO,GAAG,UAAU,SAAS,CAAC;AACjE;AAED,SAAgB,UAAaF,OAAiBG,UAAuB;CACnE,IAAI,YAAY,YAAY,IAAI,MAAM;AACtC,KAAI,aAAa,MAAM;AACrB,cAAY,IAAI;AAChB,cAAY,IAAI,OAAO,UAAU;CAClC;AACD,WAAU,IAAI,SAAS;AACxB;AAED,SAAgB,YAAeH,OAAiBG,UAAuB;CACrE,MAAM,YAAY,YAAY,IAAI,MAAM;AACxC,KAAI,UAAW,WAAU,OAAO,SAAS;AAC1C;;;;ACvBD,SAAgB,eAAkBC,GAAoBC,UAAa;AACjE,YAAW,MAAM,WAAY,QAAO,AAAC,EAAyB,SAAS;AACvE,QAAO;AACR;;;;ACDD,MAAM,WAAW,IAAI;AAErB,SAAgB,YAAeC,SAAoC;CACjE,MAAM,QAAQ,OAAO,WAAW,CAAE,EAAC;AACnC,UAAS,IAAI,OAAO,QAAQ,aAAa;AACzC,QAAO;AACR;AAED,SAAS,OAAUC,SAAqC;AACtD,QAAO,EAAE,MAAM,QAAQ,KAAM;AAC9B;AAED,SAAgB,IAAOC,OAAoB;AACzC,QAAO,SAAS,IAAI,MAAM;AAC3B;AAED,SAAgB,IAAOA,OAAiBC,UAA2B;CACjE,MAAM,WAAW,IAAI,MAAM;CAC3B,MAAM,OAAO,eAAe,UAAU,SAAS;AAC/C,UAAS,IAAI,OAAO,KAAK;AACzB,QAAO,OAAO,UAAU,KAAK;AAC9B;;;;ACFD,SAAgB,qBACdC,WACAC,UACA;AACA,QAAO,SAAS,eAAkBC,OAAyC;EACzE,MAAM,CAAC,OAAO,SAAS,GAAG,SAAS,MAAM,IAAI,MAAM,CAAC;AAEpD,YAAU,MAAM;GACd,MAAM,WAAW,CAACC,GAAMC,WAAc,SAAS,OAAO;AACtD,aAAU,OAAO,SAAS;AAC1B,UAAO,MAAM,YAAY,OAAO,SAAS;EAC1C,GAAE,CAAC,KAAM,EAAC;AAEX,SAAO,CAAC,OAAO,CAACC,cAA+B,IAAI,OAAO,UAAU,AAAC;CACtE;AACF;AAED,SAAgB,kBACdL,WACAC,UACAK,SACAC,aACA;AACA,QAAO,SAAS,YAAeC,MAA6C;EAC1E,MAAM,gBAAgB,QAAQ,MAAM,IAAI,OAAuB,CAAE,EAAC;EAClE,MAAM,GAAG,SAAS,GAAG,SAAwB,KAAK;EAClD,MAAM,KAAK,YAAY,MAAM,SAAS,CAAE,EAAC,EAAE,CAAE,EAAC;EAC9C,MAAM,OAAO,YACX,CAAIC,MAAmB;AACrB,QAAK,cAAc,IAAI,EAAoB,EAAE;AAC3C,kBAAc,IAAI,EAAoB;AACtC,cAAU,GAAG,GAAG;GACjB;AACD,UAAO,IAAI,EAAE;EACd,GACD,CAAC,aAAc,EAChB;AACD,YACE,MAAM,MAAM,cAAc,QAAQ,CAAC,MAAM,YAAY,GAAG,GAAG,CAAC,EAC5D,CAAC,eAAe,EAAG,EACpB;AACD,SAAO,KAAK,EAAC,KAAK,KAAK,EAAC;CACzB;AACF;AAED,SAAgB,0BACdC,gBACA;AACA,QAAO,SAAS,oBAAuBC,GAAgB;AACrD,SAAO,eAAe,EAAE,CAAC;CAC1B;AACF;AACD,SAAgB,wBACdD,gBACA;AACA,QAAO,SAAS,kBAAqBC,GAAgC;AACnE,SAAO,eAAe,EAAE,CAAC;CAC1B;AACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { FunctionUpdater, SelectorOptions, SetSharedState, State, StateOptions, StateUpdater, createState$1 as createState, get$1 as get, notify$1 as notify, set$1 as set, subscribe$1 as subscribe, unsubscribe$1 as unsubscribe } from "./subscribe-DZeYBamT.js";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks.d.ts
|
|
4
|
+
declare const useSharedState: <T>(state: State<T>) => [T, SetSharedState<T>];
|
|
5
|
+
declare const useSelector: <R>(func: (options: SelectorOptions<R>) => R) => R;
|
|
6
|
+
declare const useSharedStateValue: <T>(s: State<T>) => T;
|
|
7
|
+
declare const useSetSharedState: <T>(s: State<T>) => SetSharedState<T>;
|
|
8
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
export { type FunctionUpdater, type SelectorOptions, type SetSharedState, type State, type StateOptions, type StateUpdater, createState, get, notify, set, subscribe, unsubscribe, useSelector, useSetSharedState, useSharedState, useSharedStateValue };
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/hooks.ts"],"sourcesContent":[],"mappings":";;;cASa,2BAAc,MAAA,QAAA,GAAA,eAAA;cAKd,iCAAW,gBAAA,OAAA,MAAA;cAOX,4BAAmB,MAAA,OAAA;AAZnB,cAeA,iBAZZ,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAY6B,KAZ7B,CAY6B,CAZ7B,CAAA,EAAA,GAY6B,cAZ7B,CAY6B,CAZ7B,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createState, createUseSelector, createUseSetSharedState, createUseSharedState, createUseSharedStateValue, get, notify, set, subscribe, unsubscribe } from "./_hook_factory-Du0yg9Jl.js";
|
|
2
|
+
import { useCallback, useEffect, useMemo, useState } from "@hydrophobefireman/ui-lib";
|
|
3
|
+
|
|
4
|
+
//#region src/hooks.ts
|
|
5
|
+
const useSharedState = /* @__PURE__ */ createUseSharedState(useEffect, useState);
|
|
6
|
+
const useSelector = /* @__PURE__ */ createUseSelector(useEffect, useState, useMemo, useCallback);
|
|
7
|
+
const useSharedStateValue = /* @__PURE__ */ createUseSharedStateValue(useSharedState);
|
|
8
|
+
const useSetSharedState = /* @__PURE__ */ createUseSetSharedState(useSharedState);
|
|
9
|
+
|
|
10
|
+
//#endregion
|
|
11
|
+
export { createState, get, notify, set, subscribe, unsubscribe, useSelector, useSetSharedState, useSharedState, useSharedStateValue };
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/hooks.ts"],"sourcesContent":["import {\n useCallback,\n useEffect,\n useMemo,\n useState,\n} from \"@hydrophobefireman/ui-lib\";\n\nimport * as factory from \"./_hook_factory\";\n\nexport const useSharedState = /*#__PURE__*/ factory.createUseSharedState(\n useEffect,\n useState,\n);\n\nexport const useSelector = /*#__PURE__*/ factory.createUseSelector(\n useEffect,\n useState,\n useMemo,\n useCallback,\n);\n\nexport const useSharedStateValue =\n /*#__PURE__*/ factory.createUseSharedStateValue(useSharedState);\n\nexport const useSetSharedState =\n /*#__PURE__*/ factory.createUseSetSharedState(useSharedState);\n"],"mappings":";;;;AASA,MAAa,iCAA+B,qBAC1C,WACA,SACD;AAED,MAAa,8BAA4B,kBACvC,WACA,UACA,SACA,YACD;AAED,MAAa,sCACG,0BAAkC,eAAe;AAEjE,MAAa,oCACG,wBAAgC,eAAe"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { FunctionUpdater, SelectorOptions, SetSharedState, State, StateOptions, StateUpdater, createState$1 as createState, get$1 as get, notify$1 as notify, set$1 as set, subscribe$1 as subscribe, unsubscribe$1 as unsubscribe } from "./subscribe-DZeYBamT.js";
|
|
2
|
+
|
|
3
|
+
//#region src/_hook_factory.d.ts
|
|
4
|
+
declare namespace Hooks {
|
|
5
|
+
type useEffect = (effect: () => void | (() => void), deps?: any[]) => void;
|
|
6
|
+
type useState = <S>(initialState: S | (() => S)) => [S, (value: any) => void];
|
|
7
|
+
type useCallback = (callback: any, deps: any[]) => any;
|
|
8
|
+
type useMemo = <T>(factory: () => T, deps: any[]) => T;
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/react-hooks.d.ts
|
|
12
|
+
declare function buildReactStatedrive(data: {
|
|
13
|
+
useEffect: Hooks.useEffect;
|
|
14
|
+
useState: Hooks.useState;
|
|
15
|
+
useMemo: Hooks.useMemo;
|
|
16
|
+
useCallback: Hooks.useCallback;
|
|
17
|
+
}): {
|
|
18
|
+
useSharedState: <T>(state: State<T>) => [T, SetSharedState<T>];
|
|
19
|
+
useSelector: <R>(func: (options: SelectorOptions<R>) => R) => R;
|
|
20
|
+
useSharedStateValue: <T>(s: State<T>) => T;
|
|
21
|
+
useSetSharedState: <T>(s: State<T>) => SetSharedState<T>;
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=react-hooks.d.ts.map
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { type FunctionUpdater, type SelectorOptions, type SetSharedState, type State, type StateOptions, type StateUpdater, buildReactStatedrive, createState, get, notify, set, subscribe, unsubscribe };
|
|
27
|
+
//# sourceMappingURL=react-index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react-index.d.ts","names":[],"sources":["../src/_hook_factory.ts","../src/react-hooks.ts"],"sourcesContent":[],"mappings":";;;kBAWiB,KAAA;;EAAA,KAAA,QAAK,GAAA,CAAA,CAAA,CAAA,CAAA,YAAA,EAMJ,CANI,GAAA,CAAA,GAAA,GAMO,CANP,CAAA,EAAA,GAAA,CAOd,CAPc,EAAA,CAAA,KAAA,EAAA,GAAA,EAAA,GAAA,IAAA,CAAA;EAAA,KAAA,WAAA,GAAA,CAAA,QAAA,EAAA,GAAA,EAAA,IAAA,EAAA,GAAA,EAAA,EAAA,GAAA,GAAA;EAAA,KAMJ,OAAA,GAAA,CAAA,CAAA,CAAA,CAAA,OAAA,EAAA,GAAA,GAGyB,CAHzB,EAAA,IAAA,EAAA,GAAA,EAAA,EAAA,GAG4C,CAH5C;;;;iBCfF,oBAAA;aAAoB,KAAA,CACvB;EDQI,QAAK,ECRc,KAAA,CACxB,QDOU;EAAA,OAAA,ECPY,KAAA,CACvB,ODMW;EAAA,WAMJ,ECZc,KAAA,CACjB,WDWG;CAAC,CAAA,EAAA;EAAW,cACtB,EAAA,CAAA,CAAA,CAAA,CAAA,KAAA,OAAA,CCZgC,CDYhC,CAAA,EAAA,GAAA,EAAA,gBAAA,EAAA,CAAA,CAAA;EAAC,WAEkC,EAAA,CAAA,CAAA,CAAA,CAAA,IAAA,EAAA,CAAA,OAAA,iBAAA,EAAA,CAAA,EAAA,IAAA,EAAA,IAAA;EAAC,mBAAkB,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,OAAA,EAAA,CAAA,EAAA,IAAA;EAAC,iBAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,OAAA,EAAA,CAAA,EAAA,iBAAA,EAAA,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createState, createUseSelector, createUseSetSharedState, createUseSharedState, createUseSharedStateValue, get, notify, set, subscribe, unsubscribe } from "./_hook_factory-Du0yg9Jl.js";
|
|
2
|
+
|
|
3
|
+
//#region src/react-hooks.ts
|
|
4
|
+
function buildReactStatedrive(data) {
|
|
5
|
+
const useSharedState = /* @__PURE__ */ createUseSharedState(data.useEffect, data.useState);
|
|
6
|
+
const useSelector = /* @__PURE__ */ createUseSelector(data.useEffect, data.useState, data.useMemo, data.useCallback);
|
|
7
|
+
const useSharedStateValue = /* @__PURE__ */ createUseSharedStateValue(useSharedState);
|
|
8
|
+
const useSetSharedState = /* @__PURE__ */ createUseSetSharedState(useSharedState);
|
|
9
|
+
return {
|
|
10
|
+
useSharedState,
|
|
11
|
+
useSelector,
|
|
12
|
+
useSharedStateValue,
|
|
13
|
+
useSetSharedState
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { buildReactStatedrive, createState, get, notify, set, subscribe, unsubscribe };
|
|
19
|
+
//# sourceMappingURL=react-index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react-index.js","names":["data: {\n useEffect: factory.Hooks.useEffect;\n useState: factory.Hooks.useState;\n useMemo: factory.Hooks.useMemo;\n useCallback: factory.Hooks.useCallback;\n}"],"sources":["../src/react-hooks.ts"],"sourcesContent":["import * as factory from \"./_hook_factory\";\n\nexport function buildReactStatedrive(data: {\n useEffect: factory.Hooks.useEffect;\n useState: factory.Hooks.useState;\n useMemo: factory.Hooks.useMemo;\n useCallback: factory.Hooks.useCallback;\n}) {\n const useSharedState = /*#__PURE__*/ factory.createUseSharedState(\n data.useEffect,\n data.useState\n );\n\n const useSelector = /*#__PURE__*/ factory.createUseSelector(\n data.useEffect,\n data.useState,\n data.useMemo,\n data.useCallback\n );\n\n const useSharedStateValue =\n /*#__PURE__*/ factory.createUseSharedStateValue(useSharedState);\n\n const useSetSharedState =\n /*#__PURE__*/ factory.createUseSetSharedState(useSharedState);\n return { useSharedState, useSelector, useSharedStateValue, useSetSharedState };\n}\n"],"mappings":";;;AAEA,SAAgB,qBAAqBA,MAKlC;CACD,MAAM,iCAA+B,qBACnC,KAAK,WACL,KAAK,SACN;CAED,MAAM,8BAA4B,kBAChC,KAAK,WACL,KAAK,UACL,KAAK,SACL,KAAK,YACN;CAED,MAAM,sCACU,0BAAkC,eAAe;CAEjE,MAAM,oCACU,wBAAgC,eAAe;AAC/D,QAAO;EAAE;EAAgB;EAAa;EAAqB;CAAmB;AAC/E"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
interface StateOptions<T> {
|
|
3
|
+
name?: string;
|
|
4
|
+
initialValue?: T;
|
|
5
|
+
}
|
|
6
|
+
interface State<T> {
|
|
7
|
+
name: string | undefined;
|
|
8
|
+
}
|
|
9
|
+
interface FunctionUpdater<T> {
|
|
10
|
+
(previous?: T): T;
|
|
11
|
+
}
|
|
12
|
+
interface SetSharedState<T> {
|
|
13
|
+
(val: StateUpdater<T>): void;
|
|
14
|
+
}
|
|
15
|
+
interface SelectorOptions<T> {
|
|
16
|
+
get(s: State<T>): T;
|
|
17
|
+
}
|
|
18
|
+
type StateUpdater<T> = T | FunctionUpdater<T>;
|
|
19
|
+
//# sourceMappingURL=types.d.ts.map
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/state.d.ts
|
|
22
|
+
declare function createState<T>(options: StateOptions<T>): State<T>;
|
|
23
|
+
declare function get<T>(state: State<T>): T;
|
|
24
|
+
declare function set<T>(state: State<T>, newValue: StateUpdater<T>): void;
|
|
25
|
+
//# sourceMappingURL=state.d.ts.map
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/subscribe.d.ts
|
|
28
|
+
interface Listener<T> {
|
|
29
|
+
(oldValue: T, newValue: T): void;
|
|
30
|
+
}
|
|
31
|
+
declare function notify<T>(state: State<T>, oldValue: T, newValue: T): void;
|
|
32
|
+
declare function subscribe<T>(state: State<T>, callback: Listener<T>): void;
|
|
33
|
+
declare function unsubscribe<T>(state: State<T>, callback: Listener<T>): void;
|
|
34
|
+
//#endregion
|
|
35
|
+
export { FunctionUpdater, SelectorOptions, SetSharedState, State, StateOptions, StateUpdater, createState as createState$1, get as get$1, notify as notify$1, set as set$1, subscribe as subscribe$1, unsubscribe as unsubscribe$1 };
|
|
36
|
+
//# sourceMappingURL=subscribe-DZeYBamT.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subscribe-DZeYBamT.d.ts","names":[],"sources":["../src/types.ts","../src/state.ts","../src/subscribe.ts"],"sourcesContent":[],"mappings":";UAAiB;EAAA,IAAA,CAAA,EAAA,MAAA;EAIA,YAAK,CAAA,EAFL,CAEK;AAGtB;AAAgC,UAHf,KAGe,CAAA,CAAA,CAAA,CAAA;EAAA,IAClB,EAAA,MAAA,GAAA,SAAA;;AAAK,UADF,eACE,CAAA,CAAA,CAAA,CAAA;EAEF,CAAA,QAAA,CAAA,EAFH,CAEG,CAAA,EAFC,CAEa;;AACV,UADJ,cACI,CAAA,CAAA,CAAA,CAAA;EAAC,CAAA,GAAd,EAAA,YAAA,CAAa,CAAb,CAAA,CAAA,EAAA,IAAA;AAAY;AAEH,UAAA,eAAe,CAAA,CAAA,CAAA,CAAA;EAAA,GAAA,CAAA,CAAA,EACvB,KADuB,CACjB,CADiB,CAAA,CAAA,EACZ,CADY;;AACvB,KAGG,YAHH,CAAA,CAAA,CAAA,GAGqB,CAHrB,GAGyB,eAHzB,CAGyC,CAHzC,CAAA;;;;AAdQ,iBCMD,WDJE,CAAA,CAAA,CAAA,CAAA,OAAA,ECIsB,YDJtB,CCImC,CDJnC,CAAA,CAAA,ECIwC,KDJxC,CCI8C,CDJ9C,CAAA;AAED,iBCYD,GDZM,CAAA,CAAA,CAAA,CAAA,KAAA,ECYQ,KDZR,CCYc,CDZd,CAAA,CAAA,ECYmB,CDZnB;AAGL,iBCaD,GDbgB,CAAA,CAAA,CAAA,CAAA,KAAA,ECaF,KDbE,CCaI,CDbJ,CAAA,EAAA,QAAA,ECakB,YDblB,CCa+B,CDb/B,CAAA,CAAA,EAAA,IAAA;;;;AAPhC,UEEU,QFFO,CAAY,CAAA,CAAA,CAAA;EAIZ,CAAA,QAAK,EEDT,CFCS,EAAA,QAAA,EEDI,CFCJ,CAAA,EAAA,IAAA;AAGtB;AAAgC,iBEChB,MFDgB,CAAA,CAAA,CAAA,CAAA,KAAA,EECC,KFDD,CECO,CFDP,CAAA,EAAA,QAAA,EECqB,CFDrB,EAAA,QAAA,EECkC,CFDlC,CAAA,EAAA,IAAA;AAClB,iBEKE,SFLF,CAAA,CAAA,CAAA,CAAA,KAAA,EEKsB,KFLtB,CEK4B,CFL5B,CAAA,EAAA,QAAA,EEK0C,QFL1C,CEKmD,CFLnD,CAAA,CAAA,EAAA,IAAA;AAAI,iBEcF,WFdE,CAAA,CAAA,CAAA,CAAA,KAAA,EEcoB,KFdpB,CEc0B,CFd1B,CAAA,EAAA,QAAA,EEcwC,QFdxC,CEciD,CFdjD,CAAA,CAAA,EAAA,IAAA"}
|
package/package.json
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "statedrive",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"scripts": {
|
|
8
|
-
"build:no-minify": "microbundle src/index.ts --compress false -o ./dist -f modern --target web --raw true --tsconfig tsconfig.json",
|
|
9
|
-
"build:optimize": "microbundle src/index.ts -o ./dist -f modern --target web --raw true --tsconfig tsconfig.json",
|
|
10
|
-
"build:react": "microbundle --cwd react src/index.ts -o ../dist/react.js -f modern --target web --raw true --tsconfig tsconfig.json",
|
|
11
|
-
"prebuild": "rm -rf ./dist",
|
|
12
|
-
"build": "npm run build:optimize && npm run build:react",
|
|
13
|
-
"output": "npm run build:no-minify && npm run postbuild"
|
|
14
|
-
},
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Lightweight shared state management using atoms",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
15
7
|
"exports": {
|
|
16
8
|
".": {
|
|
17
|
-
"
|
|
18
|
-
"
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
19
11
|
},
|
|
20
12
|
"./react": {
|
|
21
|
-
"
|
|
22
|
-
"
|
|
13
|
+
"types": "./dist/react-index.d.ts",
|
|
14
|
+
"default": "./dist/react-index.js"
|
|
23
15
|
}
|
|
24
16
|
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsdown",
|
|
23
|
+
"typecheck": "tsc --noEmit"
|
|
24
|
+
},
|
|
25
25
|
"repository": {
|
|
26
26
|
"type": "git",
|
|
27
27
|
"url": "git+https://github.com/Hydrophobefireman/statedrive.git"
|
|
@@ -30,15 +30,23 @@
|
|
|
30
30
|
"bugs": {
|
|
31
31
|
"url": "https://github.com/Hydrophobefireman/statedrive/issues"
|
|
32
32
|
},
|
|
33
|
-
"types": "dist/index.d.ts",
|
|
34
|
-
"sideEffects": false,
|
|
35
33
|
"homepage": "https://github.com/Hydrophobefireman/statedrive#readme",
|
|
36
34
|
"devDependencies": {
|
|
37
|
-
"
|
|
38
|
-
"
|
|
35
|
+
"@hydrophobefireman/ui-lib": "^2.3.0",
|
|
36
|
+
"@types/react": "^19.0.0",
|
|
37
|
+
"tsdown": "^0.11.0",
|
|
38
|
+
"typescript": "^5.8.0"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"@hydrophobefireman/ui-lib": "
|
|
42
|
-
"react": "
|
|
41
|
+
"@hydrophobefireman/ui-lib": ">=0.1.0",
|
|
42
|
+
"react": ">=18.0.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependenciesMeta": {
|
|
45
|
+
"@hydrophobefireman/ui-lib": {
|
|
46
|
+
"optional": true
|
|
47
|
+
},
|
|
48
|
+
"react": {
|
|
49
|
+
"optional": true
|
|
50
|
+
}
|
|
43
51
|
}
|
|
44
52
|
}
|
package/dist/_hook_factory.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { useCallback as UseCallbackType, useEffect as UseEffectType, useMemo as UseMemoType, useState as UseStateType } from "@hydrophobefireman/ui-lib";
|
|
2
|
-
import { SelectorOptions, SetSharedState, State } from "./types";
|
|
3
|
-
export declare namespace Hooks {
|
|
4
|
-
type useEffect = typeof UseEffectType;
|
|
5
|
-
type useState = typeof UseStateType;
|
|
6
|
-
type useCallback = typeof UseCallbackType;
|
|
7
|
-
type useMemo = typeof UseMemoType;
|
|
8
|
-
}
|
|
9
|
-
export declare function createUseSharedState(useEffect: Hooks.useEffect, useState: Hooks.useState): <T>(state: State<T>) => [T, SetSharedState<T>];
|
|
10
|
-
export declare function createUseSelector(useEffect: Hooks.useEffect, useState: Hooks.useState, useMemo: Hooks.useMemo, useCallback: Hooks.useCallback): <R>(func: (options: SelectorOptions<R>) => R) => R;
|
|
11
|
-
export declare function createUseSharedStateValue(useSharedState: ReturnType<typeof createUseSharedState>): <T>(s: State<T>) => T;
|
|
12
|
-
export declare function createUseSetSharedState(useSharedState: ReturnType<typeof createUseSharedState>): <T>(s: State<T>) => SetSharedState<T>;
|
package/dist/hooks.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export declare const useSharedState: <T>(state: import("./types").State<T>) => [T, import("./types").SetSharedState<T>];
|
|
2
|
-
export declare const useSelector: <R>(func: (options: import("./types").SelectorOptions<R>) => R) => R;
|
|
3
|
-
export declare const useSharedStateValue: <T>(s: import("./types").State<T>) => T;
|
|
4
|
-
export declare const useSetSharedState: <T>(s: import("./types").State<T>) => import("./types").SetSharedState<T>;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import * as factory from "../../src/_hook_factory";
|
|
2
|
-
export declare function buildReactStatedrive(data: {
|
|
3
|
-
useEffect: factory.Hooks.useEffect;
|
|
4
|
-
useState: factory.Hooks.useState;
|
|
5
|
-
useMemo: factory.Hooks.useMemo;
|
|
6
|
-
useCallback: factory.Hooks.useCallback;
|
|
7
|
-
}): {
|
|
8
|
-
useSharedState: <T>(state: import(".").State<T>) => [T, import(".").SetSharedState<T>];
|
|
9
|
-
useSelector: <R>(func: (options: import(".").SelectorOptions<R>) => R) => R;
|
|
10
|
-
useSharedStateValue: <T_1>(s: import(".").State<T_1>) => T_1;
|
|
11
|
-
useSetSharedState: <T_2>(s: import(".").State<T_2>) => import(".").SetSharedState<T_2>;
|
|
12
|
-
};
|
package/dist/react.modern.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
const t=new WeakMap;function n(n,e,u){const r=t.get(n);r&&r.forEach(t=>t(e,u))}function e(n,e){let u=t.get(n);null==u&&(u=new Set,t.set(n,u)),u.add(e)}function u(n,e){const u=t.get(n);u&&u.delete(e)}const r=new WeakMap;function c(t){const n=function(t){return{name:t.name}}(t||{});return r.set(n,t.initialValue),n}function o(t){return r.get(t)}function f(t,e){const u=o(t),c=function(t,n){return"function"==typeof t?t(n):t}(e,u);r.set(t,c),n(t,u,c)}function a(t,n){return function(r){const[c,a]=n(()=>o(r));return t(()=>{const t=(t,n)=>a(n);return e(r,t),()=>u(r,t)},[r]),[c,t=>f(r,t)]}}function s(t,n,r,c){return function(f){const a=r(()=>new Set,[]),[,s]=n(null),i=c(()=>s({}),[]),l=c(t=>(a.has(t)||(a.add(t),e(t,i)),o(t)),[a]);return t(()=>()=>a.forEach(t=>u(t,i)),[a,i]),f({get:l})}}function i(t){return function(n){return t(n)[0]}}function l(t){return function(n){return t(n)[1]}}function S(t){const n=a(t.useEffect,t.useState);return{useSharedState:n,useSelector:s(t.useEffect,t.useState,t.useMemo,t.useCallback),useSharedStateValue:i(n),useSetSharedState:l(n)}}export{S as buildReactStatedrive,c as createState,o as get,n as notify,f as set,e as subscribe,u as unsubscribe};
|
|
2
|
-
//# sourceMappingURL=react.modern.js.map
|
package/dist/react.modern.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"react.modern.js","sources":["../src/subscribe.ts","../src/state.ts","../src/util.ts","../src/_hook_factory.ts","../react/src/hooks.ts"],"sourcesContent":["import { State } from \"./types\";\n\ninterface Listener<T> {\n (oldValue: T, newValue: T): void;\n}\ntype ListenerMap<T> = WeakMap<State<T>, Set<Listener<T>>>;\nconst listenerMap: ListenerMap<unknown> = new WeakMap();\n\nexport function notify<T>(state: State<T>, oldValue: T, newValue: T) {\n const listeners = (listenerMap as ListenerMap<T>).get(state);\n if (listeners) listeners.forEach((fn) => fn(oldValue, newValue));\n}\n\nexport function subscribe<T>(state: State<T>, callback: Listener<T>) {\n let listeners = (listenerMap as ListenerMap<T>).get(state);\n if (listeners == null) {\n listeners = new Set();\n listenerMap.set(state, listeners);\n }\n listeners.add(callback);\n}\n\nexport function unsubscribe<T>(state: State<T>, callback: Listener<T>) {\n const listeners = (listenerMap as ListenerMap<T>).get(state);\n if (listeners) listeners.delete(callback);\n}\n","import { State, StateOptions, StateUpdater } from \"./types\";\n\nimport { consumeUpdater } from \"./util\";\nimport { notify } from \"./subscribe\";\n\nconst valueMap = new WeakMap<State<unknown>, unknown>();\n\nexport function createState<T>(options: StateOptions<T>): State<T> {\n const state = _state(options || {});\n valueMap.set(state, options.initialValue);\n return state;\n}\n\nfunction _state<T>(options: StateOptions<T> | State<T>) {\n return { name: options.name };\n}\n\nexport function get<T>(state: State<T>): T {\n return valueMap.get(state) as T;\n}\n\nexport function set<T>(state: State<T>, newValue: StateUpdater<T>) {\n const oldValue = get(state);\n const next = consumeUpdater(newValue, oldValue);\n valueMap.set(state, next);\n notify(state, oldValue, next);\n}\n","import { StateUpdater, FunctionUpdater } from \"./types\";\n\nexport function consumeUpdater<T>(u: StateUpdater<T>, oldValue: T) {\n if (typeof u === \"function\") return (u as FunctionUpdater<T>)(oldValue);\n return u;\n}\n","import type {\n useCallback as UseCallbackType,\n useEffect as UseEffectType,\n useMemo as UseMemoType,\n useState as UseStateType,\n} from \"@hydrophobefireman/ui-lib\";\n\nimport {get, set} from \"./state\";\nimport {subscribe, unsubscribe} from \"./subscribe\";\nimport {SelectorOptions, SetSharedState, State, StateUpdater} from \"./types\";\n\nexport namespace Hooks {\n export type useEffect = typeof UseEffectType;\n export type useState = typeof UseStateType;\n export type useCallback = typeof UseCallbackType;\n export type useMemo = typeof UseMemoType;\n}\n\nexport function createUseSharedState(\n useEffect: Hooks.useEffect,\n useState: Hooks.useState\n) {\n return function useSharedState<T>(state: State<T>): [T, SetSharedState<T>] {\n const [value, setValue] = useState(() => get(state));\n\n useEffect(() => {\n const listener = (_: T, newVal: T) => setValue(newVal);\n subscribe(state, listener);\n return () => unsubscribe(state, listener);\n }, [state]);\n\n return [value, (nextValue: StateUpdater<T>) => set(state, nextValue)];\n };\n}\n\nexport function createUseSelector(\n useEffect: Hooks.useEffect,\n useState: Hooks.useState,\n useMemo: Hooks.useMemo,\n useCallback: Hooks.useCallback\n) {\n return function useSelector<R>(func: (options: SelectorOptions<R>) => R): R {\n const hasSubscribed = useMemo(() => new Set<State<unknown>>(), []);\n const [, setState] = useState(null);\n const fn = useCallback(() => setState({}), []);\n const _get = useCallback(\n <S>(s: State<S>): S => {\n if (!hasSubscribed.has(s)) {\n hasSubscribed.add(s);\n subscribe(s, fn);\n }\n return get(s);\n },\n [hasSubscribed]\n );\n useEffect(\n () => () => hasSubscribed.forEach((x) => unsubscribe(x, fn)),\n [hasSubscribed, fn]\n );\n return func({get: _get});\n };\n}\n\nexport function createUseSharedStateValue(\n useSharedState: ReturnType<typeof createUseSharedState>\n) {\n return function useSharedStateValue<T>(s: State<T>): T {\n return useSharedState(s)[0];\n };\n}\nexport function createUseSetSharedState(\n useSharedState: ReturnType<typeof createUseSharedState>\n) {\n return function useSetSharedState<T>(s: State<T>): SetSharedState<T> {\n return useSharedState(s)[1];\n };\n}\n","import * as factory from \"../../src/_hook_factory\";\n\nexport function buildReactStatedrive(data: {\n useEffect: factory.Hooks.useEffect;\n useState: factory.Hooks.useState;\n useMemo: factory.Hooks.useMemo;\n useCallback: factory.Hooks.useCallback;\n}) {\n const useSharedState = /*#__PURE__*/ factory.createUseSharedState(\n data.useEffect,\n data.useState\n );\n\n const useSelector = /*#__PURE__*/ factory.createUseSelector(\n data.useEffect,\n data.useState,\n data.useMemo,\n data.useCallback\n );\n\n const useSharedStateValue =\n /*#__PURE__*/ factory.createUseSharedStateValue(useSharedState);\n\n const useSetSharedState =\n /*#__PURE__*/ factory.createUseSetSharedState(useSharedState);\n return {useSharedState, useSelector, useSharedStateValue, useSetSharedState};\n}\n"],"names":["listenerMap","WeakMap","notify","state","oldValue","newValue","listeners","get","forEach","fn","subscribe","callback","Set","set","add","unsubscribe","delete","valueMap","createState","options","name","_state","initialValue","next","u","consumeUpdater","createUseSharedState","useEffect","useState","value","setValue","listener","_","newVal","nextValue","createUseSelector","useMemo","useCallback","func","hasSubscribed","setState","_get","s","has","x","createUseSharedStateValue","useSharedState","createUseSetSharedState","buildReactStatedrive","data","factory","useSelector","useSharedStateValue","useSetSharedState"],"mappings":"AAMA,MAAMA,EAAoC,IAAIC,iBAE9BC,EAAUC,EAAiBC,EAAaC,GACtD,MAAMC,EAAaN,EAA+BO,IAAIJ,GAClDG,GAAWA,EAAUE,QAASC,GAAOA,EAAGL,EAAUC,aAGxCK,EAAaP,EAAiBQ,GAC5C,IAAIL,EAAaN,EAA+BO,IAAIJ,GACnC,MAAbG,IACFA,EAAY,IAAIM,IAChBZ,EAAYa,IAAIV,EAAOG,IAEzBA,EAAUQ,IAAIH,YAGAI,EAAeZ,EAAiBQ,GAC9C,MAAML,EAAaN,EAA+BO,IAAIJ,GAClDG,GAAWA,EAAUU,OAAOL,GCnBlC,MAAMM,EAAW,IAAIhB,iBAELiB,EAAeC,GAC7B,MAAMhB,EAKR,SAAmBgB,GACjB,MAAO,CAAEC,KAAMD,EAAQC,MANTC,CAAOF,GAAW,IAEhC,OADAF,EAASJ,IAAIV,EAAOgB,EAAQG,cACrBnB,WAOOI,EAAOJ,GACrB,OAAOc,EAASV,IAAIJ,YAGNU,EAAOV,EAAiBE,GACtC,MAAMD,EAAWG,EAAIJ,GACfoB,WCrB0BC,EAAoBpB,GACpD,MAAiB,mBAANoB,EAA0BA,EAAyBpB,GACvDoB,EDmBMC,CAAepB,EAAUD,GACtCa,EAASJ,IAAIV,EAAOoB,GACpBrB,EAAOC,EAAOC,EAAUmB,YEPVG,EACdC,EACAC,GAEA,gBAAkCzB,GAChC,MAAO0B,EAAOC,GAAYF,EAAS,IAAMrB,EAAIJ,IAQ7C,OANAwB,EAAU,KACR,MAAMI,EAAW,CAACC,EAAMC,IAAcH,EAASG,GAE/C,OADAvB,EAAUP,EAAO4B,GACV,IAAMhB,EAAYZ,EAAO4B,IAC/B,CAAC5B,IAEG,CAAC0B,EAAQK,GAA+BrB,EAAIV,EAAO+B,cAI9CC,EACdR,EACAC,EACAQ,EACAC,GAEA,gBAA+BC,GAC7B,MAAMC,EAAgBH,EAAQ,IAAM,IAAIxB,IAAuB,MACtD4B,GAAYZ,EAAS,MACxBnB,EAAK4B,EAAY,IAAMG,EAAS,IAAK,IACrCC,EAAOJ,EACPK,IACGH,EAAcI,IAAID,KACrBH,EAAczB,IAAI4B,GAClBhC,EAAUgC,EAAGjC,IAERF,EAAImC,IAEb,CAACH,IAMH,OAJAZ,EACE,IAAM,IAAMY,EAAc/B,QAASoC,GAAM7B,EAAY6B,EAAGnC,IACxD,CAAC8B,EAAe9B,IAEX6B,EAAK,CAAC/B,IAAKkC,cAINI,EACdC,GAEA,gBAAuCJ,GACrC,OAAOI,EAAeJ,GAAG,aAGbK,EACdD,GAEA,gBAAqCJ,GACnC,OAAOI,EAAeJ,GAAG,aCxEbM,EAAqBC,GAMnC,MAAMH,EAA+BI,EACnCD,EAAKtB,UACLsB,EAAKrB,UAeP,MAAO,CAACkB,eAAAA,EAAgBK,YAZUD,EAChCD,EAAKtB,UACLsB,EAAKrB,SACLqB,EAAKb,QACLa,EAAKZ,aAQ8Be,oBAJrBF,EAAkCJ,GAIQO,kBAD1CH,EAAgCJ"}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { useCallback as UseCallbackType, useEffect as UseEffectType, useMemo as UseMemoType, useState as UseStateType } from "@hydrophobefireman/ui-lib";
|
|
2
|
-
import { SelectorOptions, SetSharedState, State } from "./types";
|
|
3
|
-
export declare namespace Hooks {
|
|
4
|
-
type useEffect = typeof UseEffectType;
|
|
5
|
-
type useState = typeof UseStateType;
|
|
6
|
-
type useCallback = typeof UseCallbackType;
|
|
7
|
-
type useMemo = typeof UseMemoType;
|
|
8
|
-
}
|
|
9
|
-
export declare function createUseSharedState(useEffect: Hooks.useEffect, useState: Hooks.useState): <T>(state: State<T>) => [T, SetSharedState<T>];
|
|
10
|
-
export declare function createUseSelector(useEffect: Hooks.useEffect, useState: Hooks.useState, useMemo: Hooks.useMemo, useCallback: Hooks.useCallback): <R>(func: (options: SelectorOptions<R>) => R) => R;
|
|
11
|
-
export declare function createUseSharedStateValue(useSharedState: ReturnType<typeof createUseSharedState>): <T>(s: State<T>) => T;
|
|
12
|
-
export declare function createUseSetSharedState(useSharedState: ReturnType<typeof createUseSharedState>): <T>(s: State<T>) => SetSharedState<T>;
|
package/dist/src/state.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { State, StateOptions, StateUpdater } from "./types";
|
|
2
|
-
export declare function createState<T>(options: StateOptions<T>): State<T>;
|
|
3
|
-
export declare function get<T>(state: State<T>): T;
|
|
4
|
-
export declare function set<T>(state: State<T>, newValue: StateUpdater<T>): void;
|
package/dist/src/subscribe.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { State } from "./types";
|
|
2
|
-
interface Listener<T> {
|
|
3
|
-
(oldValue: T, newValue: T): void;
|
|
4
|
-
}
|
|
5
|
-
export declare function notify<T>(state: State<T>, oldValue: T, newValue: T): void;
|
|
6
|
-
export declare function subscribe<T>(state: State<T>, callback: Listener<T>): void;
|
|
7
|
-
export declare function unsubscribe<T>(state: State<T>, callback: Listener<T>): void;
|
|
8
|
-
export {};
|
package/dist/src/types.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export interface StateOptions<T> {
|
|
2
|
-
name?: string;
|
|
3
|
-
initialValue?: T;
|
|
4
|
-
}
|
|
5
|
-
export interface State<T> {
|
|
6
|
-
name: string | void;
|
|
7
|
-
}
|
|
8
|
-
export interface FunctionUpdater<T> {
|
|
9
|
-
(previous?: T): T;
|
|
10
|
-
}
|
|
11
|
-
export interface SetSharedState<T> {
|
|
12
|
-
(val: StateUpdater<T>): void;
|
|
13
|
-
}
|
|
14
|
-
export interface SelectorOptions<T> {
|
|
15
|
-
get(s: State<T>): T;
|
|
16
|
-
}
|
|
17
|
-
export declare type StateUpdater<T> = T | FunctionUpdater<T>;
|
|
18
|
-
export {};
|
package/dist/src/util.d.ts
DELETED
package/dist/state.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { State, StateOptions, StateUpdater } from "./types";
|
|
2
|
-
export declare function createState<T>(options: StateOptions<T>): State<T>;
|
|
3
|
-
export declare function get<T>(state: State<T>): T;
|
|
4
|
-
export declare function set<T>(state: State<T>, newValue: StateUpdater<T>): void;
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{useState as n,useEffect as t,useCallback as e,useMemo as o}from"@hydrophobefireman/ui-lib";const u=new WeakMap;function r(n,t,e){const o=u.get(n);o&&o.forEach(n=>n(t,e))}function c(n,t){let e=u.get(n);null==e&&(e=new Set,u.set(n,e)),e.add(t)}function i(n,t){const e=u.get(n);e&&e.delete(t)}const f=new WeakMap;function a(n){const t=function(n){return{name:n.name}}(n||{});return f.set(t,n.initialValue),t}function s(n){return f.get(n)}function l(n,t){const e=s(n),o=function(n,t){return"function"==typeof n?n(t):n}(t,e);f.set(n,o),r(n,e,o)}function d(n,t){return function(e){const[o,u]=t(()=>s(e));return n(()=>{const n=(n,t)=>u(t);return c(e,n),()=>i(e,n)},[e]),[o,n=>l(e,n)]}}function p(n,t,e,o){return function(u){const r=e(()=>new Set,[]),[,f]=t(null),a=o(()=>f({}),[]),l=o(n=>(r.has(n)||(r.add(n),c(n,a)),s(n)),[r]);return n(()=>()=>r.forEach(n=>i(n,a)),[r,a]),u({get:l})}}function g(n){return function(t){return n(t)[0]}}function h(n){return function(t){return n(t)[1]}}const m=d(t,n),w=p(t,n,o,e),b=g(m),k=h(m);export{a as createState,s as get,r as notify,l as set,c as subscribe,i as unsubscribe,w as useSelector,k as useSetSharedState,m as useSharedState,b as useSharedStateValue};
|
|
2
|
-
//# sourceMappingURL=statedrive.modern.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"statedrive.modern.js","sources":["../src/subscribe.ts","../src/state.ts","../src/util.ts","../src/_hook_factory.ts","../src/hooks.ts"],"sourcesContent":["import { State } from \"./types\";\n\ninterface Listener<T> {\n (oldValue: T, newValue: T): void;\n}\ntype ListenerMap<T> = WeakMap<State<T>, Set<Listener<T>>>;\nconst listenerMap: ListenerMap<unknown> = new WeakMap();\n\nexport function notify<T>(state: State<T>, oldValue: T, newValue: T) {\n const listeners = (listenerMap as ListenerMap<T>).get(state);\n if (listeners) listeners.forEach((fn) => fn(oldValue, newValue));\n}\n\nexport function subscribe<T>(state: State<T>, callback: Listener<T>) {\n let listeners = (listenerMap as ListenerMap<T>).get(state);\n if (listeners == null) {\n listeners = new Set();\n listenerMap.set(state, listeners);\n }\n listeners.add(callback);\n}\n\nexport function unsubscribe<T>(state: State<T>, callback: Listener<T>) {\n const listeners = (listenerMap as ListenerMap<T>).get(state);\n if (listeners) listeners.delete(callback);\n}\n","import { State, StateOptions, StateUpdater } from \"./types\";\n\nimport { consumeUpdater } from \"./util\";\nimport { notify } from \"./subscribe\";\n\nconst valueMap = new WeakMap<State<unknown>, unknown>();\n\nexport function createState<T>(options: StateOptions<T>): State<T> {\n const state = _state(options || {});\n valueMap.set(state, options.initialValue);\n return state;\n}\n\nfunction _state<T>(options: StateOptions<T> | State<T>) {\n return { name: options.name };\n}\n\nexport function get<T>(state: State<T>): T {\n return valueMap.get(state) as T;\n}\n\nexport function set<T>(state: State<T>, newValue: StateUpdater<T>) {\n const oldValue = get(state);\n const next = consumeUpdater(newValue, oldValue);\n valueMap.set(state, next);\n notify(state, oldValue, next);\n}\n","import { StateUpdater, FunctionUpdater } from \"./types\";\n\nexport function consumeUpdater<T>(u: StateUpdater<T>, oldValue: T) {\n if (typeof u === \"function\") return (u as FunctionUpdater<T>)(oldValue);\n return u;\n}\n","import type {\n useCallback as UseCallbackType,\n useEffect as UseEffectType,\n useMemo as UseMemoType,\n useState as UseStateType,\n} from \"@hydrophobefireman/ui-lib\";\n\nimport {get, set} from \"./state\";\nimport {subscribe, unsubscribe} from \"./subscribe\";\nimport {SelectorOptions, SetSharedState, State, StateUpdater} from \"./types\";\n\nexport namespace Hooks {\n export type useEffect = typeof UseEffectType;\n export type useState = typeof UseStateType;\n export type useCallback = typeof UseCallbackType;\n export type useMemo = typeof UseMemoType;\n}\n\nexport function createUseSharedState(\n useEffect: Hooks.useEffect,\n useState: Hooks.useState\n) {\n return function useSharedState<T>(state: State<T>): [T, SetSharedState<T>] {\n const [value, setValue] = useState(() => get(state));\n\n useEffect(() => {\n const listener = (_: T, newVal: T) => setValue(newVal);\n subscribe(state, listener);\n return () => unsubscribe(state, listener);\n }, [state]);\n\n return [value, (nextValue: StateUpdater<T>) => set(state, nextValue)];\n };\n}\n\nexport function createUseSelector(\n useEffect: Hooks.useEffect,\n useState: Hooks.useState,\n useMemo: Hooks.useMemo,\n useCallback: Hooks.useCallback\n) {\n return function useSelector<R>(func: (options: SelectorOptions<R>) => R): R {\n const hasSubscribed = useMemo(() => new Set<State<unknown>>(), []);\n const [, setState] = useState(null);\n const fn = useCallback(() => setState({}), []);\n const _get = useCallback(\n <S>(s: State<S>): S => {\n if (!hasSubscribed.has(s)) {\n hasSubscribed.add(s);\n subscribe(s, fn);\n }\n return get(s);\n },\n [hasSubscribed]\n );\n useEffect(\n () => () => hasSubscribed.forEach((x) => unsubscribe(x, fn)),\n [hasSubscribed, fn]\n );\n return func({get: _get});\n };\n}\n\nexport function createUseSharedStateValue(\n useSharedState: ReturnType<typeof createUseSharedState>\n) {\n return function useSharedStateValue<T>(s: State<T>): T {\n return useSharedState(s)[0];\n };\n}\nexport function createUseSetSharedState(\n useSharedState: ReturnType<typeof createUseSharedState>\n) {\n return function useSetSharedState<T>(s: State<T>): SetSharedState<T> {\n return useSharedState(s)[1];\n };\n}\n","import * as factory from \"./_hook_factory\";\n\nimport {\n useCallback,\n useEffect,\n useMemo,\n useState,\n} from \"@hydrophobefireman/ui-lib\";\n\nexport const useSharedState = /*#__PURE__*/ factory.createUseSharedState(\n useEffect,\n useState\n);\n\nexport const useSelector = /*#__PURE__*/ factory.createUseSelector(\n useEffect,\n useState,\n useMemo,\n useCallback\n);\n\nexport const useSharedStateValue =\n /*#__PURE__*/ factory.createUseSharedStateValue(useSharedState);\n\nexport const useSetSharedState =\n /*#__PURE__*/ factory.createUseSetSharedState(useSharedState);\n"],"names":["listenerMap","WeakMap","notify","state","oldValue","newValue","listeners","get","forEach","fn","subscribe","callback","Set","set","add","unsubscribe","delete","valueMap","createState","options","name","_state","initialValue","next","u","consumeUpdater","createUseSharedState","useEffect","useState","value","setValue","listener","_","newVal","nextValue","createUseSelector","useMemo","useCallback","func","hasSubscribed","setState","_get","s","has","x","createUseSharedStateValue","useSharedState","createUseSetSharedState","factory","useSelector","useSharedStateValue","useSetSharedState"],"mappings":"kGAMA,MAAMA,EAAoC,IAAIC,iBAE9BC,EAAUC,EAAiBC,EAAaC,GACtD,MAAMC,EAAaN,EAA+BO,IAAIJ,GAClDG,GAAWA,EAAUE,QAASC,GAAOA,EAAGL,EAAUC,aAGxCK,EAAaP,EAAiBQ,GAC5C,IAAIL,EAAaN,EAA+BO,IAAIJ,GACnC,MAAbG,IACFA,EAAY,IAAIM,IAChBZ,EAAYa,IAAIV,EAAOG,IAEzBA,EAAUQ,IAAIH,YAGAI,EAAeZ,EAAiBQ,GAC9C,MAAML,EAAaN,EAA+BO,IAAIJ,GAClDG,GAAWA,EAAUU,OAAOL,GCnBlC,MAAMM,EAAW,IAAIhB,iBAELiB,EAAeC,GAC7B,MAAMhB,EAKR,SAAmBgB,GACjB,MAAO,CAAEC,KAAMD,EAAQC,MANTC,CAAOF,GAAW,IAEhC,OADAF,EAASJ,IAAIV,EAAOgB,EAAQG,cACrBnB,WAOOI,EAAOJ,GACrB,OAAOc,EAASV,IAAIJ,YAGNU,EAAOV,EAAiBE,GACtC,MAAMD,EAAWG,EAAIJ,GACfoB,WCrB0BC,EAAoBpB,GACpD,MAAiB,mBAANoB,EAA0BA,EAAyBpB,GACvDoB,EDmBMC,CAAepB,EAAUD,GACtCa,EAASJ,IAAIV,EAAOoB,GACpBrB,EAAOC,EAAOC,EAAUmB,YEPVG,EACdC,EACAC,GAEA,gBAAkCzB,GAChC,MAAO0B,EAAOC,GAAYF,EAAS,IAAMrB,EAAIJ,IAQ7C,OANAwB,EAAU,KACR,MAAMI,EAAW,CAACC,EAAMC,IAAcH,EAASG,GAE/C,OADAvB,EAAUP,EAAO4B,GACV,IAAMhB,EAAYZ,EAAO4B,IAC/B,CAAC5B,IAEG,CAAC0B,EAAQK,GAA+BrB,EAAIV,EAAO+B,cAI9CC,EACdR,EACAC,EACAQ,EACAC,GAEA,gBAA+BC,GAC7B,MAAMC,EAAgBH,EAAQ,IAAM,IAAIxB,IAAuB,MACtD4B,GAAYZ,EAAS,MACxBnB,EAAK4B,EAAY,IAAMG,EAAS,IAAK,IACrCC,EAAOJ,EACPK,IACGH,EAAcI,IAAID,KACrBH,EAAczB,IAAI4B,GAClBhC,EAAUgC,EAAGjC,IAERF,EAAImC,IAEb,CAACH,IAMH,OAJAZ,EACE,IAAM,IAAMY,EAAc/B,QAASoC,GAAM7B,EAAY6B,EAAGnC,IACxD,CAAC8B,EAAe9B,IAEX6B,EAAK,CAAC/B,IAAKkC,cAINI,EACdC,GAEA,gBAAuCJ,GACrC,OAAOI,EAAeJ,GAAG,aAGbK,EACdD,GAEA,gBAAqCJ,GACnC,OAAOI,EAAeJ,GAAG,ICjEhBI,MAAAA,EAA+BE,EAC1CrB,EACAC,GAGWqB,EAA4BD,EACvCrB,EACAC,EACAQ,EACAC,GAGWa,EACGF,EAAkCF,GAErCK,EACGH,EAAgCF"}
|
package/dist/subscribe.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { State } from "./types";
|
|
2
|
-
interface Listener<T> {
|
|
3
|
-
(oldValue: T, newValue: T): void;
|
|
4
|
-
}
|
|
5
|
-
export declare function notify<T>(state: State<T>, oldValue: T, newValue: T): void;
|
|
6
|
-
export declare function subscribe<T>(state: State<T>, callback: Listener<T>): void;
|
|
7
|
-
export declare function unsubscribe<T>(state: State<T>, callback: Listener<T>): void;
|
|
8
|
-
export {};
|
package/dist/types.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export interface StateOptions<T> {
|
|
2
|
-
name?: string;
|
|
3
|
-
initialValue?: T;
|
|
4
|
-
}
|
|
5
|
-
export interface State<T> {
|
|
6
|
-
name: string | void;
|
|
7
|
-
}
|
|
8
|
-
export interface FunctionUpdater<T> {
|
|
9
|
-
(previous?: T): T;
|
|
10
|
-
}
|
|
11
|
-
export interface SetSharedState<T> {
|
|
12
|
-
(val: StateUpdater<T>): void;
|
|
13
|
-
}
|
|
14
|
-
export interface SelectorOptions<T> {
|
|
15
|
-
get(s: State<T>): T;
|
|
16
|
-
}
|
|
17
|
-
export declare type StateUpdater<T> = T | FunctionUpdater<T>;
|
|
18
|
-
export {};
|
package/dist/util.d.ts
DELETED
package/react/package.json
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "statedrive-react",
|
|
3
|
-
"version": "1.0.0",
|
|
4
|
-
"description": "",
|
|
5
|
-
"types": "../dist/index.d.ts",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
-
},
|
|
9
|
-
"devDependencies": {
|
|
10
|
-
"@types/react": "latest"
|
|
11
|
-
},
|
|
12
|
-
"peerDependencies": {
|
|
13
|
-
"react": "latest"
|
|
14
|
-
},
|
|
15
|
-
"author": "",
|
|
16
|
-
"license": "ISC"
|
|
17
|
-
}
|
package/react/src/hooks.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import * as factory from "../../src/_hook_factory";
|
|
2
|
-
|
|
3
|
-
export function buildReactStatedrive(data: {
|
|
4
|
-
useEffect: factory.Hooks.useEffect;
|
|
5
|
-
useState: factory.Hooks.useState;
|
|
6
|
-
useMemo: factory.Hooks.useMemo;
|
|
7
|
-
useCallback: factory.Hooks.useCallback;
|
|
8
|
-
}) {
|
|
9
|
-
const useSharedState = /*#__PURE__*/ factory.createUseSharedState(
|
|
10
|
-
data.useEffect,
|
|
11
|
-
data.useState
|
|
12
|
-
);
|
|
13
|
-
|
|
14
|
-
const useSelector = /*#__PURE__*/ factory.createUseSelector(
|
|
15
|
-
data.useEffect,
|
|
16
|
-
data.useState,
|
|
17
|
-
data.useMemo,
|
|
18
|
-
data.useCallback
|
|
19
|
-
);
|
|
20
|
-
|
|
21
|
-
const useSharedStateValue =
|
|
22
|
-
/*#__PURE__*/ factory.createUseSharedStateValue(useSharedState);
|
|
23
|
-
|
|
24
|
-
const useSetSharedState =
|
|
25
|
-
/*#__PURE__*/ factory.createUseSetSharedState(useSharedState);
|
|
26
|
-
return {useSharedState, useSelector, useSharedStateValue, useSetSharedState};
|
|
27
|
-
}
|
package/react/src/index.ts
DELETED
package/react/yarn.lock
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
|
2
|
-
# yarn lockfile v1
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
"@types/prop-types@*":
|
|
6
|
-
version "15.7.3"
|
|
7
|
-
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.3.tgz#2ab0d5da2e5815f94b0b9d4b95d1e5f243ab2ca7"
|
|
8
|
-
integrity sha512-KfRL3PuHmqQLOG+2tGpRO26Ctg+Cq1E01D2DMriKEATHgWLfeNDmq9e29Q9WIky0dQ3NPkd1mzYH8Lm936Z9qw==
|
|
9
|
-
|
|
10
|
-
"@types/react@^17.0.5":
|
|
11
|
-
version "17.0.5"
|
|
12
|
-
resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.5.tgz#3d887570c4489011f75a3fc8f965bf87d09a1bea"
|
|
13
|
-
integrity sha512-bj4biDB9ZJmGAYTWSKJly6bMr4BLUiBrx9ujiJEoP9XIDY9CTaPGxE5QWN/1WjpPLzYF7/jRNnV2nNxNe970sw==
|
|
14
|
-
dependencies:
|
|
15
|
-
"@types/prop-types" "*"
|
|
16
|
-
"@types/scheduler" "*"
|
|
17
|
-
csstype "^3.0.2"
|
|
18
|
-
|
|
19
|
-
"@types/scheduler@*":
|
|
20
|
-
version "0.16.1"
|
|
21
|
-
resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.1.tgz#18845205e86ff0038517aab7a18a62a6b9f71275"
|
|
22
|
-
integrity sha512-EaCxbanVeyxDRTQBkdLb3Bvl/HK7PBK6UJjsSixB0iHKoWxE5uu2Q/DgtpOhPIojN0Zl1whvOd7PoHs2P0s5eA==
|
|
23
|
-
|
|
24
|
-
csstype@^3.0.2:
|
|
25
|
-
version "3.0.8"
|
|
26
|
-
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.0.8.tgz#d2266a792729fb227cd216fb572f43728e1ad340"
|
|
27
|
-
integrity sha512-jXKhWqXPmlUeoQnF/EhTtTl4C9SnrxSH/jZUih3jmO6lBKr99rP3/+FmrMj4EFpOXzMtXHAZkd3x0E6h6Fgflw==
|
|
28
|
-
|
|
29
|
-
"js-tokens@^3.0.0 || ^4.0.0":
|
|
30
|
-
version "4.0.0"
|
|
31
|
-
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
|
|
32
|
-
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==
|
|
33
|
-
|
|
34
|
-
loose-envify@^1.1.0:
|
|
35
|
-
version "1.4.0"
|
|
36
|
-
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
|
|
37
|
-
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
|
|
38
|
-
dependencies:
|
|
39
|
-
js-tokens "^3.0.0 || ^4.0.0"
|
|
40
|
-
|
|
41
|
-
object-assign@^4.1.1:
|
|
42
|
-
version "4.1.1"
|
|
43
|
-
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
|
|
44
|
-
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
|
|
45
|
-
|
|
46
|
-
react@^17.0.2:
|
|
47
|
-
version "17.0.2"
|
|
48
|
-
resolved "https://registry.yarnpkg.com/react/-/react-17.0.2.tgz#d0b5cc516d29eb3eee383f75b62864cfb6800037"
|
|
49
|
-
integrity sha512-gnhPt75i/dq/z3/6q/0asP78D0u592D5L1pd7M8P+dck6Fu/jJeL6iVVK23fptSUZj8Vjf++7wXA8UNclGQcbA==
|
|
50
|
-
dependencies:
|
|
51
|
-
loose-envify "^1.1.0"
|
|
52
|
-
object-assign "^4.1.1"
|
package/src/_hook_factory.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
useCallback as UseCallbackType,
|
|
3
|
-
useEffect as UseEffectType,
|
|
4
|
-
useMemo as UseMemoType,
|
|
5
|
-
useState as UseStateType,
|
|
6
|
-
} from "@hydrophobefireman/ui-lib";
|
|
7
|
-
|
|
8
|
-
import {get, set} from "./state";
|
|
9
|
-
import {subscribe, unsubscribe} from "./subscribe";
|
|
10
|
-
import {SelectorOptions, SetSharedState, State, StateUpdater} from "./types";
|
|
11
|
-
|
|
12
|
-
export namespace Hooks {
|
|
13
|
-
export type useEffect = typeof UseEffectType;
|
|
14
|
-
export type useState = typeof UseStateType;
|
|
15
|
-
export type useCallback = typeof UseCallbackType;
|
|
16
|
-
export type useMemo = typeof UseMemoType;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function createUseSharedState(
|
|
20
|
-
useEffect: Hooks.useEffect,
|
|
21
|
-
useState: Hooks.useState
|
|
22
|
-
) {
|
|
23
|
-
return function useSharedState<T>(state: State<T>): [T, SetSharedState<T>] {
|
|
24
|
-
const [value, setValue] = useState(() => get(state));
|
|
25
|
-
|
|
26
|
-
useEffect(() => {
|
|
27
|
-
const listener = (_: T, newVal: T) => setValue(newVal);
|
|
28
|
-
subscribe(state, listener);
|
|
29
|
-
return () => unsubscribe(state, listener);
|
|
30
|
-
}, [state]);
|
|
31
|
-
|
|
32
|
-
return [value, (nextValue: StateUpdater<T>) => set(state, nextValue)];
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function createUseSelector(
|
|
37
|
-
useEffect: Hooks.useEffect,
|
|
38
|
-
useState: Hooks.useState,
|
|
39
|
-
useMemo: Hooks.useMemo,
|
|
40
|
-
useCallback: Hooks.useCallback
|
|
41
|
-
) {
|
|
42
|
-
return function useSelector<R>(func: (options: SelectorOptions<R>) => R): R {
|
|
43
|
-
const hasSubscribed = useMemo(() => new Set<State<unknown>>(), []);
|
|
44
|
-
const [, setState] = useState(null);
|
|
45
|
-
const fn = useCallback(() => setState({}), []);
|
|
46
|
-
const _get = useCallback(
|
|
47
|
-
<S>(s: State<S>): S => {
|
|
48
|
-
if (!hasSubscribed.has(s)) {
|
|
49
|
-
hasSubscribed.add(s);
|
|
50
|
-
subscribe(s, fn);
|
|
51
|
-
}
|
|
52
|
-
return get(s);
|
|
53
|
-
},
|
|
54
|
-
[hasSubscribed]
|
|
55
|
-
);
|
|
56
|
-
useEffect(
|
|
57
|
-
() => () => hasSubscribed.forEach((x) => unsubscribe(x, fn)),
|
|
58
|
-
[hasSubscribed, fn]
|
|
59
|
-
);
|
|
60
|
-
return func({get: _get});
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function createUseSharedStateValue(
|
|
65
|
-
useSharedState: ReturnType<typeof createUseSharedState>
|
|
66
|
-
) {
|
|
67
|
-
return function useSharedStateValue<T>(s: State<T>): T {
|
|
68
|
-
return useSharedState(s)[0];
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
export function createUseSetSharedState(
|
|
72
|
-
useSharedState: ReturnType<typeof createUseSharedState>
|
|
73
|
-
) {
|
|
74
|
-
return function useSetSharedState<T>(s: State<T>): SetSharedState<T> {
|
|
75
|
-
return useSharedState(s)[1];
|
|
76
|
-
};
|
|
77
|
-
}
|
package/src/hooks.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import * as factory from "./_hook_factory";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
useCallback,
|
|
5
|
-
useEffect,
|
|
6
|
-
useMemo,
|
|
7
|
-
useState,
|
|
8
|
-
} from "@hydrophobefireman/ui-lib";
|
|
9
|
-
|
|
10
|
-
export const useSharedState = /*#__PURE__*/ factory.createUseSharedState(
|
|
11
|
-
useEffect,
|
|
12
|
-
useState
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
export const useSelector = /*#__PURE__*/ factory.createUseSelector(
|
|
16
|
-
useEffect,
|
|
17
|
-
useState,
|
|
18
|
-
useMemo,
|
|
19
|
-
useCallback
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
export const useSharedStateValue =
|
|
23
|
-
/*#__PURE__*/ factory.createUseSharedStateValue(useSharedState);
|
|
24
|
-
|
|
25
|
-
export const useSetSharedState =
|
|
26
|
-
/*#__PURE__*/ factory.createUseSetSharedState(useSharedState);
|
package/src/index.ts
DELETED
package/src/state.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { State, StateOptions, StateUpdater } from "./types";
|
|
2
|
-
|
|
3
|
-
import { consumeUpdater } from "./util";
|
|
4
|
-
import { notify } from "./subscribe";
|
|
5
|
-
|
|
6
|
-
const valueMap = new WeakMap<State<unknown>, unknown>();
|
|
7
|
-
|
|
8
|
-
export function createState<T>(options: StateOptions<T>): State<T> {
|
|
9
|
-
const state = _state(options || {});
|
|
10
|
-
valueMap.set(state, options.initialValue);
|
|
11
|
-
return state;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function _state<T>(options: StateOptions<T> | State<T>) {
|
|
15
|
-
return { name: options.name };
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function get<T>(state: State<T>): T {
|
|
19
|
-
return valueMap.get(state) as T;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function set<T>(state: State<T>, newValue: StateUpdater<T>) {
|
|
23
|
-
const oldValue = get(state);
|
|
24
|
-
const next = consumeUpdater(newValue, oldValue);
|
|
25
|
-
valueMap.set(state, next);
|
|
26
|
-
notify(state, oldValue, next);
|
|
27
|
-
}
|
package/src/subscribe.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { State } from "./types";
|
|
2
|
-
|
|
3
|
-
interface Listener<T> {
|
|
4
|
-
(oldValue: T, newValue: T): void;
|
|
5
|
-
}
|
|
6
|
-
type ListenerMap<T> = WeakMap<State<T>, Set<Listener<T>>>;
|
|
7
|
-
const listenerMap: ListenerMap<unknown> = new WeakMap();
|
|
8
|
-
|
|
9
|
-
export function notify<T>(state: State<T>, oldValue: T, newValue: T) {
|
|
10
|
-
const listeners = (listenerMap as ListenerMap<T>).get(state);
|
|
11
|
-
if (listeners) listeners.forEach((fn) => fn(oldValue, newValue));
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function subscribe<T>(state: State<T>, callback: Listener<T>) {
|
|
15
|
-
let listeners = (listenerMap as ListenerMap<T>).get(state);
|
|
16
|
-
if (listeners == null) {
|
|
17
|
-
listeners = new Set();
|
|
18
|
-
listenerMap.set(state, listeners);
|
|
19
|
-
}
|
|
20
|
-
listeners.add(callback);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function unsubscribe<T>(state: State<T>, callback: Listener<T>) {
|
|
24
|
-
const listeners = (listenerMap as ListenerMap<T>).get(state);
|
|
25
|
-
if (listeners) listeners.delete(callback);
|
|
26
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export interface StateOptions<T> {
|
|
2
|
-
name?: string;
|
|
3
|
-
initialValue?: T;
|
|
4
|
-
}
|
|
5
|
-
export interface State<T> {
|
|
6
|
-
name: string | void;
|
|
7
|
-
}
|
|
8
|
-
export interface FunctionUpdater<T> {
|
|
9
|
-
(previous?: T): T;
|
|
10
|
-
}
|
|
11
|
-
export interface SetSharedState<T> {
|
|
12
|
-
(val: StateUpdater<T>): void;
|
|
13
|
-
}
|
|
14
|
-
export interface SelectorOptions<T> {
|
|
15
|
-
get(s: State<T>): T;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export type StateUpdater<T> = T | FunctionUpdater<T>;
|
|
19
|
-
|
|
20
|
-
export {};
|
package/src/util.ts
DELETED