floppy-disk 2.1.0-beta.1 → 2.1.0-beta.2

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.
@@ -1,5 +1,5 @@
1
1
  import { useState } from 'preact/hooks';
2
- import { identityFn, noop } from '../utils';
2
+ import { hasValue, identityFn, noop } from '../utils';
3
3
  import { createStores } from './create-stores';
4
4
  const getDecision = (value, param, { ifTrue, ifAlways }) => {
5
5
  if (value === true || (typeof value === 'function' && value(param) === true)) {
@@ -84,7 +84,7 @@ export const createQuery = (queryFn, options = {}) => {
84
84
  responseAllPages.push(response);
85
85
  const newPageParam = getNextPageParam(response, responseAllPages.length);
86
86
  newPageParams.push(newPageParam);
87
- if (newPageParam !== undefined && newPageParams.length < pageParams.length) {
87
+ if (hasValue(newPageParam) && newPageParams.length < pageParams.length) {
88
88
  pageParam = newPageParam;
89
89
  callQuery();
90
90
  return;
@@ -109,7 +109,7 @@ export const createQuery = (queryFn, options = {}) => {
109
109
  retryCount: 0,
110
110
  pageParam: newPageParam,
111
111
  pageParams: newPageParams,
112
- hasNextPage: newPageParam !== undefined,
112
+ hasNextPage: hasValue(newPageParam),
113
113
  });
114
114
  onSuccess(response, stateBeforeCallQuery);
115
115
  })
@@ -131,7 +131,7 @@ export const createQuery = (queryFn, options = {}) => {
131
131
  errorUpdatedAt,
132
132
  isGoingToRetry: shouldRetry,
133
133
  pageParam,
134
- hasNextPage: pageParam !== undefined,
134
+ hasNextPage: hasValue(pageParam),
135
135
  }
136
136
  : {
137
137
  isWaiting: false,
@@ -142,7 +142,7 @@ export const createQuery = (queryFn, options = {}) => {
142
142
  errorUpdatedAt,
143
143
  isGoingToRetry: shouldRetry,
144
144
  pageParam,
145
- hasNextPage: pageParam !== undefined,
145
+ hasNextPage: hasValue(pageParam),
146
146
  });
147
147
  if (shouldRetry) {
148
148
  retryTimeoutId.set(keyHash, window.setTimeout(() => {
@@ -184,7 +184,7 @@ export const createQuery = (queryFn, options = {}) => {
184
184
  data: select(response, { key, data }),
185
185
  pageParam: newPageParam,
186
186
  pageParams: pageParams.concat(newPageParam),
187
- hasNextPage: newPageParam !== undefined,
187
+ hasNextPage: hasValue(newPageParam),
188
188
  });
189
189
  })
190
190
  .catch((error) => {
@@ -287,7 +287,7 @@ export const createQuery = (queryFn, options = {}) => {
287
287
  data: select(response, { key: key, data: null }),
288
288
  pageParam: newPageParam,
289
289
  pageParams: [undefined, newPageParam],
290
- hasNextPage: newPageParam !== undefined,
290
+ hasNextPage: hasValue(newPageParam),
291
291
  });
292
292
  });
293
293
  };
@@ -39,6 +39,10 @@ export type UseStores<TKey extends StoreKey = StoreKey, T extends StoreData = St
39
39
  };
40
40
  export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = InitStoreOptions<T> & {
41
41
  onBeforeChangeKey?: (nextKey: TKey, prevKey: TKey) => void;
42
+ /**
43
+ * Will be triggered when a single store with a specific key was initialized.
44
+ */
45
+ onStoreInitialized?: (key: TKey, keyHash: string) => void;
42
46
  defaultDeps?: SelectDeps<T>;
43
47
  hashKeyFn?: (obj: TKey) => string;
44
48
  };
@@ -2,13 +2,14 @@ import { useEffect, useMemo, useRef, useState } from 'preact/hooks';
2
2
  import { hashStoreKey, noop } from '../utils';
3
3
  import { initStore, } from '../vanilla';
