@elementor/store 0.8.3 → 0.8.5

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/CHANGELOG.md CHANGED
@@ -3,111 +3,72 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
- ## [0.8.3](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.8.2...@elementor/store@0.8.3) (2024-07-03)
7
-
8
- **Note:** Version bump only for package @elementor/store
9
-
10
-
6
+ ## [0.8.5](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.8.4...@elementor/store@0.8.5) (2024-08-05)
11
7
 
8
+ ### Bug Fixes
12
9
 
10
+ - publish only necessary files to npm ([#226](https://github.com/elementor/elementor-packages/issues/226)) ([d808e2f](https://github.com/elementor/elementor-packages/commit/d808e2f60eb7ca2d7b8560d0b79c0e62c2f969a8))
13
11
 
14
- ## [0.8.2](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.8.1...@elementor/store@0.8.2) (2024-04-16)
12
+ ## [0.8.4](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.8.3...@elementor/store@0.8.4) (2024-07-16)
15
13
 
16
14
  **Note:** Version bump only for package @elementor/store
17
15
 
16
+ ## [0.8.3](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.8.2...@elementor/store@0.8.3) (2024-07-03)
18
17
 
18
+ **Note:** Version bump only for package @elementor/store
19
19
 
20
+ ## [0.8.2](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.8.1...@elementor/store@0.8.2) (2024-04-16)
20
21
 
22
+ **Note:** Version bump only for package @elementor/store
21
23
 
22
24
  ## [0.8.1](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.8.0...@elementor/store@0.8.1) (2024-02-28)
23
25
 
24
-
25
26
  ### Bug Fixes
26
27
 
27
- * **store:** dispatch & instance type [ED-13867] ([#165](https://github.com/elementor/elementor-packages/issues/165)) ([52ce6d9](https://github.com/elementor/elementor-packages/commit/52ce6d9c9941fdb0591e3d990b69a39b4a81690e))
28
-
29
-
30
-
31
-
28
+ - **store:** dispatch & instance type [ED-13867] ([#165](https://github.com/elementor/elementor-packages/issues/165)) ([52ce6d9](https://github.com/elementor/elementor-packages/commit/52ce6d9c9941fdb0591e3d990b69a39b4a81690e))
32
29
 
33
30
  # [0.8.0](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.7.1...@elementor/store@0.8.0) (2024-02-22)
34
31
 
35
-
36
32
  ### Features
37
33
 
38
- * **store:** support createAsyncThunk [ED-13842] ([#164](https://github.com/elementor/elementor-packages/issues/164)) ([bd0ed1d](https://github.com/elementor/elementor-packages/commit/bd0ed1d11a8c6d284897f2d0e68beef07f5b5849))
39
-
40
-
41
-
42
-
34
+ - **store:** support createAsyncThunk [ED-13842] ([#164](https://github.com/elementor/elementor-packages/issues/164)) ([bd0ed1d](https://github.com/elementor/elementor-packages/commit/bd0ed1d11a8c6d284897f2d0e68beef07f5b5849))
43
35
 
44
36
  ## [0.7.1](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.7.0...@elementor/store@0.7.1) (2023-10-19)
45
37
 
46
38
  **Note:** Version bump only for package @elementor/store
47
39
 
48
-
49
-
50
-
51
-
52
40
  # [0.7.0](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.6.1...@elementor/store@0.7.0) (2023-09-11)
53
41
 
54
-
55
42
  ### Features
56
43
 
57
- * **store:** mark experimental functions [ED-12070] ([#114](https://github.com/elementor/elementor-packages/issues/114)) ([d790829](https://github.com/elementor/elementor-packages/commit/d79082911195c75f6d3a89a5619b234e2f5ce158))
58
-
59
-
60
-
61
-
44
+ - **store:** mark experimental functions [ED-12070] ([#114](https://github.com/elementor/elementor-packages/issues/114)) ([d790829](https://github.com/elementor/elementor-packages/commit/d79082911195c75f6d3a89a5619b234e2f5ce158))
62
45
 
63
46
  ## [0.6.1](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.6.0...@elementor/store@0.6.1) (2023-06-29)
64
47
 
65
48
  **Note:** Version bump only for package @elementor/store
66
49
 
67
-
68
-
69
-
70
-
71
50
  # [0.6.0](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.5.0...@elementor/store@0.6.0) (2023-06-11)
72
51
 
73
-
74
52
  ### Features
75
53
 
76
- * **editor-panels:** init [ED-10804] ([#44](https://github.com/elementor/elementor-packages/issues/44)) ([1ed4113](https://github.com/elementor/elementor-packages/commit/1ed41131db8fb9151163175bfa614f784159e04b))
77
-
78
-
79
-
80
-
54
+ - **editor-panels:** init [ED-10804] ([#44](https://github.com/elementor/elementor-packages/issues/44)) ([1ed4113](https://github.com/elementor/elementor-packages/commit/1ed41131db8fb9151163175bfa614f784159e04b))
81
55
 
82
56
  # [0.5.0](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.4.0...@elementor/store@0.5.0) (2023-06-06)
83
57
 
84
-
85
58
  ### Features
86
59
 
87
- * drop support for React 17 [ED-10982] ([#50](https://github.com/elementor/elementor-packages/issues/50)) ([59c576c](https://github.com/elementor/elementor-packages/commit/59c576ca218947dc0992616311d4d399a20e91a6))
88
-
89
-
90
-
91
-
60
+ - drop support for React 17 [ED-10982] ([#50](https://github.com/elementor/elementor-packages/issues/50)) ([59c576c](https://github.com/elementor/elementor-packages/commit/59c576ca218947dc0992616311d4d399a20e91a6))
92
61
 
93
62
  # [0.4.0](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.3.0...@elementor/store@0.4.0) (2023-06-05)
94
63
 
95
-
96
64
  ### Features
97
65
 
98
- * **store:** support creating & registering a slice independently [ED-10914] ([#47](https://github.com/elementor/elementor-packages/issues/47)) ([3cac551](https://github.com/elementor/elementor-packages/commit/3cac551d72cc2df6e11c017323559e3e5c4ab12f))
99
-
100
-
101
-
102
-
66
+ - **store:** support creating & registering a slice independently [ED-10914] ([#47](https://github.com/elementor/elementor-packages/issues/47)) ([3cac551](https://github.com/elementor/elementor-packages/commit/3cac551d72cc2df6e11c017323559e3e5c4ab12f))
103
67
 
104
68
  # 0.3.0 (2023-06-01)
105
69
 
106
-
107
70
  ### Features
108
71
 
109
- * add support for React 18 [ED-10905] ([#46](https://github.com/elementor/elementor-packages/issues/46)) ([c8915c6](https://github.com/elementor/elementor-packages/commit/c8915c6ea62550bcdeb5c8a576f311bedc35bcad))
110
-
111
-
72
+ - add support for React 18 [ED-10905] ([#46](https://github.com/elementor/elementor-packages/issues/46)) ([c8915c6](https://github.com/elementor/elementor-packages/commit/c8915c6ea62550bcdeb5c8a576f311bedc35bcad))
112
73
 
113
74
  # 0.2.0 (2023-05-09)
package/dist/index.js CHANGED
@@ -86,10 +86,7 @@ var createStore = () => {
86
86
  instance = (0, import_toolkit.configureStore)({
87
87
  reducer: getReducers(),
88
88
  middleware: (getDefaultMiddleware) => {
89
- return [
90
- ...getDefaultMiddleware(),
91
- ...Array.from(middlewares)
92
- ];
89
+ return [...getDefaultMiddleware(), ...Array.from(middlewares)];
93
90
  }
94
91
  });
95
92
  if (pendingActions.length) {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n\tReducersMapObject,\n\tconfigureStore,\n\tcombineReducers,\n\tMiddleware,\n\tSlice,\n\tAnyAction,\n\tEnhancedStore,\n\tStoreEnhancer,\n\tThunkMiddleware,\n} from '@reduxjs/toolkit';\n\nexport type {\n\tSlice,\n\tCreateSliceOptions,\n\tPayloadAction,\n\tStore,\n\tDispatch,\n\tAnyAction,\n\tAction,\n\tMiddlewareAPI,\n\tMiddleware,\n} from '@reduxjs/toolkit';\n\nexport {\n\tcreateSelector as __createSelector,\n\tcreateSlice as __createSlice,\n\tcreateAsyncThunk as __createAsyncThunk,\n\tcreateAction as __createAction,\n} from '@reduxjs/toolkit';\n\nexport {\n\tuseSelector as __useSelector,\n\tuseDispatch as __useDispatch,\n\tProvider as __StoreProvider,\n} from 'react-redux';\n\n/**\n * Usage:\n *\n * const mySlice = addSlice( ... );\n *\n * type MySliceState = SliceState<typeof mySlice>;\n *\n * const value = useSelector( ( state: MySliceState ) => state.mySlice.value );\n */\nexport type SliceState<S extends Slice> = {\n\t[ key in S['name'] ]: ReturnType<S['getInitialState']>;\n}\n\n// The `configureStore` function from Redux Toolkit infers its actions from the `reducers`\n// key of the initialization object. This is fine when creating the store statically, but\n// breaks in our case since we create the store dynamically, which means that TypeScript\n// can't infer the types. Therefore, we force the store to accept any actions using a\n// generic store type.\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype AnyStore<State = any> = EnhancedStore<\n\tState,\n\tAnyAction,\n\t[ThunkMiddleware<State, AnyAction>],\n\t[StoreEnhancer]\n>\n\ninterface SlicesMap {\n\t[key: Slice['name']]: Slice;\n}\n\nlet instance: AnyStore | null = null;\nlet slices: SlicesMap = {};\nconst pendingActions: AnyAction[] = [];\nconst middlewares = new Set<Middleware>();\n\nconst getReducers = () => {\n\tconst reducers = Object.entries( slices ).reduce( ( reducersData: ReducersMapObject, [ name, slice ] ) => {\n\t\treducersData[ name ] = slice.reducer;\n\n\t\treturn reducersData;\n\t}, {} );\n\n\treturn combineReducers( reducers );\n};\n\nfunction registerSlice( slice: Slice ) {\n\tif ( slices[ slice.name ] ) {\n\t\tthrow new Error( `Slice with name \"${ slice.name }\" already exists.` );\n\t}\n\n\tslices[ slice.name ] = slice;\n}\n\nconst addMiddleware = ( middleware: Middleware ) => {\n\tmiddlewares.add( middleware );\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any -- See the comment above about `AnyStore`\nconst dispatch = ( action: any ) => {\n\tif ( ! instance ) {\n\t\tpendingActions.push( action );\n\n\t\treturn;\n\t}\n\n\treturn instance.dispatch( action );\n};\n\nconst getState = () => {\n\tif ( ! instance ) {\n\t\tthrow new Error( 'The store instance does not exist.' );\n\t}\n\n\treturn instance.getState();\n};\n\nconst subscribe = ( listener: () => void ) => {\n\tif ( ! instance ) {\n\t\tthrow new Error( 'The store instance does not exist.' );\n\t}\n\n\treturn instance.subscribe( listener );\n};\n\nconst createStore = () => {\n\tif ( instance ) {\n\t\tthrow new Error( 'The store instance already exists.' );\n\t}\n\n\tinstance = configureStore( {\n\t\treducer: getReducers(),\n\t\tmiddleware: ( getDefaultMiddleware ) => {\n\t\t\treturn [\n\t\t\t\t...getDefaultMiddleware(),\n\t\t\t\t...Array.from( middlewares ),\n\t\t\t];\n\t\t},\n\t} );\n\n\tif ( pendingActions.length ) {\n\t\tpendingActions.forEach( ( action ) => dispatch( action ) );\n\t\tpendingActions.length = 0;\n\t}\n\n\treturn instance as AnyStore;\n};\n\nconst getStore = () => {\n\treturn instance;\n};\n\nconst deleteStore = () => {\n\tinstance = null;\n\tslices = {};\n\tpendingActions.length = 0;\n\tmiddlewares.clear();\n};\n\nexport {\n\tregisterSlice as __registerSlice,\n\taddMiddleware as __addMiddleware,\n\tdispatch as __dispatch,\n\tgetState as __getState,\n\tsubscribe as __subscribe,\n\tcreateStore as __createStore,\n\tgetStore as __getStore,\n\tdeleteStore as __deleteStore,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAUO;AAcP,IAAAA,kBAKO;AAEP,yBAIO;AAiCP,IAAI,WAA4B;AAChC,IAAI,SAAoB,CAAC;AACzB,IAAM,iBAA8B,CAAC;AACrC,IAAM,cAAc,oBAAI,IAAgB;AAExC,IAAM,cAAc,MAAM;AACzB,QAAM,WAAW,OAAO,QAAS,MAAO,EAAE,OAAQ,CAAE,cAAiC,CAAE,MAAM,KAAM,MAAO;AACzG,iBAAc,IAAK,IAAI,MAAM;AAE7B,WAAO;AAAA,EACR,GAAG,CAAC,CAAE;AAEN,aAAO,gCAAiB,QAAS;AAClC;AAEA,SAAS,cAAe,OAAe;AACtC,MAAK,OAAQ,MAAM,IAAK,GAAI;AAC3B,UAAM,IAAI,MAAO,oBAAqB,MAAM,IAAK,mBAAoB;AAAA,EACtE;AAEA,SAAQ,MAAM,IAAK,IAAI;AACxB;AAEA,IAAM,gBAAgB,CAAE,eAA4B;AACnD,cAAY,IAAK,UAAW;AAC7B;AAGA,IAAM,WAAW,CAAE,WAAiB;AACnC,MAAK,CAAE,UAAW;AACjB,mBAAe,KAAM,MAAO;AAE5B;AAAA,EACD;AAEA,SAAO,SAAS,SAAU,MAAO;AAClC;AAEA,IAAM,WAAW,MAAM;AACtB,MAAK,CAAE,UAAW;AACjB,UAAM,IAAI,MAAO,oCAAqC;AAAA,EACvD;AAEA,SAAO,SAAS,SAAS;AAC1B;AAEA,IAAM,YAAY,CAAE,aAA0B;AAC7C,MAAK,CAAE,UAAW;AACjB,UAAM,IAAI,MAAO,oCAAqC;AAAA,EACvD;AAEA,SAAO,SAAS,UAAW,QAAS;AACrC;AAEA,IAAM,cAAc,MAAM;AACzB,MAAK,UAAW;AACf,UAAM,IAAI,MAAO,oCAAqC;AAAA,EACvD;AAEA,iBAAW,+BAAgB;AAAA,IAC1B,SAAS,YAAY;AAAA,IACrB,YAAY,CAAE,yBAA0B;AACvC,aAAO;AAAA,QACN,GAAG,qBAAqB;AAAA,QACxB,GAAG,MAAM,KAAM,WAAY;AAAA,MAC5B;AAAA,IACD;AAAA,EACD,CAAE;AAEF,MAAK,eAAe,QAAS;AAC5B,mBAAe,QAAS,CAAE,WAAY,SAAU,MAAO,CAAE;AACzD,mBAAe,SAAS;AAAA,EACzB;AAEA,SAAO;AACR;AAEA,IAAM,WAAW,MAAM;AACtB,SAAO;AACR;AAEA,IAAM,cAAc,MAAM;AACzB,aAAW;AACX,WAAS,CAAC;AACV,iBAAe,SAAS;AACxB,cAAY,MAAM;AACnB;","names":["import_toolkit"]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n\tReducersMapObject,\n\tconfigureStore,\n\tcombineReducers,\n\tMiddleware,\n\tSlice,\n\tAnyAction,\n\tEnhancedStore,\n\tStoreEnhancer,\n\tThunkMiddleware,\n} from '@reduxjs/toolkit';\n\nexport type {\n\tSlice,\n\tCreateSliceOptions,\n\tPayloadAction,\n\tStore,\n\tDispatch,\n\tAnyAction,\n\tAction,\n\tMiddlewareAPI,\n\tMiddleware,\n} from '@reduxjs/toolkit';\n\nexport {\n\tcreateSelector as __createSelector,\n\tcreateSlice as __createSlice,\n\tcreateAsyncThunk as __createAsyncThunk,\n\tcreateAction as __createAction,\n} from '@reduxjs/toolkit';\n\nexport { useSelector as __useSelector, useDispatch as __useDispatch, Provider as __StoreProvider } from 'react-redux';\n\n/**\n * Usage:\n *\n * const mySlice = addSlice( ... );\n *\n * type MySliceState = SliceState<typeof mySlice>;\n *\n * const value = useSelector( ( state: MySliceState ) => state.mySlice.value );\n */\nexport type SliceState< S extends Slice > = {\n\t[ key in S[ 'name' ] ]: ReturnType< S[ 'getInitialState' ] >;\n};\n\n// The `configureStore` function from Redux Toolkit infers its actions from the `reducers`\n// key of the initialization object. This is fine when creating the store statically, but\n// breaks in our case since we create the store dynamically, which means that TypeScript\n// can't infer the types. Therefore, we force the store to accept any actions using a\n// generic store type.\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype AnyStore< State = any > = EnhancedStore<\n\tState,\n\tAnyAction,\n\t[ ThunkMiddleware< State, AnyAction > ],\n\t[ StoreEnhancer ]\n>;\n\ninterface SlicesMap {\n\t[ key: Slice[ 'name' ] ]: Slice;\n}\n\nlet instance: AnyStore | null = null;\nlet slices: SlicesMap = {};\nconst pendingActions: AnyAction[] = [];\nconst middlewares = new Set< Middleware >();\n\nconst getReducers = () => {\n\tconst reducers = Object.entries( slices ).reduce( ( reducersData: ReducersMapObject, [ name, slice ] ) => {\n\t\treducersData[ name ] = slice.reducer;\n\n\t\treturn reducersData;\n\t}, {} );\n\n\treturn combineReducers( reducers );\n};\n\nfunction registerSlice( slice: Slice ) {\n\tif ( slices[ slice.name ] ) {\n\t\tthrow new Error( `Slice with name \"${ slice.name }\" already exists.` );\n\t}\n\n\tslices[ slice.name ] = slice;\n}\n\nconst addMiddleware = ( middleware: Middleware ) => {\n\tmiddlewares.add( middleware );\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any -- See the comment above about `AnyStore`\nconst dispatch = ( action: any ) => {\n\tif ( ! instance ) {\n\t\tpendingActions.push( action );\n\n\t\treturn;\n\t}\n\n\treturn instance.dispatch( action );\n};\n\nconst getState = () => {\n\tif ( ! instance ) {\n\t\tthrow new Error( 'The store instance does not exist.' );\n\t}\n\n\treturn instance.getState();\n};\n\nconst subscribe = ( listener: () => void ) => {\n\tif ( ! instance ) {\n\t\tthrow new Error( 'The store instance does not exist.' );\n\t}\n\n\treturn instance.subscribe( listener );\n};\n\nconst createStore = () => {\n\tif ( instance ) {\n\t\tthrow new Error( 'The store instance already exists.' );\n\t}\n\n\tinstance = configureStore( {\n\t\treducer: getReducers(),\n\t\tmiddleware: ( getDefaultMiddleware ) => {\n\t\t\treturn [ ...getDefaultMiddleware(), ...Array.from( middlewares ) ];\n\t\t},\n\t} );\n\n\tif ( pendingActions.length ) {\n\t\tpendingActions.forEach( ( action ) => dispatch( action ) );\n\t\tpendingActions.length = 0;\n\t}\n\n\treturn instance as AnyStore;\n};\n\nconst getStore = () => {\n\treturn instance;\n};\n\nconst deleteStore = () => {\n\tinstance = null;\n\tslices = {};\n\tpendingActions.length = 0;\n\tmiddlewares.clear();\n};\n\nexport {\n\tregisterSlice as __registerSlice,\n\taddMiddleware as __addMiddleware,\n\tdispatch as __dispatch,\n\tgetState as __getState,\n\tsubscribe as __subscribe,\n\tcreateStore as __createStore,\n\tgetStore as __getStore,\n\tdeleteStore as __deleteStore,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAUO;AAcP,IAAAA,kBAKO;AAEP,yBAAwG;AAiCxG,IAAI,WAA4B;AAChC,IAAI,SAAoB,CAAC;AACzB,IAAM,iBAA8B,CAAC;AACrC,IAAM,cAAc,oBAAI,IAAkB;AAE1C,IAAM,cAAc,MAAM;AACzB,QAAM,WAAW,OAAO,QAAS,MAAO,EAAE,OAAQ,CAAE,cAAiC,CAAE,MAAM,KAAM,MAAO;AACzG,iBAAc,IAAK,IAAI,MAAM;AAE7B,WAAO;AAAA,EACR,GAAG,CAAC,CAAE;AAEN,aAAO,gCAAiB,QAAS;AAClC;AAEA,SAAS,cAAe,OAAe;AACtC,MAAK,OAAQ,MAAM,IAAK,GAAI;AAC3B,UAAM,IAAI,MAAO,oBAAqB,MAAM,IAAK,mBAAoB;AAAA,EACtE;AAEA,SAAQ,MAAM,IAAK,IAAI;AACxB;AAEA,IAAM,gBAAgB,CAAE,eAA4B;AACnD,cAAY,IAAK,UAAW;AAC7B;AAGA,IAAM,WAAW,CAAE,WAAiB;AACnC,MAAK,CAAE,UAAW;AACjB,mBAAe,KAAM,MAAO;AAE5B;AAAA,EACD;AAEA,SAAO,SAAS,SAAU,MAAO;AAClC;AAEA,IAAM,WAAW,MAAM;AACtB,MAAK,CAAE,UAAW;AACjB,UAAM,IAAI,MAAO,oCAAqC;AAAA,EACvD;AAEA,SAAO,SAAS,SAAS;AAC1B;AAEA,IAAM,YAAY,CAAE,aAA0B;AAC7C,MAAK,CAAE,UAAW;AACjB,UAAM,IAAI,MAAO,oCAAqC;AAAA,EACvD;AAEA,SAAO,SAAS,UAAW,QAAS;AACrC;AAEA,IAAM,cAAc,MAAM;AACzB,MAAK,UAAW;AACf,UAAM,IAAI,MAAO,oCAAqC;AAAA,EACvD;AAEA,iBAAW,+BAAgB;AAAA,IAC1B,SAAS,YAAY;AAAA,IACrB,YAAY,CAAE,yBAA0B;AACvC,aAAO,CAAE,GAAG,qBAAqB,GAAG,GAAG,MAAM,KAAM,WAAY,CAAE;AAAA,IAClE;AAAA,EACD,CAAE;AAEF,MAAK,eAAe,QAAS;AAC5B,mBAAe,QAAS,CAAE,WAAY,SAAU,MAAO,CAAE;AACzD,mBAAe,SAAS;AAAA,EACzB;AAEA,SAAO;AACR;AAEA,IAAM,WAAW,MAAM;AACtB,SAAO;AACR;AAEA,IAAM,cAAc,MAAM;AACzB,aAAW;AACX,WAAS,CAAC;AACV,iBAAe,SAAS;AACxB,cAAY,MAAM;AACnB;","names":["import_toolkit"]}
package/dist/index.mjs CHANGED
@@ -9,11 +9,7 @@ import {
9
9
  createAsyncThunk,
10
10
  createAction
11
11
  } from "@reduxjs/toolkit";
12
- import {
13
- useSelector,
14
- useDispatch,
15
- Provider
16
- } from "react-redux";
12
+ import { useSelector, useDispatch, Provider } from "react-redux";
17
13
  var instance = null;
18
14
  var slices = {};
19
15
  var pendingActions = [];
@@ -60,10 +56,7 @@ var createStore = () => {
60
56
  instance = configureStore({
61
57
  reducer: getReducers(),
62
58
  middleware: (getDefaultMiddleware) => {
63
- return [
64
- ...getDefaultMiddleware(),
65
- ...Array.from(middlewares)
66
- ];
59
+ return [...getDefaultMiddleware(), ...Array.from(middlewares)];
67
60
  }
68
61
  });
69
62
  if (pendingActions.length) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n\tReducersMapObject,\n\tconfigureStore,\n\tcombineReducers,\n\tMiddleware,\n\tSlice,\n\tAnyAction,\n\tEnhancedStore,\n\tStoreEnhancer,\n\tThunkMiddleware,\n} from '@reduxjs/toolkit';\n\nexport type {\n\tSlice,\n\tCreateSliceOptions,\n\tPayloadAction,\n\tStore,\n\tDispatch,\n\tAnyAction,\n\tAction,\n\tMiddlewareAPI,\n\tMiddleware,\n} from '@reduxjs/toolkit';\n\nexport {\n\tcreateSelector as __createSelector,\n\tcreateSlice as __createSlice,\n\tcreateAsyncThunk as __createAsyncThunk,\n\tcreateAction as __createAction,\n} from '@reduxjs/toolkit';\n\nexport {\n\tuseSelector as __useSelector,\n\tuseDispatch as __useDispatch,\n\tProvider as __StoreProvider,\n} from 'react-redux';\n\n/**\n * Usage:\n *\n * const mySlice = addSlice( ... );\n *\n * type MySliceState = SliceState<typeof mySlice>;\n *\n * const value = useSelector( ( state: MySliceState ) => state.mySlice.value );\n */\nexport type SliceState<S extends Slice> = {\n\t[ key in S['name'] ]: ReturnType<S['getInitialState']>;\n}\n\n// The `configureStore` function from Redux Toolkit infers its actions from the `reducers`\n// key of the initialization object. This is fine when creating the store statically, but\n// breaks in our case since we create the store dynamically, which means that TypeScript\n// can't infer the types. Therefore, we force the store to accept any actions using a\n// generic store type.\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype AnyStore<State = any> = EnhancedStore<\n\tState,\n\tAnyAction,\n\t[ThunkMiddleware<State, AnyAction>],\n\t[StoreEnhancer]\n>\n\ninterface SlicesMap {\n\t[key: Slice['name']]: Slice;\n}\n\nlet instance: AnyStore | null = null;\nlet slices: SlicesMap = {};\nconst pendingActions: AnyAction[] = [];\nconst middlewares = new Set<Middleware>();\n\nconst getReducers = () => {\n\tconst reducers = Object.entries( slices ).reduce( ( reducersData: ReducersMapObject, [ name, slice ] ) => {\n\t\treducersData[ name ] = slice.reducer;\n\n\t\treturn reducersData;\n\t}, {} );\n\n\treturn combineReducers( reducers );\n};\n\nfunction registerSlice( slice: Slice ) {\n\tif ( slices[ slice.name ] ) {\n\t\tthrow new Error( `Slice with name \"${ slice.name }\" already exists.` );\n\t}\n\n\tslices[ slice.name ] = slice;\n}\n\nconst addMiddleware = ( middleware: Middleware ) => {\n\tmiddlewares.add( middleware );\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any -- See the comment above about `AnyStore`\nconst dispatch = ( action: any ) => {\n\tif ( ! instance ) {\n\t\tpendingActions.push( action );\n\n\t\treturn;\n\t}\n\n\treturn instance.dispatch( action );\n};\n\nconst getState = () => {\n\tif ( ! instance ) {\n\t\tthrow new Error( 'The store instance does not exist.' );\n\t}\n\n\treturn instance.getState();\n};\n\nconst subscribe = ( listener: () => void ) => {\n\tif ( ! instance ) {\n\t\tthrow new Error( 'The store instance does not exist.' );\n\t}\n\n\treturn instance.subscribe( listener );\n};\n\nconst createStore = () => {\n\tif ( instance ) {\n\t\tthrow new Error( 'The store instance already exists.' );\n\t}\n\n\tinstance = configureStore( {\n\t\treducer: getReducers(),\n\t\tmiddleware: ( getDefaultMiddleware ) => {\n\t\t\treturn [\n\t\t\t\t...getDefaultMiddleware(),\n\t\t\t\t...Array.from( middlewares ),\n\t\t\t];\n\t\t},\n\t} );\n\n\tif ( pendingActions.length ) {\n\t\tpendingActions.forEach( ( action ) => dispatch( action ) );\n\t\tpendingActions.length = 0;\n\t}\n\n\treturn instance as AnyStore;\n};\n\nconst getStore = () => {\n\treturn instance;\n};\n\nconst deleteStore = () => {\n\tinstance = null;\n\tslices = {};\n\tpendingActions.length = 0;\n\tmiddlewares.clear();\n};\n\nexport {\n\tregisterSlice as __registerSlice,\n\taddMiddleware as __addMiddleware,\n\tdispatch as __dispatch,\n\tgetState as __getState,\n\tsubscribe as __subscribe,\n\tcreateStore as __createStore,\n\tgetStore as __getStore,\n\tdeleteStore as __deleteStore,\n};\n"],"mappings":";AAAA;AAAA,EAEC;AAAA,EACA;AAAA,OAOM;AAcP;AAAA,EACmB;AAAA,EACH;AAAA,EACK;AAAA,EACJ;AAAA,OACV;AAEP;AAAA,EACgB;AAAA,EACA;AAAA,EACH;AAAA,OACN;AAiCP,IAAI,WAA4B;AAChC,IAAI,SAAoB,CAAC;AACzB,IAAM,iBAA8B,CAAC;AACrC,IAAM,cAAc,oBAAI,IAAgB;AAExC,IAAM,cAAc,MAAM;AACzB,QAAM,WAAW,OAAO,QAAS,MAAO,EAAE,OAAQ,CAAE,cAAiC,CAAE,MAAM,KAAM,MAAO;AACzG,iBAAc,IAAK,IAAI,MAAM;AAE7B,WAAO;AAAA,EACR,GAAG,CAAC,CAAE;AAEN,SAAO,gBAAiB,QAAS;AAClC;AAEA,SAAS,cAAe,OAAe;AACtC,MAAK,OAAQ,MAAM,IAAK,GAAI;AAC3B,UAAM,IAAI,MAAO,oBAAqB,MAAM,IAAK,mBAAoB;AAAA,EACtE;AAEA,SAAQ,MAAM,IAAK,IAAI;AACxB;AAEA,IAAM,gBAAgB,CAAE,eAA4B;AACnD,cAAY,IAAK,UAAW;AAC7B;AAGA,IAAM,WAAW,CAAE,WAAiB;AACnC,MAAK,CAAE,UAAW;AACjB,mBAAe,KAAM,MAAO;AAE5B;AAAA,EACD;AAEA,SAAO,SAAS,SAAU,MAAO;AAClC;AAEA,IAAM,WAAW,MAAM;AACtB,MAAK,CAAE,UAAW;AACjB,UAAM,IAAI,MAAO,oCAAqC;AAAA,EACvD;AAEA,SAAO,SAAS,SAAS;AAC1B;AAEA,IAAM,YAAY,CAAE,aAA0B;AAC7C,MAAK,CAAE,UAAW;AACjB,UAAM,IAAI,MAAO,oCAAqC;AAAA,EACvD;AAEA,SAAO,SAAS,UAAW,QAAS;AACrC;AAEA,IAAM,cAAc,MAAM;AACzB,MAAK,UAAW;AACf,UAAM,IAAI,MAAO,oCAAqC;AAAA,EACvD;AAEA,aAAW,eAAgB;AAAA,IAC1B,SAAS,YAAY;AAAA,IACrB,YAAY,CAAE,yBAA0B;AACvC,aAAO;AAAA,QACN,GAAG,qBAAqB;AAAA,QACxB,GAAG,MAAM,KAAM,WAAY;AAAA,MAC5B;AAAA,IACD;AAAA,EACD,CAAE;AAEF,MAAK,eAAe,QAAS;AAC5B,mBAAe,QAAS,CAAE,WAAY,SAAU,MAAO,CAAE;AACzD,mBAAe,SAAS;AAAA,EACzB;AAEA,SAAO;AACR;AAEA,IAAM,WAAW,MAAM;AACtB,SAAO;AACR;AAEA,IAAM,cAAc,MAAM;AACzB,aAAW;AACX,WAAS,CAAC;AACV,iBAAe,SAAS;AACxB,cAAY,MAAM;AACnB;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n\tReducersMapObject,\n\tconfigureStore,\n\tcombineReducers,\n\tMiddleware,\n\tSlice,\n\tAnyAction,\n\tEnhancedStore,\n\tStoreEnhancer,\n\tThunkMiddleware,\n} from '@reduxjs/toolkit';\n\nexport type {\n\tSlice,\n\tCreateSliceOptions,\n\tPayloadAction,\n\tStore,\n\tDispatch,\n\tAnyAction,\n\tAction,\n\tMiddlewareAPI,\n\tMiddleware,\n} from '@reduxjs/toolkit';\n\nexport {\n\tcreateSelector as __createSelector,\n\tcreateSlice as __createSlice,\n\tcreateAsyncThunk as __createAsyncThunk,\n\tcreateAction as __createAction,\n} from '@reduxjs/toolkit';\n\nexport { useSelector as __useSelector, useDispatch as __useDispatch, Provider as __StoreProvider } from 'react-redux';\n\n/**\n * Usage:\n *\n * const mySlice = addSlice( ... );\n *\n * type MySliceState = SliceState<typeof mySlice>;\n *\n * const value = useSelector( ( state: MySliceState ) => state.mySlice.value );\n */\nexport type SliceState< S extends Slice > = {\n\t[ key in S[ 'name' ] ]: ReturnType< S[ 'getInitialState' ] >;\n};\n\n// The `configureStore` function from Redux Toolkit infers its actions from the `reducers`\n// key of the initialization object. This is fine when creating the store statically, but\n// breaks in our case since we create the store dynamically, which means that TypeScript\n// can't infer the types. Therefore, we force the store to accept any actions using a\n// generic store type.\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype AnyStore< State = any > = EnhancedStore<\n\tState,\n\tAnyAction,\n\t[ ThunkMiddleware< State, AnyAction > ],\n\t[ StoreEnhancer ]\n>;\n\ninterface SlicesMap {\n\t[ key: Slice[ 'name' ] ]: Slice;\n}\n\nlet instance: AnyStore | null = null;\nlet slices: SlicesMap = {};\nconst pendingActions: AnyAction[] = [];\nconst middlewares = new Set< Middleware >();\n\nconst getReducers = () => {\n\tconst reducers = Object.entries( slices ).reduce( ( reducersData: ReducersMapObject, [ name, slice ] ) => {\n\t\treducersData[ name ] = slice.reducer;\n\n\t\treturn reducersData;\n\t}, {} );\n\n\treturn combineReducers( reducers );\n};\n\nfunction registerSlice( slice: Slice ) {\n\tif ( slices[ slice.name ] ) {\n\t\tthrow new Error( `Slice with name \"${ slice.name }\" already exists.` );\n\t}\n\n\tslices[ slice.name ] = slice;\n}\n\nconst addMiddleware = ( middleware: Middleware ) => {\n\tmiddlewares.add( middleware );\n};\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any -- See the comment above about `AnyStore`\nconst dispatch = ( action: any ) => {\n\tif ( ! instance ) {\n\t\tpendingActions.push( action );\n\n\t\treturn;\n\t}\n\n\treturn instance.dispatch( action );\n};\n\nconst getState = () => {\n\tif ( ! instance ) {\n\t\tthrow new Error( 'The store instance does not exist.' );\n\t}\n\n\treturn instance.getState();\n};\n\nconst subscribe = ( listener: () => void ) => {\n\tif ( ! instance ) {\n\t\tthrow new Error( 'The store instance does not exist.' );\n\t}\n\n\treturn instance.subscribe( listener );\n};\n\nconst createStore = () => {\n\tif ( instance ) {\n\t\tthrow new Error( 'The store instance already exists.' );\n\t}\n\n\tinstance = configureStore( {\n\t\treducer: getReducers(),\n\t\tmiddleware: ( getDefaultMiddleware ) => {\n\t\t\treturn [ ...getDefaultMiddleware(), ...Array.from( middlewares ) ];\n\t\t},\n\t} );\n\n\tif ( pendingActions.length ) {\n\t\tpendingActions.forEach( ( action ) => dispatch( action ) );\n\t\tpendingActions.length = 0;\n\t}\n\n\treturn instance as AnyStore;\n};\n\nconst getStore = () => {\n\treturn instance;\n};\n\nconst deleteStore = () => {\n\tinstance = null;\n\tslices = {};\n\tpendingActions.length = 0;\n\tmiddlewares.clear();\n};\n\nexport {\n\tregisterSlice as __registerSlice,\n\taddMiddleware as __addMiddleware,\n\tdispatch as __dispatch,\n\tgetState as __getState,\n\tsubscribe as __subscribe,\n\tcreateStore as __createStore,\n\tgetStore as __getStore,\n\tdeleteStore as __deleteStore,\n};\n"],"mappings":";AAAA;AAAA,EAEC;AAAA,EACA;AAAA,OAOM;AAcP;AAAA,EACmB;AAAA,EACH;AAAA,EACK;AAAA,EACJ;AAAA,OACV;AAEP,SAAwB,aAA8B,aAA2B,gBAAuB;AAiCxG,IAAI,WAA4B;AAChC,IAAI,SAAoB,CAAC;AACzB,IAAM,iBAA8B,CAAC;AACrC,IAAM,cAAc,oBAAI,IAAkB;AAE1C,IAAM,cAAc,MAAM;AACzB,QAAM,WAAW,OAAO,QAAS,MAAO,EAAE,OAAQ,CAAE,cAAiC,CAAE,MAAM,KAAM,MAAO;AACzG,iBAAc,IAAK,IAAI,MAAM;AAE7B,WAAO;AAAA,EACR,GAAG,CAAC,CAAE;AAEN,SAAO,gBAAiB,QAAS;AAClC;AAEA,SAAS,cAAe,OAAe;AACtC,MAAK,OAAQ,MAAM,IAAK,GAAI;AAC3B,UAAM,IAAI,MAAO,oBAAqB,MAAM,IAAK,mBAAoB;AAAA,EACtE;AAEA,SAAQ,MAAM,IAAK,IAAI;AACxB;AAEA,IAAM,gBAAgB,CAAE,eAA4B;AACnD,cAAY,IAAK,UAAW;AAC7B;AAGA,IAAM,WAAW,CAAE,WAAiB;AACnC,MAAK,CAAE,UAAW;AACjB,mBAAe,KAAM,MAAO;AAE5B;AAAA,EACD;AAEA,SAAO,SAAS,SAAU,MAAO;AAClC;AAEA,IAAM,WAAW,MAAM;AACtB,MAAK,CAAE,UAAW;AACjB,UAAM,IAAI,MAAO,oCAAqC;AAAA,EACvD;AAEA,SAAO,SAAS,SAAS;AAC1B;AAEA,IAAM,YAAY,CAAE,aAA0B;AAC7C,MAAK,CAAE,UAAW;AACjB,UAAM,IAAI,MAAO,oCAAqC;AAAA,EACvD;AAEA,SAAO,SAAS,UAAW,QAAS;AACrC;AAEA,IAAM,cAAc,MAAM;AACzB,MAAK,UAAW;AACf,UAAM,IAAI,MAAO,oCAAqC;AAAA,EACvD;AAEA,aAAW,eAAgB;AAAA,IAC1B,SAAS,YAAY;AAAA,IACrB,YAAY,CAAE,yBAA0B;AACvC,aAAO,CAAE,GAAG,qBAAqB,GAAG,GAAG,MAAM,KAAM,WAAY,CAAE;AAAA,IAClE;AAAA,EACD,CAAE;AAEF,MAAK,eAAe,QAAS;AAC5B,mBAAe,QAAS,CAAE,WAAY,SAAU,MAAO,CAAE;AACzD,mBAAe,SAAS;AAAA,EACzB;AAEA,SAAO;AACR;AAEA,IAAM,WAAW,MAAM;AACtB,SAAO;AACR;AAEA,IAAM,cAAc,MAAM;AACzB,aAAW;AACX,WAAS,CAAC;AACV,iBAAe,SAAS;AACxB,cAAY,MAAM;AACnB;","names":[]}
package/package.json CHANGED
@@ -1,42 +1,49 @@
1
1
  {
2
- "name": "@elementor/store",
3
- "version": "0.8.3",
4
- "private": false,
5
- "author": "Elementor Team",
6
- "homepage": "https://elementor.com/",
7
- "license": "GPL-3.0-or-later",
8
- "main": "dist/index.js",
9
- "module": "dist/index.mjs",
10
- "types": "dist/index.d.ts",
11
- "exports": {
12
- ".": {
13
- "import": "./dist/index.mjs",
14
- "require": "./dist/index.js",
15
- "types": "./dist/index.d.ts"
16
- },
17
- "./package.json": "./package.json"
18
- },
19
- "repository": {
20
- "type": "git",
21
- "url": "https://github.com/elementor/elementor-packages.git",
22
- "directory": "packages/libs/store"
23
- },
24
- "bugs": {
25
- "url": "https://github.com/elementor/elementor-packages/issues"
26
- },
27
- "publishConfig": {
28
- "access": "public"
29
- },
30
- "scripts": {
31
- "build": "tsup --config=../../tsup.build.ts",
32
- "dev": "tsup --config=../../tsup.dev.ts"
33
- },
34
- "peerDependencies": {
35
- "react": "^18.3.1"
36
- },
37
- "dependencies": {
38
- "@reduxjs/toolkit": "^1.9.7",
39
- "react-redux": "^8.1.3"
40
- },
41
- "gitHead": "8fee803f32268363169a260a99c2eb7c2d548b39"
2
+ "name": "@elementor/store",
3
+ "version": "0.8.5",
4
+ "private": false,
5
+ "author": "Elementor Team",
6
+ "homepage": "https://elementor.com/",
7
+ "license": "GPL-3.0-or-later",
8
+ "main": "dist/index.js",
9
+ "module": "dist/index.mjs",
10
+ "types": "dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.js",
15
+ "types": "./dist/index.d.ts"
16
+ },
17
+ "./package.json": "./package.json"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/elementor/elementor-packages.git",
22
+ "directory": "packages/libs/store"
23
+ },
24
+ "bugs": {
25
+ "url": "https://github.com/elementor/elementor-packages/issues"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "files": [
31
+ "README.md",
32
+ "CHANGELOG.md",
33
+ "/dist",
34
+ "/src",
35
+ "!**/__tests__"
36
+ ],
37
+ "scripts": {
38
+ "build": "tsup --config=../../tsup.build.ts",
39
+ "dev": "tsup --config=../../tsup.dev.ts"
40
+ },
41
+ "peerDependencies": {
42
+ "react": "^18.3.1"
43
+ },
44
+ "dependencies": {
45
+ "@reduxjs/toolkit": "^1.9.7",
46
+ "react-redux": "^8.1.3"
47
+ },
48
+ "gitHead": "f4ca33da0842a29d83736d0a173633085edddaee"
42
49
  }
package/src/index.ts CHANGED
@@ -29,11 +29,7 @@ export {
29
29
  createAction as __createAction,
30
30
  } from '@reduxjs/toolkit';
31
31
 
32
- export {
33
- useSelector as __useSelector,
34
- useDispatch as __useDispatch,
35
- Provider as __StoreProvider,
36
- } from 'react-redux';
32
+ export { useSelector as __useSelector, useDispatch as __useDispatch, Provider as __StoreProvider } from 'react-redux';
37
33
 
38
34
  /**
39
35
  * Usage:
@@ -44,9 +40,9 @@ export {
44
40
  *
45
41
  * const value = useSelector( ( state: MySliceState ) => state.mySlice.value );
46
42
  */
47
- export type SliceState<S extends Slice> = {
48
- [ key in S['name'] ]: ReturnType<S['getInitialState']>;
49
- }
43
+ export type SliceState< S extends Slice > = {
44
+ [ key in S[ 'name' ] ]: ReturnType< S[ 'getInitialState' ] >;
45
+ };
50
46
 
51
47
  // The `configureStore` function from Redux Toolkit infers its actions from the `reducers`
52
48
  // key of the initialization object. This is fine when creating the store statically, but
@@ -55,21 +51,21 @@ export type SliceState<S extends Slice> = {
55
51
  // generic store type.
56
52
 
57
53
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
- type AnyStore<State = any> = EnhancedStore<
54
+ type AnyStore< State = any > = EnhancedStore<
59
55
  State,
60
56
  AnyAction,
61
- [ThunkMiddleware<State, AnyAction>],
62
- [StoreEnhancer]
63
- >
57
+ [ ThunkMiddleware< State, AnyAction > ],
58
+ [ StoreEnhancer ]
59
+ >;
64
60
 
65
61
  interface SlicesMap {
66
- [key: Slice['name']]: Slice;
62
+ [ key: Slice[ 'name' ] ]: Slice;
67
63
  }
68
64
 
69
65
  let instance: AnyStore | null = null;
70
66
  let slices: SlicesMap = {};
71
67
  const pendingActions: AnyAction[] = [];
72
- const middlewares = new Set<Middleware>();
68
+ const middlewares = new Set< Middleware >();
73
69
 
74
70
  const getReducers = () => {
75
71
  const reducers = Object.entries( slices ).reduce( ( reducersData: ReducersMapObject, [ name, slice ] ) => {
@@ -128,10 +124,7 @@ const createStore = () => {
128
124
  instance = configureStore( {
129
125
  reducer: getReducers(),
130
126
  middleware: ( getDefaultMiddleware ) => {
131
- return [
132
- ...getDefaultMiddleware(),
133
- ...Array.from( middlewares ),
134
- ];
127
+ return [ ...getDefaultMiddleware(), ...Array.from( middlewares ) ];
135
128
  },
136
129
  } );
137
130
 
@@ -1,321 +0,0 @@
1
- import * as React from 'react';
2
- import { PropsWithChildren } from 'react';
3
- import { renderHook } from '@testing-library/react';
4
- import {
5
- __createStore,
6
- __getStore,
7
- __createSlice,
8
- __registerSlice,
9
- __addMiddleware,
10
- __dispatch,
11
- __deleteStore,
12
- __StoreProvider as StoreProvider,
13
- __useSelector as useSelector,
14
- __useDispatch as useDispatch,
15
- __createAsyncThunk,
16
- Dispatch,
17
- AnyAction,
18
- } from '../index';
19
-
20
- interface SliceStateRoot {
21
- slice: {
22
- value?: number;
23
- }
24
- }
25
-
26
- interface Config {
27
- initialValue?: SliceStateRoot['slice']['value'];
28
- }
29
-
30
- const createStoreEntities = ( { initialValue = 1 }: Config = {} ) => {
31
- const slice = __createSlice( {
32
- name: 'slice',
33
- initialState: {
34
- value: initialValue,
35
- },
36
- reducers: {
37
- setValue: ( state, action ) => {
38
- state.value = action.payload;
39
- },
40
- },
41
- } );
42
-
43
- __registerSlice( slice );
44
-
45
- const store = __createStore();
46
-
47
- const wrapper = ( { children }: PropsWithChildren ) => (
48
- <StoreProvider store={ store }>
49
- { children }
50
- </StoreProvider>
51
- );
52
-
53
- return {
54
- slice,
55
- store,
56
- wrapper,
57
- };
58
- };
59
-
60
- describe( '@elementor/store', () => {
61
- it( 'should set an initial state of a slice', () => {
62
- // Arrange.
63
- const { wrapper } = createStoreEntities();
64
-
65
- const { result } = renderHook( () => useSelector( ( state: SliceStateRoot ) => state.slice.value ), { wrapper } );
66
-
67
- // Assert.
68
- expect( result.current ).toBe( 1 );
69
- } );
70
-
71
- it( 'should update the state value of the slice', () => {
72
- // Arrange.
73
- const { slice, wrapper } = createStoreEntities();
74
-
75
- // Act.
76
- const { result } = renderHook( () => {
77
- const dispatchAction = useDispatch();
78
-
79
- dispatchAction( slice.actions.setValue( 3 ) );
80
-
81
- return useSelector( ( state: SliceStateRoot ) => state.slice.value );
82
- }, { wrapper } );
83
-
84
- // Assert.
85
- expect( result.current ).toBe( 3 );
86
- } );
87
-
88
- it( 'should throw an error when trying to add a slice with the same name more than once', () => {
89
- // Arrange.
90
- const slice = __createSlice( {
91
- name: 'slice',
92
- initialState: {
93
- value: 1,
94
- },
95
- reducers: {},
96
- } );
97
-
98
- __registerSlice( slice );
99
-
100
- // Assert.
101
- expect( () => {
102
- __registerSlice( slice );
103
- } ).toThrow( 'Slice with name "slice" already exists.' );
104
- } );
105
-
106
- it( 'should throw an error when trying to re-create the store', () => {
107
- // Arrange.
108
- const slice = __createSlice( {
109
- name: 'slice',
110
- initialState: {
111
- value: 1,
112
- },
113
- reducers: {},
114
- } );
115
-
116
- __registerSlice( slice );
117
-
118
- __createStore();
119
-
120
- // Assert.
121
- expect( () => __createStore() ).toThrow( 'The store instance already exists.' );
122
- } );
123
-
124
- it( 'should add a middleware that blocks the state update by not running next(action)', () => {
125
- // Arrange.
126
- __addMiddleware( () => () => () => {
127
- return null;
128
- } );
129
-
130
- const { slice, wrapper } = createStoreEntities();
131
-
132
- // Act.
133
- const { result } = renderHook( () => {
134
- const dispatchAction = useDispatch();
135
-
136
- dispatchAction( slice.actions.setValue( 4 ) );
137
-
138
- return useSelector( ( state: SliceStateRoot ) => state.slice.value );
139
- }, { wrapper } );
140
-
141
- // Assert.
142
- expect( result.current ).toBe( 1 );
143
- } );
144
-
145
- it( 'should add a middleware that does not interfere with the state update', () => {
146
- // Arrange.
147
- const middlewareNextAction = jest.fn();
148
-
149
- __addMiddleware( () => ( next: Dispatch<AnyAction> ) => ( action: AnyAction ) => {
150
- middlewareNextAction( action );
151
-
152
- next( action );
153
- } );
154
-
155
- const { slice, wrapper } = createStoreEntities();
156
-
157
- // Act.
158
- const { result } = renderHook( () => {
159
- const dispatchAction = useDispatch();
160
-
161
- dispatchAction( slice.actions.setValue( 4 ) );
162
-
163
- return useSelector( ( state: SliceStateRoot ) => state.slice.value );
164
- }, { wrapper } );
165
-
166
- // Assert.
167
- expect( result.current ).toBe( 4 );
168
-
169
- expect( middlewareNextAction ).toHaveBeenCalledWith( slice.actions.setValue( 4 ) );
170
- } );
171
-
172
- it( 'should dispatch an action without using hooks', () => {
173
- // Arrange.
174
- const slice = __createSlice( {
175
- name: 'slice',
176
- initialState: {
177
- value: 1,
178
- },
179
- reducers: {
180
- setValue: ( state, action ) => {
181
- state.value = action.payload;
182
- },
183
- },
184
- } );
185
-
186
- __registerSlice( slice );
187
-
188
- const store = __createStore();
189
-
190
- // Act.
191
- store.dispatch( slice.actions.setValue( 6 ) );
192
-
193
- const stateResult = store.getState().slice.value;
194
-
195
- // Assert.
196
- expect( stateResult ).toBe( 6 );
197
- } );
198
-
199
- it( 'should collect actions that are dispatched before the store exist, and run them when the store instance is created', () => {
200
- // Act.
201
- __dispatch( { type: 'slice/setValue', payload: 7 } );
202
-
203
- const { wrapper } = createStoreEntities();
204
-
205
- const { result } = renderHook( () => useSelector( ( state: SliceStateRoot ) => state.slice.value ), { wrapper } );
206
-
207
- // Assert.
208
- expect( result.current ).toBe( 7 );
209
- } );
210
-
211
- it( 'should delete the store instance', () => {
212
- // Arrange.
213
- const { store } = createStoreEntities();
214
-
215
- let instance = __getStore();
216
-
217
- // Assert.
218
- expect( instance ).toEqual( store );
219
-
220
- // Act.
221
- __deleteStore();
222
-
223
- instance = __getStore();
224
-
225
- // Assert.
226
- expect( instance ).toBeNull();
227
- } );
228
-
229
- it( 'should delete the added slices', () => {
230
- // Arrange.
231
- createStoreEntities();
232
-
233
- // Act.
234
- __deleteStore();
235
-
236
- // Arrange.
237
- const store = __createStore();
238
-
239
- const wrapper = ( { children }: PropsWithChildren ) => (
240
- <StoreProvider store={ store }>
241
- { children }
242
- </StoreProvider>
243
- );
244
-
245
- const { result } = renderHook( () => useSelector( ( state: SliceStateRoot ) => state.slice ), { wrapper } );
246
-
247
- // Assert.
248
- expect( result.current ).toBeUndefined();
249
- expect( console ).toHaveErrored();
250
- } );
251
-
252
- it( 'should delete the added middlewares', () => {
253
- // Arrange.
254
- // Registering a middleware that blocks the state update by not running next(action).
255
- __addMiddleware( () => () => () => {
256
- return null;
257
- } );
258
-
259
- createStoreEntities();
260
-
261
- // Act.
262
- __deleteStore();
263
-
264
- const { slice, wrapper } = createStoreEntities();
265
-
266
- const { result } = renderHook( () => {
267
- const dispatchAction = useDispatch();
268
-
269
- dispatchAction( slice.actions.setValue( 8 ) );
270
-
271
- return useSelector( ( state: SliceStateRoot ) => state.slice.value );
272
- }, { wrapper } );
273
-
274
- // Assert.
275
- expect( result.current ).toBe( 8 );
276
- } );
277
-
278
- it( 'should support dispatching async actions', async () => {
279
- // Arrange.
280
- const incrementBy = __createAsyncThunk(
281
- 'value/incrementBy',
282
- async ( value: number ) => {
283
- return value;
284
- },
285
- );
286
-
287
- const slice = __createSlice( {
288
- name: 'slice',
289
- initialState: {
290
- value: 1,
291
- },
292
- reducers: {
293
- increment: ( state ) => {
294
- state.value += 1;
295
- },
296
- },
297
- extraReducers: ( builder ) => {
298
- builder.addCase( incrementBy.fulfilled, ( state, action ) => {
299
- state.value += action.payload;
300
- } );
301
- },
302
- } );
303
-
304
- __registerSlice( slice );
305
-
306
- const store = __createStore();
307
-
308
- // Act.
309
- jest.useFakeTimers();
310
-
311
- store.dispatch( incrementBy( 2 ) );
312
-
313
- // Regular reducer usage to ensure it works properly.
314
- store.dispatch( slice.actions.increment() );
315
-
316
- await jest.runAllTimersAsync();
317
-
318
- // Assert.
319
- expect( store.getState().slice.value ).toBe( 4 );
320
- } );
321
- } );