still call constructor...

master
Valerie 2021-06-11 01:01:28 -04:00
parent 13c0ef1173
commit f9cace7815
3 changed files with 9 additions and 5 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "frigid", "name": "frigid",
"version": "1.1.2", "version": "1.2.0",
"main": "out/index.js", "main": "out/index.js",
"types": "out/index.d.ts", "types": "out/index.d.ts",
"license": "MIT", "license": "MIT",

View File

@ -7,18 +7,22 @@ export const RESTORE = Symbol('RESTORE');
export default class Frigid extends Serializable { export default class Frigid extends Serializable {
static create(filename: string, ...args: any[]) { static create(filename: string, ...args: any[]) {
if(existsSync(filename)) { if(existsSync(filename)) {
const instance = this.deserialize(readFileSync(filename)); const instance = new this(...args);
const instanceStuff = this.deserialize(readFileSync(filename));
for(const key of Object.keys(instanceStuff)) {
instance[key] = instanceStuff[key]
}
// TS is plain and simply wrong... symbols can be used to index object... // TS is plain and simply wrong... symbols can be used to index object...
// @ts-ignore // @ts-ignore
instance[PERSIST_LOCATION] = filename; instance[PERSIST_LOCATION] = filename;
instance[RESTORE](); instance[RESTORE]?.();
return instance; return instance;
} else { } else {
const instance = new this(...args); const instance = new this(...args);
// again... TS is wrong... // again... TS is wrong...
// @ts-ignore // @ts-ignore
instance[PERSIST_LOCATION] = filename; instance[PERSIST_LOCATION] = filename;
instance[RESTORE](); instance[RESTORE]?.();
instance.sync(); instance.sync();
return instance; return instance;
} }

View File

@ -40,7 +40,7 @@ expect(trackingData.restoreCalls).to.equal(1);
const retest = Test.create(filepath); const retest = Test.create(filepath);
expect(trackingData.constructorCalls).to.equal(1); expect(trackingData.constructorCalls).to.equal(2);
expect(trackingData.restoreCalls).to.equal(2); expect(trackingData.restoreCalls).to.equal(2);
unlinkSync(filepath) unlinkSync(filepath)