From 2fe82e02cbc3b9114a4e1ea6ea44a62a7ba6f913 Mon Sep 17 00:00:00 2001 From: Zheng Song <41896553+szhsin@users.noreply.github.com> Date: Sun, 8 Jan 2023 22:07:40 +1100 Subject: [PATCH 1/5] Disable redux dev tools in production environment --- src/middleware/reduxDevtools.ts | 6 +++++- src/plugin/reduxDevtools.ts | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/middleware/reduxDevtools.ts b/src/middleware/reduxDevtools.ts index fa489aa..e10b4b9 100644 --- a/src/middleware/reduxDevtools.ts +++ b/src/middleware/reduxDevtools.ts @@ -5,7 +5,11 @@ type ReduxDevtools = (options?: { name?: string }) => Middleware; const reduxDevtools: ReduxDevtools = ({ name } = {}) => { let devtoolsExt: Window['__REDUX_DEVTOOLS_EXTENSION__']; - if (typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__)) + if ( + process.env.NODE_ENV === 'production' || + typeof window === 'undefined' || + !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__) + ) return ({ set }) => set; const devtools = devtoolsExt.connect({ name }); diff --git a/src/plugin/reduxDevtools.ts b/src/plugin/reduxDevtools.ts index f9d7262..6cfee3d 100644 --- a/src/plugin/reduxDevtools.ts +++ b/src/plugin/reduxDevtools.ts @@ -5,7 +5,11 @@ type ReduxDevtools = (options?: { name?: string }) => Plugin; const reduxDevtools: ReduxDevtools = ({ name } = {}) => { let devtoolsExt: Window['__REDUX_DEVTOOLS_EXTENSION__']; - if (typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__)) + if ( + process.env.NODE_ENV === 'production' || + typeof window === 'undefined' || + !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__) + ) return () => { /*do nothing*/ }; From ebb3f709f47b2763418c851c133e424dd2b6c027 Mon Sep 17 00:00:00 2001 From: Zheng Song <41896553+szhsin@users.noreply.github.com> Date: Sun, 8 Jan 2023 23:18:00 +1100 Subject: [PATCH 2/5] Allow applyMiddleware to accept undefined value --- src/middleware/applyMiddleware.ts | 4 ++-- src/middleware/reduxDevtools.ts | 4 ++-- src/plugin/applyPlugin.ts | 4 ++-- src/plugin/reduxDevtools.ts | 6 ++---- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/middleware/applyMiddleware.ts b/src/middleware/applyMiddleware.ts index d780150..ba85b01 100644 --- a/src/middleware/applyMiddleware.ts +++ b/src/middleware/applyMiddleware.ts @@ -1,13 +1,13 @@ import type { Middleware } from '../common'; const applyMiddleware: ( - middlewares: Middleware[], + middlewares: (Middleware | undefined)[], options?: { fromRight?: boolean } ) => Middleware = (middlewares, { fromRight } = {}) => (api, config) => middlewares[fromRight ? 'reduceRight' : 'reduce']( - (set, middleware) => middleware({ ...api, set }, config), + (set, middleware) => (middleware ? middleware({ ...api, set }, config) : set), api.set ); diff --git a/src/middleware/reduxDevtools.ts b/src/middleware/reduxDevtools.ts index e10b4b9..f7a9133 100644 --- a/src/middleware/reduxDevtools.ts +++ b/src/middleware/reduxDevtools.ts @@ -1,7 +1,7 @@ import type {} from '@redux-devtools/extension'; import type { Middleware } from '../common'; -type ReduxDevtools = (options?: { name?: string }) => Middleware; +type ReduxDevtools = (options?: { name?: string }) => Middleware | undefined; const reduxDevtools: ReduxDevtools = ({ name } = {}) => { let devtoolsExt: Window['__REDUX_DEVTOOLS_EXTENSION__']; @@ -10,7 +10,7 @@ const reduxDevtools: ReduxDevtools = ({ name } = {}) => { typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__) ) - return ({ set }) => set; + return; const devtools = devtoolsExt.connect({ name }); const mergedState: { [index: string]: unknown } = {}; diff --git a/src/plugin/applyPlugin.ts b/src/plugin/applyPlugin.ts index 574939d..0f296df 100644 --- a/src/plugin/applyPlugin.ts +++ b/src/plugin/applyPlugin.ts @@ -1,6 +1,6 @@ import type { Plugin } from '../common'; -const applyPlugin: (plugins: Plugin[]) => Plugin = (plugins) => (reactish, config) => - plugins.forEach((plugin) => plugin(reactish, config)); +const applyPlugin: (plugins: (Plugin | undefined)[]) => Plugin = (plugins) => (reactish, config) => + plugins.forEach((plugin) => plugin?.(reactish, config)); export { applyPlugin }; diff --git a/src/plugin/reduxDevtools.ts b/src/plugin/reduxDevtools.ts index 6cfee3d..7e729c1 100644 --- a/src/plugin/reduxDevtools.ts +++ b/src/plugin/reduxDevtools.ts @@ -1,7 +1,7 @@ import type {} from '@redux-devtools/extension'; import type { Plugin } from '../common'; -type ReduxDevtools = (options?: { name?: string }) => Plugin; +type ReduxDevtools = (options?: { name?: string }) => Plugin | undefined; const reduxDevtools: ReduxDevtools = ({ name } = {}) => { let devtoolsExt: Window['__REDUX_DEVTOOLS_EXTENSION__']; @@ -10,9 +10,7 @@ const reduxDevtools: ReduxDevtools = ({ name } = {}) => { typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__) ) - return () => { - /*do nothing*/ - }; + return; const devtools = devtoolsExt.connect({ name }); const mergedState: { [index: string]: unknown } = {}; From c41469e3bab2e8f754e5dcd2c871117664c7899d Mon Sep 17 00:00:00 2001 From: Zheng Song <41896553+szhsin@users.noreply.github.com> Date: Sun, 8 Jan 2023 23:18:25 +1100 Subject: [PATCH 3/5] Update unit tests --- src/__tests__/middleware/applyMiddleware.test.ts | 2 +- src/__tests__/middleware/reduxDevtools.test.ts | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/__tests__/middleware/applyMiddleware.test.ts b/src/__tests__/middleware/applyMiddleware.test.ts index c13c74b..826320a 100644 --- a/src/__tests__/middleware/applyMiddleware.test.ts +++ b/src/__tests__/middleware/applyMiddleware.test.ts @@ -11,7 +11,7 @@ const createMiddleware: (arg: unknown) => Middleware = middleware(arg, ...args); }; -const middlewares = [createMiddleware(1), createMiddleware(2), createMiddleware(3)]; +const middlewares = [createMiddleware(1), createMiddleware(2), createMiddleware(3), undefined]; test('applyMiddleware from left', () => { const state = createState({ diff --git a/src/__tests__/middleware/reduxDevtools.test.ts b/src/__tests__/middleware/reduxDevtools.test.ts index 1510146..461b752 100644 --- a/src/__tests__/middleware/reduxDevtools.test.ts +++ b/src/__tests__/middleware/reduxDevtools.test.ts @@ -52,8 +52,7 @@ describe('reduxDevtools', () => { }); }); -test('reduxDevtools should return original set when there is no redux devtools extension', () => { - const set = jest.fn(); +test('reduxDevtools should do nothing when there is no redux devtools extension', () => { // eslint-disable-next-line - expect(reduxDevtools()({ set } as any)).toBe(set); + expect(reduxDevtools()).toBeUndefined(); }); From 4b5a14f39f72064adeeff0eeb497aea564965fa9 Mon Sep 17 00:00:00 2001 From: Zheng Song <41896553+szhsin@users.noreply.github.com> Date: Sun, 8 Jan 2023 23:30:23 +1100 Subject: [PATCH 4/5] Rename filters in todo example --- examples/examples/todo/Filters.tsx | 2 +- examples/examples/todo/todo.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/examples/todo/Filters.tsx b/examples/examples/todo/Filters.tsx index c0c87b4..d8d99e6 100644 --- a/examples/examples/todo/Filters.tsx +++ b/examples/examples/todo/Filters.tsx @@ -2,7 +2,7 @@ import { useSnapshot } from 'reactish-state'; import { visibilityFilterState, VisibilityFilter } from './todo'; import styles from './styles.module.css'; -const filterList: VisibilityFilter[] = ['ALL', 'IN_PROGRESS', 'COMPLETED']; +const filterList: VisibilityFilter[] = ['ALL', 'ACTIVE', 'COMPLETED']; const Filters = () => { const visibilityFilter = useSnapshot(visibilityFilterState); diff --git a/examples/examples/todo/todo.ts b/examples/examples/todo/todo.ts index a5ad155..8ba7a2e 100644 --- a/examples/examples/todo/todo.ts +++ b/examples/examples/todo/todo.ts @@ -39,7 +39,7 @@ const todoListState = state( { key: 'todoList' } ); -type VisibilityFilter = 'ALL' | 'COMPLETED' | 'IN_PROGRESS'; +type VisibilityFilter = 'ALL' | 'ACTIVE' | 'COMPLETED'; const visibilityFilterState = state('ALL' as VisibilityFilter, null, { key: 'filter' }); const selector = createSelector({ plugin: devtoolsPlugin({ name: 'todoApp-selector' }) }); @@ -52,7 +52,7 @@ const visibleTodoList = selector( return todoList; case 'COMPLETED': return todoList.filter(({ isCompleted }) => isCompleted); - case 'IN_PROGRESS': + case 'ACTIVE': return todoList.filter(({ isCompleted }) => !isCompleted); } }, From 713d9bc97d724e201c00cc7963bfe6fbdd290048 Mon Sep 17 00:00:00 2001 From: Zheng Song <41896553+szhsin@users.noreply.github.com> Date: Sun, 8 Jan 2023 23:30:46 +1100 Subject: [PATCH 5/5] 0.9.2 --- dist/middleware/cjs/index.js | 15 ++++++--------- dist/middleware/es/applyMiddleware.js | 4 ++-- dist/middleware/es/reduxDevtools.js | 11 ++++------- dist/plugin/cjs/index.js | 6 ++---- dist/plugin/es/applyPlugin.js | 2 +- dist/plugin/es/reduxDevtools.js | 4 +--- package-lock.json | 16 ++++++++-------- package.json | 5 +++-- types/middleware/applyMiddleware.d.ts | 2 +- types/middleware/reduxDevtools.d.ts | 2 +- types/plugin/applyPlugin.d.ts | 2 +- types/plugin/reduxDevtools.d.ts | 2 +- 12 files changed, 31 insertions(+), 40 deletions(-) diff --git a/dist/middleware/cjs/index.js b/dist/middleware/cjs/index.js index d702cd1..3e0ce23 100644 --- a/dist/middleware/cjs/index.js +++ b/dist/middleware/cjs/index.js @@ -22,9 +22,9 @@ var applyMiddleware = function applyMiddleware(middlewares, _temp) { fromRight = _ref.fromRight; return function (api, config) { return middlewares[fromRight ? 'reduceRight' : 'reduce'](function (set, middleware) { - return middleware(_extends({}, api, { + return middleware ? middleware(_extends({}, api, { set: set - }), config); + }), config) : set; }, api.set); }; }; @@ -75,17 +75,14 @@ var reduxDevtools = function reduxDevtools(_temp) { var _ref = _temp === void 0 ? {} : _temp, name = _ref.name; var devtoolsExt; - if (typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__)) return function (_ref2) { - var set = _ref2.set; - return set; - }; + if (process.env.NODE_ENV === 'production' || typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__)) return; var devtools = devtoolsExt.connect({ name: name }); var mergedState = {}; - return function (_ref3, config) { - var set = _ref3.set, - get = _ref3.get; + return function (_ref2, config) { + var set = _ref2.set, + get = _ref2.get; var key = config == null ? void 0 : config.key; if (!key) throw new Error('[reactish-state] state should be provided with a string `key` in the config object when the `reduxDevtools` middleware is used.'); mergedState[key] = get(); diff --git a/dist/middleware/es/applyMiddleware.js b/dist/middleware/es/applyMiddleware.js index b79c7f5..5b4de28 100644 --- a/dist/middleware/es/applyMiddleware.js +++ b/dist/middleware/es/applyMiddleware.js @@ -5,9 +5,9 @@ var applyMiddleware = function applyMiddleware(middlewares, _temp) { fromRight = _ref.fromRight; return function (api, config) { return middlewares[fromRight ? 'reduceRight' : 'reduce'](function (set, middleware) { - return middleware(_extends({}, api, { + return middleware ? middleware(_extends({}, api, { set: set - }), config); + }), config) : set; }, api.set); }; }; diff --git a/dist/middleware/es/reduxDevtools.js b/dist/middleware/es/reduxDevtools.js index de85cd2..ef9db0c 100644 --- a/dist/middleware/es/reduxDevtools.js +++ b/dist/middleware/es/reduxDevtools.js @@ -2,17 +2,14 @@ var reduxDevtools = function reduxDevtools(_temp) { var _ref = _temp === void 0 ? {} : _temp, name = _ref.name; var devtoolsExt; - if (typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__)) return function (_ref2) { - var set = _ref2.set; - return set; - }; + if (process.env.NODE_ENV === 'production' || typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__)) return; var devtools = devtoolsExt.connect({ name: name }); var mergedState = {}; - return function (_ref3, config) { - var set = _ref3.set, - get = _ref3.get; + return function (_ref2, config) { + var set = _ref2.set, + get = _ref2.get; var key = config == null ? void 0 : config.key; if (!key) throw new Error('[reactish-state] state should be provided with a string `key` in the config object when the `reduxDevtools` middleware is used.'); mergedState[key] = get(); diff --git a/dist/plugin/cjs/index.js b/dist/plugin/cjs/index.js index 5122c67..a0291c2 100644 --- a/dist/plugin/cjs/index.js +++ b/dist/plugin/cjs/index.js @@ -3,7 +3,7 @@ var applyPlugin = function applyPlugin(plugins) { return function (reactish, config) { return plugins.forEach(function (plugin) { - return plugin(reactish, config); + return plugin == null ? void 0 : plugin(reactish, config); }); }; }; @@ -12,9 +12,7 @@ var reduxDevtools = function reduxDevtools(_temp) { var _ref = _temp === void 0 ? {} : _temp, name = _ref.name; var devtoolsExt; - if (typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__)) return function () { - /*do nothing*/ - }; + if (process.env.NODE_ENV === 'production' || typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__)) return; var devtools = devtoolsExt.connect({ name: name }); diff --git a/dist/plugin/es/applyPlugin.js b/dist/plugin/es/applyPlugin.js index ce30287..b6c962c 100644 --- a/dist/plugin/es/applyPlugin.js +++ b/dist/plugin/es/applyPlugin.js @@ -1,7 +1,7 @@ var applyPlugin = function applyPlugin(plugins) { return function (reactish, config) { return plugins.forEach(function (plugin) { - return plugin(reactish, config); + return plugin == null ? void 0 : plugin(reactish, config); }); }; }; diff --git a/dist/plugin/es/reduxDevtools.js b/dist/plugin/es/reduxDevtools.js index 807d2ff..3769e2a 100644 --- a/dist/plugin/es/reduxDevtools.js +++ b/dist/plugin/es/reduxDevtools.js @@ -2,9 +2,7 @@ var reduxDevtools = function reduxDevtools(_temp) { var _ref = _temp === void 0 ? {} : _temp, name = _ref.name; var devtoolsExt; - if (typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__)) return function () { - /*do nothing*/ - }; + if (process.env.NODE_ENV === 'production' || typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__)) return; var devtools = devtoolsExt.connect({ name: name }); diff --git a/package-lock.json b/package-lock.json index 93693d3..5efc705 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "reactish-state", - "version": "0.9.1", + "version": "0.9.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "reactish-state", - "version": "0.9.1", + "version": "0.9.2", "license": "MIT", "dependencies": { "use-sync-external-store": "^1.2.0" @@ -7835,9 +7835,9 @@ "dev": true }, "node_modules/json5": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz", - "integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==", + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", + "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "dev": true, "bin": { "json5": "lib/cli.js" @@ -15746,9 +15746,9 @@ "dev": true }, "json5": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz", - "integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==", + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", + "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "dev": true }, "jsx-ast-utils": { diff --git a/package.json b/package.json index c5efa93..972a832 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "reactish-state", - "version": "0.9.1", + "version": "0.9.2", "description": "", "author": "Zheng Song", "license": "MIT", @@ -11,7 +11,8 @@ "files": [ "dist/", "types/", - "middleware" + "middleware/", + "plugin/" ], "scripts": { "start": "run-p watch \"types -- --watch\"", diff --git a/types/middleware/applyMiddleware.d.ts b/types/middleware/applyMiddleware.d.ts index 2b00b91..b4e84bb 100644 --- a/types/middleware/applyMiddleware.d.ts +++ b/types/middleware/applyMiddleware.d.ts @@ -1,5 +1,5 @@ import type { Middleware } from '../common'; -declare const applyMiddleware: (middlewares: Middleware[], options?: { +declare const applyMiddleware: (middlewares: (Middleware | undefined)[], options?: { fromRight?: boolean; }) => Middleware; export { applyMiddleware }; diff --git a/types/middleware/reduxDevtools.d.ts b/types/middleware/reduxDevtools.d.ts index 9b54bf3..6b37cb9 100644 --- a/types/middleware/reduxDevtools.d.ts +++ b/types/middleware/reduxDevtools.d.ts @@ -1,6 +1,6 @@ import type { Middleware } from '../common'; declare type ReduxDevtools = (options?: { name?: string; -}) => Middleware; +}) => Middleware | undefined; declare const reduxDevtools: ReduxDevtools; export { reduxDevtools }; diff --git a/types/plugin/applyPlugin.d.ts b/types/plugin/applyPlugin.d.ts index 4a78069..6866ffd 100644 --- a/types/plugin/applyPlugin.d.ts +++ b/types/plugin/applyPlugin.d.ts @@ -1,3 +1,3 @@ import type { Plugin } from '../common'; -declare const applyPlugin: (plugins: Plugin[]) => Plugin; +declare const applyPlugin: (plugins: (Plugin | undefined)[]) => Plugin; export { applyPlugin }; diff --git a/types/plugin/reduxDevtools.d.ts b/types/plugin/reduxDevtools.d.ts index aacd9b5..75efe1e 100644 --- a/types/plugin/reduxDevtools.d.ts +++ b/types/plugin/reduxDevtools.d.ts @@ -1,6 +1,6 @@ import type { Plugin } from '../common'; declare type ReduxDevtools = (options?: { name?: string; -}) => Plugin; +}) => Plugin | undefined; declare const reduxDevtools: ReduxDevtools; export { reduxDevtools };