@vnejs/plugins.time.time 0.1.6
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/dist/index.d.ts +1 -0
- package/dist/index.js +4 -0
- package/dist/modules/time.d.ts +14 -0
- package/dist/modules/time.js +50 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.js +1 -0
- package/dist/utils/time.d.ts +13 -0
- package/dist/utils/time.js +1 -0
- package/package.json +46 -0
- package/src/index.ts +6 -0
- package/src/modules/time.ts +70 -0
- package/src/types.ts +17 -0
- package/src/utils/time.ts +15 -0
- package/tsconfig.json +9 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { regPlugin } from "@vnejs/shared";
|
|
2
|
+
import { CONSTANTS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/plugins.time.time.contract";
|
|
3
|
+
import { TimeModule } from "./modules/time.js";
|
|
4
|
+
regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS }, [TimeModule]);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
import type { TimePluginConstants, TimePluginEvents, TimePluginParams, TimePluginSettings } from "../types.js";
|
|
3
|
+
import type { ScenarioLineExecPayload, TimeState, TimeValuePayload } from "../utils/time.js";
|
|
4
|
+
export declare class TimeModule extends Module<TimePluginEvents, TimePluginConstants, TimePluginSettings, TimePluginParams, TimeState> {
|
|
5
|
+
name: string;
|
|
6
|
+
subscribe: () => void;
|
|
7
|
+
init: () => Promise<unknown[]> | undefined;
|
|
8
|
+
onLineExec: ({ line }?: ScenarioLineExecPayload) => Promise<void>;
|
|
9
|
+
onTimeSet: ({ hours, minutes, seconds }?: TimeValuePayload) => void;
|
|
10
|
+
onTimeAdd: ({ hours, minutes, seconds }?: TimeValuePayload) => void;
|
|
11
|
+
onStateSet: ({ [this.name]: state }?: Record<string, TimeState | undefined>) => Promise<void>;
|
|
12
|
+
emitTimeEvent: (action: string, arg: TimeValuePayload) => Promise<unknown[]> | undefined;
|
|
13
|
+
getDefaultState: () => TimeState;
|
|
14
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
import { tokenizeExecLine } from "@vnejs/helpers";
|
|
3
|
+
export class TimeModule extends Module {
|
|
4
|
+
name = "time";
|
|
5
|
+
subscribe = () => {
|
|
6
|
+
this.on(this.EVENTS.TIME.SET, this.onTimeSet);
|
|
7
|
+
this.on(this.EVENTS.TIME.ADD, this.onTimeAdd);
|
|
8
|
+
this.on(this.EVENTS.STATE.SET, this.onStateSet);
|
|
9
|
+
this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
|
|
10
|
+
};
|
|
11
|
+
init = () => this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { module: this.name, handler: this.onLineExec });
|
|
12
|
+
onLineExec = async ({ line = "" } = {}) => {
|
|
13
|
+
const [action = "", hours = 0, minutes = 0, seconds = 0] = tokenizeExecLine(line);
|
|
14
|
+
await this.emitTimeEvent(String(action), { hours: Number(hours), minutes: Number(minutes), seconds: Number(seconds) });
|
|
15
|
+
this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
|
|
16
|
+
};
|
|
17
|
+
onTimeSet = ({ hours = 0, minutes = 0, seconds = 0 } = {}) => {
|
|
18
|
+
this.setState({ hours, minutes, seconds });
|
|
19
|
+
this.emit(this.EVENTS.TIME.CHANGED);
|
|
20
|
+
};
|
|
21
|
+
onTimeAdd = ({ hours = 0, minutes = 0, seconds = 0 } = {}) => {
|
|
22
|
+
this.setState({
|
|
23
|
+
hours: this.state.hours + hours,
|
|
24
|
+
minutes: this.state.minutes + minutes,
|
|
25
|
+
seconds: this.state.seconds + seconds,
|
|
26
|
+
});
|
|
27
|
+
if (this.state.seconds >= 60) {
|
|
28
|
+
this.state.minutes += Math.floor(this.state.seconds / 60);
|
|
29
|
+
this.state.seconds %= 60;
|
|
30
|
+
}
|
|
31
|
+
if (this.state.minutes >= 60) {
|
|
32
|
+
this.state.hours += Math.floor(this.state.minutes / 60);
|
|
33
|
+
this.state.minutes %= 60;
|
|
34
|
+
}
|
|
35
|
+
this.setState({ ...this.state });
|
|
36
|
+
if (this.state.hours >= 24) {
|
|
37
|
+
this.emit(this.EVENTS.TIME.ROLLOVER, { count: Math.floor(this.state.hours / 24) });
|
|
38
|
+
this.state.hours %= 24;
|
|
39
|
+
}
|
|
40
|
+
this.emit(this.EVENTS.TIME.CHANGED);
|
|
41
|
+
};
|
|
42
|
+
onStateSet = async ({ [this.name]: state } = {}) => this.setState((await this.emitOne(this.EVENTS.VENDORS.CLONE, state)));
|
|
43
|
+
emitTimeEvent = (action, arg) => {
|
|
44
|
+
if (action === this.CONST.TIME.ACTIONS.ADD)
|
|
45
|
+
return this.emit(this.EVENTS.TIME.ADD, arg);
|
|
46
|
+
if (action === this.CONST.TIME.ACTIONS.SET)
|
|
47
|
+
return this.emit(this.EVENTS.TIME.SET, arg);
|
|
48
|
+
};
|
|
49
|
+
getDefaultState = () => ({ hours: 0, minutes: 0, seconds: 0 });
|
|
50
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { VnePluginConstantsMapRegistry, VnePluginEventsMapRegistry, VnePluginParamsMapRegistry, VnePluginSettingsMapRegistry } from "@vnejs/shared";
|
|
2
|
+
import type { PluginName as ScenarioPluginName, SubscribeEvents as ScenarioSubscribeEvents } from "@vnejs/plugins.core.scenario.contract";
|
|
3
|
+
import type { PluginName as StatePluginName, SubscribeEvents as StateSubscribeEvents } from "@vnejs/plugins.core.state.contract";
|
|
4
|
+
import type { PluginName as VendorsPluginName, SubscribeEvents as VendorsSubscribeEvents } from "@vnejs/plugins.core.vendors.contract";
|
|
5
|
+
import type { Constants, PluginName, SubscribeEvents } from "@vnejs/plugins.time.time.contract";
|
|
6
|
+
export type TimePluginEvents = VnePluginEventsMapRegistry & Record<PluginName, SubscribeEvents> & Record<ScenarioPluginName, ScenarioSubscribeEvents> & Record<StatePluginName, StateSubscribeEvents> & Record<VendorsPluginName, VendorsSubscribeEvents>;
|
|
7
|
+
export type TimePluginConstants = VnePluginConstantsMapRegistry & Record<PluginName, Constants>;
|
|
8
|
+
export type TimePluginSettings = VnePluginSettingsMapRegistry;
|
|
9
|
+
export type TimePluginParams = VnePluginParamsMapRegistry;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vnejs/plugins.time.time",
|
|
3
|
+
"version": "0.1.6",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"tsconfig.json"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
22
|
+
"build": "npx @vnejs/monorepo package",
|
|
23
|
+
"publish:major:plugin": "npm run publish:major",
|
|
24
|
+
"publish:minor:plugin": "npm run publish:minor",
|
|
25
|
+
"publish:patch:plugin": "npm run publish:patch",
|
|
26
|
+
"publish:major": "npx @vnejs/monorepo publish major --access public",
|
|
27
|
+
"publish:minor": "npx @vnejs/monorepo publish minor --access public",
|
|
28
|
+
"publish:patch": "npx @vnejs/monorepo publish patch --access public"
|
|
29
|
+
},
|
|
30
|
+
"author": "",
|
|
31
|
+
"license": "ISC",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@vnejs/plugins.time.time.contract": "~0.0.1"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"@vnejs/helpers": "~0.1.0",
|
|
37
|
+
"@vnejs/module": "~0.0.1",
|
|
38
|
+
"@vnejs/plugins.core.scenario.contract": "~0.0.1",
|
|
39
|
+
"@vnejs/plugins.core.state.contract": "~0.0.1",
|
|
40
|
+
"@vnejs/plugins.core.vendors.contract": "~0.0.1",
|
|
41
|
+
"@vnejs/shared": "~0.0.9"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@vnejs/configs.ts-common": "~0.0.1"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { regPlugin } from "@vnejs/shared";
|
|
2
|
+
import { CONSTANTS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/plugins.time.time.contract";
|
|
3
|
+
|
|
4
|
+
import { TimeModule } from "./modules/time.js";
|
|
5
|
+
|
|
6
|
+
regPlugin(PLUGIN_NAME, { constants: CONSTANTS, events: SUBSCRIBE_EVENTS }, [TimeModule]);
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Module } from "@vnejs/module";
|
|
2
|
+
import { tokenizeExecLine } from "@vnejs/helpers";
|
|
3
|
+
|
|
4
|
+
import type { TimePluginConstants, TimePluginEvents, TimePluginParams, TimePluginSettings } from "../types.js";
|
|
5
|
+
import type { ScenarioLineExecPayload, TimeState, TimeValuePayload } from "../utils/time.js";
|
|
6
|
+
|
|
7
|
+
export class TimeModule extends Module<TimePluginEvents, TimePluginConstants, TimePluginSettings, TimePluginParams, TimeState> {
|
|
8
|
+
name = "time";
|
|
9
|
+
|
|
10
|
+
subscribe = () => {
|
|
11
|
+
this.on(this.EVENTS.TIME.SET, this.onTimeSet);
|
|
12
|
+
this.on(this.EVENTS.TIME.ADD, this.onTimeAdd);
|
|
13
|
+
|
|
14
|
+
this.on(this.EVENTS.STATE.SET, this.onStateSet);
|
|
15
|
+
this.on(this.EVENTS.STATE.CLEAR, this.setDefaultState);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
init = () => this.emit(this.EVENTS.SCENARIO.LINE_EXEC_REG, { module: this.name, handler: this.onLineExec });
|
|
19
|
+
|
|
20
|
+
onLineExec = async ({ line = "" }: ScenarioLineExecPayload = {}) => {
|
|
21
|
+
const [action = "", hours = 0, minutes = 0, seconds = 0] = tokenizeExecLine(line);
|
|
22
|
+
|
|
23
|
+
await this.emitTimeEvent(String(action), { hours: Number(hours), minutes: Number(minutes), seconds: Number(seconds) });
|
|
24
|
+
|
|
25
|
+
this.emit(this.EVENTS.SCENARIO.NEXT, { module: this.name });
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
onTimeSet = ({ hours = 0, minutes = 0, seconds = 0 }: TimeValuePayload = {}) => {
|
|
29
|
+
this.setState({ hours, minutes, seconds });
|
|
30
|
+
|
|
31
|
+
this.emit(this.EVENTS.TIME.CHANGED);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
onTimeAdd = ({ hours = 0, minutes = 0, seconds = 0 }: TimeValuePayload = {}) => {
|
|
35
|
+
this.setState({
|
|
36
|
+
hours: this.state.hours + hours,
|
|
37
|
+
minutes: this.state.minutes + minutes,
|
|
38
|
+
seconds: this.state.seconds + seconds,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (this.state.seconds >= 60) {
|
|
42
|
+
this.state.minutes += Math.floor(this.state.seconds / 60);
|
|
43
|
+
this.state.seconds %= 60;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (this.state.minutes >= 60) {
|
|
47
|
+
this.state.hours += Math.floor(this.state.minutes / 60);
|
|
48
|
+
this.state.minutes %= 60;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
this.setState({ ...this.state });
|
|
52
|
+
|
|
53
|
+
if (this.state.hours >= 24) {
|
|
54
|
+
this.emit(this.EVENTS.TIME.ROLLOVER, { count: Math.floor(this.state.hours / 24) });
|
|
55
|
+
this.state.hours %= 24;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
this.emit(this.EVENTS.TIME.CHANGED);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
onStateSet = async ({ [this.name]: state }: Record<string, TimeState | undefined> = {}) =>
|
|
62
|
+
this.setState((await this.emitOne(this.EVENTS.VENDORS.CLONE, state)) as TimeState);
|
|
63
|
+
|
|
64
|
+
emitTimeEvent = (action: string, arg: TimeValuePayload) => {
|
|
65
|
+
if (action === this.CONST.TIME.ACTIONS.ADD) return this.emit(this.EVENTS.TIME.ADD, arg);
|
|
66
|
+
if (action === this.CONST.TIME.ACTIONS.SET) return this.emit(this.EVENTS.TIME.SET, arg);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
getDefaultState = (): TimeState => ({ hours: 0, minutes: 0, seconds: 0 });
|
|
70
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { VnePluginConstantsMapRegistry, VnePluginEventsMapRegistry, VnePluginParamsMapRegistry, VnePluginSettingsMapRegistry } from "@vnejs/shared";
|
|
2
|
+
import type { PluginName as ScenarioPluginName, SubscribeEvents as ScenarioSubscribeEvents } from "@vnejs/plugins.core.scenario.contract";
|
|
3
|
+
import type { PluginName as StatePluginName, SubscribeEvents as StateSubscribeEvents } from "@vnejs/plugins.core.state.contract";
|
|
4
|
+
import type { PluginName as VendorsPluginName, SubscribeEvents as VendorsSubscribeEvents } from "@vnejs/plugins.core.vendors.contract";
|
|
5
|
+
import type { Constants, PluginName, SubscribeEvents } from "@vnejs/plugins.time.time.contract";
|
|
6
|
+
|
|
7
|
+
export type TimePluginEvents = VnePluginEventsMapRegistry &
|
|
8
|
+
Record<PluginName, SubscribeEvents> &
|
|
9
|
+
Record<ScenarioPluginName, ScenarioSubscribeEvents> &
|
|
10
|
+
Record<StatePluginName, StateSubscribeEvents> &
|
|
11
|
+
Record<VendorsPluginName, VendorsSubscribeEvents>;
|
|
12
|
+
|
|
13
|
+
export type TimePluginConstants = VnePluginConstantsMapRegistry & Record<PluginName, Constants>;
|
|
14
|
+
|
|
15
|
+
export type TimePluginSettings = VnePluginSettingsMapRegistry;
|
|
16
|
+
|
|
17
|
+
export type TimePluginParams = VnePluginParamsMapRegistry;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type TimeState = {
|
|
2
|
+
hours: number;
|
|
3
|
+
minutes: number;
|
|
4
|
+
seconds: number;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type TimeValuePayload = {
|
|
8
|
+
hours?: number;
|
|
9
|
+
minutes?: number;
|
|
10
|
+
seconds?: number;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type ScenarioLineExecPayload = {
|
|
14
|
+
line?: string;
|
|
15
|
+
};
|