-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
ES6 Proxy #776
Conversation
target.set(property, value); | ||
return true; | ||
}, | ||
ownKeys: function(target) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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)
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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, { |
There was a problem hiding this comment.
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.
I don't know much about MobX, but I added a few general tips. Nice and clean approach in my opinion 🙂 |
TODOs:
|
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. |
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) |
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