@vnejs/plugins.core.storage 0.0.1 → 0.1.1
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/index.d.ts +15 -0
- package/modules/storage.js +11 -12
- package/package.json +4 -5
package/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as constants from "./const/const";
|
|
2
|
+
import { SUBSCRIBE_EVENTS } from "./const/events";
|
|
3
|
+
import { SETTINGS_KEYS } from "./const/settings";
|
|
4
|
+
|
|
5
|
+
type SubscribeEventsType = typeof SUBSCRIBE_EVENTS;
|
|
6
|
+
type SettingsKeysType = typeof SETTINGS_KEYS;
|
|
7
|
+
type ConstantsType = typeof constants;
|
|
8
|
+
|
|
9
|
+
const PLUGIN_NAME = "STORAGE";
|
|
10
|
+
|
|
11
|
+
declare global {
|
|
12
|
+
interface EVENTS {
|
|
13
|
+
[PLUGIN_NAME]: SubscribeEventsType;
|
|
14
|
+
}
|
|
15
|
+
}
|
package/modules/storage.js
CHANGED
|
@@ -13,10 +13,12 @@ export class Storage extends Module {
|
|
|
13
13
|
this.on(this.EVENTS.STORAGE.EXPORT, this.onStorageExport);
|
|
14
14
|
this.on(this.EVENTS.STORAGE.IMPORT, this.onStorageImport);
|
|
15
15
|
this.on(this.EVENTS.STORAGE.CLEAR, this.onStorageClear);
|
|
16
|
+
this.on(this.EVENTS.STORAGE.CHANGED, this.onStorageChanged);
|
|
16
17
|
};
|
|
17
18
|
init = () =>
|
|
18
19
|
new Promise((resolve) => {
|
|
19
20
|
const { version = 0, gameName = "defaultGameName" } = this.options?.global || {};
|
|
21
|
+
|
|
20
22
|
const name = `vne_store_${gameName}`;
|
|
21
23
|
localforage.ready(resolve);
|
|
22
24
|
localforage.config({ driver: localforage.INDEXEDDB, name, storeName: name, description: name, version });
|
|
@@ -29,7 +31,6 @@ export class Storage extends Module {
|
|
|
29
31
|
|
|
30
32
|
this.emit(this.EVENTS.STORAGE.CHANGED);
|
|
31
33
|
};
|
|
32
|
-
|
|
33
34
|
onStorageSetAll = async ({ storage }) => {
|
|
34
35
|
if (!this.isReady) await this.waitIsReady();
|
|
35
36
|
|
|
@@ -42,7 +43,7 @@ export class Storage extends Module {
|
|
|
42
43
|
wasChanged = true;
|
|
43
44
|
await localforage.setItem(key, storage[key]);
|
|
44
45
|
}
|
|
45
|
-
})
|
|
46
|
+
}),
|
|
46
47
|
);
|
|
47
48
|
|
|
48
49
|
const keys = await localforage.keys();
|
|
@@ -53,14 +54,13 @@ export class Storage extends Module {
|
|
|
53
54
|
wasChanged = true;
|
|
54
55
|
await localforage.removeItem(key);
|
|
55
56
|
}
|
|
56
|
-
})
|
|
57
|
+
}),
|
|
57
58
|
);
|
|
58
59
|
|
|
59
60
|
await this.emit(this.EVENTS.STORAGE.CHANGED);
|
|
60
61
|
|
|
61
62
|
return wasChanged;
|
|
62
63
|
};
|
|
63
|
-
|
|
64
64
|
onStorageGet = async ({ prefix = "", key = "" } = {}) => {
|
|
65
65
|
if (!this.isReady) await this.waitIsReady();
|
|
66
66
|
|
|
@@ -77,50 +77,49 @@ export class Storage extends Module {
|
|
|
77
77
|
await Promise.all(
|
|
78
78
|
realKeys.map(async (key) => {
|
|
79
79
|
result[prefix ? key.slice(prefix.length + 1) : key] = await localforage.getItem(key);
|
|
80
|
-
})
|
|
80
|
+
}),
|
|
81
81
|
);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
return result;
|
|
85
85
|
};
|
|
86
|
-
|
|
87
|
-
onStorageRm = async ({ prefix, key }) => {
|
|
86
|
+
onStorageRm = async ({ prefix = "", key = "" } = {}) => {
|
|
88
87
|
if (!this.isReady) await this.waitIsReady();
|
|
89
88
|
|
|
90
89
|
localforage.removeItem(`${prefix}.${key}`);
|
|
91
90
|
|
|
92
91
|
this.emit(this.EVENTS.STORAGE.CHANGED);
|
|
93
92
|
};
|
|
94
|
-
|
|
95
93
|
onStorageExport = async () => {
|
|
96
94
|
if (!this.isReady) await this.waitIsReady();
|
|
97
95
|
|
|
98
|
-
const state = await this.
|
|
96
|
+
const state = await this.emitOne(this.EVENTS.STORAGE.GET);
|
|
99
97
|
|
|
100
98
|
await this.emit(this.EVENTS.SYSTEM.FILE_LOAD, { obj: state, name: "state" });
|
|
101
99
|
|
|
102
100
|
return state;
|
|
103
101
|
};
|
|
104
|
-
|
|
105
102
|
onStorageImport = async () => {
|
|
106
103
|
if (!this.isReady) await this.waitIsReady();
|
|
107
104
|
|
|
108
|
-
const
|
|
105
|
+
const newStorage = await this.emitOne(this.EVENTS.SYSTEM.FILE_READ);
|
|
109
106
|
if (!newStorage) return;
|
|
110
107
|
|
|
111
108
|
const keys = await localforage.keys();
|
|
109
|
+
|
|
112
110
|
await Promise.all(keys.map((key) => localforage.removeItem(key)));
|
|
113
111
|
await Promise.all(Object.keys(newStorage).map((key) => localforage.setItem(key, newStorage[key])));
|
|
114
112
|
|
|
115
113
|
await this.emit(this.EVENTS.SYSTEM.RELOAD);
|
|
116
114
|
};
|
|
117
|
-
|
|
118
115
|
onStorageClear = async () => {
|
|
119
116
|
if (!this.isReady) await this.waitIsReady();
|
|
120
117
|
|
|
121
118
|
const keys = await localforage.keys();
|
|
119
|
+
|
|
122
120
|
await Promise.all(keys.map((key) => localforage.removeItem(key)));
|
|
123
121
|
|
|
124
122
|
await this.emit(this.EVENTS.SYSTEM.RELOAD);
|
|
125
123
|
};
|
|
124
|
+
onStorageChanged = () => this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "changed" } });
|
|
126
125
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vnejs/plugins.core.storage",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
7
|
+
"publish:major:plugin": "npm run publish:major",
|
|
8
|
+
"publish:minor:plugin": "npm run publish:minor",
|
|
9
|
+
"publish:patch:plugin": "npm run publish:patch",
|
|
7
10
|
"publish:major": "npm version major && npm publish --access public",
|
|
8
11
|
"publish:minor": "npm version minor && npm publish --access public",
|
|
9
12
|
"publish:patch": "npm version patch && npm publish --access public"
|
|
@@ -13,9 +16,5 @@
|
|
|
13
16
|
"description": "",
|
|
14
17
|
"dependencies": {
|
|
15
18
|
"localforage": "1.10.0"
|
|
16
|
-
},
|
|
17
|
-
"peerDependencies": {
|
|
18
|
-
"@vnejs/shared": "*",
|
|
19
|
-
"@vnejs/module": "*"
|
|
20
19
|
}
|
|
21
20
|
}
|