add day10

This commit is contained in:
Zoe
2022-10-02 14:45:59 -05:00
parent cd3fdbc149
commit 15da8f76d7
29 changed files with 4272 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
export class Reactive {
[x: string]: any;
constructor(obj: any) {
this.contents = obj;
this.listeners = {};
this.makeReactive(obj);
}
makeReactive(obj: any) {
Object.keys(obj).forEach(prop => this.makePropReactive(obj, prop));
}
makePropReactive(obj: any, key: string) {
let value = obj[key];
// Gotta be careful with this here
const that = this;
Object.defineProperty(obj, key, {
get() {
return value;
},
set(newValue) {
value = newValue;
that.notify(key)
}
})
}
listen(prop: any, handler: any) {
if (!this.listeners[prop]) this.listeners[prop] = [];
this.listeners[prop].push(handler);
}
notify(prop: any) {
this.listeners[prop].forEach((listener: (arg0: any) => any) => listener(this.contents[prop]));
}
}