4
4
  export const createStores = (initializer, options = {}) => {
5
- const { onBeforeChangeKey = noop, defaultDeps, hashKeyFn = hashStoreKey } = options;
5
+ const { onBeforeChangeKey = noop, onStoreInitialized = noop, defaultDeps, hashKeyFn = hashStoreKey, } = options;
6
6
  const stores = new Map();
7
7
  const getStore = (_key) => {
8
8
  const key = _key || {};
9
9
  const keyHash = hashKeyFn(key);
10
10
  if (!stores.has(keyHash)) {
11
11
  stores.set(keyHash, initStore((api) => initializer({ key, keyHash, ...api }), options));
12
+ onStoreInitialized(key, keyHash);
12
13
  }
13
14
  return stores.get(keyHash);
14
15
  };
@@ -1,5 +1,5 @@
1
1
  import { useState } from 'react';
2
- import { identityFn, noop } from '../utils';
2
+ import { hasValue, identityFn, noop } from '../utils';
3
3
  import { createStores } from './create-stores';
4
4
  const getDecision = (value, param, { ifTrue, ifAlways }) => {
5
5
  if (value === true || (typeof value === 'function' && value(param) === true)) {
@@ -84,7 +84,7 @@ export const createQuery = (queryFn, options = {}) => {
84
84
  responseAllPages.push(response);
85
85
  const newPageParam = getNextPageParam(response, responseAllPages.length);
86
86
  newPageParams.push(newPageParam);
87
- if (newPageParam !== undefined && newPageParams.length < pageParams.length) {
87
+ if (hasValue(newPageParam) && newPageParams.length < pageParams.length) {
88
88
  pageParam = newPageParam;
89
89
  callQuery();
90
90
  return;
@@ -109,7 +109,7 @@ export const createQuery = (queryFn, options = {}) => {
109
109
  retryCount: 0,
110
110
  pageParam: newPageParam,
111
111
  pageParams: newPageParams,
112
- hasNextPage: newPageParam !== undefined,
112
+ hasNextPage: hasValue(newPageParam),
113
113
  });
114
114
  onSuccess(response, stateBeforeCallQuery);
115
115
  })
@@ -131,7 +131,7 @@ export const createQuery = (queryFn, options = {}) => {
131
131
  errorUpdatedAt,
132
132
  isGoingToRetry: shouldRetry,
133
133
  pageParam,
134
- hasNextPage: pageParam !== undefined,
134
+ hasNextPage: hasValue(pageParam),
135
135
  }
136
136
  : {
137
137
  isWaiting: false,
@@ -142,7 +142,7 @@ export const createQuery = (queryFn, options = {}) => {
142
142
  errorUpdatedAt,
143
143
  isGoingToRetry: shouldRetry,
144
144
  pageParam,
145
- hasNextPage: pageParam !== undefined,
145
+ hasNextPage: hasValue(pageParam),
146
146
  });
147
147
  if (shouldRetry) {
148
148
  retryTimeoutId.set(keyHash, window.setTimeout(() => {
@@ -184,7 +184,7 @@ export const createQuery = (queryFn, options = {}) => {
184
184
  data: select(response, { key, data }),
185
185
  pageParam: newPageParam,
186
186
  pageParams: pageParams.concat(newPageParam),
187
- hasNextPage: newPageParam !== undefined,
187
+ hasNextPage: hasValue(newPageParam),
188
188
  });
189
189
  })
190
190
  .catch((error) => {
@@ -287,7 +287,7 @@ export const createQuery = (queryFn, options = {}) => {
287
287
  data: select(response, { key: key, data: null }),
288
288
  pageParam: newPageParam,
289
289
  pageParams: [undefined, newPageParam],
290
- hasNextPage: newPageParam !== undefined,
290
+ hasNextPage: hasValue(newPageParam),
291
291
  });
292
292
  });
293
293
  };
@@ -39,6 +39,10 @@ export type UseStores<TKey extends StoreKey = StoreKey, T extends StoreData = St
39
39
  };
40
40
  export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = InitStoreOptions<T> & {
41
41
  onBeforeChangeKey?: (nextKey: TKey, prevKey: TKey) => void;
42
+ /**
43
+ * Will be triggered when a single store with a specific key was initialized.
44
+ */
45
+ onStoreInitialized?: (key: TKey, keyHash: string) => void;
42
46
  defaultDeps?: SelectDeps<T>;
43
47
  hashKeyFn?: (obj: TKey) => string;
44
48
  };
@@ -2,13 +2,14 @@ import { useEffect, useMemo, useRef, useState } from 'react';
2
2
  import { hashStoreKey, noop } from '../utils';
3
3
  import { initStore, } from '../vanilla';
4
4
  export const createStores = (initializer, options = {}) => {
5
- const { onBeforeChangeKey = noop, defaultDeps, hashKeyFn = hashStoreKey } = options;
5
+ const { onBeforeChangeKey = noop, onStoreInitialized = noop, defaultDeps, hashKeyFn = hashStoreKey, } = options;
6
6
  const stores = new Map();
7
7
  const getStore = (_key) => {
8
8
  const key = _key || {};
9
9
  const keyHash = hashKeyFn(key);
10
10
  if (!stores.has(keyHash)) {
11
11
  stores.set(keyHash, initStore((api) => initializer({ key, keyHash, ...api }), options));
12
+ onStoreInitialized(key, keyHash);
12
13
  }
13
14
  return stores.get(keyHash);
14
15
  };
@@ -1,3 +1,4 @@
1
1
  export declare const noop: () => void;
2
- export declare const identityFn: <T>(a: T) => T;
2
+ export declare const identityFn: <T>(value: T) => T;
3
+ export declare const hasValue: (value: any) => boolean;
3
4
  export declare const hashStoreKey: (obj?: any) => string;
@@ -1,3 +1,4 @@
1
1
  export const noop = () => { };
2
- export const identityFn = (a) => a;
2
+ export const identityFn = (value) => value;
3
+ export const hasValue = (value) => value !== undefined && value !== null;
3
4
  export const hashStoreKey = (obj) => JSON.stringify(obj, Object.keys(obj).sort());
@@ -87,7 +87,7 @@ const createQuery = (queryFn, options = {}) => {
87
87
  responseAllPages.push(response);
88
88
  const newPageParam = getNextPageParam(response, responseAllPages.length);
89
89
  newPageParams.push(newPageParam);
90
- if (newPageParam !== undefined && newPageParams.length < pageParams.length) {
90
+ if ((0, utils_1.hasValue)(newPageParam) && newPageParams.length < pageParams.length) {
91
91
  pageParam = newPageParam;
92
92
  callQuery();
93
93
  return;
@@ -112,7 +112,7 @@ const createQuery = (queryFn, options = {}) => {
112
112
  retryCount: 0,
113
113
  pageParam: newPageParam,
114
114
  pageParams: newPageParams,
115
- hasNextPage: newPageParam !== undefined,
115
+ hasNextPage: (0, utils_1.hasValue)(newPageParam),
116
116
  });
117
117
  onSuccess(response, stateBeforeCallQuery);
118
118
  })
@@ -134,7 +134,7 @@ const createQuery = (queryFn, options = {}) => {
134
134
  errorUpdatedAt,
135
135
  isGoingToRetry: shouldRetry,
136
136
  pageParam,
137
- hasNextPage: pageParam !== undefined,
137
+ hasNextPage: (0, utils_1.hasValue)(pageParam),
138
138
  }
139
139
  : {
140
140
  isWaiting: false,
@@ -145,7 +145,7 @@ const createQuery = (queryFn, options = {}) => {
145
145
  errorUpdatedAt,
146
146
  isGoingToRetry: shouldRetry,
147
147
  pageParam,
148
- hasNextPage: pageParam !== undefined,
148
+ hasNextPage: (0, utils_1.hasValue)(pageParam),
149
149
  });
150
150
  if (shouldRetry) {
151
151
  retryTimeoutId.set(keyHash, window.setTimeout(() => {
@@ -187,7 +187,7 @@ const createQuery = (queryFn, options = {}) => {
187
187
  data: select(response, { key, data }),
188
188
  pageParam: newPageParam,
189
189
  pageParams: pageParams.concat(newPageParam),
190
- hasNextPage: newPageParam !== undefined,
190
+ hasNextPage: (0, utils_1.hasValue)(newPageParam),
191
191
  });
192
192
  })
193
193
  .catch((error) => {
@@ -290,7 +290,7 @@ const createQuery = (queryFn, options = {}) => {
290
290
  data: select(response, { key: key, data: null }),
291
291
  pageParam: newPageParam,
292
292
  pageParams: [undefined, newPageParam],
293
- hasNextPage: newPageParam !== undefined,
293
+ hasNextPage: (0, utils_1.hasValue)(newPageParam),
294
294
  });
295
295
  });
296
296
  };
@@ -39,6 +39,10 @@ export type UseStores<TKey extends StoreKey = StoreKey, T extends StoreData = St
39
39
  };
