Skip to content

Commit

Permalink
Fixes debug helper, and updates it's docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlplusb committed Oct 18, 2019
1 parent d342234 commit fb63aac
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'] });
}),
});

Expand Down
4 changes: 2 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDraft, createDraft, finishDraft } from 'immer-peasy';
import { isDraft, original } from 'immer-peasy';
import memoizerific from 'memoizerific';
import {
actionOnSymbol,
Expand All @@ -11,7 +11,7 @@ import {

export const debug = state => {
if (isDraft(state)) {
return finishDraft(createDraft(state));
return original(state);
}
return state;
};
Expand Down
14 changes: 10 additions & 4 deletions website/docs/docs/api/debug.md
Original file line number Diff line number Diff line change
@@ -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;
})
}
```
Expand All @@ -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.
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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" "*"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit fb63aac

Please sign in to comment.