Skip to content

Commit

Permalink
Merge pull request #22 from szhsin/fix/persist-catch
Browse files Browse the repository at this point in the history
fix: persist exceptions
  • Loading branch information
szhsin authored Jan 28, 2023
2 parents 43aae5f + 3343dd6 commit e2a0acb
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 29 deletions.
2 changes: 0 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ module.exports = {
'plugin:@typescript-eslint/recommended-requiring-type-checking'
],
rules: {
'prefer-spread': 0,
'prefer-rest-params': 0,
'@typescript-eslint/no-non-null-assertion': 0
}
}
Expand Down
8 changes: 6 additions & 2 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ module.exports = {
setSpreadProperties: true,
skipForOfIteratorClosing: true
},
plugins: ['pure-annotations'],
plugins: [
'pure-annotations',
['@babel/plugin-proposal-object-rest-spread', { useBuiltIns: true }]
],
presets: [
[
'@babel/preset-env',
{
bugfixes: true,
include: [
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-nullish-coalescing-operator'
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-optional-catch-binding'
],
exclude: ['@babel/plugin-transform-typeof-symbol']
}
Expand Down
24 changes: 16 additions & 8 deletions dist/middleware/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

const applyMiddleware = (middlewares, {
fromRight
} = {}) => (api, config) => middlewares[fromRight ? 'reduceRight' : 'reduce']((set, middleware) => middleware ? middleware({
...api,
} = {}) => (api, config) => middlewares[fromRight ? 'reduceRight' : 'reduce']((set, middleware) => middleware ? middleware(Object.assign({}, api, {
set
}, config) : set, api.set);
}), config) : set, api.set);

const persist = ({
prefix,
Expand All @@ -22,13 +21,21 @@ const persist = ({
states.push([key, set]);
return (...args) => {
set(...args);
getStorage().setItem(key, JSON.stringify(get()));
try {
getStorage().setItem(key, JSON.stringify(get()));
} catch (_unused) {
/* continue regardless of error */
}
};
};
middleware.hydrate = () => {
states.forEach(([key, set]) => {
const value = getStorage().getItem(key);
value != null && set(value !== 'undefined' ? JSON.parse(value) : undefined, `HYDRATE_${key}`);
try {
const value = getStorage().getItem(key);
value != null && set(value !== 'undefined' ? JSON.parse(value) : undefined, `HYDRATE_${key}`);
} catch (_unused2) {
/* continue regardless of error */
}
});
states.length = 0;
};
Expand All @@ -52,8 +59,9 @@ const reduxDevtools = ({
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();
devtools.init(mergedState);
return function (value, action) {
set.apply(null, arguments);
return (...args) => {
const [value, action] = args;
set(...args);
mergedState[key] = get();
devtools.send(typeof action === 'string' ? {
type: action
Expand Down
5 changes: 2 additions & 3 deletions dist/middleware/esm/applyMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const applyMiddleware = (middlewares, {
fromRight
} = {}) => (api, config) => middlewares[fromRight ? 'reduceRight' : 'reduce']((set, middleware) => middleware ? middleware({
...api,
} = {}) => (api, config) => middlewares[fromRight ? 'reduceRight' : 'reduce']((set, middleware) => middleware ? middleware(Object.assign({}, api, {
set
}, config) : set, api.set);
}), config) : set, api.set);

export { applyMiddleware };
14 changes: 11 additions & 3 deletions dist/middleware/esm/persist.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ const persist = ({
states.push([key, set]);
return (...args) => {
set(...args);
getStorage().setItem(key, JSON.stringify(get()));
try {
getStorage().setItem(key, JSON.stringify(get()));
} catch (_unused) {
/* continue regardless of error */
}
};
};
middleware.hydrate = () => {
states.forEach(([key, set]) => {
const value = getStorage().getItem(key);
value != null && set(value !== 'undefined' ? JSON.parse(value) : undefined, `HYDRATE_${key}`);
try {
const value = getStorage().getItem(key);
value != null && set(value !== 'undefined' ? JSON.parse(value) : undefined, `HYDRATE_${key}`);
} catch (_unused2) {
/* continue regardless of error */
}
});
states.length = 0;
};
Expand Down
5 changes: 3 additions & 2 deletions dist/middleware/esm/reduxDevtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ const reduxDevtools = ({
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();
devtools.init(mergedState);
return function (value, action) {
set.apply(null, arguments);
return (...args) => {
const [value, action] = args;
set(...args);
mergedState[key] = get();
devtools.send(typeof action === 'string' ? {
type: action
Expand Down
2 changes: 1 addition & 1 deletion examples/package-lock.json

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

4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reactish-state",
"version": "0.10.3",
"version": "0.10.4",
"description": "Simple, decentralized state management for React.",
"author": "Zheng Song",
"license": "MIT",
Expand Down
15 changes: 12 additions & 3 deletions src/middleware/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@ const persist: Persist = ({ prefix, getStorage = () => localStorage } = {}) => {

return (...args) => {
set(...args);
getStorage().setItem(key, JSON.stringify(get()));
try {
getStorage().setItem(key, JSON.stringify(get()));
} catch {
/* continue regardless of error */
}
};
};

middleware.hydrate = () => {
states.forEach(([key, set]) => {
const value = getStorage().getItem(key);
value != null && set(value !== 'undefined' ? JSON.parse(value) : undefined, `HYDRATE_${key}`);
try {
const value = getStorage().getItem(key);
value != null &&
set(value !== 'undefined' ? JSON.parse(value) : undefined, `HYDRATE_${key}`);
} catch {
/* continue regardless of error */
}
});
states.length = 0;
};
Expand Down
5 changes: 3 additions & 2 deletions src/middleware/reduxDevtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ const reduxDevtools: ReduxDevtools = ({ name } = {}) => {
mergedState[key] = get();
devtools.init(mergedState);

return function (value, action) {
set.apply(null, arguments as unknown as Parameters<typeof set>);
return (...args) => {
const [value, action] = args;
set(...args);
mergedState[key] = get();
devtools.send(
typeof action === 'string' ? { type: action } : action || { type: `SET_${key}`, value },
Expand Down

0 comments on commit e2a0acb

Please sign in to comment.