40
40
  export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = InitStoreOptions<T> & {
41
41
  onBeforeChangeKey?: (nextKey: TKey, prevKey: TKey) => void;
42
+ /**
43
+ * Will be triggered when a single store with a specific key was initialized.
44
+ */
45
+ onStoreInitialized?: (key: TKey, keyHash: string) => void;
42
46
  defaultDeps?: SelectDeps<T>;
43
47
  hashKeyFn?: (obj: TKey) => string;
44
48
  };
@@ -5,13 +5,14 @@ const hooks_1 = require("preact/hooks");
5
5
  const utils_1 = require("../utils");
6
6
  const vanilla_1 = require("../vanilla");
7
7
  const createStores = (initializer, options = {}) => {
8
- const { onBeforeChangeKey = utils_1.noop, defaultDeps, hashKeyFn = utils_1.hashStoreKey } = options;
8
+ const { onBeforeChangeKey = utils_1.noop, onStoreInitialized = utils_1.noop, defaultDeps, hashKeyFn = utils_1.hashStoreKey, } = options;
9
9
  const stores = new Map();
10
10
  const getStore = (_key) => {
11
11
  const key = _key || {};
12
12
  const keyHash = hashKeyFn(key);
13
13
  if (!stores.has(keyHash)) {
14
14
  stores.set(keyHash, (0, vanilla_1.initStore)((api) => initializer({ key, keyHash, ...api }), options));
15
+ onStoreInitialized(key, keyHash);
15
16
  }
16
17
  return stores.get(keyHash);
17
18
  };
@@ -87,7 +87,7 @@ const createQuery = (queryFn, options = {}) => {
87
87
  responseAllPages.push(response);
88
88
  const newPageParam = getNextPageParam(response, responseAllPages.length);
89
89
  newPageParams.push(newPageParam);
90
- if (newPageParam !== undefined && newPageParams.length < pageParams.length) {
90
+ if ((0, utils_1.hasValue)(newPageParam) && newPageParams.length < pageParams.length) {
91
91
  pageParam = newPageParam;
92
92
  callQuery();
93
93
  return;
@@ -112,7 +112,7 @@ const createQuery = (queryFn, options = {}) => {
112
112
  retryCount: 0,
113
113
  pageParam: newPageParam,
114
114
  pageParams: newPageParams,
115
- hasNextPage: newPageParam !== undefined,
115
+ hasNextPage: (0, utils_1.hasValue)(newPageParam),
116
116
  });
117
117
  onSuccess(response, stateBeforeCallQuery);
118
118
  })
@@ -134,7 +134,7 @@ const createQuery = (queryFn, options = {}) => {
134
134
  errorUpdatedAt,
135
135
  isGoingToRetry: shouldRetry,
136
136
  pageParam,
137
- hasNextPage: pageParam !== undefined,
137
+ hasNextPage: (0, utils_1.hasValue)(pageParam),
138
138
  }
139
139
  : {
140
140
  isWaiting: false,
@@ -145,7 +145,7 @@ const createQuery = (queryFn, options = {}) => {
145
145
  errorUpdatedAt,
146
146
  isGoingToRetry: shouldRetry,
147
147
  pageParam,
148
- hasNextPage: pageParam !== undefined,
148
+ hasNextPage: (0, utils_1.hasValue)(pageParam),
149
149
  });
150
150
  if (shouldRetry) {
151
151
  retryTimeoutId.set(keyHash, window.setTimeout(() => {
@@ -187,7 +187,7 @@ const createQuery = (queryFn, options = {}) => {
187
187
  data: select(response, { key, data }),
188
188
  pageParam: newPageParam,
189
189
  pageParams: pageParams.concat(newPageParam),
190
- hasNextPage: newPageParam !== undefined,
190
+ hasNextPage: (0, utils_1.hasValue)(newPageParam),
191
191
  });
192
192
  })
193
193
  .catch((error) => {
@@ -290,7 +290,7 @@ const createQuery = (queryFn, options = {}) => {
290
290
  data: select(response, { key: key, data: null }),
291
291
  pageParam: newPageParam,
292
292
  pageParams: [undefined, newPageParam],
293
- hasNextPage: newPageParam !== undefined,
293
+ hasNextPage: (0, utils_1.hasValue)(newPageParam),
294
294
  });
295
295
  });
296
296
  };
