eternal-timer 1.5.0 → 2.0.0
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/README.md +37 -21
- package/dist/cjs/index.cjs +173 -155
- package/dist/cjs/index.d.ts +70 -42
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/searchRoot.js.map +1 -1
- package/dist/esm/index.d.ts +70 -42
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +171 -151
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/searchRoot.js.map +1 -1
- package/dist/tsconfig.cjs.tsbuildinfo +1 -0
- package/dist/tsconfig.esm.tsbuildinfo +1 -0
- package/package.json +12 -4
- package/src/index.ts +0 -190
- package/src/searchRoot.ts +0 -17
- /package/dist/cjs/{searchRoot.js → searchRoot.cjs} +0 -0
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ A simple and persistent timer library for Node.js. Timers are saved to a file an
|
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- **Monitor Timers (asynchronous)**: Start monitoring expired timers asynchronously; the function returns immediately and the callback is
|
|
7
|
+
- **Monitor Timers (asynchronous)**: Start monitoring expired timers asynchronously; the function returns immediately and the callback is called when timers expire.
|
|
8
8
|
- **Persistence**: Save timer data to a file that persists across process restarts
|
|
9
9
|
|
|
10
10
|
## Installation
|
|
@@ -18,29 +18,40 @@ npm install eternal-timer
|
|
|
18
18
|
### Basic Example
|
|
19
19
|
|
|
20
20
|
```typescript
|
|
21
|
-
import {
|
|
21
|
+
import { TimersManager } from 'eternal-timer';
|
|
22
22
|
|
|
23
23
|
async function main() {
|
|
24
|
-
|
|
25
|
-
const timerId = await createTimer(5000);
|
|
26
|
-
console.log('Timer created:', timerId);
|
|
24
|
+
const manager = new TimersManager();
|
|
27
25
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
console.log('Timer
|
|
31
|
-
});
|
|
26
|
+
// Create a timer (5 seconds)
|
|
27
|
+
const timerId = await manager.createTimer(5000);
|
|
28
|
+
console.log('Timer created:', timerId);
|
|
32
29
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
// Monitor timers (executes when timer expires)
|
|
31
|
+
manager.checkTimers(async (timer) => {
|
|
32
|
+
console.log('Timer expired:', timer.id);
|
|
33
|
+
});
|
|
36
34
|
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
// Display all timers
|
|
36
|
+
const timers = await manager.showTimers();
|
|
37
|
+
console.log('Active timers:', timers);
|
|
38
|
+
|
|
39
|
+
// Remove a timer
|
|
40
|
+
await manager.removeTimer(timerId);
|
|
39
41
|
}
|
|
42
|
+
|
|
43
|
+
main();
|
|
40
44
|
```
|
|
41
45
|
|
|
42
46
|
## API
|
|
43
47
|
|
|
48
|
+
### `new TimersManager(timerfiledir?: string)`
|
|
49
|
+
|
|
50
|
+
Creates a new `TimersManager` instance.
|
|
51
|
+
|
|
52
|
+
**Parameters:**
|
|
53
|
+
- `timerfiledir` (string, optional): The path to the directory where the timer file is stored. If omitted, `.timers` under the project root is used.
|
|
54
|
+
|
|
44
55
|
### `createTimer(length: number): Promise<string>`
|
|
45
56
|
|
|
46
57
|
Creates a new timer.
|
|
@@ -52,7 +63,7 @@ Creates a new timer.
|
|
|
52
63
|
|
|
53
64
|
**Throws:** If length is invalid(e.g. length < 0) or file operation fails
|
|
54
65
|
|
|
55
|
-
### `removeTimer(id: string): Promise<
|
|
66
|
+
### `removeTimer(id: string): Promise<void>`
|
|
56
67
|
|
|
57
68
|
Removes a timer by ID.
|
|
58
69
|
|
|
@@ -61,12 +72,17 @@ Removes a timer by ID.
|
|
|
61
72
|
|
|
62
73
|
**Returns:** void
|
|
63
74
|
|
|
64
|
-
|
|
75
|
+
**Throws:** If the timer with the specified ID is not found or if a file operation fails.
|
|
76
|
+
|
|
77
|
+
### `checkTimers(callback: (timer: Timer) => Promise<void>, interval?: number): Promise<void>`
|
|
78
|
+
|
|
79
|
+
Starts monitoring expired timers and returns immediately.
|
|
65
80
|
|
|
66
|
-
|
|
81
|
+
The callback is invoked when a timer expires during periodic checks.
|
|
82
|
+
The callback is awaited before the next timer check continues.
|
|
67
83
|
|
|
68
84
|
**Parameters:**
|
|
69
|
-
- `callback`: Function invoked when an expired timer is detected (called
|
|
85
|
+
- `callback`: Function invoked when an expired timer is detected (called during periodic checks and awaited)
|
|
70
86
|
- `interval` (number, optional): Check interval in milliseconds (default: 50ms)
|
|
71
87
|
|
|
72
88
|
**Throws:** If file operation fails
|
|
@@ -84,8 +100,8 @@ Retrieves all active timers.
|
|
|
84
100
|
```typescript
|
|
85
101
|
type Timer = {
|
|
86
102
|
id: string; // Unique timer identifier (UUID)
|
|
87
|
-
start:
|
|
88
|
-
stop:
|
|
103
|
+
start: number; // Timer start timestamp
|
|
104
|
+
stop: number; // Timer end timestamp
|
|
89
105
|
}
|
|
90
106
|
```
|
|
91
107
|
|
|
@@ -111,4 +127,4 @@ Licensed under the Apache License, Version 2.0. See the `LICENSE` file for detai
|
|
|
111
127
|
|
|
112
128
|
## Repository
|
|
113
129
|
|
|
114
|
-
https://github.com/SUKEsann2000/eternal-timer
|
|
130
|
+
https://github.com/SUKEsann2000/eternal-timer
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -3,185 +3,203 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
7
|
-
exports.removeTimer = removeTimer;
|
|
8
|
-
exports.checkTimers = checkTimers;
|
|
9
|
-
exports.showTimers = showTimers;
|
|
6
|
+
exports.TimersManager = void 0;
|
|
10
7
|
const fs_1 = __importDefault(require("fs"));
|
|
11
8
|
const path_1 = __importDefault(require("path"));
|
|
12
9
|
const searchRoot_js_1 = __importDefault(require("./searchRoot.js"));
|
|
13
10
|
const uuid_1 = require("uuid");
|
|
14
|
-
// search root folder of project
|
|
15
|
-
const rootdir = (0, searchRoot_js_1.default)();
|
|
16
|
-
const timerfiledir = path_1.default.join(rootdir, ".timers");
|
|
17
11
|
/**
|
|
18
|
-
*
|
|
19
|
-
* @description
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
12
|
+
* TimersManager
|
|
13
|
+
* @description
|
|
14
|
+
* Manages timers stored in a file.
|
|
15
|
+
* Each timer is stored as: `id start stop`.
|
|
16
|
+
*
|
|
17
|
+
* - Timers are persisted in a file
|
|
18
|
+
* - Expired timers are detected by polling
|
|
24
19
|
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
class TimersManager {
|
|
21
|
+
timerfiledir;
|
|
22
|
+
/**
|
|
23
|
+
* constructor
|
|
24
|
+
* @param timerfiledir(string, optional)
|
|
25
|
+
* If omitted, `.timers` under the project root is used.
|
|
26
|
+
*/
|
|
27
|
+
constructor(timerfiledir) {
|
|
28
|
+
this.timerfiledir =
|
|
29
|
+
timerfiledir ?? path_1.default.join((0, searchRoot_js_1.default)(), ".timers");
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
async function createTimer(length) {
|
|
44
|
-
try {
|
|
45
|
-
await createFile();
|
|
46
|
-
if (length < 0) {
|
|
47
|
-
throw new Error(`Invailed length: ${length}`);
|
|
31
|
+
/**
|
|
32
|
+
* createFile
|
|
33
|
+
* @description create `.timers` file
|
|
34
|
+
* @returns void
|
|
35
|
+
* @throws If file operation fails
|
|
36
|
+
*/
|
|
37
|
+
async createFile() {
|
|
38
|
+
try {
|
|
39
|
+
await fs_1.default.promises.access(this.timerfiledir);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
await fs_1.default.promises.writeFile(this.timerfiledir, "");
|
|
48
43
|
}
|
|
49
|
-
length = Math.trunc(length);
|
|
50
|
-
// uuid, start, end
|
|
51
|
-
const id = (0, uuid_1.v4)();
|
|
52
|
-
const now = Date.now();
|
|
53
|
-
const newTimerData = `${id} ${now.toString()} ${(now + length).toString()}`;
|
|
54
|
-
await fs_1.default.promises.appendFile(timerfiledir, newTimerData + "\n");
|
|
55
|
-
return id;
|
|
56
|
-
}
|
|
57
|
-
catch (e) {
|
|
58
|
-
throw new Error(`Error when creating timer: ${e}`);
|
|
59
44
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
for (const timerData of timersData) {
|
|
77
|
-
if (!timerData.trim()) {
|
|
78
|
-
found = true;
|
|
79
|
-
continue;
|
|
45
|
+
/**
|
|
46
|
+
* createTimer
|
|
47
|
+
* @description Creates a new timer.
|
|
48
|
+
* @param length Timer duration in milliseconds
|
|
49
|
+
* @returns Promise that resolves to the timer ID (UUID)
|
|
50
|
+
* @throws If length is invalid(e.g. length < 0) or file operation fails
|
|
51
|
+
* @example
|
|
52
|
+
* const manager = new TimersManager();
|
|
53
|
+
* const newTimer = await manager.createTimer(5000);
|
|
54
|
+
* // newTimer will be id of the timer
|
|
55
|
+
*/
|
|
56
|
+
async createTimer(length) {
|
|
57
|
+
try {
|
|
58
|
+
await this.createFile();
|
|
59
|
+
if (length < 0) {
|
|
60
|
+
throw new Error(`Invailed length: ${length}`);
|
|
80
61
|
}
|
|
81
|
-
|
|
82
|
-
|
|
62
|
+
length = Math.trunc(length);
|
|
63
|
+
// uuid, start, end
|
|
64
|
+
const id = (0, uuid_1.v4)();
|
|
65
|
+
const now = Date.now();
|
|
66
|
+
const newTimerData = `${id} ${now.toString()} ${(now + length).toString()}`;
|
|
67
|
+
await fs_1.default.promises.appendFile(this.timerfiledir, newTimerData + "\n");
|
|
68
|
+
return id;
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
throw new Error(`Error when creating timer: ${e}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* removeTimer
|
|
76
|
+
* @description Removes a timer by ID.
|
|
77
|
+
* @param id ID of the timer to remove
|
|
78
|
+
* @returns void
|
|
79
|
+
* @throws If file operation fails
|
|
80
|
+
* @example
|
|
81
|
+
* await manager.removeTimer(id);
|
|
82
|
+
*/
|
|
83
|
+
async removeTimer(id) {
|
|
84
|
+
try {
|
|
85
|
+
const timersRaw = fs_1.default.readFileSync(this.timerfiledir, "utf-8");
|
|
86
|
+
const timersData = timersRaw.split(/\r?\n/);
|
|
87
|
+
let newTimersData = "";
|
|
88
|
+
let found = false;
|
|
89
|
+
for (const timerData of timersData) {
|
|
90
|
+
if (!timerData.trim())
|
|
91
|
+
continue;
|
|
92
|
+
const [timerId] = timerData.split(" ");
|
|
93
|
+
if (timerId === id) {
|
|
94
|
+
found = true;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
83
97
|
newTimersData += timerData + "\n";
|
|
84
98
|
}
|
|
99
|
+
if (!found) {
|
|
100
|
+
throw new Error(`Timer with id ${id} not found`);
|
|
101
|
+
}
|
|
102
|
+
await fs_1.default.promises.writeFile(this.timerfiledir, newTimersData, "utf-8");
|
|
103
|
+
return;
|
|
85
104
|
}
|
|
86
|
-
|
|
87
|
-
throw new Error(`
|
|
105
|
+
catch (e) {
|
|
106
|
+
throw new Error(`Error when removing timer: ${e}`);
|
|
88
107
|
}
|
|
89
|
-
await fs_1.default.promises.writeFile(timerfiledir, newTimersData, "utf-8");
|
|
90
|
-
return;
|
|
91
108
|
}
|
|
92
|
-
|
|
93
|
-
|
|
109
|
+
/**
|
|
110
|
+
* @description Starts monitoring expired timers asynchronously and returns immediately. The callback is invoked asynchronously when a timer expires.
|
|
111
|
+
* The callback is awaited before continuing.
|
|
112
|
+
* @param callback Function invoked when an expired timer is detected (called asynchronously)
|
|
113
|
+
* @param interval (number, optional): Check interval in milliseconds (default: 50ms)
|
|
114
|
+
* @throws If file operation fails
|
|
115
|
+
* @example
|
|
116
|
+
* manager.checkTimers((timer) => {
|
|
117
|
+
* console.log(`A timer was stopped: ${timer.id}`);
|
|
118
|
+
* });
|
|
119
|
+
*/
|
|
120
|
+
async checkTimers(callback, interval = 50) {
|
|
121
|
+
try {
|
|
122
|
+
await this.createFile();
|
|
123
|
+
setInterval(async () => {
|
|
124
|
+
const timersDataRaw = await fs_1.default.promises.readFile(this.timerfiledir, "utf-8");
|
|
125
|
+
const timersData = timersDataRaw.split(/\r?\n/);
|
|
126
|
+
const timersSet = new Set();
|
|
127
|
+
await this.checkTimerfileSyntax(timersDataRaw);
|
|
128
|
+
for (const timerData of timersData) {
|
|
129
|
+
if (!timerData.trim())
|
|
130
|
+
continue;
|
|
131
|
+
const [id, startStr, stopStr] = timerData.split(" ");
|
|
132
|
+
timersSet.add({
|
|
133
|
+
id: id,
|
|
134
|
+
start: Number(startStr),
|
|
135
|
+
stop: Number(stopStr),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
const now = Date.now();
|
|
139
|
+
for (const timer of timersSet) {
|
|
140
|
+
if (Number(timer.stop) <= now) {
|
|
141
|
+
await this.removeTimer(timer.id);
|
|
142
|
+
await callback(timer);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}, interval);
|
|
146
|
+
}
|
|
147
|
+
catch (e) {
|
|
148
|
+
throw new Error(`Error when checking alarm: ${e}`);
|
|
149
|
+
}
|
|
94
150
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const timersDataRaw = fs_1.default.readFileSync(timerfiledir, "utf-8");
|
|
111
|
-
const timersData = timersDataRaw.split(/\r?\n/);
|
|
112
|
-
const timersSet = new Set();
|
|
113
|
-
checkTimerfileSyntax(timersDataRaw);
|
|
151
|
+
/**
|
|
152
|
+
* showTimers
|
|
153
|
+
* @description Retrieves all active timers.
|
|
154
|
+
* @returns Array of `Timer` objects
|
|
155
|
+
* @throws If file operation fails
|
|
156
|
+
* @example
|
|
157
|
+
* const timers = await manager.showTimers();
|
|
158
|
+
* console.log(JSON.stringify(timers))
|
|
159
|
+
*/
|
|
160
|
+
async showTimers() {
|
|
161
|
+
try {
|
|
162
|
+
await this.createFile();
|
|
163
|
+
const timersRaw = fs_1.default.readFileSync(this.timerfiledir, "utf-8");
|
|
164
|
+
const timersData = timersRaw.split(/\r?\n/);
|
|
165
|
+
const timersJSON = [];
|
|
114
166
|
for (const timerData of timersData) {
|
|
167
|
+
const splitedTimerData = timerData.split(" ");
|
|
115
168
|
if (!timerData.trim())
|
|
116
169
|
continue;
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
stop: stopStr
|
|
170
|
+
timersJSON.push({
|
|
171
|
+
id: splitedTimerData[0],
|
|
172
|
+
start: Number(splitedTimerData[1]),
|
|
173
|
+
stop: Number(splitedTimerData[2]),
|
|
122
174
|
});
|
|
123
175
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}, interval);
|
|
132
|
-
}
|
|
133
|
-
catch (e) {
|
|
134
|
-
throw new Error(`Error when checking alarm: ${e}`);
|
|
176
|
+
return timersJSON;
|
|
177
|
+
}
|
|
178
|
+
catch (e) {
|
|
179
|
+
throw new Error(`Error when showing timers: ${e}`);
|
|
180
|
+
}
|
|
135
181
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
* console.log(JSON.stringify(timers))
|
|
145
|
-
*/
|
|
146
|
-
async function showTimers() {
|
|
147
|
-
try {
|
|
148
|
-
await createFile();
|
|
149
|
-
const timersRaw = fs_1.default.readFileSync(timerfiledir, "utf-8");
|
|
150
|
-
const timersData = timersRaw.split(/\r?\n/);
|
|
151
|
-
let timersJSON = [];
|
|
182
|
+
async checkTimerfileSyntax(fileData) {
|
|
183
|
+
const throwing = () => {
|
|
184
|
+
throw new Error(`Timer file's syntax is wrong`);
|
|
185
|
+
};
|
|
186
|
+
const timersData = fileData
|
|
187
|
+
.split('\n')
|
|
188
|
+
.map(l => l.trim())
|
|
189
|
+
.filter(l => l !== "");
|
|
152
190
|
for (const timerData of timersData) {
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
191
|
+
const timerArray = timerData.split(/\s+/);
|
|
192
|
+
if (timerArray.length !== 3)
|
|
193
|
+
throwing();
|
|
194
|
+
if (timerArray[0]?.length !== 36)
|
|
195
|
+
throwing();
|
|
196
|
+
if (timerArray[1].trim() === "")
|
|
197
|
+
throwing();
|
|
198
|
+
if (timerArray[2].trim() === "")
|
|
199
|
+
throwing();
|
|
159
200
|
}
|
|
160
|
-
return
|
|
161
|
-
}
|
|
162
|
-
catch (e) {
|
|
163
|
-
throw new Error(`Error when showing timers: ${e}`);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
async function checkTimerfileSyntax(fileData) {
|
|
167
|
-
const throwing = () => {
|
|
168
|
-
throw new Error(`Timer file's syntax is wrong`);
|
|
169
|
-
};
|
|
170
|
-
const timersData = fileData
|
|
171
|
-
.split('\n')
|
|
172
|
-
.map(l => l.trim())
|
|
173
|
-
.filter(l => l !== "");
|
|
174
|
-
for (const timerData of timersData) {
|
|
175
|
-
const timerArray = timerData.split(/\s+/);
|
|
176
|
-
if (timerArray.length !== 3)
|
|
177
|
-
throwing();
|
|
178
|
-
if (timerArray[0]?.length !== 36)
|
|
179
|
-
throwing();
|
|
180
|
-
if (timerArray[1].trim() === "")
|
|
181
|
-
throwing();
|
|
182
|
-
if (timerArray[2].trim() === "")
|
|
183
|
-
throwing();
|
|
201
|
+
return;
|
|
184
202
|
}
|
|
185
|
-
return;
|
|
186
203
|
}
|
|
204
|
+
exports.TimersManager = TimersManager;
|
|
187
205
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,48 +1,76 @@
|
|
|
1
1
|
export type Timer = {
|
|
2
2
|
id: string;
|
|
3
|
-
start:
|
|
4
|
-
stop:
|
|
3
|
+
start: number;
|
|
4
|
+
stop: number;
|
|
5
5
|
};
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
* @description
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* // newTimer will be id of the timer
|
|
7
|
+
* TimersManager
|
|
8
|
+
* @description
|
|
9
|
+
* Manages timers stored in a file.
|
|
10
|
+
* Each timer is stored as: `id start stop`.
|
|
11
|
+
*
|
|
12
|
+
* - Timers are persisted in a file
|
|
13
|
+
* - Expired timers are detected by polling
|
|
15
14
|
*/
|
|
16
|
-
export declare
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
15
|
+
export declare class TimersManager {
|
|
16
|
+
private readonly timerfiledir;
|
|
17
|
+
/**
|
|
18
|
+
* constructor
|
|
19
|
+
* @param timerfiledir(string, optional)
|
|
20
|
+
* If omitted, `.timers` under the project root is used.
|
|
21
|
+
*/
|
|
22
|
+
constructor(timerfiledir?: string);
|
|
23
|
+
/**
|
|
24
|
+
* createFile
|
|
25
|
+
* @description create `.timers` file
|
|
26
|
+
* @returns void
|
|
27
|
+
* @throws If file operation fails
|
|
28
|
+
*/
|
|
29
|
+
private createFile;
|
|
30
|
+
/**
|
|
31
|
+
* createTimer
|
|
32
|
+
* @description Creates a new timer.
|
|
33
|
+
* @param length Timer duration in milliseconds
|
|
34
|
+
* @returns Promise that resolves to the timer ID (UUID)
|
|
35
|
+
* @throws If length is invalid(e.g. length < 0) or file operation fails
|
|
36
|
+
* @example
|
|
37
|
+
* const manager = new TimersManager();
|
|
38
|
+
* const newTimer = await manager.createTimer(5000);
|
|
39
|
+
* // newTimer will be id of the timer
|
|
40
|
+
*/
|
|
41
|
+
createTimer(length: number): Promise<string>;
|
|
42
|
+
/**
|
|
43
|
+
* removeTimer
|
|
44
|
+
* @description Removes a timer by ID.
|
|
45
|
+
* @param id ID of the timer to remove
|
|
46
|
+
* @returns void
|
|
47
|
+
* @throws If file operation fails
|
|
48
|
+
* @example
|
|
49
|
+
* await manager.removeTimer(id);
|
|
50
|
+
*/
|
|
51
|
+
removeTimer(id: string): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* @description Starts monitoring expired timers asynchronously and returns immediately. The callback is invoked asynchronously when a timer expires.
|
|
54
|
+
* The callback is awaited before continuing.
|
|
55
|
+
* @param callback Function invoked when an expired timer is detected (called asynchronously)
|
|
56
|
+
* @param interval (number, optional): Check interval in milliseconds (default: 50ms)
|
|
57
|
+
* @throws If file operation fails
|
|
58
|
+
* @example
|
|
59
|
+
* manager.checkTimers((timer) => {
|
|
60
|
+
* console.log(`A timer was stopped: ${timer.id}`);
|
|
61
|
+
* });
|
|
62
|
+
*/
|
|
63
|
+
checkTimers(callback: (timer: Timer) => Promise<void>, interval?: number): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* showTimers
|
|
66
|
+
* @description Retrieves all active timers.
|
|
67
|
+
* @returns Array of `Timer` objects
|
|
68
|
+
* @throws If file operation fails
|
|
69
|
+
* @example
|
|
70
|
+
* const timers = await manager.showTimers();
|
|
71
|
+
* console.log(JSON.stringify(timers))
|
|
72
|
+
*/
|
|
73
|
+
showTimers(): Promise<Timer[]>;
|
|
74
|
+
private checkTimerfileSyntax;
|
|
75
|
+
}
|
|
48
76
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/cjs/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,KAAK,GAAG;IAChB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAA;CACf,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,KAAK,GAAG;IAChB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAA;CACf,CAAA;AAED;;;;;;;;GAQG;AACH,qBAAa,aAAa;IACzB,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAS;IAEtC;;;;OAIM;gBAEL,YAAY,CAAC,EAAE,MAAM;IAMtB;;;;;OAKM;YACQ,UAAU;IAQxB;;;;;;;;;;OAUM;IACO,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAqBzD;;;;;;;;OAQM;IACO,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA2BnD;;;;;;;;;;OAUM;IACO,WAAW,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,IAAI,CAAC,EAAE,QAAQ,GAAE,MAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAkCzG;;;;;;;;OAQM;IACO,UAAU,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;YAsB7B,oBAAoB;CAiBlC"}
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,gDAAwB;AACxB,oEAAyC;AACzC,+BAAoC;AAQpC;;;;;;;;GAQG;AACH,MAAa,aAAa;IACR,YAAY,CAAS;IAEtC;;;;OAIM;IACN,YACC,YAAqB;QAErB,IAAI,CAAC,YAAY;YACP,YAAY,IAAI,cAAI,CAAC,IAAI,CAAC,IAAA,uBAAU,GAAE,EAAE,SAAS,CAAC,CAAC;IAC9D,CAAC;IAED;;;;;OAKM;IACE,KAAK,CAAC,UAAU;QACvB,IAAI,CAAC;YACJ,MAAM,YAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;QAC7C,CAAC;QAAC,MAAM,CAAC;YACR,MAAM,YAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;QACpD,CAAC;IACF,CAAC;IAED;;;;;;;;;;OAUM;IACC,KAAK,CAAC,WAAW,CAAC,MAAc;QACtC,IAAI,CAAC;YACJ,MAAM,IAAI,CAAC,UAAU,EAAE,CAAC;YAExB,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;gBAChB,MAAM,IAAI,KAAK,CAAC,oBAAoB,MAAM,EAAE,CAAC,CAAC;YAC/C,CAAC;YAED,MA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AAE,CAAC,CAAC;QACpD,CAAC;IACF,CAAC;IAEO,KAAK,CAAC,oBAAoB,CAAC,QAAgB;QAClD,MAAM,QAAQ,GAAG,GAAG,EAAE;YACrB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;QACjD,CAAC,CAAC;QACF,MAAM,UAAU,GAAG,QAAQ;aACzB,KAAK,CAAC,IAAI,CAAC;aACX,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;aAClB,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC;QACxB,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;YACpC,MAAM,UAAU,GAAa,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACpD,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC;gBAAE,QAAQ,EAAE,CAAC;YACxC,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,MAAM,KAAK,EAAE;gBAAE,QAAQ,EAAE,CAAC;YAC7C,IAAI,UAAU,CAAC,CAAC,CAAE,CAAC,IAAI,EAAE,KAAK,EAAE;gBAAE,QAAQ,EAAE,CAAC;YAC7C,IAAI,UAAU,CAAC,CAAC,CAAE,CAAC,IAAI,EAAE,KAAK,EAAE;gBAAE,QAAQ,EAAE,CAAC;QAC9C,CAAC;QACD,OAAO;IACR,CAAC;CACD;AA9LD,sCA8LC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"searchRoot.js","sourceRoot":"","sources":["../../src/searchRoot.ts"],"names":[],"mappings":";;;;;AAQA,6BAQC;AAhBD,4CAAoB;AACpB,gDAAwB;AAExB;;;;GAIG;AACH,SAAwB,UAAU;
|
|
1
|
+
{"version":3,"file":"searchRoot.js","sourceRoot":"","sources":["../../src/searchRoot.ts"],"names":[],"mappings":";;;;;AAQA,6BAQC;AAhBD,4CAAoB;AACpB,gDAAwB;AAExB;;;;GAIG;AACH,SAAwB,UAAU;IACjC,IAAI,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;IACxB,OAAO,CAAC,YAAE,CAAC,UAAU,CAAC,cAAI,CAAC,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,CAAC,EAAE,CAAC;QACvD,MAAM,MAAM,GAAG,cAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACjC,IAAI,MAAM,KAAK,GAAG;YAAE,MAAM;QAC1B,GAAG,GAAG,MAAM,CAAC;IACd,CAAC;IACD,OAAO,GAAG,CAAC;AACZ,CAAC"}
|