floppy-disk 2.16.0-alpha.1 → 2.16.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.
@@ -70,6 +70,7 @@ export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends Stor
70
70
  defaultDeps?: SelectDeps<T>;
71
71
  hashKeyFn?: (obj: TKey) => string;
72
72
  };
73
+ export declare const hashStoreKey: (value?: any) => string;
73
74
  /**
74
75
  * @see https://floppy-disk.vercel.app/docs/api#createstores
75
76
  */
@@ -1,6 +1,32 @@
1
1
  import { useEffect, useMemo, useRef, useState } from 'react';
2
- import { hashStoreKey, initStore, } from '../store';
2
+ import { initStore, } from '../store';
3
3
  import { getValue, noop } from '../utils';
4
+ const hasObjectPrototype = (value) => {
5
+ return Object.prototype.toString.call(value) === '[object Object]';
6
+ };
7
+ const isPlainObject = (value) => {
8
+ if (!hasObjectPrototype(value))
9
+ return false;
10
+ const ctor = value.constructor;
11
+ if (typeof ctor === 'undefined')
12
+ return true;
13
+ const prot = ctor.prototype;
14
+ if (!hasObjectPrototype(prot))
15
+ return false;
16
+ if (!prot.hasOwnProperty('isPrototypeOf'))
17
+ return false;
18
+ return true;
19
+ };
20
+ export const hashStoreKey = (value) =>
21
+ // Copied from: https://github.com/TanStack/query/blob/main/packages/query-core/src/utils.ts
22
+ JSON.stringify(value, (_, val) => isPlainObject(val)
23
+ ? Object.keys(val)
24
+ .sort()
25
+ .reduce((result, key) => {
26
+ result[key] = val[key];
27
+ return result;
28
+ }, {})
29
+ : val);
4
30
  /**
5
31
  * @see https://floppy-disk.vercel.app/docs/api#createstores
6
32
  */
package/esm/store.d.ts CHANGED
@@ -9,7 +9,7 @@ export type StoreInitializer<T> = T | ((api: {
9
9
  }) => T);
10
10
  export type StoreEvent<T> = (state: T) => void;
11
11
  export type InitStoreOptions<T> = {
12
- intercept?: (nextState: T, prevState: T) => Maybe<Partial<T>>;
12
+ intercept?: (nextState: T, prevState: T) => void | Maybe<Partial<T>>;
13
13
  onFirstSubscribe?: StoreEvent<T>;
14
14
  onSubscribe?: StoreEvent<T>;
15
15
  onUnsubscribe?: StoreEvent<T>;
@@ -22,4 +22,3 @@ export type InitStoreReturn<T> = {
22
22
  getSubscribers: () => Subscribers<T>;
23
23
  };
24
24
  export declare const initStore: <T extends StoreData>(initializer: StoreInitializer<T>, options?: InitStoreOptions<T>) => InitStoreReturn<T>;
25
- export declare const hashStoreKey: (obj?: any) => string;
package/esm/store.js CHANGED
@@ -49,4 +49,3 @@ export const initStore = (initializer, options = {}) => {
49
49
  data = getValue(initializer, { get, set });
50
50
  return { get, set, subscribe, getSubscribers };
51
51
  };
52
- export const hashStoreKey = (obj) => JSON.stringify(obj, Object.keys(obj).sort());
@@ -70,6 +70,7 @@ export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends Stor
70
70
  defaultDeps?: SelectDeps<T>;
71
71
  hashKeyFn?: (obj: TKey) => string;
72
72
  };
73
+ export declare const hashStoreKey: (value?: any) => string;
73
74
  /**
74
75
  * @see https://floppy-disk.vercel.app/docs/api#createstores
75
76
  */
@@ -1,14 +1,41 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createStores = void 0;
3
+ exports.createStores = exports.hashStoreKey = void 0;
4
4
  const react_1 = require("react");
5
5
  const store_1 = require("../store");
