floppy-disk 2.10.0 → 2.11.0-beta.1
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/esm/react/create-query.js +11 -11
- package/esm/react/create-stores.d.ts +2 -2
- package/esm/react/create-stores.js +2 -2
- package/esm/utils/fetcher.js +3 -2
- package/esm/utils/index.d.ts +1 -1
- package/esm/utils/index.js +1 -1
- package/esm/vanilla.d.ts +2 -2
- package/esm/vanilla.js +3 -3
- package/lib/react/create-query.js +10 -10
- package/lib/react/create-stores.d.ts +2 -2
- package/lib/react/create-stores.js +1 -1
- package/lib/utils/fetcher.js +2 -1
- package/lib/utils/index.d.ts +1 -1
- package/lib/utils/index.js +3 -3
- package/lib/vanilla.d.ts +2 -2
- package/lib/vanilla.js +2 -2
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createElement, useState } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { getValue, hasValue, identityFn, isClient, noop } from '../utils';
|
|
3
3
|
import { createStores } from './create-stores';
|
|
4
4
|
const INITIAL_QUERY_STATE = {
|
|
5
5
|
isWaiting: false,
|
|
@@ -50,8 +50,8 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
50
50
|
const key = _key;
|
|
51
51
|
const getRetryProps = (error, retryCount) => {
|
|
52
52
|
const prevState = get();
|
|
53
|
-
const maxRetryCount =
|
|
54
|
-
const delay =
|
|
53
|
+
const maxRetryCount = getValue(retry, error, prevState) || 0;
|
|
54
|
+
const delay = getValue(retryDelay, error, prevState) || 0;
|
|
55
55
|
return { shouldRetry: retryCount < maxRetryCount, delay };
|
|
56
56
|
};
|
|
57
57
|
const forceFetch = () => new Promise((resolve) => {
|
|
@@ -61,7 +61,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
61
61
|
const newPageParams = [pageParams[0]];
|
|
62
62
|
let pageParam = pageParams[0];
|
|
63
63
|
clearTimeout(refetchIntervalTimeoutId.get(keyHash));
|
|
64
|
-
if (isWaiting || !
|
|
64
|
+
if (isWaiting || !getValue(enabled, key))
|
|
65
65
|
return resolve(state);
|
|
66
66
|
if (isLoading)
|
|
67
67
|
set({ isWaiting: true });
|
|
@@ -113,7 +113,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
113
113
|
pageParams: newPageParams,
|
|
114
114
|
hasNextPage: hasValue(newPageParam),
|
|
115
115
|
};
|
|
116
|
-
const refetchIntervalValue = isClient &&
|
|
116
|
+
const refetchIntervalValue = isClient && getValue(refetchInterval, { ...get(), ...nextState });
|
|
117
117
|
if (refetchIntervalValue) {
|
|
118
118
|
refetchIntervalTimeoutId.set(keyHash, window.setTimeout(() => {
|
|
119
119
|
forceFetch();
|
|
@@ -190,7 +190,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
190
190
|
const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
|
|
191
191
|
if (isLoading)
|
|
192
192
|
return resolve(forceFetch());
|
|
193
|
-
if (isWaitingNextPage || !hasNextPage || !
|
|
193
|
+
if (isWaitingNextPage || !hasNextPage || !getValue(enabled, key))
|
|
194
194
|
return resolve(state);
|
|
195
195
|
set({ isWaitingNextPage: true, isGoingToRetryNextPage: false });
|
|
196
196
|
clearTimeout(retryNextPageTimeoutId.get(keyHash));
|
|
@@ -258,7 +258,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
258
258
|
}, (() => {
|
|
259
259
|
const windowFocusHandler = () => {
|
|
260
260
|
useQuery.getAllWithSubscriber().forEach((state) => {
|
|
261
|
-
const result =
|
|
261
|
+
const result = getValue(fetchOnWindowFocus, state.key);
|
|
262
262
|
if (result === 'always')
|
|
263
263
|
state.forceFetch();
|
|
264
264
|
else if (result)
|
|
@@ -267,7 +267,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
267
267
|
};
|
|
268
268
|
const reconnectHandler = () => {
|
|
269
269
|
useQuery.getAllWithSubscriber().forEach((state) => {
|
|
270
|
-
const result =
|
|
270
|
+
const result = getValue(fetchOnReconnect, state.key);
|
|
271
271
|
if (result === 'always')
|
|
272
272
|
state.forceFetch();
|
|
273
273
|
else if (result)
|
|
@@ -279,7 +279,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
279
279
|
defaultDeps,
|
|
280
280
|
onFirstSubscribe: (state) => {
|
|
281
281
|
if (state.isSuccess) {
|
|
282
|
-
const refetchIntervalValue = isClient &&
|
|
282
|
+
const refetchIntervalValue = isClient && getValue(refetchInterval, state);
|
|
283
283
|
if (refetchIntervalValue) {
|
|
284
284
|
refetchIntervalTimeoutId.set(state.keyHash, window.setTimeout(() => {
|
|
285
285
|
state.forceFetch();
|
|
@@ -296,7 +296,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
296
296
|
onFirstSubscribe(state);
|
|
297
297
|
},
|
|
298
298
|
onSubscribe: (state) => {
|
|
299
|
-
const result =
|
|
299
|
+
const result = getValue(fetchOnMount, state.key);
|
|
300
300
|
if (result === 'always')
|
|
301
301
|
state.forceFetch();
|
|
302
302
|
else if (result)
|
|
@@ -390,7 +390,7 @@ export const createQuery = (queryFn, options = {}) => {
|
|
|
390
390
|
};
|
|
391
391
|
useQuery.optimisticUpdate = ({ key, response }) => {
|
|
392
392
|
const prevState = useQuery.get(key);
|
|
393
|
-
const optimisticResponse =
|
|
393
|
+
const optimisticResponse = getValue(response, prevState);
|
|
394
394
|
useQuery.set(key, {
|
|
395
395
|
isOptimisticData: true,
|
|
396
396
|
response: optimisticResponse,
|
|
@@ -2,12 +2,12 @@ import { Maybe } from '../utils';
|
|
|
2
2
|
import { InitStoreOptions, InitStoreReturn, SelectDeps, SetStoreData, StoreData, Subscribers } from '../vanilla';
|
|
3
3
|
import { WatchProps } from './create-store';
|
|
4
4
|
export type StoreKey = Record<string, any> | undefined;
|
|
5
|
-
export type StoresInitializer<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = (api: {
|
|
5
|
+
export type StoresInitializer<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = T | ((api: {
|
|
6
6
|
get: () => T;
|
|
7
7
|
set: (value: SetStoreData<T>, silent?: boolean) => void;
|
|
8
8
|
key: TKey;
|
|
9
9
|
keyHash: string;
|
|
10
|
-
}) => T;
|
|
10
|
+
}) => T);
|
|
11
11
|
export type UseStores<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = {
|
|
12
12
|
/**
|
|
13
13
|
* @param key (Optional) Store key, an object that will be hashed into a string as a store identifier.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
-
import { hashStoreKey, noop } from '../utils';
|
|
2
|
+
import { getValue, hashStoreKey, noop } from '../utils';
|
|
3
3
|
import { initStore, } from '../vanilla';
|
|
4
4
|
/**
|
|
5
5
|
* @see https://floppy-disk.vercel.app/docs/api#createstores
|
|
@@ -11,7 +11,7 @@ export const createStores = (initializer, options = {}) => {
|
|
|
11
11
|
const key = _key || {};
|
|
12
12
|
const keyHash = hashKeyFn(key);
|
|
13
13
|
if (!stores.has(keyHash)) {
|
|
14
|
-
stores.set(keyHash, initStore((api) => initializer
|
|
14
|
+
stores.set(keyHash, initStore((api) => getValue(initializer, { key, keyHash, ...api }), options));
|
|
15
15
|
onStoreInitialized(key, keyHash);
|
|
16
16
|
}
|
|
17
17
|
return stores.get(keyHash);
|
package/esm/utils/fetcher.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { createError,
|
|
1
|
+
import { createError, getValue, identityFn } from '.';
|
|
2
2
|
const encodeParams = (params) => Object.entries(params)
|
|
3
|
+
.filter(([, value]) => value !== undefined && value !== null)
|
|
3
4
|
.map((kv) => kv.map(encodeURIComponent).join('='))
|
|
4
5
|
.join('&');
|
|
5
6
|
/**
|
|
@@ -14,7 +15,7 @@ const encodeParams = (params) => Object.entries(params)
|
|
|
14
15
|
* @returns A function to fetch data
|
|
15
16
|
*/
|
|
16
17
|
export const fetcher = (options) => async (...args) => {
|
|
17
|
-
const { url, query, params, payload, headers, interceptRequest = identityFn, interceptResponse, ...rest } =
|
|
18
|
+
const { url, query, params, payload, headers, interceptRequest = identityFn, interceptResponse, ...rest } = getValue(options, ...args);
|
|
18
19
|
let autoOptions = {};
|
|
19
20
|
let searchParams = params;
|
|
20
21
|
if (query) {
|
package/esm/utils/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export declare const noop: () => void;
|
|
|
2
2
|
export declare const identityFn: <T>(value: T) => T;
|
|
3
3
|
export declare const hasValue: (value: any) => boolean;
|
|
4
4
|
export declare const hashStoreKey: (obj?: any) => string;
|
|
5
|
-
export declare const
|
|
5
|
+
export declare const getValue: <T, P extends any[]>(valueOrComputeValueFn: T | ((...params: P) => T), ...params: P) => T;
|
|
6
6
|
export type Maybe<T> = T | null | undefined;
|
|
7
7
|
export declare const isClient: boolean;
|
|
8
8
|
export declare const createError: (message: string, props: Record<string, any>) => Error & Record<string, any>;
|
package/esm/utils/index.js
CHANGED
|
@@ -2,7 +2,7 @@ export const noop = () => { };
|
|
|
2
2
|
export const identityFn = (value) => value;
|
|
3
3
|
export const hasValue = (value) => value !== undefined && value !== null;
|
|
4
4
|
export const hashStoreKey = (obj) => JSON.stringify(obj, Object.keys(obj).sort());
|
|
5
|
-
export const
|
|
5
|
+
export const getValue = (valueOrComputeValueFn, ...params) => {
|
|
6
6
|
if (typeof valueOrComputeValueFn === 'function') {
|
|
7
7
|
return valueOrComputeValueFn(...params);
|
|
8
8
|
}
|
package/esm/vanilla.d.ts
CHANGED
|
@@ -2,10 +2,10 @@ export type StoreData = Record<string, any>;
|
|
|
2
2
|
export type SetStoreData<T> = Partial<T> | ((prevState: T) => Partial<T>);
|
|
3
3
|
export type SelectDeps<T> = ((state: T) => any[]) | undefined | null;
|
|
4
4
|
export type Subscribers<T> = Map<(state: T) => void, SelectDeps<T>>;
|
|
5
|
-
export type StoreInitializer<T> = (api: {
|
|
5
|
+
export type StoreInitializer<T> = T | ((api: {
|
|
6
6
|
get: () => T;
|
|
7
7
|
set: (value: SetStoreData<T>, silent?: boolean) => void;
|
|
8
|
-
}) => T;
|
|
8
|
+
}) => T);
|
|
9
9
|
export type StoreEvent<T> = (state: T) => void;
|
|
10
10
|
export type InitStoreOptions<T> = {
|
|
11
11
|
intercept?: (nextState: T, prevState: T) => Partial<T>;
|
package/esm/vanilla.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getValue, noop } from './utils';
|
|
2
2
|
export const initStore = (initializer, options = {}) => {
|
|
3
3
|
const { intercept, onFirstSubscribe = noop, onSubscribe = noop, onUnsubscribe = noop, onLastUnsubscribe = noop, } = options;
|
|
4
4
|
const subscribers = new Map();
|
|
@@ -7,7 +7,7 @@ export const initStore = (initializer, options = {}) => {
|
|
|
7
7
|
const get = () => data;
|
|
8
8
|
const set = (value, silent = false) => {
|
|
9
9
|
const prevData = data;
|
|
10
|
-
data = { ...data, ...
|
|
10
|
+
data = { ...data, ...getValue(value, data) };
|
|
11
11
|
if (intercept) {
|
|
12
12
|
data = { ...data, ...intercept(data, prevData) };
|
|
13
13
|
}
|
|
@@ -46,6 +46,6 @@ export const initStore = (initializer, options = {}) => {
|
|
|
46
46
|
onLastUnsubscribe(data);
|
|
47
47
|
};
|
|
48
48
|
};
|
|
49
|
-
data = initializer
|
|
49
|
+
data = getValue(initializer, { get, set });
|
|
50
50
|
return { get, set, subscribe, getSubscribers };
|
|
51
51
|
};
|
|
@@ -53,8 +53,8 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
53
53
|
const key = _key;
|
|
54
54
|
const getRetryProps = (error, retryCount) => {
|
|
55
55
|
const prevState = get();
|
|
56
|
-
const maxRetryCount = (0, utils_1.
|
|
57
|
-
const delay = (0, utils_1.
|
|
56
|
+
const maxRetryCount = (0, utils_1.getValue)(retry, error, prevState) || 0;
|
|
57
|
+
const delay = (0, utils_1.getValue)(retryDelay, error, prevState) || 0;
|
|
58
58
|
return { shouldRetry: retryCount < maxRetryCount, delay };
|
|
59
59
|
};
|
|
60
60
|
const forceFetch = () => new Promise((resolve) => {
|
|
@@ -64,7 +64,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
64
64
|
const newPageParams = [pageParams[0]];
|
|
65
65
|
let pageParam = pageParams[0];
|
|
66
66
|
clearTimeout(refetchIntervalTimeoutId.get(keyHash));
|
|
67
|
-
if (isWaiting || !(0, utils_1.
|
|
67
|
+
if (isWaiting || !(0, utils_1.getValue)(enabled, key))
|
|
68
68
|
return resolve(state);
|
|
69
69
|
if (isLoading)
|
|
70
70
|
set({ isWaiting: true });
|
|
@@ -116,7 +116,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
116
116
|
pageParams: newPageParams,
|
|
117
117
|
hasNextPage: (0, utils_1.hasValue)(newPageParam),
|
|
118
118
|
};
|
|
119
|
-
const refetchIntervalValue = utils_1.isClient && (0, utils_1.
|
|
119
|
+
const refetchIntervalValue = utils_1.isClient && (0, utils_1.getValue)(refetchInterval, { ...get(), ...nextState });
|
|
120
120
|
if (refetchIntervalValue) {
|
|
121
121
|
refetchIntervalTimeoutId.set(keyHash, window.setTimeout(() => {
|
|
122
122
|
forceFetch();
|
|
@@ -193,7 +193,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
193
193
|
const { isLoading, isWaitingNextPage, data, hasNextPage, pageParam, pageParams } = state;
|
|
194
194
|
if (isLoading)
|
|
195
195
|
return resolve(forceFetch());
|
|
196
|
-
if (isWaitingNextPage || !hasNextPage || !(0, utils_1.
|
|
196
|
+
if (isWaitingNextPage || !hasNextPage || !(0, utils_1.getValue)(enabled, key))
|
|
197
197
|
return resolve(state);
|
|
198
198
|
set({ isWaitingNextPage: true, isGoingToRetryNextPage: false });
|
|
199
199
|
clearTimeout(retryNextPageTimeoutId.get(keyHash));
|
|
@@ -261,7 +261,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
261
261
|
}, (() => {
|
|
262
262
|
const windowFocusHandler = () => {
|
|
263
263
|
useQuery.getAllWithSubscriber().forEach((state) => {
|
|
264
|
-
const result = (0, utils_1.
|
|
264
|
+
const result = (0, utils_1.getValue)(fetchOnWindowFocus, state.key);
|
|
265
265
|
if (result === 'always')
|
|
266
266
|
state.forceFetch();
|
|
267
267
|
else if (result)
|
|
@@ -270,7 +270,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
270
270
|
};
|
|
271
271
|
const reconnectHandler = () => {
|
|
272
272
|
useQuery.getAllWithSubscriber().forEach((state) => {
|
|
273
|
-
const result = (0, utils_1.
|
|
273
|
+
const result = (0, utils_1.getValue)(fetchOnReconnect, state.key);
|
|
274
274
|
if (result === 'always')
|
|
275
275
|
state.forceFetch();
|
|
276
276
|
else if (result)
|
|
@@ -282,7 +282,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
282
282
|
defaultDeps,
|
|
283
283
|
onFirstSubscribe: (state) => {
|
|
284
284
|
if (state.isSuccess) {
|
|
285
|
-
const refetchIntervalValue = utils_1.isClient && (0, utils_1.
|
|
285
|
+
const refetchIntervalValue = utils_1.isClient && (0, utils_1.getValue)(refetchInterval, state);
|
|
286
286
|
if (refetchIntervalValue) {
|
|
287
287
|
refetchIntervalTimeoutId.set(state.keyHash, window.setTimeout(() => {
|
|
288
288
|
state.forceFetch();
|
|
@@ -299,7 +299,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
299
299
|
onFirstSubscribe(state);
|
|
300
300
|
},
|
|
301
301
|
onSubscribe: (state) => {
|
|
302
|
-
const result = (0, utils_1.
|
|
302
|
+
const result = (0, utils_1.getValue)(fetchOnMount, state.key);
|
|
303
303
|
if (result === 'always')
|
|
304
304
|
state.forceFetch();
|
|
305
305
|
else if (result)
|
|
@@ -393,7 +393,7 @@ const createQuery = (queryFn, options = {}) => {
|
|
|
393
393
|
};
|
|
394
394
|
useQuery.optimisticUpdate = ({ key, response }) => {
|
|
395
395
|
const prevState = useQuery.get(key);
|
|
396
|
-
const optimisticResponse = (0, utils_1.
|
|
396
|
+
const optimisticResponse = (0, utils_1.getValue)(response, prevState);
|
|
397
397
|
useQuery.set(key, {
|
|
398
398
|
isOptimisticData: true,
|
|
399
399
|
response: optimisticResponse,
|
|
@@ -2,12 +2,12 @@ import { Maybe } from '../utils';
|
|
|
2
2
|
import { InitStoreOptions, InitStoreReturn, SelectDeps, SetStoreData, StoreData, Subscribers } from '../vanilla';
|
|
3
3
|
import { WatchProps } from './create-store';
|
|
4
4
|
export type StoreKey = Record<string, any> | undefined;
|
|
5
|
-
export type StoresInitializer<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = (api: {
|
|
5
|
+
export type StoresInitializer<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = T | ((api: {
|
|
6
6
|
get: () => T;
|
|
7
7
|
set: (value: SetStoreData<T>, silent?: boolean) => void;
|
|
8
8
|
key: TKey;
|
|
9
9
|
keyHash: string;
|
|
10
|
-
}) => T;
|
|
10
|
+
}) => T);
|
|
11
11
|
export type UseStores<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = {
|
|
12
12
|
/**
|
|
13
13
|
* @param key (Optional) Store key, an object that will be hashed into a string as a store identifier.
|
|
@@ -14,7 +14,7 @@ const createStores = (initializer, options = {}) => {
|
|
|
14
14
|
const key = _key || {};
|
|
15
15
|
const keyHash = hashKeyFn(key);
|
|
16
16
|
if (!stores.has(keyHash)) {
|
|
17
|
-
stores.set(keyHash, (0, vanilla_1.initStore)((api) => initializer
|
|
17
|
+
stores.set(keyHash, (0, vanilla_1.initStore)((api) => (0, utils_1.getValue)(initializer, { key, keyHash, ...api }), options));
|
|
18
18
|
onStoreInitialized(key, keyHash);
|
|
19
19
|
}
|
|
20
20
|
return stores.get(keyHash);
|
package/lib/utils/fetcher.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.fetcher = void 0;
|
|
4
4
|
const _1 = require(".");
|
|
5
5
|
const encodeParams = (params) => Object.entries(params)
|
|
6
|
+
.filter(([, value]) => value !== undefined && value !== null)
|
|
6
7
|
.map((kv) => kv.map(encodeURIComponent).join('='))
|
|
7
8
|
.join('&');
|
|
8
9
|
/**
|
|
@@ -17,7 +18,7 @@ const encodeParams = (params) => Object.entries(params)
|
|
|
17
18
|
* @returns A function to fetch data
|
|
18
19
|
*/
|
|
19
20
|
const fetcher = (options) => async (...args) => {
|
|
20
|
-
const { url, query, params, payload, headers, interceptRequest = _1.identityFn, interceptResponse, ...rest } = (0, _1.
|
|
21
|
+
const { url, query, params, payload, headers, interceptRequest = _1.identityFn, interceptResponse, ...rest } = (0, _1.getValue)(options, ...args);
|
|
21
22
|
let autoOptions = {};
|
|
22
23
|
let searchParams = params;
|
|
23
24
|
if (query) {
|
package/lib/utils/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export declare const noop: () => void;
|
|
|
2
2
|
export declare const identityFn: <T>(value: T) => T;
|
|
3
3
|
export declare const hasValue: (value: any) => boolean;
|
|
4
4
|
export declare const hashStoreKey: (obj?: any) => string;
|
|
5
|
-
export declare const
|
|
5
|
+
export declare const getValue: <T, P extends any[]>(valueOrComputeValueFn: T | ((...params: P) => T), ...params: P) => T;
|
|
6
6
|
export type Maybe<T> = T | null | undefined;
|
|
7
7
|
export declare const isClient: boolean;
|
|
8
8
|
export declare const createError: (message: string, props: Record<string, any>) => Error & Record<string, any>;
|
package/lib/utils/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createError = exports.isClient = exports.
|
|
3
|
+
exports.createError = exports.isClient = exports.getValue = exports.hashStoreKey = exports.hasValue = exports.identityFn = exports.noop = void 0;
|
|
4
4
|
const noop = () => { };
|
|
5
5
|
exports.noop = noop;
|
|
6
6
|
const identityFn = (value) => value;
|
|
@@ -9,13 +9,13 @@ const hasValue = (value) => value !== undefined && value !== null;
|
|
|
9
9
|
exports.hasValue = hasValue;
|
|
10
10
|
const hashStoreKey = (obj) => JSON.stringify(obj, Object.keys(obj).sort());
|
|
11
11
|
exports.hashStoreKey = hashStoreKey;
|
|
12
|
-
const
|
|
12
|
+
const getValue = (valueOrComputeValueFn, ...params) => {
|
|
13
13
|
if (typeof valueOrComputeValueFn === 'function') {
|
|
14
14
|
return valueOrComputeValueFn(...params);
|
|
15
15
|
}
|
|
16
16
|
return valueOrComputeValueFn;
|
|
17
17
|
};
|
|
18
|
-
exports.
|
|
18
|
+
exports.getValue = getValue;
|
|
19
19
|
exports.isClient = typeof window !== 'undefined' && !('Deno' in window);
|
|
20
20
|
const createError = (message, props) => {
|
|
21
21
|
const error = Object.assign(new Error(message), props);
|
package/lib/vanilla.d.ts
CHANGED
|
@@ -2,10 +2,10 @@ export type StoreData = Record<string, any>;
|
|
|
2
2
|
export type SetStoreData<T> = Partial<T> | ((prevState: T) => Partial<T>);
|
|
3
3
|
export type SelectDeps<T> = ((state: T) => any[]) | undefined | null;
|
|
4
4
|
export type Subscribers<T> = Map<(state: T) => void, SelectDeps<T>>;
|
|
5
|
-
export type StoreInitializer<T> = (api: {
|
|
5
|
+
export type StoreInitializer<T> = T | ((api: {
|
|
6
6
|
get: () => T;
|
|
7
7
|
set: (value: SetStoreData<T>, silent?: boolean) => void;
|
|
8
|
-
}) => T;
|
|
8
|
+
}) => T);
|
|
9
9
|
export type StoreEvent<T> = (state: T) => void;
|
|
10
10
|
export type InitStoreOptions<T> = {
|
|
11
11
|
intercept?: (nextState: T, prevState: T) => Partial<T>;
|
package/lib/vanilla.js
CHANGED
|
@@ -10,7 +10,7 @@ const initStore = (initializer, options = {}) => {
|
|
|
10
10
|
const get = () => data;
|
|
11
11
|
const set = (value, silent = false) => {
|
|
12
12
|
const prevData = data;
|
|
13
|
-
data = { ...data, ...(0, utils_1.
|
|
13
|
+
data = { ...data, ...(0, utils_1.getValue)(value, data) };
|
|
14
14
|
if (intercept) {
|
|
15
15
|
data = { ...data, ...intercept(data, prevData) };
|
|
16
16
|
}
|
|
@@ -49,7 +49,7 @@ const initStore = (initializer, options = {}) => {
|
|
|
49
49
|
onLastUnsubscribe(data);
|
|
50
50
|
};
|
|
51
51
|
};
|
|
52
|
-
data = initializer
|
|
52
|
+
data = (0, utils_1.getValue)(initializer, { get, set });
|
|
53
53
|
return { get, set, subscribe, getSubscribers };
|
|
54
54
|
};
|
|
55
55
|
exports.initStore = initStore;
|