Skip to content

Commit

Permalink
Merge pull request #602 from immerjs/fix-600-isFrozen
Browse files Browse the repository at this point in the history
Fix: Object.isFrozen fails in IE #600
  • Loading branch information
mweststrate committed May 21, 2020
2 parents 497d1a0 + 974bd70 commit 8d4e0de
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/core/finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
ProxyTypeSet,
getPlugin,
die,
revokeScope
revokeScope,
isFrozen
} from "../internal"

export function processResult(result: any, scope: ImmerScope) {
Expand Down Expand Up @@ -59,7 +60,7 @@ export function processResult(result: any, scope: ImmerScope) {

function finalize(rootScope: ImmerScope, value: any, path?: PatchPath) {
// Don't recurse in tho recursive data structures
if (Object.isFrozen(value)) return value
if (isFrozen(value)) return value

const state: ImmerState = value[DRAFT_STATE]
// A plain object, might need freezing, might contain drafts
Expand Down
8 changes: 7 additions & 1 deletion src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export function shallowCopy(base: any, invokeGetters = false) {
}

export function freeze(obj: any, deep: boolean): void {
if (isDraft(obj) || Object.isFrozen(obj) || !isDraftable(obj)) return
if (isDraft(obj) || isFrozen(obj) || !isDraftable(obj)) return
if (getArchtype(obj) > 1 /* Map or Set */) {
obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any
}
Expand All @@ -187,3 +187,9 @@ export function freeze(obj: any, deep: boolean): void {
function dontMutateFrozenCollections() {
die(2)
}

export function isFrozen(obj: any): boolean {
if (obj == null || typeof obj !== "object") return true
// See #600, IE dies on non-objects in Object.isFrozen
return Object.isFrozen(obj)
}
4 changes: 3 additions & 1 deletion src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ const errors = {
18(plugin: string) {
return `The plugin for '${plugin}' has not been loaded into Immer. To enable the plugin, import and call \`enable${plugin}()\` when initializing your application.`
},
19: "plugin not loaded",
19(plugin: string) {
return "plugin not loaded: " + plugin
},
20: "Cannot use proxies if Proxy, Proxy.revocable or Reflect are not available"
} as const

Expand Down

0 comments on commit 8d4e0de

Please sign in to comment.