@elementor/store 0.7.1 → 0.8.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.
- package/CHANGELOG.md +11 -0
- package/dist/index.d.mts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +9 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/store.test.tsx +45 -0
- package/src/index.ts +25 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
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.0](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.7.1...@elementor/store@0.8.0) (2024-02-22)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **store:** support createAsyncThunk [ED-13842] ([#164](https://github.com/elementor/elementor-packages/issues/164)) ([bd0ed1d](https://github.com/elementor/elementor-packages/commit/bd0ed1d11a8c6d284897f2d0e68beef07f5b5849))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
## [0.7.1](https://github.com/elementor/elementor-packages/compare/@elementor/store@0.7.0...@elementor/store@0.7.1) (2023-10-19)
|
|
7
18
|
|
|
8
19
|
**Note:** Version bump only for package @elementor/store
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import * as _reduxjs_toolkit_dist_configureStore from '@reduxjs/toolkit/dist/configureStore';
|
|
1
2
|
import * as redux from 'redux';
|
|
2
|
-
import { Slice, Middleware, AnyAction, Store } from '@reduxjs/toolkit';
|
|
3
|
-
export { Action, AnyAction, CreateSliceOptions, Dispatch, Middleware, MiddlewareAPI, PayloadAction, Slice, Store, createSelector as __createSelector, createSlice as __createSlice } from '@reduxjs/toolkit';
|
|
3
|
+
import { Slice, Middleware, AnyAction, ThunkMiddleware, Store } from '@reduxjs/toolkit';
|
|
4
|
+
export { Action, AnyAction, CreateSliceOptions, Dispatch, Middleware, MiddlewareAPI, PayloadAction, Slice, Store, createAsyncThunk as __createAsyncThunk, createSelector as __createSelector, createSlice as __createSlice } from '@reduxjs/toolkit';
|
|
4
5
|
export { Provider as __StoreProvider, useDispatch as __useDispatch, useSelector as __useSelector } from 'react-redux';
|
|
5
6
|
|
|
6
7
|
/**
|
|
@@ -20,7 +21,7 @@ declare const addMiddleware: (middleware: Middleware) => void;
|
|
|
20
21
|
declare const dispatch: (action: AnyAction) => AnyAction | undefined;
|
|
21
22
|
declare const getState: () => any;
|
|
22
23
|
declare const subscribe: (listener: () => void) => redux.Unsubscribe;
|
|
23
|
-
declare const createStore: () =>
|
|
24
|
+
declare const createStore: () => _reduxjs_toolkit_dist_configureStore.ToolkitStore<any, AnyAction, [ThunkMiddleware<any, AnyAction>]>;
|
|
24
25
|
declare const getStore: () => Store<any, AnyAction> | null;
|
|
25
26
|
declare const deleteStore: () => void;
|
|
26
27
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import * as _reduxjs_toolkit_dist_configureStore from '@reduxjs/toolkit/dist/configureStore';
|
|
1
2
|
import * as redux from 'redux';
|
|
2
|
-
import { Slice, Middleware, AnyAction, Store } from '@reduxjs/toolkit';
|
|
3
|
-
export { Action, AnyAction, CreateSliceOptions, Dispatch, Middleware, MiddlewareAPI, PayloadAction, Slice, Store, createSelector as __createSelector, createSlice as __createSlice } from '@reduxjs/toolkit';
|
|
3
|
+
import { Slice, Middleware, AnyAction, ThunkMiddleware, Store } from '@reduxjs/toolkit';
|
|
4
|
+
export { Action, AnyAction, CreateSliceOptions, Dispatch, Middleware, MiddlewareAPI, PayloadAction, Slice, Store, createAsyncThunk as __createAsyncThunk, createSelector as __createSelector, createSlice as __createSlice } from '@reduxjs/toolkit';
|
|
4
5
|
export { Provider as __StoreProvider, useDispatch as __useDispatch, useSelector as __useSelector } from 'react-redux';
|
|
5
6
|
|
|
6
7
|
/**
|
|
@@ -20,7 +21,7 @@ declare const addMiddleware: (middleware: Middleware) => void;
|
|
|
20
21
|
declare const dispatch: (action: AnyAction) => AnyAction | undefined;
|
|
21
22
|
declare const getState: () => any;
|
|
22
23
|
declare const subscribe: (listener: () => void) => redux.Unsubscribe;
|
|
23
|
-
declare const createStore: () =>
|
|
24
|
+
declare const createStore: () => _reduxjs_toolkit_dist_configureStore.ToolkitStore<any, AnyAction, [ThunkMiddleware<any, AnyAction>]>;
|
|
24
25
|
declare const getStore: () => Store<any, AnyAction> | null;
|
|
25
26
|
declare const deleteStore: () => void;
|
|
26
27
|
|
package/dist/index.js
CHANGED
|
@@ -22,6 +22,7 @@ var src_exports = {};
|
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
__StoreProvider: () => import_react_redux.Provider,
|
|
24
24
|
__addMiddleware: () => addMiddleware,
|
|
25
|
+
__createAsyncThunk: () => import_toolkit2.createAsyncThunk,
|
|
25
26
|
__createSelector: () => import_toolkit2.createSelector,
|
|
26
27
|
__createSlice: () => import_toolkit2.createSlice,
|
|
27
28
|
__createStore: () => createStore,
|
|
@@ -83,7 +84,12 @@ var createStore = () => {
|
|
|
83
84
|
}
|
|
84
85
|
instance = (0, import_toolkit.configureStore)({
|
|
85
86
|
reducer: getReducers(),
|
|
86
|
-
middleware:
|
|
87
|
+
middleware: (getDefaultMiddleware) => {
|
|
88
|
+
return [
|
|
89
|
+
...getDefaultMiddleware(),
|
|
90
|
+
...Array.from(middlewares)
|
|
91
|
+
];
|
|
92
|
+
}
|
|
87
93
|
});
|
|
88
94
|
if (pendingActions.length) {
|
|
89
95
|
pendingActions.forEach((action) => dispatch(action));
|
|
@@ -104,6 +110,7 @@ var deleteStore = () => {
|
|
|
104
110
|
0 && (module.exports = {
|
|
105
111
|
__StoreProvider,
|
|
106
112
|
__addMiddleware,
|
|
113
|
+
__createAsyncThunk,
|
|
107
114
|
__createSelector,
|
|
108
115
|
__createSlice,
|
|
109
116
|
__createStore,
|
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\tStore,\n\tSlice,\n\tAnyAction,\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} 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\ninterface SlicesMap {\n\t[key: Slice['name']]: Slice;\n}\n\nlet instance: Store | 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\nconst dispatch = ( action: AnyAction ) => {\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: Array.from( middlewares ),\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;\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,
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n\tReducersMapObject,\n\tconfigureStore,\n\tcombineReducers,\n\tMiddleware,\n\tStore,\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} 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: Store | 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\nconst dispatch = ( action: AnyAction ) => {\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,qBAWO;AAcP,IAAAA,kBAIO;AAEP,yBAIO;AAiCP,IAAI,WAAyB;AAC7B,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;AAEA,IAAM,WAAW,CAAE,WAAuB;AACzC,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"]}
|
package/dist/index.mjs
CHANGED
|
@@ -5,7 +5,8 @@ import {
|
|
|
5
5
|
} from "@reduxjs/toolkit";
|
|
6
6
|
import {
|
|
7
7
|
createSelector,
|
|
8
|
-
createSlice
|
|
8
|
+
createSlice,
|
|
9
|
+
createAsyncThunk
|
|
9
10
|
} from "@reduxjs/toolkit";
|
|
10
11
|
import {
|
|
11
12
|
useSelector,
|
|
@@ -57,7 +58,12 @@ var createStore = () => {
|
|
|
57
58
|
}
|
|
58
59
|
instance = configureStore({
|
|
59
60
|
reducer: getReducers(),
|
|
60
|
-
middleware:
|
|
61
|
+
middleware: (getDefaultMiddleware) => {
|
|
62
|
+
return [
|
|
63
|
+
...getDefaultMiddleware(),
|
|
64
|
+
...Array.from(middlewares)
|
|
65
|
+
];
|
|
66
|
+
}
|
|
61
67
|
});
|
|
62
68
|
if (pendingActions.length) {
|
|
63
69
|
pendingActions.forEach((action) => dispatch(action));
|
|
@@ -77,6 +83,7 @@ var deleteStore = () => {
|
|
|
77
83
|
export {
|
|
78
84
|
Provider as __StoreProvider,
|
|
79
85
|
addMiddleware as __addMiddleware,
|
|
86
|
+
createAsyncThunk as __createAsyncThunk,
|
|
80
87
|
createSelector as __createSelector,
|
|
81
88
|
createSlice as __createSlice,
|
|
82
89
|
createStore as __createStore,
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n\tReducersMapObject,\n\tconfigureStore,\n\tcombineReducers,\n\tMiddleware,\n\tStore,\n\tSlice,\n\tAnyAction,\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} 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\ninterface SlicesMap {\n\t[key: Slice['name']]: Slice;\n}\n\nlet instance: Store | 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\nconst dispatch = ( action: AnyAction ) => {\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: Array.from( middlewares ),\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;\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,
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import {\n\tReducersMapObject,\n\tconfigureStore,\n\tcombineReducers,\n\tMiddleware,\n\tStore,\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} 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: Store | 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\nconst dispatch = ( action: AnyAction ) => {\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,OAQM;AAcP;AAAA,EACmB;AAAA,EACH;AAAA,EACK;AAAA,OACd;AAEP;AAAA,EACgB;AAAA,EACA;AAAA,EACH;AAAA,OACN;AAiCP,IAAI,WAAyB;AAC7B,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;AAEA,IAAM,WAAW,CAAE,WAAuB;AACzC,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":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elementor/store",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": "Elementor Team",
|
|
6
6
|
"homepage": "https://elementor.com/",
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"@reduxjs/toolkit": "^1.9.7",
|
|
39
39
|
"react-redux": "^8.1.3"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "144060e529eb9200b59668732922a5635e75d882"
|
|
42
42
|
}
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
__StoreProvider as StoreProvider,
|
|
13
13
|
__useSelector as useSelector,
|
|
14
14
|
__useDispatch as useDispatch,
|
|
15
|
+
__createAsyncThunk,
|
|
15
16
|
Dispatch,
|
|
16
17
|
AnyAction,
|
|
17
18
|
} from '../index';
|
|
@@ -273,4 +274,48 @@ describe( '@elementor/store', () => {
|
|
|
273
274
|
// Assert.
|
|
274
275
|
expect( result.current ).toBe( 8 );
|
|
275
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
|
+
} );
|
|
276
321
|
} );
|
package/src/index.ts
CHANGED
|
@@ -6,6 +6,9 @@ import {
|
|
|
6
6
|
Store,
|
|
7
7
|
Slice,
|
|
8
8
|
AnyAction,
|
|
9
|
+
EnhancedStore,
|
|
10
|
+
StoreEnhancer,
|
|
11
|
+
ThunkMiddleware,
|
|
9
12
|
} from '@reduxjs/toolkit';
|
|
10
13
|
|
|
11
14
|
export type {
|
|
@@ -23,6 +26,7 @@ export type {
|
|
|
23
26
|
export {
|
|
24
27
|
createSelector as __createSelector,
|
|
25
28
|
createSlice as __createSlice,
|
|
29
|
+
createAsyncThunk as __createAsyncThunk,
|
|
26
30
|
} from '@reduxjs/toolkit';
|
|
27
31
|
|
|
28
32
|
export {
|
|
@@ -44,6 +48,20 @@ export type SliceState<S extends Slice> = {
|
|
|
44
48
|
[ key in S['name'] ]: ReturnType<S['getInitialState']>;
|
|
45
49
|
}
|
|
46
50
|
|
|
51
|
+
// The `configureStore` function from Redux Toolkit infers its actions from the `reducers`
|
|
52
|
+
// key of the initialization object. This is fine when creating the store statically, but
|
|
53
|
+
// breaks in our case since we create the store dynamically, which means that TypeScript
|
|
54
|
+
// can't infer the types. Therefore, we force the store to accept any actions using a
|
|
55
|
+
// generic store type.
|
|
56
|
+
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
58
|
+
type AnyStore<State = any> = EnhancedStore<
|
|
59
|
+
State,
|
|
60
|
+
AnyAction,
|
|
61
|
+
[ThunkMiddleware<State, AnyAction>],
|
|
62
|
+
[StoreEnhancer]
|
|
63
|
+
>
|
|
64
|
+
|
|
47
65
|
interface SlicesMap {
|
|
48
66
|
[key: Slice['name']]: Slice;
|
|
49
67
|
}
|
|
@@ -108,7 +126,12 @@ const createStore = () => {
|
|
|
108
126
|
|
|
109
127
|
instance = configureStore( {
|
|
110
128
|
reducer: getReducers(),
|
|
111
|
-
middleware:
|
|
129
|
+
middleware: ( getDefaultMiddleware ) => {
|
|
130
|
+
return [
|
|
131
|
+
...getDefaultMiddleware(),
|
|
132
|
+
...Array.from( middlewares ),
|
|
133
|
+
];
|
|
134
|
+
},
|
|
112
135
|
} );
|
|
113
136
|
|
|
114
137
|
if ( pendingActions.length ) {
|
|
@@ -116,7 +139,7 @@ const createStore = () => {
|
|
|
116
139
|
pendingActions.length = 0;
|
|
117
140
|
}
|
|
118
141
|
|
|
119
|
-
return instance;
|
|
142
|
+
return instance as AnyStore;
|
|
120
143
|
};
|
|
121
144
|
|
|
122
145
|
const getStore = () => {
|