chore: Minor cleanup of imperativeState

This commit is contained in:
Sidharth Vinod 2024-01-11 09:42:56 +05:30
parent b60fc1b056
commit 0ac339494f
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD
1 changed files with 4 additions and 16 deletions

View File

@ -22,28 +22,16 @@
* ```
*/
export class ImperativeState<S extends Record<string, unknown>> {
init: () => S;
records: S;
public records: S;
/**
* @param init - Function that creates the default state.
*/
constructor(init: () => S) {
this.init = init;
this.records = init();
constructor(private init: () => S) {
this.records = this.init();
}
reset() {
Object.keys(this.records).forEach((key) => {
delete this.records[key];
});
Object.entries(this.init()).forEach(
([key, value]: [
keyof S,
any // eslint-disable-line @typescript-eslint/no-explicit-any
]) => {
this.records[key] = value;
}
);
this.records = this.init();
}
}