@webkrafters/react-observable-context 4.7.7 → 5.0.0-rc.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.
Files changed (54) hide show
  1. package/dist/constants.d.ts +2 -9
  2. package/dist/constants.js +64 -19
  3. package/dist/index.d.ts +90 -0
  4. package/dist/index.js +98 -0
  5. package/dist/main/hooks/use-prehooks-ref/index.d.ts +3 -3
  6. package/dist/main/hooks/use-prehooks-ref/index.js +8 -8
  7. package/dist/main/hooks/use-prehooks-ref/index.test.d.ts +1 -0
  8. package/dist/main/hooks/use-prehooks-ref/index.test.js +21 -0
  9. package/dist/main/hooks/use-render-key-provider/index.d.ts +4 -5
  10. package/dist/main/hooks/use-render-key-provider/index.js +23 -11
  11. package/dist/main/hooks/use-render-key-provider/index.test.d.ts +1 -0
  12. package/dist/main/hooks/use-render-key-provider/index.test.js +72 -0
  13. package/dist/main/hooks/use-store/index.d.ts +8 -21
  14. package/dist/main/hooks/use-store/index.js +113 -93
  15. package/dist/main/hooks/use-store/index.test.d.ts +1 -0
  16. package/dist/main/hooks/use-store/index.test.js +456 -0
  17. package/dist/main/index.d.ts +11 -140
  18. package/dist/main/index.js +301 -159
  19. package/dist/main/index.test.d.ts +11 -0
  20. package/dist/main/index.test.js +1298 -0
  21. package/dist/main/test-apps/normal.d.ts +36 -0
  22. package/dist/main/test-apps/normal.js +243 -0
  23. package/dist/main/test-apps/with-connected-children.d.ts +25 -0
  24. package/dist/main/test-apps/with-connected-children.js +229 -0
  25. package/dist/main/test-apps/with-pure-children.d.ts +15 -0
  26. package/dist/main/test-apps/with-pure-children.js +127 -0
  27. package/dist/model/storage/index.d.ts +7 -7
  28. package/dist/model/storage/index.js +55 -59
  29. package/dist/model/storage/index.test.d.ts +1 -0
  30. package/dist/model/storage/index.test.js +139 -0
  31. package/dist/test-artifacts/data/create-state-obj.d.ts +58 -0
  32. package/dist/test-artifacts/data/create-state-obj.js +95 -0
  33. package/dist/test-artifacts/suppress-render-compat.d.ts +1 -0
  34. package/dist/test-artifacts/suppress-render-compat.js +7 -0
  35. package/logo.png +0 -0
  36. package/package.json +31 -68
  37. package/dist/main/hooks/use-state-manager/index.d.ts +0 -12
  38. package/dist/main/hooks/use-state-manager/index.js +0 -42
  39. package/dist/main/set-state/index.d.ts +0 -31
  40. package/dist/main/set-state/index.js +0 -211
  41. package/dist/main/set-state/tag-functions/index.d.ts +0 -38
  42. package/dist/main/set-state/tag-functions/index.js +0 -360
  43. package/dist/model/accessor/index.d.ts +0 -23
  44. package/dist/model/accessor/index.js +0 -180
  45. package/dist/model/accessor-cache/index.d.ts +0 -12
  46. package/dist/model/accessor-cache/index.js +0 -170
  47. package/dist/model/atom/index.d.ts +0 -10
  48. package/dist/model/atom/index.js +0 -79
  49. package/dist/types.d.ts +0 -91
  50. package/dist/utils/clonedeep-eligibility-check.d.ts +0 -7
  51. package/dist/utils/clonedeep-eligibility-check.js +0 -52
  52. package/dist/utils/index.d.ts +0 -12
  53. package/dist/utils/index.js +0 -173
  54. package/index.js +0 -1
