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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/api/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!

if (arguments.length > 2)
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.

get(target, property:string, receiver){
if (property === '$mobx'){
return target[property];
}
return target.get(property);
},
set(target, property:string, value, receiver){
if (property === '$mobx'){
return false;}
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.

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.

return {enumerable: target.has(property), configurable:true};
}
});

return result as any;
}


/**
* Decorator that creates an observable that only observes the references, but doesn't try to turn the assigned value into an observable.ts.
Expand Down
5 changes: 1 addition & 4 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ export function getGlobal() {
return global;
}

export interface Lambda {
(): void;
name?: string;
}
export type Lambda = Function;

export function getNextId() {
return ++globalState.mobxGuid;
Expand Down
38 changes: 38 additions & 0 deletions test/makereactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,44 @@ test("boxed value json", t => {
t.end();
})

test("proxied value json", t => {
if(typeof Proxy === 'function') {
try {
const log = [];
const todos = mobx.observable.dynamic({
"a23bf": {completed: false}
});

const cleanup = mobx.autorun(() => {
try {
log.push(Object.keys(todos));
} catch (e) {
t.error('error in autorun', e);
}
});

t.equal(log.length, 1);
t.equal(log[0][0], "a23bf");

todos["a8235"] = {completed: true};

t.equal(log.length, 2);
t.equal(log[1][0], "a23bf");
t.equal(log[1][1], "a8235");
cleanup();
} catch (e) {
t.error('error in test', e);
}
} else {
t.throws(() => mobx.observable.dynamic({
"a23bf": {completed: false}
}))
}
t.end();
});



test("computed value scope", t => {
var a = mobx.observable({
x: 1,
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"noImplicitUseStrict": true,
"noImplicitAny": false,
"lib": [
"es5",
"ES2015.Proxy",
"es6",
"dom"
]
},
Expand Down