automatic syncing of members on change. Closes #2

pull/22/head
Bronwen 2021-05-21 23:43:08 -04:00
parent f604948d71
commit 7a12c1adb8
4 changed files with 23 additions and 4 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules
out
.system

View File

@ -2,4 +2,5 @@ node_modules
src
test
.editorconfig
tsconfig.json
tsconfig.json
.system

View File

@ -52,8 +52,11 @@ export default class Instance {
initialContext[link.name] = [];
for(const link of this.module.links.filter((v: Link) => !v.array && !v.required))
initialContext[link.name] = null;
for(const variable of this.module.variables)
initialContext[variable.name] = null;
for(const variable of this.module.variables) {
attachHookedProperty(initialContext, variable.name, null, () => {
this.system.saveInstance(this);
})
}
for(const name in this.module.imports)
initialContext[name] = this.module.imports[name];
@ -133,4 +136,19 @@ ${name} = ${name}.bind(this);
toString() {
return this.module.name.full + '(' + this._id + ')';
}
}
function attachHookedProperty(target: any, name: string, initialValue: any, changedHook: () => void) {
const propId = uuid.v4();
target[propId] = initialValue;
// TODO if its an object, replace it with a dead simple proxy? for detecting internal changes...
Object.defineProperty(target, name, {
get() {
return target[propId];
},
set(value) {
target[propId] = value;
changedHook();
}
})
}

View File

@ -2,7 +2,6 @@ member count;
increment() {
count ++;
sync();
}
getCount() {