@vnejs/plugins.save 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/const/events.js +9 -0
- package/index.js +8 -0
- package/modules/load.js +21 -0
- package/modules/save.js +54 -0
- package/package.json +17 -0
package/const/events.js
ADDED
package/index.js
ADDED
package/modules/load.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
export class Load extends Module {
|
|
4
|
+
name = "save.load";
|
|
5
|
+
|
|
6
|
+
subscribe = () => {
|
|
7
|
+
this.on(this.EVENTS.SAVE.LOAD, this.onLoad);
|
|
8
|
+
this.on(this.EVENTS.SAVE.PRELOAD, this.onPreload);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
onLoad = ({ key } = {}) => this.emitSave(key, this.EVENTS.STATE.LOAD, "load");
|
|
12
|
+
onPreload = ({ key } = {}) => this.emitSave(key, this.EVENTS.STATE.PRELOAD, "preload");
|
|
13
|
+
|
|
14
|
+
emitSave = async (key, event, logAction) => {
|
|
15
|
+
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: logAction, key } });
|
|
16
|
+
|
|
17
|
+
const save = await this.emitOne(this.EVENTS.SAVE.GET, { key });
|
|
18
|
+
|
|
19
|
+
return save && this.emit(event, save.state);
|
|
20
|
+
};
|
|
21
|
+
}
|
package/modules/save.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
export class Save extends Module {
|
|
4
|
+
name = "save";
|
|
5
|
+
|
|
6
|
+
subscribe = () => {
|
|
7
|
+
this.on(this.EVENTS.SAVE.CREATE, this.onCreate);
|
|
8
|
+
this.on(this.EVENTS.SAVE.GET, this.onGet);
|
|
9
|
+
this.on(this.EVENTS.SAVE.DELETE, this.onDelete);
|
|
10
|
+
|
|
11
|
+
this.on(this.EVENTS.SAVE.CLEAR, this.onClear);
|
|
12
|
+
|
|
13
|
+
this.on(this.EVENTS.STATE.SET, this.setStateFromGlobalState);
|
|
14
|
+
this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
init = () => this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { module: this.name, handler: this.execHandler });
|
|
18
|
+
|
|
19
|
+
execHandler = (line) => {
|
|
20
|
+
this.setState({ key: line });
|
|
21
|
+
|
|
22
|
+
this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
onGet = async ({ key } = {}) => {
|
|
26
|
+
const save = await this.emitOne(this.EVENTS.STORAGE.GET, { prefix: this.name, key });
|
|
27
|
+
|
|
28
|
+
return JSON.parse(save);
|
|
29
|
+
};
|
|
30
|
+
onCreate = async ({ key, withScreenshot = true } = {}) => {
|
|
31
|
+
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { type: "save", key } });
|
|
32
|
+
|
|
33
|
+
const state = await this.emitOne(this.EVENTS.STATE.GET);
|
|
34
|
+
const save = { state, version: this.PARAMS.STORAGE.VERSION, ts: new Date().getTime(), key: this.state.key };
|
|
35
|
+
|
|
36
|
+
if (withScreenshot) {
|
|
37
|
+
const [width, height] = [this.shared.settings[this.SETTINGS.LAYER.WIDTH] / 2, this.shared.settings[this.SETTINGS.LAYER.HEIGHT] / 2];
|
|
38
|
+
const canvas = await this.emitOne(this.EVENTS.VENDORS.SCREEN, { id: "app", width, height, quality: 0.5 });
|
|
39
|
+
|
|
40
|
+
save.screen = canvas;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return this.emit(this.EVENTS.STORAGE.SET, { prefix: this.name, key, value: JSON.stringify(save) });
|
|
44
|
+
};
|
|
45
|
+
onDelete = ({ key } = {}) => {
|
|
46
|
+
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { type: "delete", key } });
|
|
47
|
+
|
|
48
|
+
return this.emit(this.EVENTS.STORAGE.RM, { prefix: this.name, key });
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
onClear = () => this.emit(this.EVENTS.STORAGE.RM_ALL, { prefix: this.name });
|
|
52
|
+
|
|
53
|
+
getDefaultState = () => ({ key: "" });
|
|
54
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vnejs/plugins.save",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"scripts": {
|
|
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",
|
|
10
|
+
"publish:major": "npm version major && npm publish --access public",
|
|
11
|
+
"publish:minor": "npm version minor && npm publish --access public",
|
|
12
|
+
"publish:patch": "npm version patch && npm publish --access public"
|
|
13
|
+
},
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"description": ""
|
|
17
|
+
}
|