From 7a12c1adb8c74ce55497d9fb9ddf4fbc764f35b5 Mon Sep 17 00:00:00 2001 From: Bronwen Date: Fri, 21 May 2021 23:43:08 -0400 Subject: [PATCH] automatic syncing of members on change. Closes #2 --- .gitignore | 1 + .npmignore | 3 ++- src/Instance.ts | 22 ++++++++++++++++++++-- test/counter.v | 1 - 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 884a4d8..1998067 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules out +.system \ No newline at end of file diff --git a/.npmignore b/.npmignore index ff53e28..4a8977f 100644 --- a/.npmignore +++ b/.npmignore @@ -2,4 +2,5 @@ node_modules src test .editorconfig -tsconfig.json \ No newline at end of file +tsconfig.json +.system \ No newline at end of file diff --git a/src/Instance.ts b/src/Instance.ts index ff84599..567fc49 100644 --- a/src/Instance.ts +++ b/src/Instance.ts @@ -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(); + } + }) } \ No newline at end of file diff --git a/test/counter.v b/test/counter.v index f241772..3e04bf3 100644 --- a/test/counter.v +++ b/test/counter.v @@ -2,7 +2,6 @@ member count; increment() { count ++; - sync(); } getCount() {