@atomiqlabs/sdk 5.0.0-dev.4 → 5.0.0-dev.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -201
- package/README.md +1242 -1242
- package/dist/SmartChainAssets.d.ts +93 -93
- package/dist/SmartChainAssets.js +95 -95
- package/dist/SwapperFactory.d.ts +52 -51
- package/dist/SwapperFactory.js +115 -110
- package/dist/Utils.d.ts +11 -11
- package/dist/Utils.js +37 -37
- package/dist/fs-storage/FileSystemStorageManager.d.ts +15 -15
- package/dist/fs-storage/FileSystemStorageManager.js +60 -60
- package/dist/fs-storage/index.d.ts +1 -1
- package/dist/fs-storage/index.js +17 -17
- package/dist/index.d.ts +5 -5
- package/dist/index.js +21 -21
- package/dist/storage/LocalStorageManager.d.ts +24 -24
- package/dist/storage/LocalStorageManager.js +68 -68
- package/package.json +32 -32
- package/src/SmartChainAssets.js +75 -75
- package/src/SmartChainAssets.ts +96 -96
- package/src/SwapperFactory.js +120 -120
- package/src/SwapperFactory.ts +209 -203
- package/src/Utils.js +37 -37
- package/src/Utils.ts +31 -31
- package/src/fs-storage/FileSystemStorageManager.ts +71 -71
- package/src/fs-storage/index.ts +1 -1
- package/src/index.js +21 -21
- package/src/index.ts +5 -5
- package/src/storage/LocalStorageManager.js +72 -72
- package/src/storage/LocalStorageManager.ts +81 -81
|
@@ -1,71 +1,71 @@
|
|
|
1
|
-
import {StorageObject, IStorageManager} from "@atomiqlabs/base";
|
|
2
|
-
import * as fs from "fs/promises";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* StorageManager using local filesystem to persists data, creates a new file for every save object
|
|
6
|
-
*/
|
|
7
|
-
export class FileSystemStorageManager<T extends StorageObject> implements IStorageManager<T> {
|
|
8
|
-
|
|
9
|
-
private readonly directory: string;
|
|
10
|
-
data: {
|
|
11
|
-
[key: string]: T
|
|
12
|
-
} = {};
|
|
13
|
-
|
|
14
|
-
constructor(directory: string) {
|
|
15
|
-
this.directory = directory;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async init(): Promise<void> {
|
|
19
|
-
try {
|
|
20
|
-
await fs.mkdir(this.directory);
|
|
21
|
-
} catch (e) {}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async saveData(hash: string, object: T): Promise<void> {
|
|
25
|
-
|
|
26
|
-
try {
|
|
27
|
-
await fs.mkdir(this.directory)
|
|
28
|
-
} catch (e) {}
|
|
29
|
-
|
|
30
|
-
this.data[hash] = object;
|
|
31
|
-
|
|
32
|
-
const cpy = object.serialize();
|
|
33
|
-
|
|
34
|
-
await fs.writeFile(this.directory+"/"+hash+".json", JSON.stringify(cpy));
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async removeData(hash: string): Promise<void> {
|
|
39
|
-
const paymentHash = hash;
|
|
40
|
-
try {
|
|
41
|
-
if(this.data[paymentHash]!=null) delete this.data[paymentHash];
|
|
42
|
-
await fs.rm(this.directory+"/"+paymentHash+".json");
|
|
43
|
-
} catch (e) {
|
|
44
|
-
console.error("FileSystemStorageManager: removeData(): Error: ", e);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async loadData(type: new(data: any) => T): Promise<T[]> {
|
|
49
|
-
let files;
|
|
50
|
-
try {
|
|
51
|
-
files = await fs.readdir(this.directory);
|
|
52
|
-
} catch (e) {
|
|
53
|
-
console.error("FileSystemStorageManager: loadData(): Error: ", e);
|
|
54
|
-
return [];
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const arr = [];
|
|
58
|
-
|
|
59
|
-
for(let file of files) {
|
|
60
|
-
const paymentHash = file.split(".")[0];
|
|
61
|
-
const result = await fs.readFile(this.directory+"/"+file);
|
|
62
|
-
const obj = JSON.parse(result.toString());
|
|
63
|
-
const parsed = new type(obj);
|
|
64
|
-
arr.push(parsed);
|
|
65
|
-
this.data[paymentHash] = parsed;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return arr;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
}
|
|
1
|
+
import {StorageObject, IStorageManager} from "@atomiqlabs/base";
|
|
2
|
+
import * as fs from "fs/promises";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* StorageManager using local filesystem to persists data, creates a new file for every save object
|
|
6
|
+
*/
|
|
7
|
+
export class FileSystemStorageManager<T extends StorageObject> implements IStorageManager<T> {
|
|
8
|
+
|
|
9
|
+
private readonly directory: string;
|
|
10
|
+
data: {
|
|
11
|
+
[key: string]: T
|
|
12
|
+
} = {};
|
|
13
|
+
|
|
14
|
+
constructor(directory: string) {
|
|
15
|
+
this.directory = directory;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async init(): Promise<void> {
|
|
19
|
+
try {
|
|
20
|
+
await fs.mkdir(this.directory);
|
|
21
|
+
} catch (e) {}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async saveData(hash: string, object: T): Promise<void> {
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
await fs.mkdir(this.directory)
|
|
28
|
+
} catch (e) {}
|
|
29
|
+
|
|
30
|
+
this.data[hash] = object;
|
|
31
|
+
|
|
32
|
+
const cpy = object.serialize();
|
|
33
|
+
|
|
34
|
+
await fs.writeFile(this.directory+"/"+hash+".json", JSON.stringify(cpy));
|
|
35
|
+
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async removeData(hash: string): Promise<void> {
|
|
39
|
+
const paymentHash = hash;
|
|
40
|
+
try {
|
|
41
|
+
if(this.data[paymentHash]!=null) delete this.data[paymentHash];
|
|
42
|
+
await fs.rm(this.directory+"/"+paymentHash+".json");
|
|
43
|
+
} catch (e) {
|
|
44
|
+
console.error("FileSystemStorageManager: removeData(): Error: ", e);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async loadData(type: new(data: any) => T): Promise<T[]> {
|
|
49
|
+
let files;
|
|
50
|
+
try {
|
|
51
|
+
files = await fs.readdir(this.directory);
|
|
52
|
+
} catch (e) {
|
|
53
|
+
console.error("FileSystemStorageManager: loadData(): Error: ", e);
|
|
54
|
+
return [];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const arr = [];
|
|
58
|
+
|
|
59
|
+
for(let file of files) {
|
|
60
|
+
const paymentHash = file.split(".")[0];
|
|
61
|
+
const result = await fs.readFile(this.directory+"/"+file);
|
|
62
|
+
const obj = JSON.parse(result.toString());
|
|
63
|
+
const parsed = new type(obj);
|
|
64
|
+
arr.push(parsed);
|
|
65
|
+
this.data[paymentHash] = parsed;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return arr;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
}
|
package/src/fs-storage/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./FileSystemStorageManager";
|
|
1
|
+
export * from "./FileSystemStorageManager";
|
package/src/index.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("@atomiqlabs/base"), exports);
|
|
18
|
-
__exportStar(require("@atomiqlabs/sdk-lib"), exports);
|
|
19
|
-
__exportStar(require("./SwapperFactory"), exports);
|
|
20
|
-
__exportStar(require("./Utils"), exports);
|
|
21
|
-
__exportStar(require("./storage/LocalStorageManager"), exports);
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("@atomiqlabs/base"), exports);
|
|
18
|
+
__exportStar(require("@atomiqlabs/sdk-lib"), exports);
|
|
19
|
+
__exportStar(require("./SwapperFactory"), exports);
|
|
20
|
+
__exportStar(require("./Utils"), exports);
|
|
21
|
+
__exportStar(require("./storage/LocalStorageManager"), exports);
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from "@atomiqlabs/base";
|
|
2
|
-
export * from "@atomiqlabs/sdk-lib";
|
|
3
|
-
export * from "./SwapperFactory";
|
|
4
|
-
export * from "./Utils";
|
|
5
|
-
export * from "./storage/LocalStorageManager";
|
|
1
|
+
export * from "@atomiqlabs/base";
|
|
2
|
+
export * from "@atomiqlabs/sdk-lib";
|
|
3
|
+
export * from "./SwapperFactory";
|
|
4
|
+
export * from "./Utils";
|
|
5
|
+
export * from "./storage/LocalStorageManager";
|
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LocalStorageManager = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* StorageManager using browser's local storage API
|
|
6
|
-
*/
|
|
7
|
-
var LocalStorageManager = /** @class */ (function () {
|
|
8
|
-
function LocalStorageManager(storageKey) {
|
|
9
|
-
this.rawData = null;
|
|
10
|
-
this.data = {};
|
|
11
|
-
this.storageKey = storageKey;
|
|
12
|
-
}
|
|
13
|
-
LocalStorageManager.prototype.init = function () {
|
|
14
|
-
var completedTxt = window.localStorage.getItem(this.storageKey);
|
|
15
|
-
if (completedTxt != null) {
|
|
16
|
-
this.rawData = JSON.parse(completedTxt);
|
|
17
|
-
if (this.rawData == null)
|
|
18
|
-
this.rawData = {};
|
|
19
|
-
}
|
|
20
|
-
else {
|
|
21
|
-
this.rawData = {};
|
|
22
|
-
}
|
|
23
|
-
return Promise.resolve();
|
|
24
|
-
};
|
|
25
|
-
LocalStorageManager.prototype.saveData = function (hash, object) {
|
|
26
|
-
this.data[hash] = object;
|
|
27
|
-
this.rawData[hash] = object.serialize();
|
|
28
|
-
return this.save();
|
|
29
|
-
};
|
|
30
|
-
LocalStorageManager.prototype.saveDataArr = function (arr) {
|
|
31
|
-
var _this = this;
|
|
32
|
-
arr.forEach(function (e) {
|
|
33
|
-
_this.data[e.id] = e.object;
|
|
34
|
-
_this.rawData[e.id] = e.object.serialize();
|
|
35
|
-
});
|
|
36
|
-
return this.save();
|
|
37
|
-
};
|
|
38
|
-
LocalStorageManager.prototype.removeData = function (hash) {
|
|
39
|
-
if (this.rawData[hash] != null) {
|
|
40
|
-
if (this.data[hash] != null)
|
|
41
|
-
delete this.data[hash];
|
|
42
|
-
delete this.rawData[hash];
|
|
43
|
-
return this.save();
|
|
44
|
-
}
|
|
45
|
-
return Promise.resolve();
|
|
46
|
-
};
|
|
47
|
-
LocalStorageManager.prototype.removeDataArr = function (hashArr) {
|
|
48
|
-
var _this = this;
|
|
49
|
-
hashArr.forEach(function (hash) {
|
|
50
|
-
if (_this.rawData[hash] != null) {
|
|
51
|
-
if (_this.data[hash] != null)
|
|
52
|
-
delete _this.data[hash];
|
|
53
|
-
delete _this.rawData[hash];
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
return this.save();
|
|
57
|
-
};
|
|
58
|
-
LocalStorageManager.prototype.loadData = function (type) {
|
|
59
|
-
var _this = this;
|
|
60
|
-
return Promise.resolve(Object.keys(this.rawData).map(function (e) {
|
|
61
|
-
var deserialized = new type(_this.rawData[e]);
|
|
62
|
-
_this.data[e] = deserialized;
|
|
63
|
-
return deserialized;
|
|
64
|
-
}));
|
|
65
|
-
};
|
|
66
|
-
LocalStorageManager.prototype.save = function () {
|
|
67
|
-
window.localStorage.setItem(this.storageKey, JSON.stringify(this.rawData));
|
|
68
|
-
return Promise.resolve();
|
|
69
|
-
};
|
|
70
|
-
return LocalStorageManager;
|
|
71
|
-
}());
|
|
72
|
-
exports.LocalStorageManager = LocalStorageManager;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalStorageManager = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* StorageManager using browser's local storage API
|
|
6
|
+
*/
|
|
7
|
+
var LocalStorageManager = /** @class */ (function () {
|
|
8
|
+
function LocalStorageManager(storageKey) {
|
|
9
|
+
this.rawData = null;
|
|
10
|
+
this.data = {};
|
|
11
|
+
this.storageKey = storageKey;
|
|
12
|
+
}
|
|
13
|
+
LocalStorageManager.prototype.init = function () {
|
|
14
|
+
var completedTxt = window.localStorage.getItem(this.storageKey);
|
|
15
|
+
if (completedTxt != null) {
|
|
16
|
+
this.rawData = JSON.parse(completedTxt);
|
|
17
|
+
if (this.rawData == null)
|
|
18
|
+
this.rawData = {};
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
this.rawData = {};
|
|
22
|
+
}
|
|
23
|
+
return Promise.resolve();
|
|
24
|
+
};
|
|
25
|
+
LocalStorageManager.prototype.saveData = function (hash, object) {
|
|
26
|
+
this.data[hash] = object;
|
|
27
|
+
this.rawData[hash] = object.serialize();
|
|
28
|
+
return this.save();
|
|
29
|
+
};
|
|
30
|
+
LocalStorageManager.prototype.saveDataArr = function (arr) {
|
|
31
|
+
var _this = this;
|
|
32
|
+
arr.forEach(function (e) {
|
|
33
|
+
_this.data[e.id] = e.object;
|
|
34
|
+
_this.rawData[e.id] = e.object.serialize();
|
|
35
|
+
});
|
|
36
|
+
return this.save();
|
|
37
|
+
};
|
|
38
|
+
LocalStorageManager.prototype.removeData = function (hash) {
|
|
39
|
+
if (this.rawData[hash] != null) {
|
|
40
|
+
if (this.data[hash] != null)
|
|
41
|
+
delete this.data[hash];
|
|
42
|
+
delete this.rawData[hash];
|
|
43
|
+
return this.save();
|
|
44
|
+
}
|
|
45
|
+
return Promise.resolve();
|
|
46
|
+
};
|
|
47
|
+
LocalStorageManager.prototype.removeDataArr = function (hashArr) {
|
|
48
|
+
var _this = this;
|
|
49
|
+
hashArr.forEach(function (hash) {
|
|
50
|
+
if (_this.rawData[hash] != null) {
|
|
51
|
+
if (_this.data[hash] != null)
|
|
52
|
+
delete _this.data[hash];
|
|
53
|
+
delete _this.rawData[hash];
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
return this.save();
|
|
57
|
+
};
|
|
58
|
+
LocalStorageManager.prototype.loadData = function (type) {
|
|
59
|
+
var _this = this;
|
|
60
|
+
return Promise.resolve(Object.keys(this.rawData).map(function (e) {
|
|
61
|
+
var deserialized = new type(_this.rawData[e]);
|
|
62
|
+
_this.data[e] = deserialized;
|
|
63
|
+
return deserialized;
|
|
64
|
+
}));
|
|
65
|
+
};
|
|
66
|
+
LocalStorageManager.prototype.save = function () {
|
|
67
|
+
window.localStorage.setItem(this.storageKey, JSON.stringify(this.rawData));
|
|
68
|
+
return Promise.resolve();
|
|
69
|
+
};
|
|
70
|
+
return LocalStorageManager;
|
|
71
|
+
}());
|
|
72
|
+
exports.LocalStorageManager = LocalStorageManager;
|
|
@@ -1,81 +1,81 @@
|
|
|
1
|
-
import {IStorageManager, StorageObject} from "@atomiqlabs/base";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* StorageManager using browser's local storage API
|
|
5
|
-
*/
|
|
6
|
-
export class LocalStorageManager<T extends StorageObject> implements IStorageManager<T> {
|
|
7
|
-
|
|
8
|
-
storageKey: string;
|
|
9
|
-
|
|
10
|
-
rawData: {
|
|
11
|
-
[hash: string]: any
|
|
12
|
-
} = null;
|
|
13
|
-
data: {
|
|
14
|
-
[hash: string]: T
|
|
15
|
-
} = {};
|
|
16
|
-
|
|
17
|
-
constructor(storageKey: string) {
|
|
18
|
-
this.storageKey = storageKey;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
init(): Promise<void> {
|
|
22
|
-
const completedTxt = window.localStorage.getItem(this.storageKey);
|
|
23
|
-
if(completedTxt!=null) {
|
|
24
|
-
this.rawData = JSON.parse(completedTxt);
|
|
25
|
-
if(this.rawData==null) this.rawData = {};
|
|
26
|
-
} else {
|
|
27
|
-
this.rawData = {};
|
|
28
|
-
}
|
|
29
|
-
return Promise.resolve();
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
saveData(hash: string, object: T): Promise<void> {
|
|
33
|
-
this.data[hash] = object;
|
|
34
|
-
this.rawData[hash] = object.serialize();
|
|
35
|
-
|
|
36
|
-
return this.save();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
saveDataArr(arr: {id: string, object: T}[]): Promise<void> {
|
|
40
|
-
arr.forEach(e => {
|
|
41
|
-
this.data[e.id] = e.object;
|
|
42
|
-
this.rawData[e.id] = e.object.serialize();
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
return this.save();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
removeData(hash: string): Promise<void> {
|
|
49
|
-
if(this.rawData[hash]!=null) {
|
|
50
|
-
if(this.data[hash]!=null) delete this.data[hash];
|
|
51
|
-
delete this.rawData[hash];
|
|
52
|
-
return this.save();
|
|
53
|
-
}
|
|
54
|
-
return Promise.resolve();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
removeDataArr(hashArr: string[]): Promise<void> {
|
|
58
|
-
hashArr.forEach(hash => {
|
|
59
|
-
if(this.rawData[hash]!=null) {
|
|
60
|
-
if(this.data[hash]!=null) delete this.data[hash];
|
|
61
|
-
delete this.rawData[hash];
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
return this.save();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
loadData(type: new (data: any) => T): Promise<T[]> {
|
|
68
|
-
return Promise.resolve(
|
|
69
|
-
Object.keys(this.rawData).map(e => {
|
|
70
|
-
const deserialized = new type(this.rawData[e]);
|
|
71
|
-
this.data[e] = deserialized;
|
|
72
|
-
return deserialized;
|
|
73
|
-
})
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
private save(): Promise<void> {
|
|
78
|
-
window.localStorage.setItem(this.storageKey, JSON.stringify(this.rawData));
|
|
79
|
-
return Promise.resolve();
|
|
80
|
-
}
|
|
81
|
-
}
|
|
1
|
+
import {IStorageManager, StorageObject} from "@atomiqlabs/base";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* StorageManager using browser's local storage API
|
|
5
|
+
*/
|
|
6
|
+
export class LocalStorageManager<T extends StorageObject> implements IStorageManager<T> {
|
|
7
|
+
|
|
8
|
+
storageKey: string;
|
|
9
|
+
|
|
10
|
+
rawData: {
|
|
11
|
+
[hash: string]: any
|
|
12
|
+
} = null;
|
|
13
|
+
data: {
|
|
14
|
+
[hash: string]: T
|
|
15
|
+
} = {};
|
|
16
|
+
|
|
17
|
+
constructor(storageKey: string) {
|
|
18
|
+
this.storageKey = storageKey;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
init(): Promise<void> {
|
|
22
|
+
const completedTxt = window.localStorage.getItem(this.storageKey);
|
|
23
|
+
if(completedTxt!=null) {
|
|
24
|
+
this.rawData = JSON.parse(completedTxt);
|
|
25
|
+
if(this.rawData==null) this.rawData = {};
|
|
26
|
+
} else {
|
|
27
|
+
this.rawData = {};
|
|
28
|
+
}
|
|
29
|
+
return Promise.resolve();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
saveData(hash: string, object: T): Promise<void> {
|
|
33
|
+
this.data[hash] = object;
|
|
34
|
+
this.rawData[hash] = object.serialize();
|
|
35
|
+
|
|
36
|
+
return this.save();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
saveDataArr(arr: {id: string, object: T}[]): Promise<void> {
|
|
40
|
+
arr.forEach(e => {
|
|
41
|
+
this.data[e.id] = e.object;
|
|
42
|
+
this.rawData[e.id] = e.object.serialize();
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
return this.save();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
removeData(hash: string): Promise<void> {
|
|
49
|
+
if(this.rawData[hash]!=null) {
|
|
50
|
+
if(this.data[hash]!=null) delete this.data[hash];
|
|
51
|
+
delete this.rawData[hash];
|
|
52
|
+
return this.save();
|
|
53
|
+
}
|
|
54
|
+
return Promise.resolve();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
removeDataArr(hashArr: string[]): Promise<void> {
|
|
58
|
+
hashArr.forEach(hash => {
|
|
59
|
+
if(this.rawData[hash]!=null) {
|
|
60
|
+
if(this.data[hash]!=null) delete this.data[hash];
|
|
61
|
+
delete this.rawData[hash];
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
return this.save();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
loadData(type: new (data: any) => T): Promise<T[]> {
|
|
68
|
+
return Promise.resolve(
|
|
69
|
+
Object.keys(this.rawData).map(e => {
|
|
70
|
+
const deserialized = new type(this.rawData[e]);
|
|
71
|
+
this.data[e] = deserialized;
|
|
72
|
+
return deserialized;
|
|
73
|
+
})
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private save(): Promise<void> {
|
|
78
|
+
window.localStorage.setItem(this.storageKey, JSON.stringify(this.rawData));
|
|
79
|
+
return Promise.resolve();
|
|
80
|
+
}
|
|
81
|
+
}
|