@vnejs/plugins.feature.time 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.
- package/const/const.js +1 -0
- package/const/events.js +6 -0
- package/index.js +8 -0
- package/modules/time.js +68 -0
- package/package.json +17 -0
package/const/const.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const ACTIONS = { ADD: "add", SET: "set" };
|
package/const/events.js
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { regPlugin } from "@vnejs/shared";
|
|
2
|
+
|
|
3
|
+
import * as constants from "./const/const";
|
|
4
|
+
import { SUBSCRIBE_EVENTS } from "./const/events";
|
|
5
|
+
|
|
6
|
+
import { TimeModule } from "./modules/time";
|
|
7
|
+
|
|
8
|
+
regPlugin("TIME", { constants, events: SUBSCRIBE_EVENTS }, [TimeModule]);
|
package/modules/time.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
|
|
3
|
+
export class TimeModule extends Module {
|
|
4
|
+
name = "time";
|
|
5
|
+
|
|
6
|
+
subscribe = () => {
|
|
7
|
+
this.on(this.EVENTS.TIME.SET, this.onTimeSet);
|
|
8
|
+
this.on(this.EVENTS.TIME.ADD, this.onTimeAdd);
|
|
9
|
+
|
|
10
|
+
this.on(this.EVENTS.STATE.SET, this.setStateFromGlobalState);
|
|
11
|
+
this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
init = () => this.emit(this.EVENTS.SCENARIO.EXEC_REG, { module: this.name, handler: this.execHandler });
|
|
15
|
+
|
|
16
|
+
execHandler = async (line) => {
|
|
17
|
+
const [action, ...values] = line.split(" ");
|
|
18
|
+
|
|
19
|
+
const hours = Number(values[0] || 0) || 0;
|
|
20
|
+
const minutes = Number(values[1] || 0) || 0;
|
|
21
|
+
const seconds = Number(values[2] || 0) || 0;
|
|
22
|
+
|
|
23
|
+
switch (action) {
|
|
24
|
+
case this.CONST.TIME.ACTIONS.ADD:
|
|
25
|
+
await this.emit(this.EVENTS.TIME.ADD, { hours, minutes, seconds });
|
|
26
|
+
break;
|
|
27
|
+
case this.CONST.TIME.ACTIONS.SET:
|
|
28
|
+
await this.emit(this.EVENTS.TIME.SET, { hours, minutes, seconds });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
onTimeSet = ({ hours = 0, minutes = 0, seconds = 0 }) => {
|
|
35
|
+
this.setState({ hours, minutes, seconds });
|
|
36
|
+
|
|
37
|
+
this.emit(this.EVENTS.TIME.CHANGED);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
onTimeAdd = ({ hours = 0, minutes = 0, seconds = 0 }) => {
|
|
41
|
+
this.setState({
|
|
42
|
+
hours: this.state.hours + hours,
|
|
43
|
+
minutes: this.state.minutes + minutes,
|
|
44
|
+
seconds: this.state.seconds + seconds,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if (this.state.seconds >= 60) {
|
|
48
|
+
this.state.minutes += Math.floor(this.state.seconds / 60);
|
|
49
|
+
this.state.seconds %= 60;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (this.state.minutes >= 60) {
|
|
53
|
+
this.state.hours += Math.floor(this.state.minutes / 60);
|
|
54
|
+
this.state.minutes %= 60;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
this.setState({ ...this.state });
|
|
58
|
+
|
|
59
|
+
if (this.state.hours >= 24) {
|
|
60
|
+
this.emit(this.EVENTS.TIME.ROLLOVER, { count: Math.floor(this.state.hours / 24) });
|
|
61
|
+
this.state.hours %= 24;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this.emit(this.EVENTS.TIME.CHANGED);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
getDefaultState = () => ({ hours: 0, minutes: 0, seconds: 0 });
|
|
68
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vnejs/plugins.feature.time",
|
|
3
|
+
"version": "0.0.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
|
+
}
|