Skip to content

Commit

Permalink
Merge pull request #17 from szhsin/feat/prod-devtools
Browse files Browse the repository at this point in the history
feat: prod devtools
  • Loading branch information
szhsin authored Jan 8, 2023
2 parents feb021c + 713d9bc commit 55bba92
Show file tree
Hide file tree
Showing 20 changed files with 55 additions and 59 deletions.
15 changes: 6 additions & 9 deletions dist/middleware/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
};
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions dist/middleware/es/applyMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
};
Expand Down
11 changes: 4 additions & 7 deletions dist/middleware/es/reduxDevtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 2 additions & 4 deletions dist/plugin/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
};
};
Expand All @@ -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
});
Expand Down
2 changes: 1 addition & 1 deletion dist/plugin/es/applyPlugin.js
Original file line number Diff line number Diff line change
@@ -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);
});
};
};
Expand Down
4 changes: 1 addition & 3 deletions dist/plugin/es/reduxDevtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/todo/Filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/todo/todo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }) });
Expand All @@ -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);
}
},
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reactish-state",
"version": "0.9.1",
"version": "0.9.2",
"description": "",
"author": "Zheng Song",
"license": "MIT",
Expand All @@ -11,7 +11,8 @@
"files": [
"dist/",
"types/",
"middleware"
"middleware/",
"plugin/"
],
"scripts": {
"start": "run-p watch \"types -- --watch\"",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/middleware/applyMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
5 changes: 2 additions & 3 deletions src/__tests__/middleware/reduxDevtools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
4 changes: 2 additions & 2 deletions src/middleware/applyMiddleware.ts
Original file line number Diff line number Diff line change
@@ -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
);

Expand Down
10 changes: 7 additions & 3 deletions src/middleware/reduxDevtools.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
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__'];
if (typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__))
return ({ set }) => set;
if (
process.env.NODE_ENV === 'production' ||
typeof window === 'undefined' ||
!(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__)
)
return;

const devtools = devtoolsExt.connect({ name });
const mergedState: { [index: string]: unknown } = {};
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/applyPlugin.ts
Original file line number Diff line number Diff line change
@@ -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 };
12 changes: 7 additions & 5 deletions src/plugin/reduxDevtools.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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__'];
if (typeof window === 'undefined' || !(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__))
return () => {
/*do nothing*/
};
if (
process.env.NODE_ENV === 'production' ||
typeof window === 'undefined' ||
!(devtoolsExt = window.__REDUX_DEVTOOLS_EXTENSION__)
)
return;

const devtools = devtoolsExt.connect({ name });
const mergedState: { [index: string]: unknown } = {};
Expand Down
2 changes: 1 addition & 1 deletion types/middleware/applyMiddleware.d.ts
Original file line number Diff line number Diff line change
@@ -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 };
2 changes: 1 addition & 1 deletion types/middleware/reduxDevtools.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Middleware } from '../common';
declare type ReduxDevtools = (options?: {
name?: string;
}) => Middleware;
}) => Middleware | undefined;
declare const reduxDevtools: ReduxDevtools;
export { reduxDevtools };
2 changes: 1 addition & 1 deletion types/plugin/applyPlugin.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { Plugin } from '../common';
declare const applyPlugin: (plugins: Plugin[]) => Plugin;
declare const applyPlugin: (plugins: (Plugin | undefined)[]) => Plugin;
export { applyPlugin };
2 changes: 1 addition & 1 deletion types/plugin/reduxDevtools.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Plugin } from '../common';
declare type ReduxDevtools = (options?: {
name?: string;
}) => Plugin;
}) => Plugin | undefined;
declare const reduxDevtools: ReduxDevtools;
export { reduxDevtools };

0 comments on commit 55bba92

Please sign in to comment.