@vnejs/plugins.feature.history 0.1.0 → 0.1.2
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/modules/history.js +24 -37
- package/package.json +1 -1
package/modules/history.js
CHANGED
|
@@ -3,12 +3,13 @@ import { Module } from "@vnejs/module";
|
|
|
3
3
|
export class History extends Module {
|
|
4
4
|
name = "history";
|
|
5
5
|
|
|
6
|
+
isLoadInProcess = false;
|
|
7
|
+
|
|
6
8
|
subscribe = () => {
|
|
7
9
|
this.on(this.EVENTS.HISTORY.LOAD, this.onHistoryLoad);
|
|
8
10
|
this.on(this.EVENTS.HISTORY.BACK, this.onHistoryBack);
|
|
9
11
|
|
|
10
|
-
this.on(this.EVENTS.TEXT.
|
|
11
|
-
this.on(this.EVENTS.TEXT.EMIT_END, this.onTextEmitEnd);
|
|
12
|
+
this.on(this.EVENTS.TEXT.INTERACT, this.onTextInteract);
|
|
12
13
|
|
|
13
14
|
this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
|
|
14
15
|
this.on(this.EVENTS.STATE.SET, this.onSetState);
|
|
@@ -18,61 +19,47 @@ export class History extends Module {
|
|
|
18
19
|
this.shared.history = { storage: {}, storageKeys: [] };
|
|
19
20
|
};
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
this.
|
|
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;
|
|
22
|
+
onTextInteract = async ({ state: textInteractState } = {}) => {
|
|
23
|
+
if (this.isLoadInProcess || textInteractState !== this.CONST.TEXT.INTERACT_STATES.EMPTY) return;
|
|
31
24
|
|
|
25
|
+
const state = await this.emitOne(this.EVENTS.STATE.GET);
|
|
32
26
|
const uid = (await this.emitOne(this.EVENTS.VENDORS.UID)).split("-").slice(0, 3).join("");
|
|
33
27
|
|
|
34
28
|
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "add", uid } });
|
|
35
29
|
|
|
36
|
-
const speaker =
|
|
30
|
+
const [speaker, label, text] = [state?.text?.text?.speaker, state?.scenario.label, state?.text?.originalText];
|
|
37
31
|
|
|
38
|
-
this.shared.history.storage[uid] =
|
|
32
|
+
this.shared.history.storage[uid] = state;
|
|
39
33
|
this.shared.history.storageKeys.push(uid);
|
|
40
|
-
this.state.history.push({
|
|
41
|
-
uid,
|
|
42
|
-
label: this.presavedHistory?.scenario.label,
|
|
43
|
-
text: this.presavedHistory?.text?.originalText,
|
|
44
|
-
speaker,
|
|
45
|
-
meet: this.presavedHistory?.["text.meet"][speaker] || 0,
|
|
46
|
-
});
|
|
34
|
+
this.state.history.push({ uid, label, text, speaker, meet: state?.["text.meet"]?.[speaker] || 0 });
|
|
47
35
|
|
|
48
36
|
while (this.state.history.length > 50) this.state.history.shift();
|
|
49
|
-
this.maxCurrentItem = this.state.history.length - 1;
|
|
50
37
|
while (this.shared.history.storageKeys.length > 100) delete this.shared.history.storage[this.shared.history.storageKeys.shift()];
|
|
51
38
|
};
|
|
52
39
|
|
|
53
40
|
onHistoryLoad = async ({ uid } = {}) => {
|
|
41
|
+
this.isLoadInProcess = true;
|
|
42
|
+
|
|
54
43
|
const state = this.shared.history.storage[uid];
|
|
55
44
|
|
|
56
|
-
if (state) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
await this.emit(this.EVENTS.SAVE.LOAD_STATE, { state: await this.emitOne(this.EVENTS.VENDORS.CLONE, state) });
|
|
63
|
-
this.emit(this.EVENTS.SCENARIO.LABEL_GET, { label: state.scenario.label });
|
|
64
|
-
} else {
|
|
65
|
-
this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "deleted" } });
|
|
45
|
+
if (!state) return this.emit(this.EVENTS.LOGS.EMIT, { module: this.name, value: { action: "deleted" } });
|
|
46
|
+
|
|
47
|
+
while (this.shared.history.storageKeys.length) {
|
|
48
|
+
const uidToDelete = this.shared.history.storageKeys.pop();
|
|
49
|
+
delete this.shared.history.storage[uidToDelete];
|
|
50
|
+
if (uidToDelete === uid) break;
|
|
66
51
|
}
|
|
52
|
+
const clonedState = await this.emitOne(this.EVENTS.VENDORS.CLONE, state);
|
|
53
|
+
await this.emit(this.EVENTS.STATE.LOAD, clonedState);
|
|
54
|
+
this.emit(this.EVENTS.SCENARIO.LABEL_GET, { label: state.scenario.label });
|
|
67
55
|
|
|
68
|
-
this.
|
|
56
|
+
this.isLoadInProcess = false;
|
|
69
57
|
};
|
|
70
58
|
|
|
71
|
-
onHistoryBack = () => this.state.history.length && this.emit(this.EVENTS.HISTORY.LOAD, { uid: this.state.history[this.state.history.length - 1].uid });
|
|
59
|
+
onHistoryBack = () => this.state.history.length && void this.emit(this.EVENTS.HISTORY.LOAD, { uid: this.state.history[this.state.history.length - 1].uid });
|
|
72
60
|
|
|
73
|
-
onSetState =
|
|
74
|
-
this.state.history =
|
|
75
|
-
this.presavedHistory = await this.emitOne(this.EVENTS.VENDORS.CLONE, globalState);
|
|
61
|
+
onSetState = ({ [this.name]: state } = {}) => {
|
|
62
|
+
this.state.history = state.history;
|
|
76
63
|
};
|
|
77
64
|
|
|
78
65
|
getDefaultState = () => ({ history: [] });
|