eternal-timer 2.2.1 → 2.4.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 +89 -46
- package/dist/cjs/Log.cjs +0 -21
- package/dist/cjs/{Log.js.map → Log.cjs.map} +1 -1
- package/dist/cjs/TimersManager/JSONLTimersManager.cjs +25 -0
- package/dist/cjs/TimersManager/JSONLTimersManager.cjs.map +1 -0
- package/dist/cjs/TimersManager/JSONLTimersManager.d.ts +18 -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 +18 -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 -0
- package/dist/cjs/index.d.ts +3 -3
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/types.d.ts +11 -1
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/esm/Log.js +0 -21
- package/dist/esm/Log.js.map +1 -1
- package/dist/esm/TimersManager/JSONLTimersManager.d.ts +18 -0
- package/dist/esm/TimersManager/JSONLTimersManager.d.ts.map +1 -0
- package/dist/esm/TimersManager/JSONLTimersManager.js +21 -0
- package/dist/esm/TimersManager/JSONLTimersManager.js.map +1 -0
- package/dist/esm/TimersManager/PlainTextTimersManager.d.ts +18 -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 +3 -3
- 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 +11 -1
- package/dist/esm/types.d.ts.map +1 -1
- package/package.json +7 -4
- package/dist/cjs/JSONLTimersManager.cjs +0 -198
- package/dist/cjs/JSONLTimersManager.d.ts +0 -63
- package/dist/cjs/JSONLTimersManager.d.ts.map +0 -1
- package/dist/cjs/JSONLTimersManager.js.map +0 -1
- package/dist/cjs/PlainTextTimersManager.cjs +0 -200
- package/dist/cjs/PlainTextTimersManager.d.ts +0 -60
- package/dist/cjs/PlainTextTimersManager.d.ts.map +0 -1
- package/dist/cjs/PlainTextTimersManager.js.map +0 -1
- package/dist/cjs/TimersManager.cjs +0 -39
- package/dist/cjs/TimersManager.d.ts +0 -75
- package/dist/cjs/TimersManager.d.ts.map +0 -1
- package/dist/cjs/TimersManager.js.map +0 -1
- package/dist/cjs/index.js.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 -191
- 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/dist/cjs/{searchRoot.js.map → searchRoot.cjs.map} +0 -0
- /package/dist/cjs/{types.js.map → types.cjs.map} +0 -0
package/README.md
CHANGED
|
@@ -33,11 +33,11 @@ async function main() {
|
|
|
33
33
|
const manager = new JSONLTimersManager();
|
|
34
34
|
|
|
35
35
|
// Create a timer (5 seconds) with a title and description
|
|
36
|
-
const timerId = await manager.createTimer(5000, 'My Timer', 'This is a test timer.');
|
|
36
|
+
const timerId = await manager.createTimer({length: 5000, title: 'My Timer', description: 'This is a test timer.'});
|
|
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
42
|
});
|
|
43
43
|
|
|
@@ -45,8 +45,11 @@ async function main() {
|
|
|
45
45
|
const timers = await manager.showTimers();
|
|
46
46
|
console.log('Active timers:', timers);
|
|
47
47
|
|
|
48
|
-
//
|
|
49
|
-
|
|
48
|
+
// To stop monitoring, for example after 10 seconds
|
|
49
|
+
setTimeout(() => {
|
|
50
|
+
clearInterval(interval);
|
|
51
|
+
console.log('Stopped monitoring timers.');
|
|
52
|
+
}, 10000);
|
|
50
53
|
}
|
|
51
54
|
|
|
52
55
|
main();
|
|
@@ -68,9 +71,19 @@ async function main() {
|
|
|
68
71
|
console.log('Timer created:', timerId);
|
|
69
72
|
|
|
70
73
|
// Monitor timers
|
|
71
|
-
const interval = manager.checkTimers(async (timer) => {
|
|
74
|
+
const interval = await manager.checkTimers(async (timer) => {
|
|
72
75
|
console.log('Timer expired:', timer.id);
|
|
73
76
|
});
|
|
77
|
+
|
|
78
|
+
// Display all timers
|
|
79
|
+
const timers = await manager.showTimers();
|
|
80
|
+
console.log('Active timers:', timers);
|
|
81
|
+
|
|
82
|
+
// Stop monitoring after a while
|
|
83
|
+
setTimeout(() => {
|
|
84
|
+
clearInterval(interval);
|
|
85
|
+
console.log('Stopped monitoring timers.');
|
|
86
|
+
}, 10000);
|
|
74
87
|
}
|
|
75
88
|
|
|
76
89
|
main();
|
|
@@ -78,79 +91,109 @@ main();
|
|
|
78
91
|
|
|
79
92
|
## API
|
|
80
93
|
|
|
81
|
-
### `
|
|
94
|
+
### `JSONLTimersManager`
|
|
82
95
|
|
|
83
|
-
|
|
96
|
+
#### `constructor(timerfiledir?: string)`
|
|
97
|
+
Creates a manager for timers stored in **JSON Lines** format.
|
|
84
98
|
|
|
85
|
-
|
|
86
|
-
- `timerfiledir` (string, optional): The path to the timer file. If omitted, the default is `.timers.jsonl` in the project root.
|
|
99
|
+
- **`timerfiledir`** (optional, string): Path to the timer file. Defaults to `.timers.jsonl` in the project root.
|
|
87
100
|
|
|
88
|
-
### `
|
|
101
|
+
### `PlainTextTimersManager`
|
|
89
102
|
|
|
90
|
-
|
|
103
|
+
#### `constructor(timerfiledir?: string)`
|
|
104
|
+
Creates a manager for timers stored in **plain-text** format.
|
|
91
105
|
|
|
92
|
-
|
|
93
|
-
- `timerfiledir` (string, optional): The path to the timer file. If omitted, the default is `.timers` in the project root.
|
|
106
|
+
- **`timerfiledir`** (optional, string): Path to the timer file. Defaults to `.timers` in the project root.
|
|
94
107
|
|
|
95
108
|
---
|
|
96
109
|
|
|
97
|
-
### `createTimer(
|
|
110
|
+
### `createTimer(options: CreateTimerOptions<T>): Promise<string>`
|
|
111
|
+
|
|
112
|
+
Creates a new timer and saves it to the file.
|
|
98
113
|
|
|
99
|
-
|
|
114
|
+
#### Parameters
|
|
100
115
|
|
|
101
|
-
|
|
102
|
-
- `length` (number): Timer duration in milliseconds
|
|
103
|
-
- `title` (string, optional): A title for the timer. **Only available for `JSONLTimersManager`**.
|
|
104
|
-
- `description` (string, optional): A description for the timer. **Only available for `JSONLTimersManager`**.
|
|
116
|
+
- **`options`**
|
|
105
117
|
|
|
106
|
-
|
|
118
|
+
When using `"PlainText"` storage:
|
|
119
|
+
- `number` — Timer duration in milliseconds.
|
|
107
120
|
|
|
108
|
-
|
|
121
|
+
When using `"JSONL"` storage:
|
|
122
|
+
- `{ length: number; title?: string; description?: string }`
|
|
123
|
+
- `length` (number): Timer duration in milliseconds.
|
|
124
|
+
- `title` (optional, string): A title for the timer.
|
|
125
|
+
- `description` (optional, string): A description for the timer.
|
|
126
|
+
|
|
127
|
+
- `number` — Timer duration in milliseconds.
|
|
128
|
+
⚠ Not recommended. See [Storage Formats](#storage-formats).
|
|
129
|
+
|
|
130
|
+
**Returns** A `Promise<string>` that resolves to the timer's unique ID (UUID).
|
|
131
|
+
|
|
132
|
+
**Throws** An error if: `length` is invalid (e.g., negative) or a file operation fails
|
|
109
133
|
|
|
110
134
|
### `removeTimer(id: string): Promise<void>`
|
|
111
135
|
|
|
112
|
-
Removes a timer by ID.
|
|
136
|
+
Removes a timer by its ID.
|
|
113
137
|
|
|
114
|
-
|
|
115
|
-
- `id` (string): ID of the timer to remove
|
|
138
|
+
- **`id`**: The ID of the timer to remove.
|
|
116
139
|
|
|
117
|
-
**Returns:**
|
|
140
|
+
**Returns:** A `Promise` that resolves when the timer is removed.
|
|
118
141
|
|
|
119
|
-
**Throws:**
|
|
142
|
+
**Throws:** An error if the timer with the specified ID is not found or if a file operation fails.
|
|
120
143
|
|
|
121
|
-
### `checkTimers(callback: (timer: Timer) => Promise<void>, interval?: number): Promise<NodeJS.Timeout>`
|
|
144
|
+
### `checkTimers(callback: (timer: Timer) => void | Promise<void>, interval?: number): Promise<NodeJS.Timeout>`
|
|
122
145
|
|
|
123
|
-
Starts monitoring
|
|
146
|
+
Starts monitoring timers at the specified interval and invokes the provided
|
|
147
|
+
`callback` when a timer expires.
|
|
124
148
|
|
|
125
|
-
|
|
126
|
-
|
|
149
|
+
For each expired timer, the `callback` is awaited before processing continues.
|
|
150
|
+
Monitoring runs periodically in the background and can be stopped using the
|
|
151
|
+
returned `NodeJS.Timeout`.
|
|
127
152
|
|
|
128
|
-
**
|
|
129
|
-
-
|
|
130
|
-
|
|
153
|
+
- **callback**: An async function invoked with the expired `timer`.
|
|
154
|
+
- **interval** *(optional, number)*: Interval in milliseconds for checking timers.
|
|
155
|
+
Defaults to `200`.
|
|
131
156
|
|
|
132
|
-
**Returns:**
|
|
157
|
+
**Returns:** A `NodeJS.Timeout` that can be passed to `clearInterval()` to stop monitoring.
|
|
133
158
|
|
|
134
|
-
**Throws:**
|
|
159
|
+
**Throws:** An error if a timer storage operation fails.
|
|
135
160
|
|
|
136
161
|
### `showTimers(): Promise<Timer[]>`
|
|
137
162
|
|
|
138
163
|
Retrieves all active timers.
|
|
139
164
|
|
|
140
|
-
**Returns:**
|
|
165
|
+
**Returns:** A `Promise` that resolves to an array of `Timer` objects.
|
|
166
|
+
|
|
167
|
+
**Throws:** An error if a file operation fails.
|
|
141
168
|
|
|
142
|
-
|
|
169
|
+
### `adjustRemainingTime(id: string, delay: number): Promise<void>`
|
|
170
|
+
|
|
171
|
+
Adjusts the remaining time of a specified timer. This can be used to extend or shorten a timer's duration.
|
|
172
|
+
|
|
173
|
+
- **`id`**: The ID of the timer to modify.
|
|
174
|
+
- **`delay`**: The amount of time in milliseconds to add to (if positive) or subtract from (if negative) the timer's remaining duration.
|
|
175
|
+
|
|
176
|
+
**Returns:** A `Promise<void>` that resolves when the timer's remaining time has been adjusted.
|
|
177
|
+
|
|
178
|
+
**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.
|
|
143
179
|
|
|
144
180
|
## Type Definition
|
|
145
181
|
|
|
182
|
+
The `StorageType` has the following structure:
|
|
146
183
|
```typescript
|
|
147
|
-
type
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
184
|
+
type StorageType = "JSONL" | "PlainText"
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
The `Timer` object has the following structure:
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
type Timer<T extends StorageType> = {
|
|
191
|
+
id: string;
|
|
192
|
+
start: number;
|
|
193
|
+
stop: number;
|
|
194
|
+
} & (T extends "JSONL"
|
|
195
|
+
? { title?: string; description?: string }
|
|
196
|
+
: {});
|
|
154
197
|
```
|
|
155
198
|
|
|
156
199
|
## Scripts
|
|
@@ -168,7 +211,7 @@ You can choose between two storage formats by selecting the appropriate manager
|
|
|
168
211
|
### 1. JSON Lines (via `JSONLTimersManager`)
|
|
169
212
|
This is the recommended format for storing rich metadata.
|
|
170
213
|
|
|
171
|
-
- **Pros**: Allows for storing `title` and `description`.
|
|
214
|
+
- **Pros**: Allows for storing `title` and `description`. Improved memory efficiency due to line-by-line file reading.
|
|
172
215
|
- **Cons**: Involves JSON parsing, which may have a minor performance overhead.
|
|
173
216
|
- **Default File**: `.timers.jsonl`
|
|
174
217
|
- **Format**:
|
|
@@ -179,7 +222,7 @@ This is the recommended format for storing rich metadata.
|
|
|
179
222
|
### 2. Plain Text (via `PlainTextTimersManager`)
|
|
180
223
|
This format is more lightweight and slightly faster.
|
|
181
224
|
|
|
182
|
-
- **Pros**: Simple and efficient.
|
|
225
|
+
- **Pros**: Simple and efficient. Improved memory efficiency due to line-by-line file reading.
|
|
183
226
|
- **Cons**: Cannot store additional data like `title` or `description`.
|
|
184
227
|
- **Default File**: `.timers`
|
|
185
228
|
- **Format**:
|
package/dist/cjs/Log.cjs
CHANGED
|
@@ -58,25 +58,4 @@ class Log {
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
exports.Log = Log;
|
|
61
|
-
/*
|
|
62
|
-
let logger: Logger | null = null;
|
|
63
|
-
let initPromise: Promise<void> | null = null;
|
|
64
|
-
|
|
65
|
-
export async function ensureLogger() {
|
|
66
|
-
if (logger) return;
|
|
67
|
-
if (!initPromise) {
|
|
68
|
-
initPromise = (async () => {
|
|
69
|
-
try {
|
|
70
|
-
const logtape = await import("@logtape/logtape");
|
|
71
|
-
logger = logtape.getLogger(["eternal-timer"]);
|
|
72
|
-
} catch {
|
|
73
|
-
console.info(
|
|
74
|
-
"Tip: Install the optional package '@logtape/logtape' to customize logging behavior.",
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
})();
|
|
78
|
-
}
|
|
79
|
-
await initPromise;
|
|
80
|
-
}
|
|
81
|
-
*/
|
|
82
61
|
//# sourceMappingURL=Log.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Log.js","sourceRoot":"","sources":["../../src/Log.ts"],"names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AAC,CAAC;oBACjD,GAAG,CAAC,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC;gBACnD,CAAC;gBAAC,MAAM,CAAC;oBACR,OAAO,CAAC,IAAI,CACX,qFAAqF,CACrF,CAAC;gBACH,CAAC;YACF,CAAC,CAAC,EAAE,CAAC;QACN,CAAC;QACD,MAAM,GAAG,CAAC,WAAW,CAAC;IACvB,CAAC;IAEM,MAAM,KAAK,cAAc;QAC/B,OAAO,GAAG,CAAC,MAAM,CAAC;IACnB,CAAC;;AAvBF,kBAwBC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
}
|
|
24
|
+
exports.JSONLTimersManager = JSONLTimersManager;
|
|
25
|
+
//# 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;AAGtE;;;;;;;;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;CACD;AAVD,gDAUC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { TimersManager } from "./TimersManager.js";
|
|
2
|
+
import { JSONLTimersStore } from "../TimersStore/JSONLTimersStore.js";
|
|
3
|
+
import type { TimersStore } from "src/TimersStore/TimersStore.js";
|
|
4
|
+
/**
|
|
5
|
+
* JSONLTimersManager
|
|
6
|
+
* @description
|
|
7
|
+
* Manages timers stored in a JSONL file.
|
|
8
|
+
* (This is a abstract class)
|
|
9
|
+
*
|
|
10
|
+
* - Timers are persisted in a file
|
|
11
|
+
* - Expired timers are detected by polling
|
|
12
|
+
*/
|
|
13
|
+
export declare class JSONLTimersManager extends TimersManager<"JSONL"> {
|
|
14
|
+
protected TimersStore: JSONLTimersStore | null;
|
|
15
|
+
protected getDefaultFilename(): string;
|
|
16
|
+
protected createTimersStore(): Promise<TimersStore<"JSONL">>;
|
|
17
|
+
}
|
|
18
|
+
//# 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;AACtE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAC;AAElE;;;;;;;;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,WAAW,CAAC,OAAO,CAAC,CAAC;CAG3E"}
|
|
@@ -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;AAG9E;;;;;;;;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,18 @@
|
|
|
1
|
+
import { TimersManager } from "./TimersManager.js";
|
|
2
|
+
import { PlainTextTimersStore } from "../TimersStore/PlainTextTimersStore.js";
|
|
3
|
+
import type { TimersStore } from "src/TimersStore/TimersStore.js";
|
|
4
|
+
/**
|
|
5
|
+
* PlainTextTimersManager
|
|
6
|
+
* @description
|
|
7
|
+
* Manages timers stored in a PlainText file.
|
|
8
|
+
* (This is a abstract class)
|
|
9
|
+
*
|
|
10
|
+
* - Timers are persisted in a file
|
|
11
|
+
* - Expired timers are detected by polling
|
|
12
|
+
*/
|
|
13
|
+
export declare class PlainTextTimersManager extends TimersManager<"PlainText"> {
|
|
14
|
+
protected TimersStore: PlainTextTimersStore | null;
|
|
15
|
+
protected getDefaultFilename(): string;
|
|
16
|
+
protected createTimersStore(): Promise<TimersStore<"PlainText">>;
|
|
17
|
+
}
|
|
18
|
+
//# 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;AAC9E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAC;AAElE;;;;;;;;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,WAAW,CAAC,WAAW,CAAC,CAAC;CAGtE"}
|
|
@@ -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;IACzC,YAAY,CAAI,EAAoB;QAC3C,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC9B,IAAI,CAAC,KA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AAY,CAAC,CAAC;YAClD,CAAC;YAED,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YAEvB,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,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
|
+
private runExclusive;
|
|
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
|