Skip to content
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

non-enumerable (immutable) methods on Map/Set #1069

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/utils/common.ts
Expand Up @@ -188,7 +188,12 @@ export function freeze<T>(obj: T, deep?: boolean): T
export function freeze<T>(obj: any, deep: boolean = false): T {
if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj
if (getArchtype(obj) > 1 /* Map or Set */) {
obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any
Object.defineProperties(obj, {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good to me! As a nit optimisation, since the property map is constant, it could be lifted outside the function (usually that really doesn't matter, but with freeze potentially touching a gazillion of objects it might)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'm waiting with sending the updated PR because the current state of the repo is failing some tests, both locally and using 'Open in Gitpod' (albeit a bit differently – locally tests/produce.ts, tests/draft.ts, and tests/redux.ts suits are failing, while in Gitpod the tests/immutable.ts is failing in addition to the other three).

set: {value: dontMutateFrozenCollections as any},
add: {value: dontMutateFrozenCollections as any},
clear: {value: dontMutateFrozenCollections as any},
delete: {value: dontMutateFrozenCollections as any}
})
}
Object.freeze(obj)
if (deep) each(obj, (_key, value) => freeze(value, true), true)
Expand Down