eternal-timer 2.3.0 → 2.5.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 +45 -13
- package/dist/cjs/TimersManager/JSONLTimersManager.cjs +59 -0
- package/dist/cjs/TimersManager/JSONLTimersManager.cjs.map +1 -0
- package/dist/cjs/TimersManager/JSONLTimersManager.d.ts +19 -0
- package/dist/cjs/TimersManager/JSONLTimersManager.d.ts.map +1 -0
- package/dist/cjs/TimersManager/PlainTextTimersManager.cjs +25 -0
- package/dist/cjs/TimersManager/PlainTextTimersManager.cjs.map +1 -0
- package/dist/cjs/TimersManager/PlainTextTimersManager.d.ts +17 -0
- package/dist/cjs/TimersManager/PlainTextTimersManager.d.ts.map +1 -0
- package/dist/cjs/TimersManager/TimersManager.cjs +211 -0
- package/dist/cjs/TimersManager/TimersManager.cjs.map +1 -0
- package/dist/cjs/TimersManager/TimersManager.d.ts +87 -0
- package/dist/cjs/TimersManager/TimersManager.d.ts.map +1 -0
- package/dist/cjs/TimersStore/JSONLTimersStore.cjs +50 -0
- package/dist/cjs/TimersStore/JSONLTimersStore.cjs.map +1 -0
- package/dist/cjs/TimersStore/JSONLTimersStore.d.ts +16 -0
- package/dist/cjs/TimersStore/JSONLTimersStore.d.ts.map +1 -0
- package/dist/cjs/TimersStore/PlainTextTimersStore.cjs +56 -0
- package/dist/cjs/TimersStore/PlainTextTimersStore.cjs.map +1 -0
- package/dist/cjs/TimersStore/PlainTextTimersStore.d.ts +16 -0
- package/dist/cjs/TimersStore/PlainTextTimersStore.d.ts.map +1 -0
- package/dist/cjs/TimersStore/TimersStore.cjs +44 -0
- package/dist/cjs/TimersStore/TimersStore.cjs.map +1 -0
- package/dist/cjs/TimersStore/TimersStore.d.ts +12 -0
- package/dist/cjs/TimersStore/TimersStore.d.ts.map +1 -0
- package/dist/cjs/index.cjs +2 -2
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/types.d.ts +6 -3
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/esm/TimersManager/JSONLTimersManager.d.ts +19 -0
- package/dist/esm/TimersManager/JSONLTimersManager.d.ts.map +1 -0
- package/dist/esm/TimersManager/JSONLTimersManager.js +55 -0
- package/dist/esm/TimersManager/JSONLTimersManager.js.map +1 -0
- package/dist/esm/TimersManager/PlainTextTimersManager.d.ts +17 -0
- package/dist/esm/TimersManager/PlainTextTimersManager.d.ts.map +1 -0
- package/dist/esm/TimersManager/PlainTextTimersManager.js +21 -0
- package/dist/esm/TimersManager/PlainTextTimersManager.js.map +1 -0
- package/dist/esm/TimersManager/TimersManager.d.ts +87 -0
- package/dist/esm/TimersManager/TimersManager.d.ts.map +1 -0
- package/dist/esm/TimersManager/TimersManager.js +205 -0
- package/dist/esm/TimersManager/TimersManager.js.map +1 -0
- package/dist/esm/TimersStore/JSONLTimersStore.d.ts +16 -0
- package/dist/esm/TimersStore/JSONLTimersStore.d.ts.map +1 -0
- package/dist/esm/TimersStore/JSONLTimersStore.js +46 -0
- package/dist/esm/TimersStore/JSONLTimersStore.js.map +1 -0
- package/dist/esm/TimersStore/PlainTextTimersStore.d.ts +16 -0
- package/dist/esm/TimersStore/PlainTextTimersStore.d.ts.map +1 -0
- package/dist/esm/TimersStore/PlainTextTimersStore.js +52 -0
- package/dist/esm/TimersStore/PlainTextTimersStore.js.map +1 -0
- package/dist/esm/TimersStore/TimersStore.d.ts +12 -0
- package/dist/esm/TimersStore/TimersStore.d.ts.map +1 -0
- package/dist/esm/TimersStore/TimersStore.js +37 -0
- package/dist/esm/TimersStore/TimersStore.js.map +1 -0
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types.d.ts +6 -3
- package/dist/esm/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/cjs/JSONLTimersManager.cjs +0 -199
- package/dist/cjs/JSONLTimersManager.cjs.map +0 -1
- package/dist/cjs/JSONLTimersManager.d.ts +0 -63
- package/dist/cjs/JSONLTimersManager.d.ts.map +0 -1
- package/dist/cjs/PlainTextTimersManager.cjs +0 -200
- package/dist/cjs/PlainTextTimersManager.cjs.map +0 -1
- package/dist/cjs/PlainTextTimersManager.d.ts +0 -60
- package/dist/cjs/PlainTextTimersManager.d.ts.map +0 -1
- package/dist/cjs/TimersManager.cjs +0 -39
- package/dist/cjs/TimersManager.cjs.map +0 -1
- package/dist/cjs/TimersManager.d.ts +0 -75
- package/dist/cjs/TimersManager.d.ts.map +0 -1
- package/dist/esm/JSONLTimersManager.d.ts +0 -63
- package/dist/esm/JSONLTimersManager.d.ts.map +0 -1
- package/dist/esm/JSONLTimersManager.js +0 -192
- package/dist/esm/JSONLTimersManager.js.map +0 -1
- package/dist/esm/PlainTextTimersManager.d.ts +0 -60
- package/dist/esm/PlainTextTimersManager.d.ts.map +0 -1
- package/dist/esm/PlainTextTimersManager.js +0 -193
- package/dist/esm/PlainTextTimersManager.js.map +0 -1
- package/dist/esm/TimersManager.d.ts +0 -75
- package/dist/esm/TimersManager.d.ts.map +0 -1
- package/dist/esm/TimersManager.js +0 -32
- package/dist/esm/TimersManager.js.map +0 -1
package/README.md
CHANGED
|
@@ -37,10 +37,8 @@ async function main() {
|
|
|
37
37
|
console.log('Timer created:', timerId);
|
|
38
38
|
|
|
39
39
|
// Monitor timers (executes when timer expires)
|
|
40
|
-
const interval = manager.checkTimers(async (timer) => {
|
|
40
|
+
const interval = await manager.checkTimers(async (timer) => {
|
|
41
41
|
console.log('Timer expired:', timer.id, timer.title);
|
|
42
|
-
// Once the timer expires, you can remove it
|
|
43
|
-
await manager.removeTimer(timer.id);
|
|
44
42
|
});
|
|
45
43
|
|
|
46
44
|
// Display all timers
|
|
@@ -73,9 +71,8 @@ async function main() {
|
|
|
73
71
|
console.log('Timer created:', timerId);
|
|
74
72
|
|
|
75
73
|
// Monitor timers
|
|
76
|
-
const interval = manager.checkTimers(async (timer) => {
|
|
74
|
+
const interval = await manager.checkTimers(async (timer) => {
|
|
77
75
|
console.log('Timer expired:', timer.id);
|
|
78
|
-
await manager.removeTimer(timer.id);
|
|
79
76
|
});
|
|
80
77
|
|
|
81
78
|
// Display all timers
|
|
@@ -101,6 +98,26 @@ Creates a manager for timers stored in **JSON Lines** format.
|
|
|
101
98
|
|
|
102
99
|
- **`timerfiledir`** (optional, string): Path to the timer file. Defaults to `.timers.jsonl` in the project root.
|
|
103
100
|
|
|
101
|
+
#### `changeTitle(id: string, newTitle: string): Promise<void>`
|
|
102
|
+
Changes the title of an existing timer.
|
|
103
|
+
|
|
104
|
+
- **`id`**: The ID of the timer to modify.
|
|
105
|
+
- **`newTitle`**: The new title for the timer.
|
|
106
|
+
|
|
107
|
+
**Returns:** A `Promise<void>` that resolves when the timer's title has been updated.
|
|
108
|
+
|
|
109
|
+
**Throws:** An error if: the timer with the specified ID is not found, or a file operation fails.
|
|
110
|
+
|
|
111
|
+
#### `changeDescription(id: string, newDescription: string): Promise<void>`
|
|
112
|
+
Changes the description of an existing timer.
|
|
113
|
+
|
|
114
|
+
- **`id`**: The ID of the timer to modify.
|
|
115
|
+
- **`newDescription`**: The new description for the timer.
|
|
116
|
+
|
|
117
|
+
**Returns:** A `Promise<void>` that resolves when the timer's description has been updated.
|
|
118
|
+
|
|
119
|
+
**Throws:** An error if: the timer with the specified ID is not found, or a file operation fails.
|
|
120
|
+
|
|
104
121
|
### `PlainTextTimersManager`
|
|
105
122
|
|
|
106
123
|
#### `constructor(timerfiledir?: string)`
|
|
@@ -144,18 +161,22 @@ Removes a timer by its ID.
|
|
|
144
161
|
|
|
145
162
|
**Throws:** An error if the timer with the specified ID is not found or if a file operation fails.
|
|
146
163
|
|
|
147
|
-
### `checkTimers(callback: (timer: Timer) => Promise<void>, interval?: number): NodeJS.Timeout
|
|
164
|
+
### `checkTimers(callback: (timer: Timer) => void | Promise<void>, interval?: number): Promise<NodeJS.Timeout>`
|
|
148
165
|
|
|
149
|
-
Starts monitoring
|
|
166
|
+
Starts monitoring timers at the specified interval and invokes the provided
|
|
167
|
+
`callback` when a timer expires.
|
|
150
168
|
|
|
151
|
-
|
|
169
|
+
For each expired timer, the `callback` is awaited before processing continues.
|
|
170
|
+
Monitoring runs periodically in the background and can be stopped using the
|
|
171
|
+
returned `NodeJS.Timeout`.
|
|
152
172
|
|
|
153
|
-
-
|
|
154
|
-
-
|
|
173
|
+
- **callback**: An async function invoked with the expired `timer`.
|
|
174
|
+
- **interval** *(optional, number)*: Interval in milliseconds for checking timers.
|
|
175
|
+
Defaults to `200`.
|
|
155
176
|
|
|
156
|
-
**Returns:** A `NodeJS.Timeout`
|
|
177
|
+
**Returns:** A `NodeJS.Timeout` that can be passed to `clearInterval()` to stop monitoring.
|
|
157
178
|
|
|
158
|
-
**Throws:** An error if a
|
|
179
|
+
**Throws:** An error if a timer storage operation fails.
|
|
159
180
|
|
|
160
181
|
### `showTimers(): Promise<Timer[]>`
|
|
161
182
|
|
|
@@ -165,6 +186,17 @@ Retrieves all active timers.
|
|
|
165
186
|
|
|
166
187
|
**Throws:** An error if a file operation fails.
|
|
167
188
|
|
|
189
|
+
### `adjustRemainingTime(id: string, delay: number): Promise<void>`
|
|
190
|
+
|
|
191
|
+
Adjusts the remaining time of a specified timer. This can be used to extend or shorten a timer's duration.
|
|
192
|
+
|
|
193
|
+
- **`id`**: The ID of the timer to modify.
|
|
194
|
+
- **`delay`**: The amount of time in milliseconds to add to (if positive) or subtract from (if negative) the timer's remaining duration.
|
|
195
|
+
|
|
196
|
+
**Returns:** A `Promise<void>` that resolves when the timer's remaining time has been adjusted.
|
|
197
|
+
|
|
198
|
+
**Throws:** An error if: the timer with the specified ID is not found, the resulting remaining time would be negative, or a file operation fails.
|
|
199
|
+
|
|
168
200
|
## Type Definition
|
|
169
201
|
|
|
170
202
|
The `StorageType` has the following structure:
|
|
@@ -181,7 +213,7 @@ type Timer<T extends StorageType> = {
|
|
|
181
213
|
stop: number;
|
|
182
214
|
} & (T extends "JSONL"
|
|
183
215
|
? { title?: string; description?: string }
|
|
184
|
-
:
|
|
216
|
+
: {});
|
|
185
217
|
```
|
|
186
218
|
|
|
187
219
|
## Scripts
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JSONLTimersManager = void 0;
|
|
4
|
+
const TimersManager_js_1 = require("./TimersManager.cjs");
|
|
5
|
+
const JSONLTimersStore_js_1 = require("../TimersStore/JSONLTimersStore.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* JSONLTimersManager
|
|
8
|
+
* @description
|
|
9
|
+
* Manages timers stored in a JSONL file.
|
|
10
|
+
* (This is a abstract class)
|
|
11
|
+
*
|
|
12
|
+
* - Timers are persisted in a file
|
|
13
|
+
* - Expired timers are detected by polling
|
|
14
|
+
*/
|
|
15
|
+
class JSONLTimersManager extends TimersManager_js_1.TimersManager {
|
|
16
|
+
TimersStore = null;
|
|
17
|
+
getDefaultFilename() {
|
|
18
|
+
return ".timers.jsonl";
|
|
19
|
+
}
|
|
20
|
+
async createTimersStore() {
|
|
21
|
+
return new JSONLTimersStore_js_1.JSONLTimersStore(this.timerfiledir);
|
|
22
|
+
}
|
|
23
|
+
async changeTitle(id, newTitle) {
|
|
24
|
+
return this.runExclusive(async () => {
|
|
25
|
+
this.TimersStore ??= await this.createTimersStore();
|
|
26
|
+
try {
|
|
27
|
+
const timers = await this.TimersStore.loadTimers();
|
|
28
|
+
const index = timers?.findIndex(t => t.id === id);
|
|
29
|
+
if (index === -1) {
|
|
30
|
+
throw new Error(`Timer with id ${id} not found`);
|
|
31
|
+
}
|
|
32
|
+
timers[index].title = newTitle;
|
|
33
|
+
await this.TimersStore.saveTimers(timers);
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
throw new Error(`Error when changing title`, { cause: e });
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
async changeDescription(id, newDescription) {
|
|
41
|
+
return this.runExclusive(async () => {
|
|
42
|
+
this.TimersStore ??= await this.createTimersStore();
|
|
43
|
+
try {
|
|
44
|
+
const timers = await this.TimersStore.loadTimers();
|
|
45
|
+
const index = timers?.findIndex(t => t.id === id);
|
|
46
|
+
if (index === -1) {
|
|
47
|
+
throw new Error(`Timer with id ${id} not found`);
|
|
48
|
+
}
|
|
49
|
+
timers[index].description = newDescription;
|
|
50
|
+
await this.TimersStore.saveTimers(timers);
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
throw new Error(`Error when changing description`, { cause: e });
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.JSONLTimersManager = JSONLTimersManager;
|
|
59
|
+
//# sourceMappingURL=JSONLTimersManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JSONLTimersManager.js","sourceRoot":"","sources":["../../../src/TimersManager/JSONLTimersManager.ts"],"names":[],"mappings":";;;AAAA,yDAAmD;AACnD,4EAAsE;AAEtE;;;;;;;;GAQG;AACH,MAAa,kBAAmB,SAAQ,gCAAsB;IAC1C,WAAW,GAA4B,IAAI,CAAC;IAE5C,kBAAkB;QACpC,OAAO,eAAe,CAAC;IACxB,CAAC;IAEkB,KAAK,CAAC,iBAAiB;QACzC,OAAO,IAAI,sCAAgB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAChD,CAAC;IAEM,KAAK,CAAC,WAAW,CAAC,EAAU,EAAE,QAAgB;QACpD,OAAO,IAAI,CAAC,YAAY,CAAC,KAAK,IAAI,EAAE;YACnC,IAAI,CAAC,WAAW,KAAK,MAAM,IAAI,CAAC,iBAAiB,EAAE,CAAC;YACpD,IAAI,CAAC;gBACJ,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,CAAC;gBAEn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,MAAM,IAAI,KAAK,CAAC,iCAAiC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;YAClE,CAAC;QACF,CAAC,CAAC,CAAC;IACJ,CAAC;CACD;AAhDD,gDAgDC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { TimersManager } from "./TimersManager.js";
|
|
2
|
+
import { JSONLTimersStore } from "../TimersStore/JSONLTimersStore.js";
|
|
3
|
+
/**
|
|
4
|
+
* JSONLTimersManager
|
|
5
|
+
* @description
|
|
6
|
+
* Manages timers stored in a JSONL file.
|
|
7
|
+
* (This is a abstract class)
|
|
8
|
+
*
|
|
9
|
+
* - Timers are persisted in a file
|
|
10
|
+
* - Expired timers are detected by polling
|
|
11
|
+
*/
|
|
12
|
+
export declare class JSONLTimersManager extends TimersManager<"JSONL"> {
|
|
13
|
+
protected TimersStore: JSONLTimersStore | null;
|
|
14
|
+
protected getDefaultFilename(): string;
|
|
15
|
+
protected createTimersStore(): Promise<JSONLTimersStore>;
|
|
16
|
+
changeTitle(id: string, newTitle: string): Promise<void>;
|
|
17
|
+
changeDescription(id: string, newDescription: string): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=JSONLTimersManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JSONLTimersManager.d.ts","sourceRoot":"","sources":["../../../src/TimersManager/JSONLTimersManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AAEtE;;;;;;;;GAQG;AACH,qBAAa,kBAAmB,SAAQ,aAAa,CAAC,OAAO,CAAC;IAC7D,UAAmB,WAAW,EAAE,gBAAgB,GAAG,IAAI,CAAQ;cAE5C,kBAAkB,IAAI,MAAM;cAItB,iBAAiB,IAAI,OAAO,CAAC,gBAAgB,CAAC;IAI1D,WAAW,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAmBxD,iBAAiB,CAAC,EAAE,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAkBjF"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PlainTextTimersManager = void 0;
|
|
4
|
+
const TimersManager_js_1 = require("./TimersManager.cjs");
|
|
5
|
+
const PlainTextTimersStore_js_1 = require("../TimersStore/PlainTextTimersStore.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* PlainTextTimersManager
|
|
8
|
+
* @description
|
|
9
|
+
* Manages timers stored in a PlainText file.
|
|
10
|
+
* (This is a abstract class)
|
|
11
|
+
*
|
|
12
|
+
* - Timers are persisted in a file
|
|
13
|
+
* - Expired timers are detected by polling
|
|
14
|
+
*/
|
|
15
|
+
class PlainTextTimersManager extends TimersManager_js_1.TimersManager {
|
|
16
|
+
TimersStore = null;
|
|
17
|
+
getDefaultFilename() {
|
|
18
|
+
return ".timers";
|
|
19
|
+
}
|
|
20
|
+
async createTimersStore() {
|
|
21
|
+
return new PlainTextTimersStore_js_1.PlainTextTimersStore(this.timerfiledir);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.PlainTextTimersManager = PlainTextTimersManager;
|
|
25
|
+
//# sourceMappingURL=PlainTextTimersManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlainTextTimersManager.js","sourceRoot":"","sources":["../../../src/TimersManager/PlainTextTimersManager.ts"],"names":[],"mappings":";;;AAAA,yDAAmD;AACnD,oFAA8E;AAE9E;;;;;;;;GAQG;AACH,MAAa,sBAAuB,SAAQ,gCAA0B;IAClD,WAAW,GAAgC,IAAI,CAAC;IAEhD,kBAAkB;QACpC,OAAO,SAAS,CAAC;IAClB,CAAC;IAES,KAAK,CAAC,iBAAiB;QAChC,OAAO,IAAI,8CAAoB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACpD,CAAC;CACD;AAVD,wDAUC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { TimersManager } from "./TimersManager.js";
|
|
2
|
+
import { PlainTextTimersStore } from "../TimersStore/PlainTextTimersStore.js";
|
|
3
|
+
/**
|
|
4
|
+
* PlainTextTimersManager
|
|
5
|
+
* @description
|
|
6
|
+
* Manages timers stored in a PlainText file.
|
|
7
|
+
* (This is a abstract class)
|
|
8
|
+
*
|
|
9
|
+
* - Timers are persisted in a file
|
|
10
|
+
* - Expired timers are detected by polling
|
|
11
|
+
*/
|
|
12
|
+
export declare class PlainTextTimersManager extends TimersManager<"PlainText"> {
|
|
13
|
+
protected TimersStore: PlainTextTimersStore | null;
|
|
14
|
+
protected getDefaultFilename(): string;
|
|
15
|
+
protected createTimersStore(): Promise<PlainTextTimersStore>;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=PlainTextTimersManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlainTextTimersManager.d.ts","sourceRoot":"","sources":["../../../src/TimersManager/PlainTextTimersManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wCAAwC,CAAC;AAE9E;;;;;;;;GAQG;AACH,qBAAa,sBAAuB,SAAQ,aAAa,CAAC,WAAW,CAAC;IACrE,UAAmB,WAAW,EAAE,oBAAoB,GAAG,IAAI,CAAQ;cAEhD,kBAAkB,IAAI,MAAM;cAI/B,iBAAiB,IAAI,OAAO,CAAC,oBAAoB,CAAC;CAGlE"}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.TimersManager = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const uuid_1 = require("uuid");
|
|
10
|
+
const searchRoot_js_1 = __importDefault(require("../searchRoot.cjs"));
|
|
11
|
+
const Log_js_1 = require("../Log.cjs");
|
|
12
|
+
/**
|
|
13
|
+
* TimersManager
|
|
14
|
+
* @description
|
|
15
|
+
* Manages timers stored in a file.
|
|
16
|
+
* (This is a abstract class)
|
|
17
|
+
*
|
|
18
|
+
* - Timers are persisted in a file
|
|
19
|
+
* - Expired timers are detected by polling
|
|
20
|
+
*/
|
|
21
|
+
class TimersManager {
|
|
22
|
+
timerfiledir;
|
|
23
|
+
checkLock = false;
|
|
24
|
+
TimersStore = null;
|
|
25
|
+
queue = Promise.resolve();
|
|
26
|
+
runExclusive(fn) {
|
|
27
|
+
const p = this.queue.then(fn);
|
|
28
|
+
this.queue = p.then(() => { }, () => { });
|
|
29
|
+
return p;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* constructor
|
|
33
|
+
* @description Initializes the TimersManager instance. If the timer file does not exist, an empty file is created.
|
|
34
|
+
* @param options (TimersManagerOptions | string, optional) Configuration object or timer file path. If a string is provided, it is treated as the timer file path. If an object is provided, `timerfiledir` can be specified.
|
|
35
|
+
* @throws If file access or creation fails
|
|
36
|
+
* @example
|
|
37
|
+
* const manager = new TimersManager(); // Uses default timer file path
|
|
38
|
+
* const manager = new TimersManager("/path/to/timers.txt"); // Uses specified timer file path
|
|
39
|
+
*/
|
|
40
|
+
constructor(options) {
|
|
41
|
+
const rootDir = (0, searchRoot_js_1.default)();
|
|
42
|
+
this.timerfiledir = path_1.default.resolve(rootDir, options ?? this.getDefaultFilename());
|
|
43
|
+
if (!this.timerfiledir.startsWith(rootDir)) {
|
|
44
|
+
throw new Error(`Timer file path must be within the project directory`);
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
fs_1.default.accessSync(this.timerfiledir);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
fs_1.default.writeFileSync(this.timerfiledir, "");
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* createTimer
|
|
55
|
+
* @description Creates a new timer.
|
|
56
|
+
* @param length Timer duration in milliseconds
|
|
57
|
+
* @returns Promise that resolves to the timer ID (UUID)
|
|
58
|
+
* @throws If length is invalid(e.g. length < 0) or file operation fails
|
|
59
|
+
* @example
|
|
60
|
+
* const manager = new TimersManager();
|
|
61
|
+
* const newTimer = await manager.createTimer(5000);
|
|
62
|
+
* // newTimer will be id of the timer
|
|
63
|
+
*/
|
|
64
|
+
async createTimer(options) {
|
|
65
|
+
return this.runExclusive(async () => {
|
|
66
|
+
this.TimersStore ??= await this.createTimersStore();
|
|
67
|
+
let length = typeof options === "object" ? options.length : options;
|
|
68
|
+
if (length < 0)
|
|
69
|
+
throw new Error(`Invalid length: ${length}`);
|
|
70
|
+
length = Math.trunc(length);
|
|
71
|
+
const id = (0, uuid_1.v4)();
|
|
72
|
+
const now = Date.now();
|
|
73
|
+
const stopTime = now + Math.max(1, length);
|
|
74
|
+
const newTimerData = {
|
|
75
|
+
id,
|
|
76
|
+
start: now,
|
|
77
|
+
stop: stopTime,
|
|
78
|
+
...(typeof options === "object" && options.title !== undefined && { title: options.title }),
|
|
79
|
+
...(typeof options === "object" && options.description !== undefined && { description: options.description }),
|
|
80
|
+
};
|
|
81
|
+
await this.TimersStore.appendTimer(newTimerData);
|
|
82
|
+
return id;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* removeTimer
|
|
87
|
+
* @description Removes a timer by ID.
|
|
88
|
+
* @param id ID of the timer to remove
|
|
89
|
+
* @returns void
|
|
90
|
+
* @throws If file operation fails
|
|
91
|
+
* @example
|
|
92
|
+
* await manager.removeTimer(id);
|
|
93
|
+
*/
|
|
94
|
+
async removeTimer(id) {
|
|
95
|
+
return this.runExclusive(async () => {
|
|
96
|
+
this.TimersStore ??= await this.createTimersStore();
|
|
97
|
+
const timers = await this.TimersStore.loadTimers();
|
|
98
|
+
const index = timers.findIndex(t => t.id === id);
|
|
99
|
+
if (index === -1) {
|
|
100
|
+
throw new Error(`Timer with id ${id} not found`);
|
|
101
|
+
}
|
|
102
|
+
timers.splice(index, 1);
|
|
103
|
+
await this.TimersStore.saveTimers(timers);
|
|
104
|
+
return;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* checkTimers
|
|
109
|
+
* @description Starts monitoring timers at the specified interval.
|
|
110
|
+
* When a timer expires, the provided `callback` is invoked with the timer.
|
|
111
|
+
* The callback is awaited before the next processing cycle continues.
|
|
112
|
+
* @param callback Function invoked when an expired timer is detected (called asynchronously)
|
|
113
|
+
* @param interval (number, optional): Check interval in milliseconds (default: 200ms)
|
|
114
|
+
* @throws If file operation fails
|
|
115
|
+
* @returns (Promise<NodeJS.Timeout>) intervalId interval id of checkTimers
|
|
116
|
+
* @example
|
|
117
|
+
* const interval = await manager.checkTimers((timer) => {
|
|
118
|
+
* console.log(`A timer was stopped: ${timer.id}`);
|
|
119
|
+
* });
|
|
120
|
+
*/
|
|
121
|
+
async checkTimers(callback, interval = 200) {
|
|
122
|
+
this.TimersStore ??= await this.createTimersStore();
|
|
123
|
+
const loop = async () => {
|
|
124
|
+
if (this.checkLock) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
this.checkLock = true;
|
|
128
|
+
try {
|
|
129
|
+
const expiredTimers = await this.runExclusive(async () => {
|
|
130
|
+
const allTimers = await this.TimersStore.loadTimers();
|
|
131
|
+
const now = Date.now();
|
|
132
|
+
const expired = [];
|
|
133
|
+
const active = [];
|
|
134
|
+
for (const timer of allTimers) {
|
|
135
|
+
if (timer.stop <= now) {
|
|
136
|
+
expired.push(timer);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
active.push(timer);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (expired.length > 0) {
|
|
143
|
+
await this.TimersStore.saveTimers(active);
|
|
144
|
+
}
|
|
145
|
+
return expired;
|
|
146
|
+
});
|
|
147
|
+
for (const timerData of expiredTimers) {
|
|
148
|
+
try {
|
|
149
|
+
await callback(timerData);
|
|
150
|
+
}
|
|
151
|
+
catch (e) {
|
|
152
|
+
await Log_js_1.Log.ensureLogger();
|
|
153
|
+
Log_js_1.Log.loggerInstance?.error(`Error in callback of checkTimers: ${e}`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
catch (e) {
|
|
158
|
+
await Log_js_1.Log.ensureLogger();
|
|
159
|
+
Log_js_1.Log.loggerInstance?.error(`Error when checking timer: ${e}`);
|
|
160
|
+
}
|
|
161
|
+
finally {
|
|
162
|
+
this.checkLock = false;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
return setInterval(loop, interval);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* showTimers
|
|
169
|
+
* @description Retrieves all active timers.
|
|
170
|
+
* @returns Array of `Timer` objects
|
|
171
|
+
* @throws If file operation fails
|
|
172
|
+
* @example
|
|
173
|
+
* const timers = await manager.showTimers();
|
|
174
|
+
* console.log(JSON.stringify(timers))
|
|
175
|
+
*/
|
|
176
|
+
async showTimers() {
|
|
177
|
+
return this.runExclusive(async () => {
|
|
178
|
+
this.TimersStore ??= await this.createTimersStore();
|
|
179
|
+
const timersData = await this.TimersStore.loadTimers();
|
|
180
|
+
return timersData;
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* adjustRemainingTime
|
|
185
|
+
* @description Adjusts the remaining time of a timer.
|
|
186
|
+
* @param id ID of the timer to modify
|
|
187
|
+
* @param delay Delay in milliseconds to add/subtract from the remaining time
|
|
188
|
+
* @returns Promise resolving when the operation is complete
|
|
189
|
+
* @throws If file operation fails
|
|
190
|
+
*/
|
|
191
|
+
async adjustRemainingTime(id, delay) {
|
|
192
|
+
return this.runExclusive(async () => {
|
|
193
|
+
this.TimersStore ??= await this.createTimersStore();
|
|
194
|
+
const timers = await this.TimersStore.loadTimers();
|
|
195
|
+
const index = timers.findIndex(t => t.id === id);
|
|
196
|
+
if (index === -1) {
|
|
197
|
+
throw new Error(`Timer with id ${id} not found`);
|
|
198
|
+
}
|
|
199
|
+
const now = Date.now();
|
|
200
|
+
const timer = timers[index];
|
|
201
|
+
const remaining = Math.max(0, timer.stop - now);
|
|
202
|
+
const newRemaining = Math.max(0, remaining + delay);
|
|
203
|
+
timer.stop = now + newRemaining;
|
|
204
|
+
timers[index] = timer;
|
|
205
|
+
await this.TimersStore.saveTimers(timers);
|
|
206
|
+
return;
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
exports.TimersManager = TimersManager;
|
|
211
|
+
//# sourceMappingURL=TimersManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TimersManager.js","sourceRoot":"","sources":["../../../src/TimersManager/TimersManager.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAwB;AACxB,4CAAoB;AACpB,+BAAoC;AAEpC,qEAA0C;AAG1C,sCAAgC;AAEhC;;;;;;;;GAQG;AACH,MAAsB,aAAa;IACf,YAAY,CAAS;IAChC,SAAS,GAAY,KAAK,CAAC;IAEzB,WAAW,GAA0B,IAAI,CAAC;IAE5C,KAAK,GAAkB,OAAO,CAAC,OAAO,EAAE,CAAC;IACvC,YAAY,CAAI,EAAoB;QAC7C,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC9B,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,GAAE,CAAC,EAAE,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QACxC,OAAO,CAAC,CAAC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K,CAAE,CAAC;YAC7B,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,IAAI,GAAG,GAAG,CAAC,CAAC;YAChD,MAAM,YAAY,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,SAAS,GAAG,KAAK,CAAC,CAAC;YAEpD,KAAK,CAAC,IAAI,GAAG,GAAG,GAAG,YAAY,CAAC;YAChC,MAAM,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;YACtB,MAAM,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;YAC1C,OAAO;QACR,CAAC,CAAC,CAAC;IACH,CAAC;CACF;AA1ND,sCA0NC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { CreateTimerOptions, StorageType, Timer } from "../types.js";
|
|
2
|
+
import { TimersStore } from "../TimersStore/TimersStore.js";
|
|
3
|
+
/**
|
|
4
|
+
* TimersManager
|
|
5
|
+
* @description
|
|
6
|
+
* Manages timers stored in a file.
|
|
7
|
+
* (This is a abstract class)
|
|
8
|
+
*
|
|
9
|
+
* - Timers are persisted in a file
|
|
10
|
+
* - Expired timers are detected by polling
|
|
11
|
+
*/
|
|
12
|
+
export declare abstract class TimersManager<T extends StorageType> {
|
|
13
|
+
protected readonly timerfiledir: string;
|
|
14
|
+
private checkLock;
|
|
15
|
+
protected TimersStore: TimersStore<T> | null;
|
|
16
|
+
private queue;
|
|
17
|
+
protected runExclusive<T>(fn: () => Promise<T>): Promise<T>;
|
|
18
|
+
protected abstract getDefaultFilename(): string;
|
|
19
|
+
protected abstract createTimersStore(): Promise<TimersStore<T>>;
|
|
20
|
+
/**
|
|
21
|
+
* constructor
|
|
22
|
+
* @description Initializes the TimersManager instance. If the timer file does not exist, an empty file is created.
|
|
23
|
+
* @param options (TimersManagerOptions | string, optional) Configuration object or timer file path. If a string is provided, it is treated as the timer file path. If an object is provided, `timerfiledir` can be specified.
|
|
24
|
+
* @throws If file access or creation fails
|
|
25
|
+
* @example
|
|
26
|
+
* const manager = new TimersManager(); // Uses default timer file path
|
|
27
|
+
* const manager = new TimersManager("/path/to/timers.txt"); // Uses specified timer file path
|
|
28
|
+
*/
|
|
29
|
+
constructor(options?: string);
|
|
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(options: CreateTimerOptions<T>): 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
|
+
* checkTimers
|
|
54
|
+
* @description Starts monitoring timers at the specified interval.
|
|
55
|
+
* When a timer expires, the provided `callback` is invoked with the timer.
|
|
56
|
+
* The callback is awaited before the next processing cycle continues.
|
|
57
|
+
* @param callback Function invoked when an expired timer is detected (called asynchronously)
|
|
58
|
+
* @param interval (number, optional): Check interval in milliseconds (default: 200ms)
|
|
59
|
+
* @throws If file operation fails
|
|
60
|
+
* @returns (Promise<NodeJS.Timeout>) intervalId interval id of checkTimers
|
|
61
|
+
* @example
|
|
62
|
+
* const interval = await manager.checkTimers((timer) => {
|
|
63
|
+
* console.log(`A timer was stopped: ${timer.id}`);
|
|
64
|
+
* });
|
|
65
|
+
*/
|
|
66
|
+
checkTimers(callback: (timer: Timer<T>) => void | Promise<void>, interval?: number): Promise<NodeJS.Timeout>;
|
|
67
|
+
/**
|
|
68
|
+
* showTimers
|
|
69
|
+
* @description Retrieves all active timers.
|
|
70
|
+
* @returns Array of `Timer` objects
|
|
71
|
+
* @throws If file operation fails
|
|
72
|
+
* @example
|
|
73
|
+
* const timers = await manager.showTimers();
|
|
74
|
+
* console.log(JSON.stringify(timers))
|
|
75
|
+
*/
|
|
76
|
+
showTimers(): Promise<Timer<T>[]>;
|
|
77
|
+
/**
|
|
78
|
+
* adjustRemainingTime
|
|
79
|
+
* @description Adjusts the remaining time of a timer.
|
|
80
|
+
* @param id ID of the timer to modify
|
|
81
|
+
* @param delay Delay in milliseconds to add/subtract from the remaining time
|
|
82
|
+
* @returns Promise resolving when the operation is complete
|
|
83
|
+
* @throws If file operation fails
|
|
84
|
+
*/
|
|
85
|
+
adjustRemainingTime(id: string, delay: number): Promise<void>;
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=TimersManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TimersManager.d.ts","sourceRoot":"","sources":["../../../src/TimersManager/TimersManager.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,kBAAkB,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAC1E,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAG5D;;;;;;;;GAQG;AACH,8BAAsB,aAAa,CAAC,CAAC,SAAS,WAAW;IACxD,SAAS,CAAC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IACxC,OAAO,CAAC,SAAS,CAAkB;IAEnC,SAAS,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,IAAI,CAAQ;IAEpD,OAAO,CAAC,KAAK,CAAoC;IACjD,SAAS,CAAC,YAAY,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC;IAM9C,SAAS,CAAC,QAAQ,CAAC,kBAAkB,IAAI,MAAM;IAC/C,SAAS,CAAC,QAAQ,CAAC,iBAAiB,IAAI,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IAE/D;;;;;;;;QAQO;gBAEN,OAAO,CAAC,EAAE,MAAM;IAcjB;;;;;;;;;;OAUM;IACO,WAAW,CAAC,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;IA0BzE;;;;;;;;OAQM;IACO,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBnD;;;;;;;;;;;;;OAaM;IACO,WAAW,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,EAAE,QAAQ,GAAE,MAAY,GAAG,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC;IAuD9H;;;;;;;;OAQM;IACO,UAAU,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;IAQ9C;;;;;;;QAOO;IACM,mBAAmB,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAsB1E"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JSONLTimersStore = void 0;
|
|
4
|
+
const uuid_1 = require("uuid");
|
|
5
|
+
const TimersStore_js_1 = require("./TimersStore.cjs");
|
|
6
|
+
class JSONLTimersStore extends TimersStore_js_1.TimersStore {
|
|
7
|
+
constructor(timerfile) {
|
|
8
|
+
super(timerfile);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* checkTimerfileSyntax
|
|
12
|
+
* @description Checks the syntax of the timer file.
|
|
13
|
+
* @param timers Array of timer objects to check
|
|
14
|
+
* @returns void
|
|
15
|
+
* @throws If syntax is invalid
|
|
16
|
+
*/
|
|
17
|
+
async checkTimerfileSyntax(timers) {
|
|
18
|
+
const throwing = () => {
|
|
19
|
+
throw new Error(`Timer file's syntax is wrong`);
|
|
20
|
+
};
|
|
21
|
+
for (const timer of timers) {
|
|
22
|
+
if (!timer.id || !(0, uuid_1.validate)(timer.id))
|
|
23
|
+
throwing();
|
|
24
|
+
if (!timer.start || typeof timer.start !== "number")
|
|
25
|
+
throwing();
|
|
26
|
+
if (!timer.stop || typeof timer.stop !== "number")
|
|
27
|
+
throwing();
|
|
28
|
+
if (timer.start > timer.stop)
|
|
29
|
+
throwing();
|
|
30
|
+
if (timer.title && typeof timer.title !== "string")
|
|
31
|
+
throwing();
|
|
32
|
+
if (timer.description && typeof timer.description !== "string")
|
|
33
|
+
throwing();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
toStringifyTimers(timers) {
|
|
37
|
+
if (timers.length === 0) {
|
|
38
|
+
return "";
|
|
39
|
+
}
|
|
40
|
+
return timers.map(t => JSON.stringify(t)).join("\n");
|
|
41
|
+
}
|
|
42
|
+
parseTimers(data) {
|
|
43
|
+
return data
|
|
44
|
+
.split(/\r?\n/)
|
|
45
|
+
.filter((line) => line.trim())
|
|
46
|
+
.map((line) => JSON.parse(line));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.JSONLTimersStore = JSONLTimersStore;
|
|
50
|
+
//# sourceMappingURL=JSONLTimersStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JSONLTimersStore.js","sourceRoot":"","sources":["../../../src/TimersStore/JSONLTimersStore.ts"],"names":[],"mappings":";;;AAAA,+BAAgC;AAGhC,qDAA+C;AAE/C,MAAa,gBAAiB,SAAQ,4BAAoB;IAEzD,YACC,SAAiB;QAEjB,KAAK,CAAC,SAAS,CAAC,CAAC;IAClB,CAAC;IAED;;;;;;OAMG;IACgB,KAAK,CAAC,oBAAoB,CAAC,MAAwB;QACrE,MAAM,QAAQ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AC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAmB,CAAC,CAAC;IACrD,CAAC;CACD;AA1CD,4CA0CC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Timer } from "../types.js";
|
|
2
|
+
import { TimersStore } from "./TimersStore.js";
|
|
3
|
+
export declare class JSONLTimersStore extends TimersStore<"JSONL"> {
|
|
4
|
+
constructor(timerfile: string);
|
|
5
|
+
/**
|
|
6
|
+
* checkTimerfileSyntax
|
|
7
|
+
* @description Checks the syntax of the timer file.
|
|
8
|
+
* @param timers Array of timer objects to check
|
|
9
|
+
* @returns void
|
|
10
|
+
* @throws If syntax is invalid
|
|
11
|
+
*/
|
|
12
|
+
protected checkTimerfileSyntax(timers: Timer<"JSONL">[]): Promise<void>;
|
|
13
|
+
toStringifyTimers(timers: Timer<"JSONL">[]): string;
|
|
14
|
+
parseTimers(data: string): Timer<"JSONL">[];
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=JSONLTimersStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JSONLTimersStore.d.ts","sourceRoot":"","sources":["../../../src/TimersStore/JSONLTimersStore.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,qBAAa,gBAAiB,SAAQ,WAAW,CAAC,OAAO,CAAC;gBAGxD,SAAS,EAAE,MAAM;IAKlB;;;;;;OAMG;cACsB,oBAAoB,CAAC,MAAM,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IActE,iBAAiB,CAAC,MAAM,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE,GAAG,MAAM;IAOnD,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,EAAE;CAM3D"}
|