@coaction/redux 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Michael Lin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # @coaction/redux
2
+
3
+ ![Node CI](https://github.com/unadlib/coaction/workflows/Node%20CI/badge.svg)
4
+ [![npm](https://img.shields.io/npm/v/@coaction/redux.svg)](https://www.npmjs.com/package/@coaction/redux)
5
+ ![license](https://img.shields.io/npm/l/@coaction/redux)
6
+
7
+ A Coaction integration tool for Redux Toolkit.
8
+
9
+ ## Installation
10
+
11
+ You can install it via npm, yarn or pnpm.
12
+
13
+ ```sh
14
+ npm install coaction @coaction/redux @reduxjs/toolkit
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```ts
20
+ import { create } from 'coaction';
21
+ import {
22
+ adapt,
23
+ bindRedux,
24
+ createSlice,
25
+ configureStore,
26
+ withCoactionReducer
27
+ } from '@coaction/redux';
28
+
29
+ const counterSlice = createSlice({
30
+ name: 'counter',
31
+ initialState: { count: 0 },
32
+ reducers: {
33
+ increment(state) {
34
+ state.count += 1;
35
+ }
36
+ }
37
+ });
38
+
39
+ const reduxStore = configureStore({
40
+ reducer: withCoactionReducer(counterSlice.reducer)
41
+ });
42
+
43
+ const store = create(() => adapt(bindRedux(reduxStore)));
44
+ store.getState().dispatch(counterSlice.actions.increment());
45
+ ```
46
+
47
+ ## Limitations
48
+
49
+ - `@coaction/redux` only supports binding a whole Redux store.
50
+ - Coaction `Slices` mode is not supported in this adapter.
51
+
52
+ ## Documentation
53
+
54
+ You can find the documentation [here](https://github.com/unadlib/coaction).
@@ -0,0 +1,27 @@
1
+ import { AnyAction, Store, Reducer } from '@reduxjs/toolkit';
2
+ export * from '@reduxjs/toolkit';
3
+
4
+ declare const COACTION_REDUX_REPLACE = "@@coaction/redux/replace";
5
+ type ReplaceStateAction<S> = {
6
+ type: typeof COACTION_REDUX_REPLACE;
7
+ payload: S;
8
+ };
9
+ declare function replaceStateAction<S>(payload: S): ReplaceStateAction<S>;
10
+ declare const withCoactionReducer: <S, A extends AnyAction = AnyAction>(reducer: Reducer<S, A>) => Reducer<S, A | ReplaceStateAction<S>>;
11
+ type BoundReduxStore<S extends object, A extends AnyAction> = Store<S, A> & {
12
+ getState: () => BoundReduxState<S, A>;
13
+ };
14
+ type BoundReduxState<S extends object, A extends AnyAction> = S & {
15
+ dispatch: Store<S, A>['dispatch'];
16
+ };
17
+ /**
18
+ * Bind a redux toolkit store to coaction.
19
+ */
20
+ declare const bindRedux: <S extends object, A extends AnyAction = AnyAction>(reduxStore: Store<S, A>) => BoundReduxStore<S, A>;
21
+ /**
22
+ * Adapt a redux store type to state type.
23
+ */
24
+ declare function adapt<T extends object, A extends AnyAction = AnyAction>(store: BoundReduxStore<T, A>): BoundReduxState<T, A>;
25
+ declare function adapt<T extends object>(store: Store<T>): T;
26
+
27
+ export { COACTION_REDUX_REPLACE, type ReplaceStateAction, adapt, bindRedux, replaceStateAction, withCoactionReducer };
@@ -0,0 +1,27 @@
1
+ import { AnyAction, Store, Reducer } from '@reduxjs/toolkit';
2
+ export * from '@reduxjs/toolkit';
3
+
4
+ declare const COACTION_REDUX_REPLACE = "@@coaction/redux/replace";
5
+ type ReplaceStateAction<S> = {
6
+ type: typeof COACTION_REDUX_REPLACE;
7
+ payload: S;
8
+ };
9
+ declare function replaceStateAction<S>(payload: S): ReplaceStateAction<S>;
10
+ declare const withCoactionReducer: <S, A extends AnyAction = AnyAction>(reducer: Reducer<S, A>) => Reducer<S, A | ReplaceStateAction<S>>;
11
+ type BoundReduxStore<S extends object, A extends AnyAction> = Store<S, A> & {
12
+ getState: () => BoundReduxState<S, A>;
13
+ };
14
+ type BoundReduxState<S extends object, A extends AnyAction> = S & {
15
+ dispatch: Store<S, A>['dispatch'];
16
+ };
17
+ /**
18
+ * Bind a redux toolkit store to coaction.
19
+ */
20
+ declare const bindRedux: <S extends object, A extends AnyAction = AnyAction>(reduxStore: Store<S, A>) => BoundReduxStore<S, A>;
21
+ /**
22
+ * Adapt a redux store type to state type.
23
+ */
24
+ declare function adapt<T extends object, A extends AnyAction = AnyAction>(store: BoundReduxStore<T, A>): BoundReduxState<T, A>;
25
+ declare function adapt<T extends object>(store: Store<T>): T;
26
+
27
+ export { COACTION_REDUX_REPLACE, type ReplaceStateAction, adapt, bindRedux, replaceStateAction, withCoactionReducer };
package/dist/index.js ADDED
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ COACTION_REDUX_REPLACE: () => COACTION_REDUX_REPLACE,
25
+ adapt: () => adapt,
26
+ bindRedux: () => bindRedux,
27
+ replaceStateAction: () => replaceStateAction,
28
+ withCoactionReducer: () => withCoactionReducer
29
+ });
30
+ module.exports = __toCommonJS(index_exports);
31
+
32
+ // src/index.ts
33
+ var src_exports = {};
34
+ __export(src_exports, {
35
+ COACTION_REDUX_REPLACE: () => COACTION_REDUX_REPLACE,
36
+ adapt: () => adapt,
37
+ bindRedux: () => bindRedux,
38
+ replaceStateAction: () => replaceStateAction,
39
+ withCoactionReducer: () => withCoactionReducer
40
+ });
41
+ var import_coaction = require("coaction");
42
+ __reExport(src_exports, require("@reduxjs/toolkit"));
43
+ var COACTION_REDUX_REPLACE = "@@coaction/redux/replace";
44
+ function stripFunctions(value) {
45
+ if (Array.isArray(value)) {
46
+ return value.map((item) => stripFunctions(item));
47
+ }
48
+ if (typeof value === "object" && value !== null) {
49
+ const next = {};
50
+ for (const key in value) {
51
+ const child = value[key];
52
+ if (typeof child === "function") {
53
+ continue;
54
+ }
55
+ next[key] = stripFunctions(child);
56
+ }
57
+ return next;
58
+ }
59
+ return value;
60
+ }
61
+ function replaceStateAction(payload) {
62
+ return {
63
+ type: COACTION_REDUX_REPLACE,
64
+ payload
65
+ };
66
+ }
67
+ var withCoactionReducer = (reducer) => (state, action) => {
68
+ if (action.type === COACTION_REDUX_REPLACE) {
69
+ return stripFunctions(action.payload);
70
+ }
71
+ return reducer(state, action);
72
+ };
73
+ var bindRedux = (reduxStore) => {
74
+ const originalGetState = reduxStore.getState.bind(reduxStore);
75
+ let isReduxUpdating = false;
76
+ let isCoactionUpdating = false;
77
+ const bindState = (0, import_coaction.createBinder)({
78
+ handleStore: (coactionStore, rawState, state, internal) => {
79
+ if (coactionStore.share === "client") {
80
+ throw new Error("client redux store cannot be updated");
81
+ }
82
+ reduxStore.subscribe(() => {
83
+ if (isCoactionUpdating) {
84
+ return;
85
+ }
86
+ isReduxUpdating = true;
87
+ try {
88
+ coactionStore.setState(reduxStore.getState());
89
+ } finally {
90
+ isReduxUpdating = false;
91
+ }
92
+ });
93
+ internal.updateImmutable = (nextState) => {
94
+ if (isReduxUpdating) {
95
+ return;
96
+ }
97
+ isCoactionUpdating = true;
98
+ try {
99
+ reduxStore.dispatch(replaceStateAction(nextState));
100
+ } finally {
101
+ isCoactionUpdating = false;
102
+ }
103
+ };
104
+ },
105
+ handleState: ((state) => {
106
+ const copyState = Object.defineProperties(
107
+ {},
108
+ {
109
+ ...Object.getOwnPropertyDescriptors(state),
110
+ dispatch: {
111
+ enumerable: false,
112
+ configurable: true,
113
+ writable: false,
114
+ value: reduxStore.dispatch.bind(reduxStore)
115
+ }
116
+ }
117
+ );
118
+ return {
119
+ copyState,
120
+ bind: (rawState) => rawState
121
+ };
122
+ })
123
+ });
124
+ const store = reduxStore;
125
+ store.getState = () => bindState(originalGetState());
126
+ return store;
127
+ };
128
+ function adapt(store) {
129
+ return store;
130
+ }
131
+
132
+ // index.ts
133
+ __reExport(index_exports, src_exports, module.exports);
134
+ // Annotate the CommonJS export names for ESM import in node:
135
+ 0 && (module.exports = {
136
+ COACTION_REDUX_REPLACE,
137
+ adapt,
138
+ bindRedux,
139
+ replaceStateAction,
140
+ withCoactionReducer
141
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,138 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
+
19
+ // index.ts
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
+ COACTION_REDUX_REPLACE: () => COACTION_REDUX_REPLACE,
23
+ adapt: () => adapt,
24
+ bindRedux: () => bindRedux,
25
+ replaceStateAction: () => replaceStateAction,
26
+ withCoactionReducer: () => withCoactionReducer
27
+ });
28
+
29
+ // src/index.ts
30
+ var src_exports = {};
31
+ __export(src_exports, {
32
+ COACTION_REDUX_REPLACE: () => COACTION_REDUX_REPLACE,
33
+ adapt: () => adapt,
34
+ bindRedux: () => bindRedux,
35
+ replaceStateAction: () => replaceStateAction,
36
+ withCoactionReducer: () => withCoactionReducer
37
+ });
38
+ __reExport(src_exports, toolkit_star);
39
+ import { createBinder } from "coaction";
40
+ import * as toolkit_star from "@reduxjs/toolkit";
41
+ var COACTION_REDUX_REPLACE = "@@coaction/redux/replace";
42
+ function stripFunctions(value) {
43
+ if (Array.isArray(value)) {
44
+ return value.map((item) => stripFunctions(item));
45
+ }
46
+ if (typeof value === "object" && value !== null) {
47
+ const next = {};
48
+ for (const key in value) {
49
+ const child = value[key];
50
+ if (typeof child === "function") {
51
+ continue;
52
+ }
53
+ next[key] = stripFunctions(child);
54
+ }
55
+ return next;
56
+ }
57
+ return value;
58
+ }
59
+ function replaceStateAction(payload) {
60
+ return {
61
+ type: COACTION_REDUX_REPLACE,
62
+ payload
63
+ };
64
+ }
65
+ var withCoactionReducer = (reducer) => (state, action) => {
66
+ if (action.type === COACTION_REDUX_REPLACE) {
67
+ return stripFunctions(action.payload);
68
+ }
69
+ return reducer(state, action);
70
+ };
71
+ var bindRedux = (reduxStore) => {
72
+ const originalGetState = reduxStore.getState.bind(reduxStore);
73
+ let isReduxUpdating = false;
74
+ let isCoactionUpdating = false;
75
+ const bindState = createBinder({
76
+ handleStore: (coactionStore, rawState, state, internal) => {
77
+ if (coactionStore.share === "client") {
78
+ throw new Error("client redux store cannot be updated");
79
+ }
80
+ reduxStore.subscribe(() => {
81
+ if (isCoactionUpdating) {
82
+ return;
83
+ }
84
+ isReduxUpdating = true;
85
+ try {
86
+ coactionStore.setState(reduxStore.getState());
87
+ } finally {
88
+ isReduxUpdating = false;
89
+ }
90
+ });
91
+ internal.updateImmutable = (nextState) => {
92
+ if (isReduxUpdating) {
93
+ return;
94
+ }
95
+ isCoactionUpdating = true;
96
+ try {
97
+ reduxStore.dispatch(replaceStateAction(nextState));
98
+ } finally {
99
+ isCoactionUpdating = false;
100
+ }
101
+ };
102
+ },
103
+ handleState: ((state) => {
104
+ const copyState = Object.defineProperties(
105
+ {},
106
+ {
107
+ ...Object.getOwnPropertyDescriptors(state),
108
+ dispatch: {
109
+ enumerable: false,
110
+ configurable: true,
111
+ writable: false,
112
+ value: reduxStore.dispatch.bind(reduxStore)
113
+ }
114
+ }
115
+ );
116
+ return {
117
+ copyState,
118
+ bind: (rawState) => rawState
119
+ };
120
+ })
121
+ });
122
+ const store = reduxStore;
123
+ store.getState = () => bindState(originalGetState());
124
+ return store;
125
+ };
126
+ function adapt(store) {
127
+ return store;
128
+ }
129
+
130
+ // index.ts
131
+ __reExport(index_exports, src_exports);
132
+ export {
133
+ COACTION_REDUX_REPLACE,
134
+ adapt,
135
+ bindRedux,
136
+ replaceStateAction,
137
+ withCoactionReducer
138
+ };
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@coaction/redux",
3
+ "version": "1.0.0",
4
+ "description": "A Coaction integration tool for Redux Toolkit",
5
+ "keywords": [
6
+ "state",
7
+ "coaction",
8
+ "redux",
9
+ "redux-toolkit"
10
+ ],
11
+ "authors": [
12
+ "Michael Lin <unadlib@gmail.com> (https://github.com/unadlib)"
13
+ ],
14
+ "homepage": "https://github.com/unadlib/coaction/tree/main/packages/coaction-redux#readme",
15
+ "license": "MIT",
16
+ "main": "dist/index.js",
17
+ "module": "dist/index.mjs",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.mjs",
22
+ "require": "./dist/index.js",
23
+ "default": "./dist/index.mjs"
24
+ },
25
+ "./package.json": "./package.json"
26
+ },
27
+ "types": "dist/index.d.ts",
28
+ "sideEffects": false,
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "https://github.com/unadlib/coaction.git",
35
+ "directory": "packages/coaction-redux"
36
+ },
37
+ "bugs": {
38
+ "url": "https://github.com/unadlib/coaction/issues"
39
+ },
40
+ "peerDependencies": {
41
+ "@reduxjs/toolkit": "^2.0.0",
42
+ "coaction": "^0.2.0"
43
+ },
44
+ "peerDependenciesMeta": {
45
+ "coaction": {
46
+ "optional": true
47
+ },
48
+ "@reduxjs/toolkit": {
49
+ "optional": true
50
+ }
51
+ },
52
+ "devDependencies": {
53
+ "@reduxjs/toolkit": "^2.10.1",
54
+ "coaction": "^0.2.0"
55
+ },
56
+ "publishConfig": {
57
+ "access": "public",
58
+ "provenance": true
59
+ },
60
+ "author": "unadlib",
61
+ "scripts": {
62
+ "clean": "rm -rf dist",
63
+ "test": "vitest run test",
64
+ "build": "tsup index.ts --format cjs,esm --dts --clean --out-dir dist",
65
+ "dev": "tsup index.ts --format cjs,esm --dts --watch --out-dir dist"
66
+ }
67
+ }