6
6
  const utils_1 = require("../utils");
7
+ const hasObjectPrototype = (value) => {
8
+ return Object.prototype.toString.call(value) === '[object Object]';
9
+ };
10
+ const isPlainObject = (value) => {
11
+ if (!hasObjectPrototype(value))
12
+ return false;
13
+ const ctor = value.constructor;
14
+ if (typeof ctor === 'undefined')
15
+ return true;
16
+ const prot = ctor.prototype;
17
+ if (!hasObjectPrototype(prot))
18
+ return false;
19
+ if (!prot.hasOwnProperty('isPrototypeOf'))
20
+ return false;
21
+ return true;
22
+ };
23
+ const hashStoreKey = (value) =>
24
+ // Copied from: https://github.com/TanStack/query/blob/main/packages/query-core/src/utils.ts
25
+ JSON.stringify(value, (_, val) => isPlainObject(val)
26
+ ? Object.keys(val)
27
+ .sort()
28
+ .reduce((result, key) => {
29
+ result[key] = val[key];
30
+ return result;
31
+ }, {})
32
+ : val);
33
+ exports.hashStoreKey = hashStoreKey;
7
34
  /**
8
35
  * @see https://floppy-disk.vercel.app/docs/api#createstores
9
36
  */
10
37
  const createStores = (initializer, options = {}) => {
11
- const { onBeforeChangeKey = utils_1.noop, onStoreInitialized = utils_1.noop, defaultDeps, hashKeyFn = store_1.hashStoreKey, } = options;
38
+ const { onBeforeChangeKey = utils_1.noop, onStoreInitialized = utils_1.noop, defaultDeps, hashKeyFn = exports.hashStoreKey, } = options;
12
39
  const stores = new Map();
13
40
  const getStore = (_key) => {
14
41
  const key = _key || {};
package/lib/store.d.ts CHANGED
@@ -9,7 +9,7 @@ export type StoreInitializer<T> = T | ((api: {
9
9
  }) => T);
10
10
  export type StoreEvent<T> = (state: T) => void;
11
11
  export type InitStoreOptions<T> = {
12
- intercept?: (nextState: T, prevState: T) => Maybe<Partial<T>>;
12
+ intercept?: (nextState: T, prevState: T) => void | Maybe<Partial<T>>;
13
13
  onFirstSubscribe?: StoreEvent<T>;
14
14
  onSubscribe?: StoreEvent<T>;
15
15
  onUnsubscribe?: StoreEvent<T>;
@@ -22,4 +22,3 @@ export type InitStoreReturn<T> = {
22
22
  getSubscribers: () => Subscribers<T>;
23
23
  };
24
24
  export declare const initStore: <T extends StoreData>(initializer: StoreInitializer<T>, options?: InitStoreOptions<T>) => InitStoreReturn<T>;
25
- export declare const hashStoreKey: (obj?: any) => string;
package/lib/store.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hashStoreKey = exports.initStore = void 0;
3
+ exports.initStore = void 0;
4
4
  const utils_1 = require("./utils");
5
5
  const initStore = (initializer, options = {}) => {
6
6
  const { intercept, onFirstSubscribe = utils_1.noop, onSubscribe = utils_1.noop, onUnsubscribe = utils_1.noop, onLastUnsubscribe = utils_1.noop, } = options;
@@ -53,5 +53,3 @@ const initStore = (initializer, options = {}) => {
53
53
  return { get, set, subscribe, getSubscribers };
54
54
  };
55
55
  exports.initStore = initStore;
56
- const hashStoreKey = (obj) => JSON.stringify(obj, Object.keys(obj).sort());
57
- exports.hashStoreKey = hashStoreKey;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floppy-disk",
3
- "version": "2.16.0-alpha.1",
3
+ "version": "2.16.0",
4
4
  "description": "FloppyDisk - lightweight, simple, and powerful state management library",
5
5
  "keywords": [
6
6
  "state",