Skip to content

Commit

Permalink
Fix ES6 Map/Set checks for cross-window scripts (#3893)
Browse files Browse the repository at this point in the history
  • Loading branch information
g6123 committed Jun 30, 2024
1 parent a73710c commit ba89034
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-apricots-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx": patch
---

Fix ES6 Map/Set checks for cross-window scripts
3 changes: 2 additions & 1 deletion packages/mobx/src/types/observablemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
hasListeners,
interceptChange,
isES6Map,
isPlainES6Map,
isPlainObject,
isSpyEnabled,
makeIterable,
Expand Down Expand Up @@ -351,7 +352,7 @@ export class ObservableMap<K = any, V = any>
} else if (Array.isArray(other)) {
other.forEach(([key, value]) => this.set(key, value))
} else if (isES6Map(other)) {
if (other.constructor !== Map) {
if (!isPlainES6Map(other)) {
die(19, other)
}
other.forEach((value, key) => this.set(key, value))
Expand Down
11 changes: 9 additions & 2 deletions packages/mobx/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,18 @@ export function createInstanceofPredicate<T>(
}

export function isES6Map(thing: any): thing is Map<any, any> {
return thing instanceof Map
return thing != null && Object.prototype.toString.call(thing) === "[object Map]"
}

export function isPlainES6Map(thing: Map<any, any>): boolean {
const mapProto = Object.getPrototypeOf(thing)
const objectProto = Object.getPrototypeOf(mapProto)
const nullProto = Object.getPrototypeOf(objectProto)
return nullProto === null
}

export function isES6Set(thing: any): thing is Set<any> {
return thing instanceof Set
return thing != null && Object.prototype.toString.call(thing) === "[object Set]"
}

const hasGetOwnPropertySymbols = typeof Object.getOwnPropertySymbols !== "undefined"
Expand Down

0 comments on commit ba89034

Please sign in to comment.