@@ -1,9 +1,2 @@
1
- export const CLEAR_TAG: "@@CLEAR";
2
- export const DELETE_TAG: "@@DELETE";
3
- export const FULL_STATE_SELECTOR: "@@STATE";
4
- export const MOVE_TAG: "@@MOVE";
5
- export const NULL_STATE_SELECTOR: "";
6
- export const PUSH_TAG: "@@PUSH";
7
- export const REPLACE_TAG: "@@REPLACE";
8
- export const SET_TAG: "@@SET";
9
- export const SPLICE_TAG: "@@SPLICE";
1
+ export { CLEAR_TAG, DELETE_TAG, GLOBAL_SELECTOR, MOVE_TAG, NULL_SELECTOR, PUSH_TAG, REPLACE_TAG, SET_TAG, SPLICE_TAG, Tag } from '@webkrafters/auto-immutable';
2
+ export declare const FULL_STATE_SELECTOR = "@@STATE";
package/dist/constants.js CHANGED
@@ -1,23 +1,68 @@
1
1
  "use strict";
2
+
2
3
  Object.defineProperty(exports, "__esModule", {
3
4
  value: true
4
5
  });
5
- exports.SPLICE_TAG = exports.SET_TAG = exports.REPLACE_TAG = exports.PUSH_TAG = exports.NULL_STATE_SELECTOR = exports.MOVE_TAG = exports.FULL_STATE_SELECTOR = exports.DELETE_TAG = exports.CLEAR_TAG = void 0;
6
- var CLEAR_TAG = '@@CLEAR';
7
- exports.CLEAR_TAG = CLEAR_TAG;
8
- var DELETE_TAG = '@@DELETE';
9
- exports.DELETE_TAG = DELETE_TAG;
10
- var FULL_STATE_SELECTOR = '@@STATE';
11
- exports.FULL_STATE_SELECTOR = FULL_STATE_SELECTOR;
12
- var MOVE_TAG = '@@MOVE';
13
- exports.MOVE_TAG = MOVE_TAG;
14
- var NULL_STATE_SELECTOR = '';
15
- exports.NULL_STATE_SELECTOR = NULL_STATE_SELECTOR;
16
- var PUSH_TAG = '@@PUSH';
17
- exports.PUSH_TAG = PUSH_TAG;
18
- var REPLACE_TAG = '@@REPLACE';
19
- exports.REPLACE_TAG = REPLACE_TAG;
20
- var SET_TAG = '@@SET';
21
- exports.SET_TAG = SET_TAG;
22
- var SPLICE_TAG = '@@SPLICE';
23
- exports.SPLICE_TAG = SPLICE_TAG;
6
+ exports.FULL_STATE_SELECTOR = exports.Tag = exports.SPLICE_TAG = exports.SET_TAG = exports.REPLACE_TAG = exports.PUSH_TAG = exports.NULL_SELECTOR = exports.MOVE_TAG = exports.GLOBAL_SELECTOR = exports.DELETE_TAG = exports.CLEAR_TAG = void 0;
7
+ var auto_immutable_1 = require("@webkrafters/auto-immutable");
8
+ Object.defineProperty(exports, "CLEAR_TAG", {
9
+ enumerable: true,
10
+ get: function get() {
11
+ return auto_immutable_1.CLEAR_TAG;
12
+ }
13
+ });
14
+ Object.defineProperty(exports, "DELETE_TAG", {
15
+ enumerable: true,
16
+ get: function get() {
17
+ return auto_immutable_1.DELETE_TAG;
18
+ }
19
+ });
20
+ Object.defineProperty(exports, "GLOBAL_SELECTOR", {
21
+ enumerable: true,
22
+ get: function get() {
23
+ return auto_immutable_1.GLOBAL_SELECTOR;
24
+ }
25
+ });
26
+ Object.defineProperty(exports, "MOVE_TAG", {
27
+ enumerable: true,
28
+ get: function get() {
29
+ return auto_immutable_1.MOVE_TAG;
30
+ }
31
+ });
32
+ Object.defineProperty(exports, "NULL_SELECTOR", {
33
+ enumerable: true,
34
+ get: function get() {
35
+ return auto_immutable_1.NULL_SELECTOR;
36
+ }
37
+ });
38
+ Object.defineProperty(exports, "PUSH_TAG", {
39
+ enumerable: true,
40
+ get: function get() {
41
+ return auto_immutable_1.PUSH_TAG;
42
+ }
43
+ });
44
+ Object.defineProperty(exports, "REPLACE_TAG", {
45
+ enumerable: true,
46
+ get: function get() {
47
+ return auto_immutable_1.REPLACE_TAG;
48
+ }
49
+ });
50
+ Object.defineProperty(exports, "SET_TAG", {
51
+ enumerable: true,
52
+ get: function get() {
53
+ return auto_immutable_1.SET_TAG;
54
+ }
55
+ });
56
+ Object.defineProperty(exports, "SPLICE_TAG", {
57
+ enumerable: true,
58
+ get: function get() {
59
+ return auto_immutable_1.SPLICE_TAG;
60
+ }
61
+ });
62
+ Object.defineProperty(exports, "Tag", {
63
+ enumerable: true,
64
+ get: function get() {
65
+ return auto_immutable_1.Tag;
66
+ }
67
+ });
68
+ exports.FULL_STATE_SELECTOR = '@@STATE';
@@ -0,0 +1,90 @@
1
+ import type { ComponentType, Context, ForwardRefExoticComponent, MemoExoticComponent, NamedExoticComponent, ReactNode, PropsWithoutRef, RefAttributes } from 'react';
2
+ import type { Changes as BaseChanges, Connection, Immutable, Value } from '@webkrafters/auto-immutable';
3
+ import { FULL_STATE_SELECTOR } from './constants';
4
+ export type { BaseType, ClearCommand, KeyType, MoveCommand, PushCommand, ReplaceCommand, SetCommand, SpliceCommand, TagCommand, TagType, UpdateStats, UpdatePayload, UpdatePayloadArray } from '@webkrafters/auto-immutable';
5
+ export type State = Value;
6
+ export type ObservableContext<T extends State> = IObservableContext<T> | PublicObservableContext<T>;
7
+ export type PublicObservableContext<T extends State> = WithObservableProvider<Context<Store<T>>, T>;
8
+ export type IObservableContext<T extends State> = WithObservableProvider<Context<IStore>, T>;
9
+ export type WithObservableProvider<LOCAL_DATA extends Record<any, any> = {}, T extends State = State> = LOCAL_DATA & {
10
+ Provider: ObservableProvider<T>;
11
+ };
12
+ export type ObservableProvider<T extends State> = ForwardRefExoticComponent<ProviderProps<T> & RefAttributes<StoreRef<T>>>;
13
+ export interface ProviderProps<T extends State> {
14
+ children?: ReactNode;
15
+ prehooks?: Prehooks<T>;
16
+ storage?: IStorage<T>;
17
+ value: PartialState<T>;
18
+ }
19
+ export type ConnectProps<OWNPROPS extends OwnProps = IProps, STATE extends State = State, SELECTOR_MAP extends SelectorMap = SelectorMap> = {
20
+ [K in keyof Store<STATE, SELECTOR_MAP>]: Store<STATE, SELECTOR_MAP>[K];
21
+ } & Omit<OWNPROPS, "ref"> & RefAttributes<OWNPROPS["ref"]>;
22
+ export type ConnectedComponent<P extends OwnProps = IProps> = MemoExoticComponent<ForwardRefExoticComponent<PropsWithoutRef<Omit<P, "ref">> & RefAttributes<P["ref"]>>>;
23
+ export type PropsExtract<C, STATE extends State, SELECTOR_MAP extends SelectorMap> = C extends ComponentType<ConnectProps<infer U, STATE, SELECTOR_MAP>> ? U extends OwnProps ? U : IProps : C extends NamedExoticComponent<ConnectProps<infer U, STATE, SELECTOR_MAP>> ? U extends OwnProps ? U : IProps : IProps;
24
+ export type InjectedProps<P extends IProps = IProps> = {
25
+ [K in keyof P]: P[K];
26
+ };
27
+ export interface IProps {
28
+ ref?: unknown;
29
+ }
30
+ export type OwnProps = IProps & Record<any, any>;
31
+ export type Text = string | number;
32
+ export type FullStateSelector = typeof FULL_STATE_SELECTOR;
33
+ export type ObjectSelector = Record<Text, Text | FullStateSelector>;
34
+ export type ArraySelector = Array<Text | FullStateSelector>;
35
+ export type SelectorMap = ObjectSelector | ArraySelector | void;
36
+ export type Data<SELECTOR_MAP extends SelectorMap> = (SELECTOR_MAP extends ObjectSelector ? {
37
+ [selectorKey in keyof SELECTOR_MAP]: Readonly<any>;
38
+ } : SELECTOR_MAP extends ArraySelector ? {
39
+ [selectorKey: number]: Readonly<any>;
40
+ } : never);
41
+ export type Changes<T extends State = State> = BaseChanges<T>;
42
+ export interface IStorage<T extends State = State> {
43
+ clone: (data: T) => T;
44
+ getItem: (key: string) => T;
45
+ removeItem: (key: string) => void;
46
+ setItem: (key: string, data: T) => void;
47
+ }
48
+ export type NonReactUsageReport = (...args: Array<unknown>) => void;
49
+ export type Listener = <T extends State>(changes: Changes<T>) => void;
50
+ export type PartialState<T extends State> = Partial<T>;
51
+ export interface Prehooks<T extends State = State> {
52
+ resetState?: (resetData: PartialState<T>, state: {
53
+ current: T;
54
+ original: T;
55
+ }) => boolean;
56
+ setState?: (newChanges: Changes<T>) => boolean;
57
+ }
58
+ export type Unsubscribe = (...args: Array<unknown>) => void;
59
+ export interface IStore {
60
+ resetState: Function;
61
+ setState: Function;
62
+ }
63
+ export interface IStoreInternal extends IStore {
64
+ subscribe: Function;
65
+ }
66
+ export interface Store<T extends State, SELECTOR_MAP extends SelectorMap = SelectorMap> extends IStore {
67
+ data: Data<SELECTOR_MAP>;
68
+ resetState: (propertyPaths?: Array<string>) => void;
69
+ setState: (changes: Changes<T>) => void;
70
+ }
71
+ export interface StoreInternal<T extends State> extends IStoreInternal {
72
+ cache: Immutable<T>;
73
+ resetState: (connection: Connection<T>, propertyPaths?: Array<string>) => void;
74
+ setState: (connection: Connection<T>, changes: Changes<T>) => void;
75
+ subscribe: (listener: Listener) => Unsubscribe;
76
+ }
77
+ export interface StorePlaceholder extends IStoreInternal {
78
+ getState: NonReactUsageReport;
79
+ resetState: NonReactUsageReport;
80
+ setState: NonReactUsageReport;
81
+ subscribe: NonReactUsageReport;
82
+ }
83
+ export interface StoreRef<T extends State = State> extends StorePlaceholder {
84
+ getState: () => T;
85
+ resetState: (propertyPaths?: string[]) => void;
86
+ setState: (changes: Changes<T>) => void;
87
+ subscribe: (listener: Listener) => Unsubscribe;
88
+ }
89
+ export { CLEAR_TAG, DELETE_TAG, FULL_STATE_SELECTOR, MOVE_TAG, NULL_SELECTOR, PUSH_TAG, REPLACE_TAG, SET_TAG, SPLICE_TAG, Tag, } from './constants';
90
+ export { connect, createContext, UsageError, useContext } from './main';
package/dist/index.js ADDED
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useContext = exports.UsageError = exports.createContext = exports.connect = exports.Tag = exports.SPLICE_TAG = exports.SET_TAG = exports.REPLACE_TAG = exports.PUSH_TAG = exports.NULL_SELECTOR = exports.MOVE_TAG = exports.FULL_STATE_SELECTOR = exports.DELETE_TAG = exports.CLEAR_TAG = void 0;
7
+ ;
8
+ ;
9
+ ;
10
+ ;
11
+ ;
12
+ ;
13
+ var constants_1 = require("./constants");
14
+ Object.defineProperty(exports, "CLEAR_TAG", {
15
+ enumerable: true,
16
+ get: function get() {
17
+ return constants_1.CLEAR_TAG;
18
+ }
19
+ });
20
+ Object.defineProperty(exports, "DELETE_TAG", {
21
+ enumerable: true,
22
+ get: function get() {
23
+ return constants_1.DELETE_TAG;
24
+ }
25
+ });
26
+ Object.defineProperty(exports, "FULL_STATE_SELECTOR", {
27
+ enumerable: true,
28
+ get: function get() {
29
+ return constants_1.FULL_STATE_SELECTOR;
30
+ }
31
+ });
32
+ Object.defineProperty(exports, "MOVE_TAG", {
33
+ enumerable: true,
34
+ get: function get() {
35
+ return constants_1.MOVE_TAG;
36
+ }
37
+ });
38
+ Object.defineProperty(exports, "NULL_SELECTOR", {
39
+ enumerable: true,
40
+ get: function get() {
41
+ return constants_1.NULL_SELECTOR;
42
+ }
43
+ });
44
+ Object.defineProperty(exports, "PUSH_TAG", {
45
+ enumerable: true,
46
+ get: function get() {
47
+ return constants_1.PUSH_TAG;
48
+ }
49
+ });
50
+ Object.defineProperty(exports, "REPLACE_TAG", {
51
+ enumerable: true,
52
+ get: function get() {
53
+ return constants_1.REPLACE_TAG;
54
+ }
55
+ });
56
+ Object.defineProperty(exports, "SET_TAG", {
57
+ enumerable: true,
58
+ get: function get() {
59
+ return constants_1.SET_TAG;
60
+ }
61
+ });
62
+ Object.defineProperty(exports, "SPLICE_TAG", {
63
+ enumerable: true,
64
+ get: function get() {
65
+ return constants_1.SPLICE_TAG;
66
+ }
67
+ });
68
+ Object.defineProperty(exports, "Tag", {
69
+ enumerable: true,
70
+ get: function get() {
71
+ return constants_1.Tag;
72
+ }
73
+ });
74
+ var main_1 = require("./main");
75
+ Object.defineProperty(exports, "connect", {
76
+ enumerable: true,
77
+ get: function get() {
78
+ return main_1.connect;
79
+ }
80
+ });
81
+ Object.defineProperty(exports, "createContext", {
82
+ enumerable: true,
83
+ get: function get() {
84
+ return main_1.createContext;
85
+ }
86
+ });
87
+ Object.defineProperty(exports, "UsageError", {
88
+ enumerable: true,
89
+ get: function get() {
90
+ return main_1.UsageError;
91
+ }
92
+ });
93
+ Object.defineProperty(exports, "useContext", {
94
+ enumerable: true,
95
+ get: function get() {
96
+ return main_1.useContext;
97
+ }
98
+ });
@@ -1,4 +1,4 @@
1
+ /// <reference types="react" />
2
+ import type { Prehooks, State } from '../../..';
3
+ declare function usePrehooksRef<T extends State>(prehooks: Prehooks<T>): import("react").MutableRefObject<Prehooks<T>>;
1
4
  export default usePrehooksRef;
