use of createRef instead of not working { corrent: xxx }

This commit is contained in:
Jan Prochazka
2021-03-17 18:20:26 +01:00
parent 321d5d71de
commit 9c7df42948
9 changed files with 73 additions and 39 deletions

View File

@@ -0,0 +1,26 @@
interface Reference<T> {
set(value: T);
get(): T;
reset(): void;
update(func: (arg: T) => T);
}
export default function createRef<T>(value: T): Reference<T> {
return {
value,
initValue: value,
set(value) {
this.value = value;
},
reset() {
this.value = this.initValue;
},
get() {
return this.value;
},
update(func) {
this.value = func(this.value);
},
} as Reference<T>;
}