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

construct trap fails for observed constructors #48

Open
justinweiss opened this issue Jun 17, 2020 · 1 comment
Open

construct trap fails for observed constructors #48

justinweiss opened this issue Jun 17, 2020 · 1 comment

Comments

@justinweiss
Copy link

Hi! Thanks so much for the library, it's been great to use.

We've been trying 4.3.0-alpha.2 to get the react-easy-state fix RisingStack/react-easy-state#184, and ran into a problem using constructors that have been observed -- at least with @babel/plugin-transform-classes. I haven't been able to test without it, but either way the behavior doesn't seem right.

An example might be easiest:

class Thing {}

const thing = new Thing(); // => Thing instance
const observableThing = observable(thing); // => Proxy wrapping Thing instance
const Constructor = observableThing.constructor; // => Proxy wrapping Thing()
const newThing = new Constructor(); // => Fails with Uncaught TypeError: Cannot call a class as a function

The failure happens because the construct function here is called as

construct(Thing, [], Proxy { Thing() })

When constructing the object, Thing() is called with this set to Proxy { Thing() }.prototype, which is also a Proxy. This means that this instanceof Thing will be false, which seems incorrect.

I think using the raw constructor as newTarget would be the easiest fix, but I don't know whether that's right.

@ximing
Copy link

ximing commented Jul 20, 2020

I had the same problem
If use the raw constructor as newTarget, it seems that the unit test could pass。

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

No branches or pull requests

2 participants