@@ -39,6 +39,10 @@ export type UseStores<TKey extends StoreKey = StoreKey, T extends StoreData = St
39
39
  };
40
40
  export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = InitStoreOptions<T> & {
41
41
  onBeforeChangeKey?: (nextKey: TKey, prevKey: TKey) => void;
42
+ /**
43
+ * Will be triggered when a single store with a specific key was initialized.
44
+ */
45
+ onStoreInitialized?: (key: TKey, keyHash: string) => void;
42
46
  defaultDeps?: SelectDeps<T>;
43
47
  hashKeyFn?: (obj: TKey) => string;
44
48
  };
@@ -5,13 +5,14 @@ const react_1 = require("react");
5
5
  const utils_1 = require("../utils");
6
6
  const vanilla_1 = require("../vanilla");
7
7
  const createStores = (initializer, options = {}) => {
8
- const { onBeforeChangeKey = utils_1.noop, defaultDeps, hashKeyFn = utils_1.hashStoreKey } = options;
8
+ const { onBeforeChangeKey = utils_1.noop, onStoreInitialized = utils_1.noop, defaultDeps, hashKeyFn = utils_1.hashStoreKey, } = options;
9
9
  const stores = new Map();
10
10
  const getStore = (_key) => {
11
11
  const key = _key || {};
12
12
  const keyHash = hashKeyFn(key);
13
13
  if (!stores.has(keyHash)) {
14
14
  stores.set(keyHash, (0, vanilla_1.initStore)((api) => initializer({ key, keyHash, ...api }), options));
15
+ onStoreInitialized(key, keyHash);
15
16
  }
16
17
  return stores.get(keyHash);
17
18
  };
@@ -1,3 +1,4 @@
1
1
  export declare const noop: () => void;
2
- export declare const identityFn: <T>(a: T) => T;
2
+ export declare const identityFn: <T>(value: T) => T;
3
+ export declare const hasValue: (value: any) => boolean;
3
4
  export declare const hashStoreKey: (obj?: any) => string;
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hashStoreKey = exports.identityFn = exports.noop = void 0;
3
+ exports.hashStoreKey = exports.hasValue = exports.identityFn = exports.noop = void 0;
4
4
  const noop = () => { };
5
5
  exports.noop = noop;
6
- const identityFn = (a) => a;
6
+ const identityFn = (value) => value;
7
7
  exports.identityFn = identityFn;
8
+ const hasValue = (value) => value !== undefined && value !== null;
9
+ exports.hasValue = hasValue;
8
10
  const hashStoreKey = (obj) => JSON.stringify(obj, Object.keys(obj).sort());
9
11
  exports.hashStoreKey = hashStoreKey;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floppy-disk",
3
- "version": "2.1.0-beta.1",
3
+ "version": "2.1.0-beta.2",
4
4
  "description": "FloppyDisk - lightweight, simple, and powerful state management library",
5
5
  "keywords": [
6
6
  "state",