sylvainpolletvillard / ObjectModel

Strong Dynamically Typed Object Modeling for JavaScript
http://objectmodel.js.org
MIT License
467 stars 30 forks source link

Polyfill for React Native Android environment #73

Closed serhiipalash closed 5 years ago

serhiipalash commented 6 years ago

HI @sylvainpolletvillard !

I tried your library with React Native in Android simulator and got Can't find variable: Symbol for v3.4.6 and undefined is not a function (evaluating Object.setPrototypeOf(child, parent)) for v3.2.0.

Can you advice how to fix it right? Is it possible with some babel-plugin?

We really want to keep using your great library. In React Native iOS it works fine.

serhiipalash commented 6 years ago
screen shot 2018-06-14 at 6 17 23 pm
sylvainpolletvillard commented 6 years ago

Hi @serhiipalash ,

ObjectModel v3 only targets ES2015-compliant environments, so it might break on older browsers or exotic environments. Symbol and Object.setPrototypeOf are both ES6 features, so it looks like the React Native environment on Android is not supporting ES6 (at least, not all of it). I found some possible solutions here: https://medium.com/@jordan_mohi/cant-find-variable-symbol-in-react-native-782489d86bd7

If you need broader support, please consider using ObjectModel v2. v2 is supporting older browsers like IE11, so it should work on React Native for Android too. The v2 documentation is still available on the website: http://objectmodel.js.org/docs/v2/

serhiipalash commented 6 years ago

Thanks @sylvainpolletvillard !

I'll try that fix and let you know if it works.

We can't use v2. If you remember there was a bug report from me to make your library work with Firebase, and it works great now. The fix was somewhere in 3.2.0 I think.

serhiipalash commented 6 years ago

When I add this

import 'es6-symbol/implement'

and this

Object.setPrototypeOf = Object.setPrototypeOf ||
  function(obj, proto) {
    obj.__proto__ = proto
    return obj
  }

all works, but only if I comment delete model.name; here

initModel = (model, def) => {
    model.definition = def;
    model.assertions = [...model.assertions];
    define(model, "errors", []);
    // delete model.name;
},
sylvainpolletvillard commented 6 years ago

I can try to report this specific fix on v2 if this is all what is required.

If you manage to get it working with polyfills, good for you ! I cannot guarantee that everything will work perfectly though, these polyfills are not 100% spec compliant and have some limitations

rclai commented 6 years ago

Vote here: https://expo.canny.io/feature-requests/p/javascript-proxy-support-for-android-upgrade-jscore

serhiipalash commented 6 years ago

@rclai already did )

serhiipalash commented 6 years ago

Btw, @sylvainpolletvillard can you tell after what Node.js version I can safely use objectmodel in it?

sylvainpolletvillard commented 6 years ago

Yeah I upgraded the docs about browser/node support.

v2: Node >= 4 v3: Node >= 6

So ObjectModel v3 can be used with the latest LTS 👍 Unit tested here : https://travis-ci.org/sylvainpolletvillard/ObjectModel

sylvainpolletvillard commented 6 years ago

@serhiipalash I checked the fix I did on v3.2, from your previous issue with Firebase here : https://github.com/sylvainpolletvillard/ObjectModel/issues/57#issuecomment-342782200

The bug was related to the ownKeys trap behaviour, but this is a Proxy-related feature, and Proxies are only used since v3. So there is no fix to report 😕

serhiipalash commented 6 years ago

I checked the fix I did on v3.2, from your previous issue with Firebase

@sylvainpolletvillard thanks for checking!

We will have Android app release in a month and I think we have to remove objectmodel from the code :(

We will do this for the stability of both our mobile applications and code consistency. But we still going to use it in our Google Cloud Functions.

sylvainpolletvillard commented 6 years ago

No problem, this lib has been made to avoid problems, not to create new ones. Use it where you can and where it truely provides value

serhiipalash commented 6 years ago

Lets keep this open if you don't mind. As I understand proxies for Android JsCore are in progress.

serhiipalash commented 5 years ago

Expo 31 has updated JSCore for Android and proxies are there! https://expo.canny.io/feature-requests/p/javascript-proxy-support-for-android-upgrade-jscore

Finally I can use the same model library on Web, iOS, Android and Node.js! 🎉