@vnejs/plugins.feature.history 0.0.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.
@@ -0,0 +1,4 @@
1
+ export const SUBSCRIBE_EVENTS = {
2
+ LOAD: "vne:history:load",
3
+ BACK: "vne:history:back",
4
+ };
package/index.js ADDED
@@ -0,0 +1,7 @@
1
+ import { regPlugin } from "@vnejs/shared";
2
+
3
+ import { SUBSCRIBE_EVENTS } from "./const/events";
4
+
5
+ import { History } from "./modules/history";
6
+
7
+ regPlugin("HISTORY", { events: SUBSCRIBE_EVENTS }, [History]);
@@ -0,0 +1,78 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ export class History extends Module {
4
+ name = "history";
5
+
6
+ subscribe = () => {
7
+ this.on(this.EVENTS.HISTORY.LOAD, this.onHistoryLoad);
8
+ this.on(this.EVENTS.HISTORY.BACK, this.onHistoryBack);
9
+
10
+ this.on(this.EVENTS.TEXT.EMIT, this.onTextEmit);
11
+ this.on(this.EVENTS.TEXT.EMIT_END, this.onTextEmitEnd);
12
+
13
+ this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
14
+ this.on(this.EVENTS.STATE.SET, this.onSetState);
15
+ };
16
+
17
+ init = () => {
18
+ this.shared.history = { storage: {}, storageKeys: [] };
19
+ };
20
+
21
+ onTextEmitEnd = async () => {
22
+ this.presavedHistory = await this.emitOne(this.EVENTS.VENDORS.CLONE, this.globalState);
23
+ this.presavedHistory.scenario.curLine++;
24
+ };
25
+
26
+ onTextEmit = async () => {
27
+ if (!this.presavedHistory) return;
28
+
29
+ const lastHistory = this.state.history[this.state.history.length - 1];
30
+ if (lastHistory && this.presavedHistory.text.originalText === lastHistory.text) return;
31
+
32
+ const uid = (await this.emitOne(this.EVENTS.VENDORS.UID)).split("-").slice(0, 3).join("");
33
+
34
+ this.log({ action: "add", uid });
35
+
36
+ this.shared.history.storage[uid] = this.presavedHistory;
37
+ this.shared.history.storageKeys.push(uid);
38
+ this.state.history.push({
39
+ uid,
40
+ label: this.presavedHistory.scenario.label,
41
+ text: this.presavedHistory.text.originalText,
42
+ speaker: this.presavedHistory.text.speaker,
43
+ meet: this.presavedHistory.meet[this.presavedHistory.text.speaker] || 0,
44
+ });
45
+
46
+ while (this.state.history.length > 50) this.state.history.shift();
47
+ this.maxCurrentItem = this.state.history.length - 1;
48
+ while (this.shared.history.storageKeys.length > 100)
49
+ delete this.shared.history.storage[this.shared.history.storageKeys.shift()];
50
+ };
51
+
52
+ onHistoryLoad = async ({ uid } = {}) => {
53
+ const state = this.shared.history.storage[uid];
54
+
55
+ if (state) {
56
+ while (this.shared.history.storageKeys.length) {
57
+ const uidToDelete = this.shared.history.storageKeys.pop();
58
+ delete this.shared.history.storage[uidToDelete];
59
+ if (uidToDelete === uid) break;
60
+ }
61
+ await this.emit(this.EVENTS.SAVE.LOAD_STATE, { state: await this.emitOne(this.EVENTS.VENDORS.CLONE, state) });
62
+ this.emit(this.EVENTS.SCENARIO.LABEL_GET, { label: state.scenario.label });
63
+ } else this.log({ action: "deleted" });
64
+
65
+ this.currentItem = null;
66
+ };
67
+
68
+ onHistoryBack = () =>
69
+ this.state.history.length &&
70
+ this.emit(this.EVENTS.HISTORY.LOAD, { uid: this.state.history[this.state.history.length - 1].uid });
71
+
72
+ onSetState = async (globalState = {}) => {
73
+ this.state.history = globalState[this.name].history;
74
+ this.presavedHistory = await this.emitOne(this.EVENTS.VENDORS.CLONE, globalState);
75
+ };
76
+
77
+ getDefaultState = () => ({ history: [] });
78
+ }
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@vnejs/plugins.feature.history",
3
+ "version": "0.0.1",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1",
7
+ "publish:major": "npm version major && npm publish --access public",
8
+ "publish:minor": "npm version minor && npm publish --access public",
9
+ "publish:patch": "npm version patch && npm publish --access public"
10
+ },
11
+ "author": "",
12
+ "license": "ISC",
13
+ "description": "",
14
+ "peerDependencies": {
15
+ "@vnejs/shared": "*",
16
+ "@vnejs/module": "*"
17
+ }
18
+ }