2
- export type Prehooks<T extends import("../../../types").State> = import("../../../types").Prehooks<T>;
3
- export type State = import("../../../types").State;
4
- declare function usePrehooksRef<T extends import("../../../types").State>(prehooks: Prehooks<T>): import("react").MutableRefObject<Prehooks<T>>;
@@ -1,15 +1,15 @@
1
1
  "use strict";
2
+
2
3
  Object.defineProperty(exports, "__esModule", {
3
4
  value: true
4
5
  });
5
- exports["default"] = void 0;
6
- var _react = require("react");
7
- var usePrehooksRef = function usePrehooksRef(prehooks) {
8
- var prehooksRef = (0, _react.useRef)(prehooks);
9
- (0, _react.useEffect)(function () {
6
+ var react_1 = require("react");
7
+ function usePrehooksRef(prehooks) {
8
+ var prehooksRef = (0, react_1.useRef)(prehooks);
9
+ (0, react_1.useEffect)(function () {
10
10
  prehooksRef.current = prehooks;
11
11
  }, [prehooks]);
12
12
  return prehooksRef;
13
- };
14
- var _default = usePrehooksRef;
15
- exports["default"] = _default;
13
+ }
14
+ ;
15
+ exports["default"] = usePrehooksRef;
@@ -0,0 +1 @@
1
+ import '../../../test-artifacts/suppress-render-compat';
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const react_1 = require("@testing-library/react");
7
+ require("../../../test-artifacts/suppress-render-compat");
8
+ const _1 = __importDefault(require("."));
9
+ describe('usePrehooksRef', () => {
10
+ test('retains prehooks prop in a `react-ref` object', () => {
11
+ const PREHOOK = 'PREHOOK_STUB';
12
+ const { result, rerender } = (0, react_1.renderHook)(_1.default, {
13
+ initialProps: PREHOOK
14
+ });
15
+ expect(result.current).toEqual({ current: PREHOOK });
16
+ const PREHOOK2 = 'PREHOOK2_STUB';
17
+ rerender(PREHOOK2);
18
+ expect(result.current).not.toEqual({ current: PREHOOK });
19
+ expect(result.current).toEqual({ current: PREHOOK2 });
20
+ });
21
+ });
@@ -1,6 +1,5 @@
1
+ import type { ArraySelector, ObjectSelector, SelectorMap, Text } from '../../..';
2
+ declare function useRenderKeyProvider(selectorMap: ArraySelector): Array<Text>;
3
+ declare function useRenderKeyProvider(selectorMap: ObjectSelector): Array<Text>;
4
+ declare function useRenderKeyProvider(selectorMap: SelectorMap): Array<Text>;
1
5
  export default useRenderKeyProvider;
2
- export type Provider<T extends import("../../../types").State, MAP extends BaseSelectorMap<T> = BaseSelectorMap<T>> = (selectorMap: SelectorMap<T, MAP>) => [string | keyof T];
3
- export type SelectorMap<STATE extends import("../../../types").State = import("../../../types").State, MAP extends BaseSelectorMap<STATE> = BaseSelectorMap<STATE>> = import("../../../types").SelectorMap<STATE>;
4
- export type BaseSelectorMap<STATE extends import("../../../types").State = import("../../../types").State> = import("../../../types").BaseSelectorMap<STATE>;
5
- export type State = import("../../../types").State;
6
- declare const useRenderKeyProvider: Provider<T, BaseSelectorMap<T>>;
@@ -1,15 +1,27 @@
1
1
  "use strict";
2
+
3
+ var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
4
+ return mod && mod.__esModule ? mod : {
5
+ "default": mod
6
+ };
7
+ };
2
8
  Object.defineProperty(exports, "__esModule", {
3
9
  value: true
4
10
  });
5
- exports["default"] = void 0;
6
- var _react = require("react");
7
- var getCurrKeys = function getCurrKeys(selectorMap) {
8
- var currKeys = Object.values(selectorMap);
9
- return currKeys.length ? Array.from(new Set(currKeys)) : [];
10
- };
11
- var useRenderKeyProvider = function useRenderKeyProvider(selectorMap) {
12
- var renderKeys = (0, _react.useRef)([]);
11
+ var react_1 = require("react");
12
+ var lodash_isplainobject_1 = __importDefault(require("lodash.isplainobject"));
13
+ function getCurrKeys(selectorMap) {
14
+ if ((0, lodash_isplainobject_1["default"])(selectorMap) || Array.isArray(selectorMap)) {
15
+ return Array.from(new Set(Object.values(selectorMap)));
16
+ }
17
+ if (typeof selectorMap === 'undefined' || selectorMap === null) {
18
+ return [];
19
+ }
20
+ throw new TypeError('Incompatible Selector Map type provided.');
21
+ }
22
+ ;
23
+ function useRenderKeyProvider(selectorMap) {
24
+ var renderKeys = (0, react_1.useRef)([]);
13
25
  var currKeys = getCurrKeys(selectorMap);
14
26
  if (renderKeys.current.length !== currKeys.length || renderKeys.current.some(function (k, i) {
15
27
  return k !== currKeys[i];
@@ -17,6 +29,6 @@ var useRenderKeyProvider = function useRenderKeyProvider(selectorMap) {
17
29
  renderKeys.current = currKeys;
18
30
  }
19
31
  return renderKeys.current;
20
- };
21
- var _default = useRenderKeyProvider;
22
- exports["default"] = _default;
32
+ }
33
+ ;
34
+ exports["default"] = useRenderKeyProvider;
@@ -0,0 +1 @@
1
+ import '../../../test-artifacts/suppress-render-compat';
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const clone_total_1 = __importDefault(require("@webkrafters/clone-total"));
7
+ const _1 = __importDefault(require("."));
8
+ const react_1 = require("@testing-library/react");
9
+ require("../../../test-artifacts/suppress-render-compat");
10
+ describe('useRenderKeyProvider', () => {
11
+ let selectorMap;
12
+ beforeAll(() => { selectorMap = { _a: 'a', _b: 'b', _c: 'c' }; });
13
+ test('calculates new selectors for new selectorMap', () => {
14
+ const initialProps = (0, clone_total_1.default)(selectorMap);
15
+ const { result, rerender } = (0, react_1.renderHook)(_1.default, { initialProps });
16
+ const selectors = result.current;
17
+ const updateProps = { ...selectorMap, _y: 'y', _z: 'z' };
18
+ rerender(updateProps);
19
+ expect(result.current).toEqual(Object.values(updateProps));
20
+ expect(result.current).not.toEqual(selectors);
21
+ expect(result.current).not.toBe(selectors);
22
+ });
23
+ test('ensures no abrupt updates to selectors for new list with same selectorMap', () => {
24
+ const { result, rerender } = (0, react_1.renderHook)(_1.default, { initialProps: selectorMap });
25
+ const selectors = result.current;
26
+ rerender((0, clone_total_1.default)(selectorMap));
27
+ expect(result.current).toBe(selectors);
28
+ });
29
+ describe('productive call', () => {
30
+ let expected;
31
+ beforeAll(() => { expected = ['a', 'b', 'c']; });
32
+ test.each([
33
+ ['object', { _a: 'a', _b: 'b', _c: 'c' }],
34
+ ['array', ['a', 'b', 'c']]
35
+ ])('allows an %s type selectoMap', (label, value) => {
36
+ const { result } = (0, react_1.renderHook)(_1.default, { initialProps: value });
37
+ expect(result.current).toEqual(expected);
38
+ });
39
+ });
40
+ describe.each([
41
+ ['undefined', undefined],
42
+ ['null', null],
43
+ ['[]', []],
44
+ ['{}', {}]
45
+ ])('empty selectoMap = %s', (label, value) => {
46
+ test('returns empty renderKeys list', () => {
47
+ const { result } = (0, react_1.renderHook)(_1.default, { initialProps: value });
48
+ expect(result.current).toEqual([]);
49
+ });
50
+ });
51
+ describe.each([
52
+ ['string', 'abc'],
53
+ ['number', 22],
54
+ ['function', () => { }],
55
+ ['boolean', true],
56
+ ['Map', new Map()],
57
+ ['Set', new Set()],
58
+ ['Uint8Array', new Uint8Array()]
59
+ ])('incompatible selectorMap type = %s', (label, value) => {
60
+ test('throws Type error', () => {
61
+ try {
62
+ (0, react_1.renderHook)(_1.default, {
63
+ initialProps: value
64
+ });
65
+ }
66
+ catch (e) {
67
+ expect(e.constructor.name).toEqual('TypeError');
68
+ expect(e.message).toEqual('Incompatible Selector Map type provided.');
69
+ }
70
+ });
71
+ });
72
+ });
@@ -1,23 +1,10 @@
1
- export namespace deps {
2
- export { _setState as setState };
3
- export { v4 as uuid };
1
+ import type { IStorage, PartialState, Prehooks, State, StoreInternal } from '../../..';
2
+ export declare const deps: {
3
+ createStorageKey: () => string;
4
+ };
5
+ interface CurrentStorage<T extends State> extends IStorage<T> {
6
+ isKeyRequired?: boolean;
4
7
  }
8
+ /** @param storage - is Closed to modification post-initialization */
9
+ declare const useStore: <T extends import("@webkrafters/auto-immutable").Value>(prehooks: Prehooks<T>, value: PartialState<T>, storage?: CurrentStorage<T>) => StoreInternal<T>;
5
10
  export default useStore;
6
- export type IStorage<T extends import("../../../types").State> = import("../../../types").IStorage<T>;
7
- export type Changes<T extends import("../../../types").State> = import("../../../types").Changes<T>;
8
- export type PartialState<T extends import("../../../types").State> = import('../../../types').PartialState<T>;
9
- export type Prehooks<T extends import("../../../types").State> = import("../../../types").Prehooks<T>;
10
- export type StoreInternal<T extends import("../../../types").State> = import("../../../types").StoreInternal<T>;
11
- export type State = import("../../../types").State;
12
- export type MutableRefObject<T> = import('react').MutableRefObject<T>;
13
- import _setState from "../../set-state";
14
- declare function useStore<T extends import("../../../types").State>(prehooks: Prehooks<T>, value: T, storage?: IStorage<T>): {
15
- getState: (clientId: string, ...propertyPaths?: string[]) => {
16
- [propertyPaths: string]: Readonly<any>;
17
- };
18
- resetState: (propertyPaths?: string[]) => void;
19
- setState: (changes: import("../../../types").Changes<T>) => void;
20
- state: T;
21
- subscribe: (listener: import("../../../types").Listener<T>) => VoidFunction;
22
- unlinkCache: (clientId: string) => void;
23
- };