still call constructor...
parent
13c0ef1173
commit
f9cace7815
|
|
@ -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",
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
Reference in New Issue