Skip to content

Commit

Permalink
Fix getters and setters and bump version.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjrv committed Apr 27, 2018
1 parent a6cb742 commit 7008811
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "classy-mst",
"version": "1.0.0",
"version": "1.0.1",
"description": "ES6-like syntax for mobx-state-tree",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
23 changes: 19 additions & 4 deletions src/classy-mst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export function setTypeTag(tag?: string) {
typeTag = tag;
}

function dummyGetter() {}

function BaseClass() {}

/** Force TypeScript to accept an MST model as a superclass.
Expand Down Expand Up @@ -139,12 +141,25 @@ export function mst<S, T, U>(Code: new() => U, Data: IModelType<S, T>, name?: st
}

for(let { name, value } of descList) {
const { get, set } = value;
const desc: PropertyDescriptor = {};

for(let key of Object.getOwnPropertyNames(value)) {
(desc as any)[key] = (value as any)[key];
}

const { get, set } = desc;

if(get) {
desc.get = () => get.call(self);
} else if(set) {
// Properties with only setters still need a getter defined here,
// or mobx-state-tree will ignore them.
desc.get = dummyGetter;
}

if(get) value.get = () => get.call(self);
if(set) value.set = (value: any) => set.call(self, value);
if(set) desc.set = (value: any) => set.call(self, value);

Object.defineProperty(result, name, value);
Object.defineProperty(result, name, desc);
}

return(result);
Expand Down

0 comments on commit 7008811

Please sign in to comment.