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

Cannot initialize observable Map/Sets from another window #3892

Closed
g6123 opened this issue Jun 27, 2024 · 2 comments · Fixed by #3893
Closed

Cannot initialize observable Map/Sets from another window #3892

g6123 opened this issue Jun 27, 2024 · 2 comments · Fixed by #3893
Labels

Comments

@g6123
Copy link
Contributor

g6123 commented Jun 27, 2024

Hi, I'm working on a project that uses MobX to communicate between same-origin iframes and popups.

And I found some issues using observable Maps and Sets across windows (or "realms" according to the ECMAScript spec).

I should admit that this is kinda tricky use-case, but I also found several similar discussions on cross-window cases before: #1644 #2448

Intended outcome:

Can initialize observable maps and sets with a MobX instance from different windows.

For example,

<!-- index.html -->
<iframe id="frame" src="./frame"></iframe>
<script>
  const $frame = document.getElementById('frame');
  $frame.contentWindow.mobx = mobx;
</script>
<!-- frame.html -->
<script>
  const { autorun, makeObservable, observable } = window.mobx;
  const $size = document.getElementById('size');
  const set = observable.set(new Set());
  // ...
</script>

Actual outcome:

Uncaught Error: [MobX] Cannot initialize set from [object Set]

How to reproduce the issue:

Here is minimal repro: codesandbox.

Versions

Tested in MobX 6.

@g6123 g6123 added the 🐛 bug label Jun 27, 2024
@g6123
Copy link
Contributor Author

g6123 commented Jun 27, 2024

The cause is simple. Theses checks are broken when called from another window:

export function isES6Map(thing: any): thing is Map<any, any> {
return thing instanceof Map
}
export function isES6Set(thing: any): thing is Set<any> {
return thing instanceof Set
}

} else if (isES6Map(other)) {
if (other.constructor !== Map) {
die(19, other)
}
other.forEach((value, key) => this.set(key, value))

We can fix theses checks roughly like this:

export function isES6Map(thing: any): thing is Map<any, any> {
    return thing != null && thing[Symbol.toStringTag] == "Map";
    // or, `Object.prototype.toString.call` instead of `Symbol.toStringTag` for better browser coverage
}

export function isES6Set(thing: any): thing is Set<any> {
    return thing != null && thing[Symbol.toStringTag] == "Set";
}
            } else if (isES6Map(other)) {
                if (other.constructor.name !== "Map") {
                    die(19, other)
                }
                other.forEach((value, key) => this.set(key, value))

This S.O. answer might be helpful: https://stackoverflow.com/a/29926193

@g6123
Copy link
Contributor Author

g6123 commented Jun 27, 2024

I made a PR for the fix! #3893

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant