-
-
Notifications
You must be signed in to change notification settings - Fork 856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for frozen trees, and automatically freeze generated trees #15
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ab8987f
Added support for frozen trees, and automatically freeze generated trees
mweststrate 0190958
`__esModule` should be non-enumerable
mweststrate f08403a
Merge branch 'master' into feature/freeze
mweststrate 53a6f87
Fixed merge issue
mweststrate 69d93b7
Processed review comments
mweststrate 84905e7
`immer` should be the default export
mweststrate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"use strict" | ||
import immer from ".." | ||
|
||
describe("auto freeze", () => { | ||
const baseState = { | ||
object: {a: 1}, | ||
array: [1, 2], | ||
} | ||
|
||
it("should freeze objects after modifications", () => { | ||
expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen | ||
const next = immer(baseState, draft => { | ||
draft.object.c = 2 | ||
}) | ||
expect(Object.isFrozen(next.object)).toBe(true) | ||
expect(Object.isFrozen(next)).toBe(true) | ||
expect(Object.isFrozen(next.array)).toBe(false) | ||
|
||
expect(() => { | ||
next.object.a = 2 | ||
}).toThrow(/Cannot assign to read only property/) | ||
}) | ||
|
||
it("should freeze arrays after modifications", () => { | ||
expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen | ||
const next = immer(baseState, draft => { | ||
draft.array.push(3) | ||
}) | ||
expect(Object.isFrozen(next.object)).toBe(false) // not touched | ||
expect(Object.isFrozen(next)).toBe(true) | ||
expect(Object.isFrozen(next.array)).toBe(true) | ||
|
||
expect(() => { | ||
next.array.shift() | ||
}).toThrow(/Cannot add\/remove sealed array elements/) | ||
}) | ||
|
||
it("can handle already frozen trees", () => { | ||
const a = [] | ||
const b = {a: a} | ||
Object.freeze(a) | ||
Object.freeze(b) | ||
const n = immer(b, draft => { | ||
draft.c = true | ||
draft.a.push(3) | ||
}) | ||
expect(n).toEqual({c: true, a: [3]}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"use strict" | ||
import immer from ".." | ||
|
||
describe("readme example", () => { | ||
it("works", () => { | ||
const baseState = [ | ||
{ | ||
todo: "Learn typescript", | ||
done: true, | ||
}, | ||
{ | ||
todo: "Try immer", | ||
done: false, | ||
}, | ||
] | ||
|
||
const nextState = immer(baseState, draft => { | ||
draft.push({todo: "Tweet about it"}) | ||
draft[1].done = true | ||
}) | ||
|
||
// the new item is only added to the next state, | ||
// base state is unmodified | ||
expect(baseState.length).toBe(2) | ||
expect(nextState.length).toBe(3) | ||
|
||
// same for the changed 'done' prop | ||
expect(baseState[1].done).toBe(false) | ||
expect(nextState[1].done).toBe(true) | ||
|
||
// unchanged data is structurally shared | ||
expect(nextState[0]).toBe(baseState[0]) | ||
// changed data not (dûh) | ||
expect(nextState[1]).not.toBe(baseState[1]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,20 @@ | |
* Immer takes a state, and runs a function against it. | ||
* That function can freely mutate the state, as it will create copies-on-write. | ||
* This means that the original state will stay unchanged, and once the function finishes, the modified state is returned | ||
* | ||
* | ||
* @param currentState - the state to start with | ||
* @param thunk - function that receives a proxy of the current state as first argument and which can be freely modified | ||
* @returns The next state: a new state, or the current state if nothing was modified | ||
*/ | ||
export function immer<S = any>(currentState: S, thunk: (draftState: S) => void): S; | ||
export default function<S = any>( | ||
currentState: S, | ||
thunk: (draftState: S) => void, | ||
): S | ||
|
||
/** | ||
* Automatically freezes any state trees generated by immer. | ||
* This protects against accidental modifications of the state tree outside of an immer function. | ||
* This comes with a performance impact, so it is recommended to disable this option in production. | ||
* It is by default enabled. | ||
*/ | ||
export function setAutoFreeze(autoFreeze: boolean) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type should be void instead of implicit any export function setAutoFreeze(autoFreeze: boolean):void This is to prevent the use of setAutoFreeze return value in an unchecked way. Here is an example: const a = setAutoFreeze(false)
a.test(); // error if the return type of setAutoFreeze is void, but will be ok if return type is any |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also safely assert that
next.array
is not forzen in the process ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done