From fb63aac5cabc08561709c7e4d6119bf898316be2 Mon Sep 17 00:00:00 2001 From: Sean Matheson Date: Fri, 18 Oct 2019 22:49:20 +0100 Subject: [PATCH] Fixes debug helper, and updates it's docs --- package.json | 4 ++-- src/__tests__/debug.test.js | 2 +- src/helpers.js | 4 ++-- website/docs/docs/api/debug.md | 14 ++++++++++---- yarn.lock | 16 ++++++++-------- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index e6791e149..228e69ff9 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ "redux": "^4.0.4", "redux-thunk": "^2.3.0", "shallowequal": "^1.1.0", - "ts-toolbelt": "^4.8.16" + "ts-toolbelt": "^4.8.19" }, "devDependencies": { "@babel/cli": "^7.6.4", @@ -93,7 +93,7 @@ "react-dom": "^16.10.2", "react-redux": "^7.1.1", "rimraf": "^3.0.0", - "rollup": "^1.24.0", + "rollup": "^1.25.0", "rollup-plugin-babel": "^4.3.3", "rollup-plugin-commonjs": "^10.1.0", "rollup-plugin-filesize": "^6.2.1", diff --git a/src/__tests__/debug.test.js b/src/__tests__/debug.test.js index 472300718..e722398d5 100644 --- a/src/__tests__/debug.test.js +++ b/src/__tests__/debug.test.js @@ -18,7 +18,7 @@ it('should return state with changes applied', () => { add: action((state, payload) => { expect(debug(state)).toEqual({ logs: ['foo'] }); state.logs.push(payload); - expect(debug(state)).toEqual({ logs: ['foo', 'bar'] }); + expect(debug(state)).toEqual({ logs: ['foo'] }); }), }); diff --git a/src/helpers.js b/src/helpers.js index 8475734fb..a07b88695 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,4 +1,4 @@ -import { isDraft, createDraft, finishDraft } from 'immer-peasy'; +import { isDraft, original } from 'immer-peasy'; import memoizerific from 'memoizerific'; import { actionOnSymbol, @@ -11,7 +11,7 @@ import { export const debug = state => { if (isDraft(state)) { - return finishDraft(createDraft(state)); + return original(state); } return state; }; diff --git a/website/docs/docs/api/debug.md b/website/docs/docs/api/debug.md index 220d63cb2..dff2a4840 100644 --- a/website/docs/docs/api/debug.md +++ b/website/docs/docs/api/debug.md @@ -1,15 +1,18 @@ # debug -This helper is useful in the context of [actions](/docs/api/action.html). [Actions](/docs/api/action.html) use the [immer](https://github.com/mweststrate/immer) library under the hood in order to convert mutative updates into immutable ones. Therefore if you try to `console.log` your state within an [action](/doc/api/action.html) you will see wrapping `Proxy` objects printed out. +This helper is useful in the context of [actions](/docs/api/action.html). -Use this helper in order to convert your `state` into its native representation. +[Actions](/docs/api/action.html) use the [immer](https://github.com/mweststrate/immer) library under the hood in order to convert mutative updates into immutable ones. Therefore if you try to `console.log` your state within an [action](/doc/api/action.html) you will see wrapping `Proxy` objects printed out. + +Use this helper in order to get the _original_ `state` value that was provided to your [action](/docs/api/action.html) in its native representation. _Before:_ ```javascript const model = { - myAction: action((state, payload) => { + increment: action((state, payload) => { console.log(state); // 👈 prints a Proxy object + state.count += 1; }) } ``` @@ -20,10 +23,13 @@ _After:_ import { debug } from 'easy-peasy'; const model = { - myAction: action((state, payload) => { + increment: action((state, payload) => { console.log(debug(state)); // 👈 prints the "native" state representation + state.count += 1; }) } ``` +> **Note:** this helper will only return the *original* state that was provided to your action. Any mutations that are applied to the state within your action will not be represented. + > If you have set the `disableImmer` configuration value on the store you will not need to use this helper. \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 33a0b6288..bf51e4238 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6225,10 +6225,10 @@ rollup-pluginutils@^2.8.1: dependencies: estree-walker "^0.6.1" -rollup@^1.24.0: - version "1.24.0" - resolved "https://registry.yarnpkg.com/rollup/-/rollup-1.24.0.tgz#0ace969508babb7a5fcb228e831e9c9a2873c2b0" - integrity sha512-PiFETY/rPwodQ8TTC52Nz2DSCYUATznGh/ChnxActCr8rV5FIk3afBUb3uxNritQW/Jpbdn3kq1Rwh1HHYMwdQ== +rollup@^1.25.0: + version "1.25.0" + resolved "https://registry.yarnpkg.com/rollup/-/rollup-1.25.0.tgz#0c293d16be13000fa69c5a3035ab21a852d04bd7" + integrity sha512-tcf5ThhnhOUaNrxBSABvaaX9uC8hNxgyJpJmDIXaCkKHq/nPocaDz/4F/KBDiUpOt/ThvUxFrUq3XkyWiyXQiQ== dependencies: "@types/estree" "*" "@types/node" "*" @@ -6897,10 +6897,10 @@ trim-right@^1.0.1: resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003" integrity sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM= -ts-toolbelt@^4.8.16: - version "4.8.16" - resolved "https://registry.yarnpkg.com/ts-toolbelt/-/ts-toolbelt-4.8.16.tgz#790c043b37335f174e681195b0a3f332d68e437f" - integrity sha512-4xLaT2ENZ39jD+zVYgsuETHfFFnXk8unVOesBcmp0gLd57CHKIiLLtavzT+Ij1U5Z8041m1yfISDMBFG5y1zJw== +ts-toolbelt@^4.8.19: + version "4.8.19" + resolved "https://registry.yarnpkg.com/ts-toolbelt/-/ts-toolbelt-4.8.19.tgz#bf99490dacc1c941cd53be926aebcdd445dc329b" + integrity sha512-GSUJd/2qILuLTj/ekGHsiak3MkNjK2fX4baFI/9+7YDjQQM28ZzfOYJ4I7TeODdogK+MYPvhE46byO300FmPlA== tslib@^1.9.0: version "1.9.3"