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

ES6 Proxy #776

Closed
wants to merge 5 commits into from
Closed

ES6 Proxy #776

wants to merge 5 commits into from

Conversation

amir-arad
Copy link

@amir-arad amir-arad commented Jan 17, 2017

naive solution proposal for #652

uses shallow observable map internally, because it fits the data structure's dynamic nature.

done as part of a #goodnessSquad hackathon

@amir-arad
Copy link
Author

@idoros

target.set(property, value);
return true;
},
ownKeys: function(target) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used by for in loops internally too, so they work out of the box with expando props.

ownKeys: function(target) {
return target.keys();
},
getOwnPropertyDescriptor: function(target, property:string) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? Why not just leave the default property descriptor? If it is necessary shouldn't it include writable: true and value: target.get(prop)?

Copy link
Author

@amir-arad amir-arad Jan 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before we added getOwnPropertyDescriptor, Object.keys(dynamicObject) always returned an empty array, no matter what ownKeys actually returned.

Copy link
Author

@amir-arad amir-arad Jan 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK the rest of the fields have default fallbacks that fits our use case. configurable specifically breaks the Proxy when it does not match the internal property's configurable value, that is why we added it as well.

@@ -116,6 +116,35 @@ export class IObservableFactories {
return res as any;
}

dynamic<T>(props: T, name?: string): T & IObservableObject {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe check of the props object is already wrapped by a dynamic observable Proxy. Accidental double wrapping could cause some nasty bugs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think about that. thanks!

incorrectlyUsedAsDecorator("object");
invariant(typeof Proxy === 'function', "dynamic objects are not supported in this environment");
const internalMap = new ObservableMap(props, referenceEnhancer, name);
const result = new Proxy(internalMap, {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could also add a deleteProperty and maybe a has trap for the sake of completeness.

@solkimicreb
Copy link

solkimicreb commented Jan 18, 2017

I don't know much about MobX, but I added a few general tips. Nice and clean approach in my opinion 🙂

@amir-arad
Copy link
Author

amir-arad commented Jan 18, 2017

TODOs:

  • investigate and fix failing CI
  • test and avoid double-wrap on already boxed argument
  • test for, and implement, other proxy traps (has, delete, instanceOf)
  • test and support spy and observe events
  • test and implement deep wrapping
    Thanks for the feedback @solkimicreb !

@solkimicreb
Copy link

Deep wrapping will be a tricky one. It won't be enough to traverse the structure at start and wrap everything into an observable, because of the potential object valued expando properties.

@mweststrate
Copy link
Member

mweststrate commented Mar 10, 2018

Proxies are now on their way! So closing this PR in favor #1380, but thanks for the setup @amir-arad! Will borrow some stuff from this. Sad it took a year, but finally browser support is up to par :) (although, IE, RN android, sigh)

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

Successfully merging this pull request may close these issues.

3 participants