cross-state 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -0
- package/dist/cjs/hash.cjs +632 -0
- package/dist/cjs/hash.cjs.map +1 -0
- package/dist/cjs/immer.cjs +10 -0
- package/dist/cjs/immer.cjs.map +1 -0
- package/dist/cjs/index.cjs +286 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/react.cjs +220 -0
- package/dist/cjs/react.cjs.map +1 -0
- package/dist/es/hash.mjs +633 -0
- package/dist/es/hash.mjs.map +1 -0
- package/dist/es/immer.mjs +10 -0
- package/dist/es/immer.mjs.map +1 -0
- package/dist/es/index.mjs +287 -0
- package/dist/es/index.mjs.map +1 -0
- package/dist/es/react.mjs +220 -0
- package/dist/es/react.mjs.map +1 -0
- package/dist/types/core/commonTypes.d.ts +51 -0
- package/dist/types/core/derivedStore.d.ts +39 -0
- package/dist/types/core/fetchStore.d.ts +76 -0
- package/dist/types/core/index.d.ts +10 -0
- package/dist/types/core/once.d.ts +13 -0
- package/dist/types/core/resourceGroup.d.ts +9 -0
- package/dist/types/core/store.d.ts +58 -0
- package/dist/types/core/storeScope.d.ts +4 -0
- package/dist/types/immer/immerActions.d.ts +5 -0
- package/dist/types/immer/index.d.ts +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/lib/bind.d.ts +1 -0
- package/dist/types/lib/cache.d.ts +17 -0
- package/dist/types/lib/calcDuration.d.ts +2 -0
- package/dist/types/lib/calculationHelper.d.ts +22 -0
- package/dist/types/lib/diff.d.ts +7 -0
- package/dist/types/lib/equals.d.ts +3 -0
- package/dist/types/lib/forwardError.d.ts +1 -0
- package/dist/types/lib/hash.d.ts +1 -0
- package/dist/types/lib/makeSelector.d.ts +1 -0
- package/dist/types/lib/maybePromise.d.ts +1 -0
- package/dist/types/lib/propAccess.d.ts +13 -0
- package/dist/types/lib/queue.d.ts +9 -0
- package/dist/types/lib/storeActions.d.ts +26 -0
- package/dist/types/lib/throttle.d.ts +1 -0
- package/dist/types/lib/trackingProxy.d.ts +2 -0
- package/dist/types/lib/unwrapPromise.d.ts +1 -0
- package/dist/types/react/index.d.ts +5 -0
- package/dist/types/react/read.d.ts +4 -0
- package/dist/types/react/storeScope.d.ts +12 -0
- package/dist/types/react/useProp.d.ts +4 -0
- package/dist/types/react/useStore.d.ts +4 -0
- package/package.json +112 -0
- package/react.d.ts +1 -0
- package/react.js +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.cjs","sources":["../../src/react/storeScope.tsx","../../node_modules/.pnpm/use-sync-external-store@1.2.0_react@18.2.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-with-selector.production.min.js","../../node_modules/.pnpm/use-sync-external-store@1.2.0_react@18.2.0/node_modules/use-sync-external-store/cjs/use-sync-external-store-with-selector.development.js","../../node_modules/.pnpm/use-sync-external-store@1.2.0_react@18.2.0/node_modules/use-sync-external-store/with-selector.js","../../src/react/useStore.ts","../../src/react/useProp.ts"],"sourcesContent":["import type { Store } from '@core/store';\nimport { store } from '@core/store';\nimport type { StoreScope } from '@core/storeScope';\nimport type { Context, ReactNode } from 'react';\nimport { createContext, useContext, useMemo } from 'react';\n\nexport type StoreScopeProps<T> = { scope: StoreScope<T>; store?: Store<T>; children?: ReactNode };\n\nexport const contextMap = new WeakMap<StoreScope<any>, Context<Store<any>>>();\n\nexport function getStoreScopeContext<T>(scope: StoreScope<T>): Context<Store<T>> {\n let context = contextMap.get(scope);\n\n if (!context) {\n context = createContext<Store<T>>(store(scope.defaultValue));\n contextMap.set(scope, context);\n }\n\n return context;\n}\n\nexport function StoreScopeProvider<T>({ scope, store: inputStore, children }: StoreScopeProps<T>) {\n const context = getStoreScopeContext(scope);\n const currentStore = useMemo(() => inputStore ?? store(scope.defaultValue), [scope, inputStore]);\n\n return <context.Provider value={currentStore}>{children}</context.Provider>;\n}\n\nexport function useStoreScope<T>(scope: StoreScope<T>): Store<T> {\n const context = getStoreScopeContext(scope);\n return useContext(context);\n}\n","/**\n * @license React\n * use-sync-external-store-with-selector.production.min.js\n *\n * Copyright (c) Facebook, Inc. and its affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n */\n'use strict';var g=require(\"react\");function n(a,b){return a===b&&(0!==a||1/a===1/b)||a!==a&&b!==b}var p=\"function\"===typeof Object.is?Object.is:n,q=g.useSyncExternalStore,r=g.useRef,t=g.useEffect,u=g.useMemo,v=g.useDebugValue;\nexports.useSyncExternalStoreWithSelector=function(a,b,e,l,h){var c=r(null);if(null===c.current){var f={hasValue:!1,value:null};c.current=f}else f=c.current;c=u(function(){function a(a){if(!c){c=!0;d=a;a=l(a);if(void 0!==h&&f.hasValue){var b=f.value;if(h(b,a))return k=b}return k=a}b=k;if(p(d,a))return b;var e=l(a);if(void 0!==h&&h(b,e))return b;d=a;return k=e}var c=!1,d,k,m=void 0===e?null:e;return[function(){return a(b())},null===m?void 0:function(){return a(m())}]},[b,e,l,h]);var d=q(a,c[0],c[1]);\nt(function(){f.hasValue=!0;f.value=d},[d]);v(d);return d};\n","/**\n * @license React\n * use-sync-external-store-with-selector.development.js\n *\n * Copyright (c) Facebook, Inc. and its affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n */\n\n'use strict';\n\nif (process.env.NODE_ENV !== \"production\") {\n (function() {\n\n 'use strict';\n\n/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */\nif (\n typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&\n typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart ===\n 'function'\n) {\n __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());\n}\n var React = require('react');\n\n/**\n * inlined Object.is polyfill to avoid requiring consumers ship their own\n * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is\n */\nfunction is(x, y) {\n return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare\n ;\n}\n\nvar objectIs = typeof Object.is === 'function' ? Object.is : is;\n\nvar useSyncExternalStore = React.useSyncExternalStore;\n\n// for CommonJS interop.\n\nvar useRef = React.useRef,\n useEffect = React.useEffect,\n useMemo = React.useMemo,\n useDebugValue = React.useDebugValue; // Same as useSyncExternalStore, but supports selector and isEqual arguments.\n\nfunction useSyncExternalStoreWithSelector(subscribe, getSnapshot, getServerSnapshot, selector, isEqual) {\n // Use this to track the rendered snapshot.\n var instRef = useRef(null);\n var inst;\n\n if (instRef.current === null) {\n inst = {\n hasValue: false,\n value: null\n };\n instRef.current = inst;\n } else {\n inst = instRef.current;\n }\n\n var _useMemo = useMemo(function () {\n // Track the memoized state using closure variables that are local to this\n // memoized instance of a getSnapshot function. Intentionally not using a\n // useRef hook, because that state would be shared across all concurrent\n // copies of the hook/component.\n var hasMemo = false;\n var memoizedSnapshot;\n var memoizedSelection;\n\n var memoizedSelector = function (nextSnapshot) {\n if (!hasMemo) {\n // The first time the hook is called, there is no memoized result.\n hasMemo = true;\n memoizedSnapshot = nextSnapshot;\n\n var _nextSelection = selector(nextSnapshot);\n\n if (isEqual !== undefined) {\n // Even if the selector has changed, the currently rendered selection\n // may be equal to the new selection. We should attempt to reuse the\n // current value if possible, to preserve downstream memoizations.\n if (inst.hasValue) {\n var currentSelection = inst.value;\n\n if (isEqual(currentSelection, _nextSelection)) {\n memoizedSelection = currentSelection;\n return currentSelection;\n }\n }\n }\n\n memoizedSelection = _nextSelection;\n return _nextSelection;\n } // We may be able to reuse the previous invocation's result.\n\n\n // We may be able to reuse the previous invocation's result.\n var prevSnapshot = memoizedSnapshot;\n var prevSelection = memoizedSelection;\n\n if (objectIs(prevSnapshot, nextSnapshot)) {\n // The snapshot is the same as last time. Reuse the previous selection.\n return prevSelection;\n } // The snapshot has changed, so we need to compute a new selection.\n\n\n // The snapshot has changed, so we need to compute a new selection.\n var nextSelection = selector(nextSnapshot); // If a custom isEqual function is provided, use that to check if the data\n // has changed. If it hasn't, return the previous selection. That signals\n // to React that the selections are conceptually equal, and we can bail\n // out of rendering.\n\n // If a custom isEqual function is provided, use that to check if the data\n // has changed. If it hasn't, return the previous selection. That signals\n // to React that the selections are conceptually equal, and we can bail\n // out of rendering.\n if (isEqual !== undefined && isEqual(prevSelection, nextSelection)) {\n return prevSelection;\n }\n\n memoizedSnapshot = nextSnapshot;\n memoizedSelection = nextSelection;\n return nextSelection;\n }; // Assigning this to a constant so that Flow knows it can't change.\n\n\n // Assigning this to a constant so that Flow knows it can't change.\n var maybeGetServerSnapshot = getServerSnapshot === undefined ? null : getServerSnapshot;\n\n var getSnapshotWithSelector = function () {\n return memoizedSelector(getSnapshot());\n };\n\n var getServerSnapshotWithSelector = maybeGetServerSnapshot === null ? undefined : function () {\n return memoizedSelector(maybeGetServerSnapshot());\n };\n return [getSnapshotWithSelector, getServerSnapshotWithSelector];\n }, [getSnapshot, getServerSnapshot, selector, isEqual]),\n getSelection = _useMemo[0],\n getServerSelection = _useMemo[1];\n\n var value = useSyncExternalStore(subscribe, getSelection, getServerSelection);\n useEffect(function () {\n inst.hasValue = true;\n inst.value = value;\n }, [value]);\n useDebugValue(value);\n return value;\n}\n\nexports.useSyncExternalStoreWithSelector = useSyncExternalStoreWithSelector;\n /* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */\nif (\n typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&\n typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop ===\n 'function'\n) {\n __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error());\n}\n \n })();\n}\n","'use strict';\n\nif (process.env.NODE_ENV === 'production') {\n module.exports = require('./cjs/use-sync-external-store-with-selector.production.min.js');\n} else {\n module.exports = require('./cjs/use-sync-external-store-with-selector.development.js');\n}\n","import type { SubscribeOptions } from '@core/commonTypes';\nimport type { Store } from '@core/store';\nimport { hash } from '@lib/hash';\nimport { trackingProxy } from '@lib/trackingProxy';\nimport { useCallback, useDebugValue, useLayoutEffect, useRef } from 'react';\nimport { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector';\n\nexport type UseStoreOptions = Omit<SubscribeOptions, 'runNow'>;\n\nexport function useStore<T>(store: Store<T>, options?: UseStoreOptions): T {\n const lastEqualsRef = useRef<(newValue: T) => boolean>();\n\n const subOptions = { ...options, runNow: false, equals: undefined };\n\n const subscribe = useCallback(\n (listener: () => void) => {\n return store.sub(listener, subOptions);\n },\n [store, hash(subOptions)]\n );\n\n const value = useSyncExternalStoreWithSelector(\n //\n subscribe,\n store.get,\n undefined,\n (x) => x,\n options?.equals ?? ((_v, newValue) => lastEqualsRef.current?.(newValue) ?? false)\n );\n const [proxiedValue, equals] = trackingProxy(value);\n\n useLayoutEffect(() => {\n lastEqualsRef.current = equals;\n });\n\n useDebugValue(value);\n return proxiedValue;\n}\n","import type { Store } from '@core/store';\nimport type { UpdateFn } from '@core/commonTypes';\nimport type { UseStoreOptions } from './useStore';\nimport { useStore } from './useStore';\n\nexport function useProp<T>(store: Store<T>, options?: UseStoreOptions): [value: T, setValue: UpdateFn<T>] {\n const value = useStore(store, options);\n\n return [value, store.update];\n}\n"],"names":["createContext","store","useMemo","useContext","a","c","d","b","e","module","require$$0","require$$1","useRef","useCallback","hash","useSyncExternalStoreWithSelector","trackingProxy","useLayoutEffect","useDebugValue"],"mappings":";;;;;AAQa,MAAA,iCAAiB;AAEvB,SAAS,qBAAwB,OAAyC;AAC3E,MAAA,UAAU,WAAW,IAAI,KAAK;AAElC,MAAI,CAAC,SAAS;AACZ,cAAUA,WAAAA,cAAwBC,KAAAA,MAAM,MAAM,YAAY,CAAC;AAChD,eAAA,IAAI,OAAO,OAAO;AAAA,EAC/B;AAEO,SAAA;AACT;AAEO,SAAS,mBAAsB,EAAE,OAAO,OAAO,YAAY,YAAgC;AAC1F,QAAA,UAAU,qBAAqB,KAAK;AACpC,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EuC,mDAAA,mCAAG;AAE3C,UACE,OAAO,mCAAmC,eAC1C,OAAO,+BAA+B,+BACpC,YACF;AACA,uCAA+B,2BAA2B,IAAI,MAAK,CAAE;AAAA,MACtE;AAAA,IAED;EACA;;;;ACjKA,MAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,IAAAK,QAAA,UAAiBC;EACnB,OAAO;AACL,IAAAD,QAAA,UAAiBE;EACnB;;ACGgB,SAAA,SAAY,OAAiB,SAA8B;AACzE,QAAM,gBAAgBC,WAAAA;AAEtB,QAAM,aAAa,EAAE,GAAG,SAAS,QAAQ,OAAO,QAAQ;AAExD,QAAM,YAAYC,WAAA;AAAA,IAChB,CAAC,aAAyB;AACjB,aAAA,MAAM,IAAI,UAAU,UAAU;AAAA,IACvC;AAAA,IACA,CAAC,OAAOC,UAAK,UAAU,CAAC;AAAA,EAAA;AAG1B,QAAM,QAAQC,oBAAA;AAAA,IAEZ;AAAA,IACA,MAAM;AAAA,IACN;AAAA,IACA,CAAC,MAAM;AAAA,KACP,mCAAS,YAAW,CAAC,IAAI,aAAa;;AAAA,kCAAc,YAAd,uCAAwB,cAAa;AAAA;AAAA,EAAA;AAE7E,QAAM,CAAC,cAAc,MAAM,IAAIC,mBAAc,KAAK;AAElDC,aAAAA,gBAAgB,MAAM;AACpB,kBAAc,UAAU;AAAA,EAAA,CACzB;AAEDC,aAAA,cAAc,KAAK;AACZ,SAAA;AACT;AChCgB,SAAA,QAAW,OAAiB,SAA8D;AAClG,QAAA,QAAQ,SAAS,OAAO,OAAO;AAE9B,SAAA,CAAC,OAAO,MAAM,MAAM;AAC7B;;;;;"}
|
package/dist/es/hash.mjs
ADDED
|
@@ -0,0 +1,633 @@
|
|
|
1
|
+
function queue() {
|
|
2
|
+
const q = [];
|
|
3
|
+
let promise, resolve;
|
|
4
|
+
let active = false;
|
|
5
|
+
const run = async () => {
|
|
6
|
+
if (!active) {
|
|
7
|
+
active = true;
|
|
8
|
+
let next;
|
|
9
|
+
while (next = q.shift()) {
|
|
10
|
+
try {
|
|
11
|
+
let result = next.action();
|
|
12
|
+
if (result instanceof Promise) {
|
|
13
|
+
result = await result;
|
|
14
|
+
}
|
|
15
|
+
next.resolve(result);
|
|
16
|
+
} catch (e) {
|
|
17
|
+
next.reject(e);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
active = false;
|
|
21
|
+
resolve == null ? void 0 : resolve();
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
return Object.assign(
|
|
25
|
+
(action) => {
|
|
26
|
+
return new Promise((resolve2, reject) => {
|
|
27
|
+
q.push({ action, resolve: resolve2, reject });
|
|
28
|
+
run();
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
clear() {
|
|
33
|
+
q.length = 0;
|
|
34
|
+
resolve == null ? void 0 : resolve();
|
|
35
|
+
},
|
|
36
|
+
get whenDone() {
|
|
37
|
+
if (!promise) {
|
|
38
|
+
promise = new Promise((r) => {
|
|
39
|
+
resolve = () => {
|
|
40
|
+
promise = void 0;
|
|
41
|
+
resolve = void 0;
|
|
42
|
+
r();
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return promise;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
const ProxyKeys = ["get", "getOwnPropertyDescriptor", "getPrototypeOf", "has", "isExtensible", "ownKeys"];
|
|
52
|
+
const isPlainObject = (value) => typeof value === "object" && value !== null && Object.getPrototypeOf(value) === Object.prototype;
|
|
53
|
+
function trackingProxy(value) {
|
|
54
|
+
if (!isPlainObject(value) && !Array.isArray(value)) {
|
|
55
|
+
return [value, (other) => other === value];
|
|
56
|
+
}
|
|
57
|
+
const deps = new Array();
|
|
58
|
+
const proxy = new Proxy(
|
|
59
|
+
value,
|
|
60
|
+
Object.fromEntries(
|
|
61
|
+
ProxyKeys.map((key) => [
|
|
62
|
+
key,
|
|
63
|
+
(value2, ...args) => {
|
|
64
|
+
const fn = Reflect[key];
|
|
65
|
+
const [proxiedValue, equals] = trackingProxy(fn(value2, ...args));
|
|
66
|
+
deps.push((otherValue) => {
|
|
67
|
+
if (!isPlainObject(otherValue) && !Array.isArray(otherValue)) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
return equals(fn(otherValue, ...args));
|
|
71
|
+
});
|
|
72
|
+
return proxiedValue;
|
|
73
|
+
}
|
|
74
|
+
])
|
|
75
|
+
)
|
|
76
|
+
);
|
|
77
|
+
return [proxy, (other) => !!other && deps.every((equals) => equals(other))];
|
|
78
|
+
}
|
|
79
|
+
class CalculationHelper {
|
|
80
|
+
constructor(options) {
|
|
81
|
+
this.options = options;
|
|
82
|
+
options.addEffect(() => {
|
|
83
|
+
if (this.current) {
|
|
84
|
+
this.current.check();
|
|
85
|
+
} else {
|
|
86
|
+
this.execute();
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
execute() {
|
|
91
|
+
this.stop();
|
|
92
|
+
const { calculate, addEffect, getValue, setValue, setError, onInvalidate } = this.options;
|
|
93
|
+
const checks = new Array();
|
|
94
|
+
const deps = /* @__PURE__ */ new Map();
|
|
95
|
+
const q = queue();
|
|
96
|
+
let isActive = false;
|
|
97
|
+
let isCancled = false;
|
|
98
|
+
const cancelEffect = addEffect(() => {
|
|
99
|
+
isActive = true;
|
|
100
|
+
for (const dep of deps.values()) {
|
|
101
|
+
dep.on();
|
|
102
|
+
}
|
|
103
|
+
return () => {
|
|
104
|
+
isActive = false;
|
|
105
|
+
for (const dep of deps.values()) {
|
|
106
|
+
dep.off();
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
const cancel = () => {
|
|
111
|
+
isCancled = true;
|
|
112
|
+
cancelSubscription == null ? void 0 : cancelSubscription();
|
|
113
|
+
cancelEffect();
|
|
114
|
+
delete this.current;
|
|
115
|
+
};
|
|
116
|
+
const check = () => {
|
|
117
|
+
if (!checks.every((check2) => check2())) {
|
|
118
|
+
cancel();
|
|
119
|
+
onInvalidate == null ? void 0 : onInvalidate();
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
const use = (store2, { disableProxy } = {}) => {
|
|
123
|
+
if (isCancled) {
|
|
124
|
+
return store2.get();
|
|
125
|
+
}
|
|
126
|
+
let value = store2.get();
|
|
127
|
+
let equals = (newValue) => {
|
|
128
|
+
return newValue === value;
|
|
129
|
+
};
|
|
130
|
+
if (!disableProxy) {
|
|
131
|
+
[value, equals] = trackingProxy(value);
|
|
132
|
+
}
|
|
133
|
+
let sub;
|
|
134
|
+
const dep = {
|
|
135
|
+
on() {
|
|
136
|
+
this.off();
|
|
137
|
+
sub = store2.sub(check, { runNow: false });
|
|
138
|
+
},
|
|
139
|
+
off() {
|
|
140
|
+
sub == null ? void 0 : sub();
|
|
141
|
+
sub = void 0;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
if (isActive) {
|
|
145
|
+
dep.on();
|
|
146
|
+
}
|
|
147
|
+
checks.push(() => equals(store2.get()));
|
|
148
|
+
deps.set(store2, dep);
|
|
149
|
+
return value;
|
|
150
|
+
};
|
|
151
|
+
const useFetch = (store2) => {
|
|
152
|
+
if (isCancled) {
|
|
153
|
+
return store2.fetch();
|
|
154
|
+
}
|
|
155
|
+
const value = store2.fetch();
|
|
156
|
+
const ref = store2.get().ref;
|
|
157
|
+
let sub;
|
|
158
|
+
const dep = {
|
|
159
|
+
on() {
|
|
160
|
+
this.off();
|
|
161
|
+
sub = store2.sub(check, { runNow: false });
|
|
162
|
+
},
|
|
163
|
+
off() {
|
|
164
|
+
sub == null ? void 0 : sub();
|
|
165
|
+
sub = void 0;
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
if (isActive) {
|
|
169
|
+
dep.on();
|
|
170
|
+
}
|
|
171
|
+
checks.push(() => {
|
|
172
|
+
return store2.get().ref === ref;
|
|
173
|
+
});
|
|
174
|
+
deps.set(store2, dep);
|
|
175
|
+
return value;
|
|
176
|
+
};
|
|
177
|
+
const updateValue = (update) => q(async () => {
|
|
178
|
+
if (isCancled) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (update instanceof Function) {
|
|
182
|
+
try {
|
|
183
|
+
update = update(getValue());
|
|
184
|
+
} catch (error) {
|
|
185
|
+
setError == null ? void 0 : setError(error);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (update instanceof Promise) {
|
|
190
|
+
try {
|
|
191
|
+
update = await update;
|
|
192
|
+
} catch (error) {
|
|
193
|
+
if (!isCancled) {
|
|
194
|
+
setError == null ? void 0 : setError(error);
|
|
195
|
+
}
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (!isCancled) {
|
|
200
|
+
setValue == null ? void 0 : setValue(update);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
const updateError = (error) => q(() => {
|
|
204
|
+
if (!isCancled) {
|
|
205
|
+
setError == null ? void 0 : setError(error);
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
let cancelSubscription;
|
|
209
|
+
try {
|
|
210
|
+
cancelSubscription = calculate({ use, useFetch, updateValue, updateError });
|
|
211
|
+
} catch (error) {
|
|
212
|
+
setError == null ? void 0 : setError(error);
|
|
213
|
+
}
|
|
214
|
+
this.current = { cancel, check };
|
|
215
|
+
}
|
|
216
|
+
stop() {
|
|
217
|
+
var _a;
|
|
218
|
+
(_a = this.current) == null ? void 0 : _a.cancel();
|
|
219
|
+
}
|
|
220
|
+
check() {
|
|
221
|
+
var _a;
|
|
222
|
+
(_a = this.current) == null ? void 0 : _a.check();
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function get(obj, path) {
|
|
226
|
+
if (path === "") {
|
|
227
|
+
return obj;
|
|
228
|
+
}
|
|
229
|
+
if (!(obj instanceof Object)) {
|
|
230
|
+
throw new Error(`Could not get ${path} of ${obj}`);
|
|
231
|
+
}
|
|
232
|
+
const index = path.indexOf(".");
|
|
233
|
+
if (index >= 0) {
|
|
234
|
+
const key = path.slice(0, index);
|
|
235
|
+
const rest = path.slice(index + 1);
|
|
236
|
+
const subObj = obj[key];
|
|
237
|
+
if (!subObj) {
|
|
238
|
+
return void 0;
|
|
239
|
+
}
|
|
240
|
+
return get(subObj, rest);
|
|
241
|
+
}
|
|
242
|
+
return obj[path];
|
|
243
|
+
}
|
|
244
|
+
function set(obj, path, value, rootPath = path) {
|
|
245
|
+
if (path === "") {
|
|
246
|
+
return value;
|
|
247
|
+
}
|
|
248
|
+
if (!(obj instanceof Object)) {
|
|
249
|
+
throw new Error(`Could not set ${path} of ${obj}`);
|
|
250
|
+
}
|
|
251
|
+
const index = path.indexOf(".");
|
|
252
|
+
let key, update;
|
|
253
|
+
if (index >= 0) {
|
|
254
|
+
key = path.slice(0, index);
|
|
255
|
+
const rest = path.slice(index + 1);
|
|
256
|
+
const subObj = obj[key];
|
|
257
|
+
if (!subObj) {
|
|
258
|
+
const prefix = rootPath.slice(0, -rest.length - 1);
|
|
259
|
+
throw Error(`Cannot set ${rootPath} because ${prefix} is ${subObj}`);
|
|
260
|
+
}
|
|
261
|
+
update = set(subObj, rest, value, rootPath);
|
|
262
|
+
} else {
|
|
263
|
+
key = path;
|
|
264
|
+
update = value instanceof Function ? value(obj[key]) : value;
|
|
265
|
+
}
|
|
266
|
+
if (Array.isArray(obj)) {
|
|
267
|
+
const copy = Array.from(obj);
|
|
268
|
+
copy[key] = update;
|
|
269
|
+
return copy;
|
|
270
|
+
}
|
|
271
|
+
return {
|
|
272
|
+
...obj,
|
|
273
|
+
[key]: update
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
function makeSelector(selector) {
|
|
277
|
+
if (!selector) {
|
|
278
|
+
return (x) => x;
|
|
279
|
+
}
|
|
280
|
+
if (selector instanceof Function) {
|
|
281
|
+
return selector;
|
|
282
|
+
}
|
|
283
|
+
return (x) => get(x, selector);
|
|
284
|
+
}
|
|
285
|
+
const getAllProperties = (object) => {
|
|
286
|
+
const properties = /* @__PURE__ */ new Set();
|
|
287
|
+
do {
|
|
288
|
+
for (const key of Reflect.ownKeys(object)) {
|
|
289
|
+
properties.add([object, key]);
|
|
290
|
+
}
|
|
291
|
+
} while ((object = Reflect.getPrototypeOf(object)) && object !== Object.prototype);
|
|
292
|
+
return properties;
|
|
293
|
+
};
|
|
294
|
+
function bind(self) {
|
|
295
|
+
for (const [object, key] of getAllProperties(self.constructor.prototype)) {
|
|
296
|
+
if (key === "constructor") {
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(object, key);
|
|
300
|
+
if (descriptor && typeof descriptor.value === "function") {
|
|
301
|
+
self[key] = self[key].bind(self);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const calcDuration = (t) => {
|
|
306
|
+
if (typeof t === "number")
|
|
307
|
+
return t;
|
|
308
|
+
return (t.milliseconds ?? 0) + (t.seconds ?? 0) * 1e3 + (t.minutes ?? 0) * 60 * 1e3 + (t.hours ?? 0) * 60 * 60 * 1e3 + (t.days ?? 0) * 24 * 60 * 60 * 1e3;
|
|
309
|
+
};
|
|
310
|
+
const defaultEquals = (a, b) => a === b;
|
|
311
|
+
const simpleShallowEquals = (a, b) => {
|
|
312
|
+
if (a === b) {
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
316
|
+
return a.length === b.length && a.every((value, i) => value === b[i]);
|
|
317
|
+
}
|
|
318
|
+
if (typeof a === "object" && typeof b === "object") {
|
|
319
|
+
if (a === null || b === null) {
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
const e1 = Object.entries(a);
|
|
323
|
+
const e2 = Object.entries(b);
|
|
324
|
+
return e1.length === e2.length && e1.every(([key, value]) => value === b[key]);
|
|
325
|
+
}
|
|
326
|
+
return false;
|
|
327
|
+
};
|
|
328
|
+
function forwardError(error) {
|
|
329
|
+
setTimeout(() => {
|
|
330
|
+
throw error;
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
const arrMod = (prop) => function(...args) {
|
|
334
|
+
const newArr = this.get().slice();
|
|
335
|
+
const result = newArr[prop](...args);
|
|
336
|
+
this.update(newArr);
|
|
337
|
+
return result;
|
|
338
|
+
};
|
|
339
|
+
const arrayActions = {
|
|
340
|
+
splice: arrMod("splice"),
|
|
341
|
+
push: arrMod("push"),
|
|
342
|
+
pop: arrMod("pop"),
|
|
343
|
+
shift: arrMod("shift"),
|
|
344
|
+
unshift: arrMod("unshift"),
|
|
345
|
+
reverse: arrMod("reverse"),
|
|
346
|
+
sort: arrMod("sort")
|
|
347
|
+
};
|
|
348
|
+
const recordActions = {
|
|
349
|
+
set(key, value) {
|
|
350
|
+
if (value instanceof Function) {
|
|
351
|
+
value = value(this.get()[key]);
|
|
352
|
+
}
|
|
353
|
+
this.update({ ...this.get(), [key]: value });
|
|
354
|
+
return this;
|
|
355
|
+
},
|
|
356
|
+
delete(key) {
|
|
357
|
+
const copy = { ...this.get() };
|
|
358
|
+
delete copy[key];
|
|
359
|
+
this.update(copy);
|
|
360
|
+
},
|
|
361
|
+
clear() {
|
|
362
|
+
this.update({});
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
const mapActions = {
|
|
366
|
+
set(key, value) {
|
|
367
|
+
const newMap = new Map(this.get());
|
|
368
|
+
newMap.set(key, value);
|
|
369
|
+
this.update(newMap);
|
|
370
|
+
return this;
|
|
371
|
+
},
|
|
372
|
+
delete(key) {
|
|
373
|
+
const newMap = new Map(this.get());
|
|
374
|
+
const result = newMap.delete(key);
|
|
375
|
+
this.update(newMap);
|
|
376
|
+
return result;
|
|
377
|
+
},
|
|
378
|
+
clear() {
|
|
379
|
+
this.update(/* @__PURE__ */ new Map());
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
const setActions = {
|
|
383
|
+
add(value) {
|
|
384
|
+
const newSet = new Set(this.get());
|
|
385
|
+
newSet.add(value);
|
|
386
|
+
this.update(newSet);
|
|
387
|
+
},
|
|
388
|
+
delete(value) {
|
|
389
|
+
const newSet = new Set(this.get());
|
|
390
|
+
newSet.delete(value);
|
|
391
|
+
this.update(newSet);
|
|
392
|
+
},
|
|
393
|
+
clear() {
|
|
394
|
+
this.update(/* @__PURE__ */ new Set());
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
function throttle(fn, ms) {
|
|
398
|
+
let t = 0;
|
|
399
|
+
let timeout;
|
|
400
|
+
return (...args) => {
|
|
401
|
+
if (timeout !== void 0) {
|
|
402
|
+
clearTimeout(timeout);
|
|
403
|
+
}
|
|
404
|
+
const dt = t + ms - Date.now();
|
|
405
|
+
if (dt <= 0) {
|
|
406
|
+
fn(...args);
|
|
407
|
+
t = Date.now();
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
timeout = setTimeout(() => {
|
|
411
|
+
fn(...args);
|
|
412
|
+
t = Date.now();
|
|
413
|
+
}, dt);
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
const noop = () => void 0;
|
|
417
|
+
class Store {
|
|
418
|
+
constructor(initialValue, options = {}) {
|
|
419
|
+
this.initialValue = initialValue;
|
|
420
|
+
this.options = options;
|
|
421
|
+
this.value = this.initialValue;
|
|
422
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
423
|
+
this.effects = /* @__PURE__ */ new Map();
|
|
424
|
+
this.notifyId = {};
|
|
425
|
+
bind(this);
|
|
426
|
+
}
|
|
427
|
+
get() {
|
|
428
|
+
return this.value;
|
|
429
|
+
}
|
|
430
|
+
update(update) {
|
|
431
|
+
if (update instanceof Function) {
|
|
432
|
+
update = update(this.get());
|
|
433
|
+
}
|
|
434
|
+
this.value = update;
|
|
435
|
+
this.notify();
|
|
436
|
+
}
|
|
437
|
+
sub(listener, options) {
|
|
438
|
+
const { runNow = true, throttle: throttleOption, equals = defaultEquals } = options ?? {};
|
|
439
|
+
let compareToValue = this.get();
|
|
440
|
+
let previousValue;
|
|
441
|
+
let hasRun = false;
|
|
442
|
+
let innerListener = (force) => {
|
|
443
|
+
const value = this.get();
|
|
444
|
+
if (!force && equals(value, compareToValue)) {
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
compareToValue = value;
|
|
448
|
+
const _previousValue = previousValue;
|
|
449
|
+
previousValue = value;
|
|
450
|
+
hasRun = true;
|
|
451
|
+
try {
|
|
452
|
+
listener(value, _previousValue);
|
|
453
|
+
} catch (error) {
|
|
454
|
+
forwardError(error);
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
if (throttleOption) {
|
|
458
|
+
innerListener = throttle(innerListener, calcDuration(throttleOption));
|
|
459
|
+
}
|
|
460
|
+
this.listeners.add(innerListener);
|
|
461
|
+
this.onSubscribe();
|
|
462
|
+
if (runNow && !hasRun) {
|
|
463
|
+
innerListener(true);
|
|
464
|
+
}
|
|
465
|
+
return () => {
|
|
466
|
+
this.listeners.delete(innerListener);
|
|
467
|
+
this.onUnsubscribe();
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
map(_selector, options) {
|
|
471
|
+
const selector = makeSelector(_selector);
|
|
472
|
+
const derivedFrom = { store: this, selectors: [_selector] };
|
|
473
|
+
return new DerivedStore(({ use }) => {
|
|
474
|
+
return selector(use(this, options));
|
|
475
|
+
}, derivedFrom);
|
|
476
|
+
}
|
|
477
|
+
addEffect(effect, retain) {
|
|
478
|
+
this.effects.set(effect, {
|
|
479
|
+
handle: this.listeners.size > 0 ? effect() ?? noop : void 0,
|
|
480
|
+
retain: retain !== void 0 ? calcDuration(retain) : void 0
|
|
481
|
+
});
|
|
482
|
+
return () => {
|
|
483
|
+
const { handle, timeout } = this.effects.get(effect) ?? {};
|
|
484
|
+
handle == null ? void 0 : handle();
|
|
485
|
+
timeout !== void 0 && clearTimeout(timeout);
|
|
486
|
+
this.effects.delete(effect);
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
get isActive() {
|
|
490
|
+
return this.listeners.size > 0;
|
|
491
|
+
}
|
|
492
|
+
onSubscribe() {
|
|
493
|
+
if (this.listeners.size > 1)
|
|
494
|
+
return;
|
|
495
|
+
for (const [effect, { handle, retain, timeout }] of this.effects.entries()) {
|
|
496
|
+
timeout !== void 0 && clearTimeout(timeout);
|
|
497
|
+
this.effects.set(effect, {
|
|
498
|
+
handle: handle ?? effect() ?? noop,
|
|
499
|
+
retain,
|
|
500
|
+
timeout: void 0
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
onUnsubscribe() {
|
|
505
|
+
if (this.listeners.size > 0)
|
|
506
|
+
return;
|
|
507
|
+
for (const [effect, { handle, retain, timeout }] of this.effects.entries()) {
|
|
508
|
+
!retain && (handle == null ? void 0 : handle());
|
|
509
|
+
timeout !== void 0 && clearTimeout(timeout);
|
|
510
|
+
this.effects.set(effect, {
|
|
511
|
+
handle: retain ? handle : void 0,
|
|
512
|
+
retain,
|
|
513
|
+
timeout: retain && handle ? setTimeout(handle, retain) : void 0
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
notify() {
|
|
518
|
+
const n = this.notifyId = {};
|
|
519
|
+
for (const listener of [...this.listeners]) {
|
|
520
|
+
listener();
|
|
521
|
+
if (n !== this.notifyId)
|
|
522
|
+
break;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
const defaultOptions = {};
|
|
527
|
+
function _store(initialState, options) {
|
|
528
|
+
if (initialState instanceof Function) {
|
|
529
|
+
return new DerivedStore(initialState);
|
|
530
|
+
}
|
|
531
|
+
let methods = options == null ? void 0 : options.methods;
|
|
532
|
+
if (initialState instanceof Map) {
|
|
533
|
+
methods = { ...mapActions, ...methods };
|
|
534
|
+
} else if (initialState instanceof Set) {
|
|
535
|
+
methods = { ...setActions, ...methods };
|
|
536
|
+
} else if (Array.isArray(initialState)) {
|
|
537
|
+
methods = { ...arrayActions, ...methods };
|
|
538
|
+
} else if (initialState instanceof Object) {
|
|
539
|
+
methods = { ...recordActions, ...methods };
|
|
540
|
+
}
|
|
541
|
+
const store2 = new Store(initialState, options);
|
|
542
|
+
const boundActions = Object.fromEntries(
|
|
543
|
+
Object.entries(methods ?? {}).filter(([name]) => !(name in store2)).map(([name, fn]) => [name, fn.bind(store2)])
|
|
544
|
+
);
|
|
545
|
+
return Object.assign(store2, boundActions);
|
|
546
|
+
}
|
|
547
|
+
const store = Object.assign(_store, { defaultOptions });
|
|
548
|
+
class DerivedStore extends Store {
|
|
549
|
+
constructor(calculate, derivedFrom) {
|
|
550
|
+
super(void 0);
|
|
551
|
+
this.calculate = calculate;
|
|
552
|
+
this.derivedFrom = derivedFrom;
|
|
553
|
+
this.calculationHelper = new CalculationHelper({
|
|
554
|
+
calculate: ({ use }) => {
|
|
555
|
+
const value = this.calculate.apply({ use }, [{ use }]);
|
|
556
|
+
this.valid = true;
|
|
557
|
+
super.update(value);
|
|
558
|
+
},
|
|
559
|
+
addEffect: this.addEffect,
|
|
560
|
+
getValue: () => this.value,
|
|
561
|
+
onInvalidate: this.invalidate
|
|
562
|
+
});
|
|
563
|
+
this.valid = false;
|
|
564
|
+
}
|
|
565
|
+
get() {
|
|
566
|
+
if (!this.valid) {
|
|
567
|
+
this.calculationHelper.execute();
|
|
568
|
+
}
|
|
569
|
+
return super.get();
|
|
570
|
+
}
|
|
571
|
+
update(update) {
|
|
572
|
+
if (this.derivedFrom && this.derivedFrom.selectors.every((selector) => typeof selector === "string")) {
|
|
573
|
+
const path = this.derivedFrom.selectors.join(".");
|
|
574
|
+
if (update instanceof Function) {
|
|
575
|
+
const before = get(this.derivedFrom.store, path);
|
|
576
|
+
update = update(before);
|
|
577
|
+
}
|
|
578
|
+
this.derivedFrom.store.update((before) => set(before, path, update));
|
|
579
|
+
} else {
|
|
580
|
+
throw new Error("Can only updated computed stores that are derived from other stores using string selectors");
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
map(_selector) {
|
|
584
|
+
const selector = makeSelector(_selector);
|
|
585
|
+
const derivedFrom = this.derivedFrom ?? { store: this, selectors: [] };
|
|
586
|
+
const newDerivedFrom = { ...derivedFrom, selectors: derivedFrom.selectors.concat(_selector) };
|
|
587
|
+
return new DerivedStore(({ use }) => {
|
|
588
|
+
return selector(use(this));
|
|
589
|
+
}, newDerivedFrom);
|
|
590
|
+
}
|
|
591
|
+
invalidate() {
|
|
592
|
+
this.valid = false;
|
|
593
|
+
if (this.isActive) {
|
|
594
|
+
this.calculationHelper.execute();
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
function _derivedStore(calculate) {
|
|
599
|
+
return new DerivedStore(calculate);
|
|
600
|
+
}
|
|
601
|
+
const derivedStore = Object.assign(_derivedStore, {});
|
|
602
|
+
function hash(value) {
|
|
603
|
+
if (value instanceof Set) {
|
|
604
|
+
return `s[${[...value].map(hash).sort().join(",")}]`;
|
|
605
|
+
}
|
|
606
|
+
if (value instanceof Map) {
|
|
607
|
+
return `m[${[...value.entries()].map(hash).sort().join(",")}]`;
|
|
608
|
+
}
|
|
609
|
+
if (value instanceof Array) {
|
|
610
|
+
return `[${value.map(hash).join(",")}]`;
|
|
611
|
+
}
|
|
612
|
+
if (value instanceof Object) {
|
|
613
|
+
return `o[${Object.entries(value).map(hash).sort().join(",")}]`;
|
|
614
|
+
}
|
|
615
|
+
return JSON.stringify(value);
|
|
616
|
+
}
|
|
617
|
+
export {
|
|
618
|
+
CalculationHelper as C,
|
|
619
|
+
Store as S,
|
|
620
|
+
arrayActions as a,
|
|
621
|
+
mapActions as b,
|
|
622
|
+
calcDuration as c,
|
|
623
|
+
defaultEquals as d,
|
|
624
|
+
setActions as e,
|
|
625
|
+
derivedStore as f,
|
|
626
|
+
store as g,
|
|
627
|
+
hash as h,
|
|
628
|
+
makeSelector as m,
|
|
629
|
+
recordActions as r,
|
|
630
|
+
simpleShallowEquals as s,
|
|
631
|
+
trackingProxy as t
|
|
632
|
+
};
|
|
633
|
+
//# sourceMappingURL=hash.mjs.map
|