eternal-timer 4.3.1 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -22
- package/dist/cjs/TimersManager/JSONLTimersManager.d.ts +22 -7
- package/dist/cjs/TimersManager/JSONLTimersManager.d.ts.map +1 -1
- package/dist/cjs/TimersManager/JSONLTimersManager.js +44 -9
- package/dist/cjs/TimersManager/JSONLTimersManager.js.map +1 -1
- package/dist/cjs/TimersManager/PlainTextTimersManager.d.ts +19 -4
- package/dist/cjs/TimersManager/PlainTextTimersManager.d.ts.map +1 -1
- package/dist/cjs/TimersManager/PlainTextTimersManager.js +43 -6
- package/dist/cjs/TimersManager/PlainTextTimersManager.js.map +1 -1
- package/dist/cjs/TimersManager/TimersManager.d.ts +25 -26
- package/dist/cjs/TimersManager/TimersManager.d.ts.map +1 -1
- package/dist/cjs/TimersManager/TimersManager.js +39 -50
- package/dist/cjs/TimersManager/TimersManager.js.map +1 -1
- package/dist/cjs/searchRoot.d.ts +1 -1
- package/dist/cjs/searchRoot.d.ts.map +1 -1
- package/dist/cjs/searchRoot.js +13 -7
- package/dist/cjs/searchRoot.js.map +1 -1
- package/dist/cjs/throwMessage.d.ts +1 -0
- package/dist/cjs/throwMessage.d.ts.map +1 -1
- package/dist/cjs/throwMessage.js +1 -0
- package/dist/cjs/throwMessage.js.map +1 -1
- package/dist/esm/TimersManager/JSONLTimersManager.d.ts +22 -7
- package/dist/esm/TimersManager/JSONLTimersManager.d.ts.map +1 -1
- package/dist/esm/TimersManager/JSONLTimersManager.js +41 -9
- package/dist/esm/TimersManager/JSONLTimersManager.js.map +1 -1
- package/dist/esm/TimersManager/PlainTextTimersManager.d.ts +19 -4
- package/dist/esm/TimersManager/PlainTextTimersManager.d.ts.map +1 -1
- package/dist/esm/TimersManager/PlainTextTimersManager.js +40 -6
- package/dist/esm/TimersManager/PlainTextTimersManager.js.map +1 -1
- package/dist/esm/TimersManager/TimersManager.d.ts +25 -26
- package/dist/esm/TimersManager/TimersManager.d.ts.map +1 -1
- package/dist/esm/TimersManager/TimersManager.js +39 -47
- package/dist/esm/TimersManager/TimersManager.js.map +1 -1
- package/dist/esm/searchRoot.d.ts +1 -1
- package/dist/esm/searchRoot.d.ts.map +1 -1
- package/dist/esm/searchRoot.js +13 -7
- package/dist/esm/searchRoot.js.map +1 -1
- package/dist/esm/throwMessage.d.ts +1 -0
- package/dist/esm/throwMessage.d.ts.map +1 -1
- package/dist/esm/throwMessage.js +1 -0
- package/dist/esm/throwMessage.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.TimersManager = void 0;
|
|
7
|
-
const path_1 = __importDefault(require("path"));
|
|
8
|
-
const fs_1 = __importDefault(require("fs"));
|
|
9
4
|
const uuid_1 = require("uuid");
|
|
10
|
-
const searchRoot_js_1 = __importDefault(require("../searchRoot.js"));
|
|
11
5
|
const EventEmitter_js_1 = require("../EventEmitter.js");
|
|
12
6
|
const throwMessage_js_1 = require("../throwMessage.js");
|
|
13
7
|
/**
|
|
@@ -21,10 +15,16 @@ const throwMessage_js_1 = require("../throwMessage.js");
|
|
|
21
15
|
*/
|
|
22
16
|
class TimersManager extends EventEmitter_js_1.EventEmitter {
|
|
23
17
|
timerfiledir;
|
|
18
|
+
// lock if the check loop is running, or not. This is used to prevent multiple check loops from running simultaneously and to indicate whether a file operation is in progress (e.g. loading or saving timers).
|
|
24
19
|
checkLock = false;
|
|
25
|
-
|
|
20
|
+
/*
|
|
21
|
+
// Interval id of the check loop. If the loop is not running, this is undefined.
|
|
22
|
+
protected interval: NodeJS.Timeout | undefined;
|
|
23
|
+
*/
|
|
24
|
+
static intervals = new Map();
|
|
25
|
+
// Indicates whether the check loop is running. This is used to control the loop and to prevent starting multiple loops simultaneously.
|
|
26
26
|
running = false;
|
|
27
|
-
|
|
27
|
+
// A simple promise queue to ensure that file operations are performed sequentially, preventing race conditions
|
|
28
28
|
queue = Promise.resolve();
|
|
29
29
|
runExclusive(fn) {
|
|
30
30
|
const p = this.queue.then(fn);
|
|
@@ -32,27 +32,16 @@ class TimersManager extends EventEmitter_js_1.EventEmitter {
|
|
|
32
32
|
return p;
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
*/
|
|
35
|
+
* constructor
|
|
36
|
+
* @param {string} timerfile timer file path.
|
|
37
|
+
* @description Initializes the TimersManager instance.
|
|
38
|
+
* @deprecated This constructor is deprecated. Please use the static `create` method instead, which performs necessary asynchronous initialization. The constructor will be made private in a future release.
|
|
39
|
+
* @example
|
|
40
|
+
* const manager = new TimersManager("/path/to/timers"); // Uses specified timer file path
|
|
41
|
+
*/
|
|
43
42
|
constructor(timerfile) {
|
|
44
43
|
super();
|
|
45
|
-
|
|
46
|
-
this.timerfiledir = path_1.default.resolve(rootDir, timerfile ?? this.getDefaultFilename());
|
|
47
|
-
if (!this.timerfiledir.startsWith(rootDir)) {
|
|
48
|
-
throw new Error(throwMessage_js_1.throwMessage.FilePathinvalid);
|
|
49
|
-
}
|
|
50
|
-
try {
|
|
51
|
-
fs_1.default.accessSync(this.timerfiledir);
|
|
52
|
-
}
|
|
53
|
-
catch {
|
|
54
|
-
fs_1.default.writeFileSync(this.timerfiledir, "");
|
|
55
|
-
}
|
|
44
|
+
this.timerfiledir = timerfile;
|
|
56
45
|
}
|
|
57
46
|
/**
|
|
58
47
|
* createTimer
|
|
@@ -62,16 +51,15 @@ class TimersManager extends EventEmitter_js_1.EventEmitter {
|
|
|
62
51
|
* @throws If length is invalid (e.g. length < 0) or file operation fails
|
|
63
52
|
* @example
|
|
64
53
|
* // For PlainTextTimersManager
|
|
65
|
-
* const manager =
|
|
54
|
+
* const manager = await PlainTextTimersManager.create();
|
|
66
55
|
* const newTimerId = await manager.createTimer(5000); // Create a 5-second timer
|
|
67
56
|
*
|
|
68
57
|
* // For JSONLTimersManager
|
|
69
|
-
* const jsonlManager =
|
|
58
|
+
* const jsonlManager = await JSONLTimersManager.create<{ title: string }>();
|
|
70
59
|
* const jsonlTimerId = await jsonlManager.createTimer({ length: 10000, extra: { title: "My JSONL Timer" } }); // Create a 10-second timer with extra data
|
|
71
60
|
*/
|
|
72
61
|
async createTimer(options) {
|
|
73
62
|
return this.runExclusive(async () => {
|
|
74
|
-
this.TimersStore ??= await this.createTimersStore();
|
|
75
63
|
if (this.type === "JSONL" && typeof options === "number") {
|
|
76
64
|
throw new Error(throwMessage_js_1.throwMessage.NoExtra);
|
|
77
65
|
}
|
|
@@ -105,7 +93,6 @@ class TimersManager extends EventEmitter_js_1.EventEmitter {
|
|
|
105
93
|
*/
|
|
106
94
|
async removeTimer(id) {
|
|
107
95
|
return this.runExclusive(async () => {
|
|
108
|
-
this.TimersStore ??= await this.createTimersStore();
|
|
109
96
|
const timers = await this.TimersStore.loadTimers();
|
|
110
97
|
const index = timers.findIndex(t => t.id === id);
|
|
111
98
|
if (index === -1 || timers[index] === undefined) {
|
|
@@ -123,14 +110,12 @@ class TimersManager extends EventEmitter_js_1.EventEmitter {
|
|
|
123
110
|
* @returns Promise<void> that resolves when the loop has been started
|
|
124
111
|
* @throws If file operation fails during checking
|
|
125
112
|
* @example
|
|
126
|
-
* const manager = new TimersManager();
|
|
127
113
|
* await manager.checkStart(1000); // Check for expired timers every 1 second
|
|
128
114
|
*/
|
|
129
115
|
async checkStart(interval = 200) {
|
|
130
116
|
if (this.running)
|
|
131
117
|
return;
|
|
132
118
|
this.running = true;
|
|
133
|
-
this.TimersStore ??= await this.createTimersStore();
|
|
134
119
|
const loop = async () => {
|
|
135
120
|
if (!this.running)
|
|
136
121
|
return;
|
|
@@ -170,28 +155,30 @@ class TimersManager extends EventEmitter_js_1.EventEmitter {
|
|
|
170
155
|
finally {
|
|
171
156
|
this.checkLock = false;
|
|
172
157
|
if (this.running) {
|
|
173
|
-
this.
|
|
158
|
+
TimersManager.intervals.set(this.timerfiledir, setTimeout(loop, interval));
|
|
174
159
|
}
|
|
175
160
|
}
|
|
176
161
|
};
|
|
177
162
|
this.emit("started", void 0);
|
|
178
|
-
this.
|
|
163
|
+
if (TimersManager.intervals.has(this.timerfiledir)) {
|
|
164
|
+
throw new Error(throwMessage_js_1.throwMessage.AlreadyExists);
|
|
165
|
+
}
|
|
166
|
+
TimersManager.intervals.set(this.timerfiledir, setTimeout(loop, interval));
|
|
179
167
|
}
|
|
180
168
|
/**
|
|
181
169
|
* checkStop
|
|
182
170
|
* @description Stops the timer checking loop.
|
|
183
171
|
* @returns Promise resolving when the loop has been stopped
|
|
184
172
|
* @example
|
|
185
|
-
* const manager = new TimersManager();
|
|
186
173
|
* await manager.checkStart(1000);
|
|
187
174
|
* // ... later, to stop checking:
|
|
188
175
|
* await manager.checkStop();
|
|
189
176
|
*/
|
|
190
177
|
async checkStop() {
|
|
191
178
|
this.running = false;
|
|
192
|
-
if (this.
|
|
193
|
-
clearTimeout(this.
|
|
194
|
-
this.
|
|
179
|
+
if (TimersManager.intervals.has(this.timerfiledir)) {
|
|
180
|
+
clearTimeout(TimersManager.intervals.get(this.timerfiledir));
|
|
181
|
+
TimersManager.intervals.delete(this.timerfiledir);
|
|
195
182
|
}
|
|
196
183
|
this.emit("stopped", void 0);
|
|
197
184
|
}
|
|
@@ -208,35 +195,37 @@ class TimersManager extends EventEmitter_js_1.EventEmitter {
|
|
|
208
195
|
return this.checkLock;
|
|
209
196
|
}
|
|
210
197
|
/**
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
198
|
+
* showTimers
|
|
199
|
+
* @description Retrieves all active timers.
|
|
200
|
+
* @returns Array of `Timer` objects
|
|
201
|
+
* @throws If file operation fails
|
|
202
|
+
* @example
|
|
203
|
+
* const timers = await manager.showTimers();
|
|
204
|
+
* console.log(JSON.stringify(timers));
|
|
205
|
+
*/
|
|
219
206
|
async showTimers() {
|
|
220
207
|
return this.runExclusive(async () => {
|
|
221
|
-
this.TimersStore ??= await this.createTimersStore();
|
|
222
208
|
const timersData = await this.TimersStore.loadTimers();
|
|
223
209
|
return timersData;
|
|
224
210
|
});
|
|
225
211
|
}
|
|
226
212
|
/**
|
|
227
|
-
|
|
213
|
+
* adjustRemainingTime
|
|
228
214
|
* @description Adjusts the remaining time of a timer.
|
|
229
215
|
* @param {string} id ID of the timer to modify
|
|
230
216
|
* @param {number} delay Delay in milliseconds to add/subtract from the remaining time
|
|
231
217
|
* @returns Promise resolving when the operation is complete
|
|
232
218
|
* @throws If file operation fails
|
|
219
|
+
* @example
|
|
220
|
+
* const timer = await manager.createTimer(10000); // Create a 10-second timer
|
|
221
|
+
* await manager.adjustRemainingTime(timer, -2000); // Subtract 2 seconds from the remaining time (now 8 seconds left)
|
|
222
|
+
* await manager.adjustRemainingTime(timer, 3000); // Add 3 seconds to the remaining time (now 11 seconds left)
|
|
233
223
|
*/
|
|
234
224
|
async adjustRemainingTime(id, delay) {
|
|
235
225
|
return this.runExclusive(async () => {
|
|
236
226
|
if (typeof delay !== "number" || !Number.isFinite(delay)) {
|
|
237
227
|
throw new Error(throwMessage_js_1.throwMessage.InvalidAdjustment(delay));
|
|
238
228
|
}
|
|
239
|
-
this.TimersStore ??= await this.createTimersStore();
|
|
240
229
|
const timers = await this.TimersStore.loadTimers();
|
|
241
230
|
const index = timers.findIndex(t => t.id === id);
|
|
242
231
|
if (index === -1 || timers[index] === undefined) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TimersManager.js","sourceRoot":"","sources":["../../../src/TimersManager/TimersManager.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":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E,KAAK,EAAE,CAAC,CAAC;YACjD,IAAI,KAAK,KAAK,CAAC,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,SAAS,EAAE,CAAC;gBACjD,MAAM,IAAI,KAAK,CAAC,8BAAY,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;YAC5C,CAAC;YAED,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YAEvB,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;YAC5B,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,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;YAC1C,OAAO;QACR,CAAC,CAAC,CAAC;IACH,CAAC;;AAjQH,sCAkQC"}
|
package/dist/cjs/searchRoot.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"searchRoot.d.ts","sourceRoot":"","sources":["../../src/searchRoot.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,
|
|
1
|
+
{"version":3,"file":"searchRoot.d.ts","sourceRoot":"","sources":["../../src/searchRoot.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,wBAA8B,UAAU,oBAavC"}
|
package/dist/cjs/searchRoot.js
CHANGED
|
@@ -4,20 +4,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.default = searchRoot;
|
|
7
|
-
const
|
|
7
|
+
const promises_1 = __importDefault(require("fs/promises"));
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
9
|
/**
|
|
10
10
|
* searchRoot
|
|
11
11
|
* @description searching root directly of the project
|
|
12
12
|
* @returns directly of the project(string)
|
|
13
13
|
*/
|
|
14
|
-
function searchRoot() {
|
|
14
|
+
async function searchRoot() {
|
|
15
15
|
let dir = process.cwd();
|
|
16
|
-
while (
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
while (true) {
|
|
17
|
+
try {
|
|
18
|
+
await promises_1.default.access(path_1.default.join(dir, "package.json"));
|
|
19
|
+
return dir;
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
const parent = path_1.default.dirname(dir);
|
|
23
|
+
if (parent === dir)
|
|
24
|
+
break;
|
|
25
|
+
dir = parent;
|
|
26
|
+
}
|
|
21
27
|
}
|
|
22
28
|
return dir;
|
|
23
29
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"searchRoot.js","sourceRoot":"","sources":["../../src/searchRoot.ts"],"names":[],"mappings":";;;;;AAQA,
|
|
1
|
+
{"version":3,"file":"searchRoot.js","sourceRoot":"","sources":["../../src/searchRoot.ts"],"names":[],"mappings":";;;;;AAQA,6BAaC;AArBD,2DAA6B;AAC7B,gDAAwB;AAExB;;;;GAIG;AACY,KAAK,UAAU,UAAU;IACvC,IAAI,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;IACxB,OAAO,IAAI,EAAE,CAAC;QACb,IAAI,CAAC;YACJ,MAAM,kBAAE,CAAC,MAAM,CAAC,cAAI,CAAC,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,CAAC,CAAC;YAChD,OAAO,GAAG,CAAC;QACZ,CAAC;QAAC,MAAM,CAAC;YACR,MAAM,MAAM,GAAG,cAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YACjC,IAAI,MAAM,KAAK,GAAG;gBAAE,MAAM;YAC1B,GAAG,GAAG,MAAM,CAAC;QACd,CAAC;IACF,CAAC;IACD,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"throwMessage.d.ts","sourceRoot":"","sources":["../../src/throwMessage.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY;mBACT,MAAM;;;;4BAIG,MAAM;+BACH,MAAM
|
|
1
|
+
{"version":3,"file":"throwMessage.d.ts","sourceRoot":"","sources":["../../src/throwMessage.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY;mBACT,MAAM;;;;4BAIG,MAAM;+BACH,MAAM;;;;;;CAMjC,CAAC"}
|
package/dist/cjs/throwMessage.js
CHANGED
|
@@ -12,5 +12,6 @@ exports.throwMessage = {
|
|
|
12
12
|
LoadTimerData: "Error when loading timer data",
|
|
13
13
|
SaveTimerData: "Error when saving timer data",
|
|
14
14
|
AppendTimerData: "Error when appending timer data",
|
|
15
|
+
AlreadyExists: "The loop is already running",
|
|
15
16
|
};
|
|
16
17
|
//# sourceMappingURL=throwMessage.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"throwMessage.js","sourceRoot":"","sources":["../../src/throwMessage.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAG;IAC3B,QAAQ,EAAE,CAAC,EAAU,EAAE,EAAE,CAAC,iBAAiB,EAAE,YAAY;IACzD,WAAW,EAAE,2BAA2B;IACxC,eAAe,EAAE,sDAAsD;IACvE,OAAO,EAAE,mDAAmD;IAC5D,aAAa,EAAE,CAAC,MAAc,EAAE,EAAE,CAAC,mBAAmB,MAAM,EAAE;IAC9D,iBAAiB,EAAE,CAAC,KAAa,EAAE,EAAE,CAAC,uBAAuB,KAAK,EAAE;IACpE,aAAa,EAAE,8BAA8B;IAC7C,aAAa,EAAE,+BAA+B;IAC9C,aAAa,EAAE,8BAA8B;IAC7C,eAAe,EAAE,iCAAiC;
|
|
1
|
+
{"version":3,"file":"throwMessage.js","sourceRoot":"","sources":["../../src/throwMessage.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAG;IAC3B,QAAQ,EAAE,CAAC,EAAU,EAAE,EAAE,CAAC,iBAAiB,EAAE,YAAY;IACzD,WAAW,EAAE,2BAA2B;IACxC,eAAe,EAAE,sDAAsD;IACvE,OAAO,EAAE,mDAAmD;IAC5D,aAAa,EAAE,CAAC,MAAc,EAAE,EAAE,CAAC,mBAAmB,MAAM,EAAE;IAC9D,iBAAiB,EAAE,CAAC,KAAa,EAAE,EAAE,CAAC,uBAAuB,KAAK,EAAE;IACpE,aAAa,EAAE,8BAA8B;IAC7C,aAAa,EAAE,+BAA+B;IAC9C,aAAa,EAAE,8BAA8B;IAC7C,eAAe,EAAE,iCAAiC;IAClD,aAAa,EAAE,6BAA6B;CAC5C,CAAC"}
|
|
@@ -3,16 +3,31 @@ import { JSONLTimersStore } from "../TimersStore/JSONLTimersStore.js";
|
|
|
3
3
|
/**
|
|
4
4
|
* JSONLTimersManager
|
|
5
5
|
* @description
|
|
6
|
-
* Manages timers stored in a
|
|
7
|
-
* (This is a abstract class)
|
|
6
|
+
* Manages timers stored in a JSONL file.
|
|
8
7
|
*
|
|
9
8
|
* - Timers are persisted in a file
|
|
10
9
|
* - Expired timers are detected by polling
|
|
11
10
|
*/
|
|
12
|
-
export declare class JSONLTimersManager<Extra extends object
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
export declare class JSONLTimersManager<Extra extends object> extends TimersManager<"JSONL", Extra> {
|
|
12
|
+
static readonly defaultFilename = ".timers.jsonl";
|
|
13
|
+
/**
|
|
14
|
+
* create
|
|
15
|
+
* @param {string | undefined} timerfile optional timer file path. If not provided, the default path will be used.
|
|
16
|
+
* @description Creates an instance of JSONLTimersManager. If the timer file does not exist, an empty file is created.
|
|
17
|
+
* @throws If file access or creation fails
|
|
18
|
+
* @example
|
|
19
|
+
* const manager = await JSONLTimersManager.create(); // Uses default timer file path
|
|
20
|
+
* const manager = await JSONLTimersManager.create("/path/to/timers.jsonl"); // Uses specified timer file path
|
|
21
|
+
* @returns Promise resolving to an instance of JSONLTimersManager
|
|
22
|
+
*/
|
|
23
|
+
static create<Extra extends object = object>(timerfile?: string): Promise<JSONLTimersManager<Extra>>;
|
|
24
|
+
/**
|
|
25
|
+
* constructor
|
|
26
|
+
* @param {string} timerfiledir resolved timer file path.
|
|
27
|
+
* @description Initializes the JSONLTimersManager instance.
|
|
28
|
+
*/
|
|
29
|
+
protected constructor(timerfiledir: string);
|
|
30
|
+
protected TimersStore: JSONLTimersStore<Extra>;
|
|
16
31
|
protected type: "JSONL";
|
|
17
32
|
/**
|
|
18
33
|
* changeExtra
|
|
@@ -23,7 +38,7 @@ export declare class JSONLTimersManager<Extra extends object = object> extends T
|
|
|
23
38
|
* @throws If timer with id not found or file operation fails
|
|
24
39
|
* @example
|
|
25
40
|
* const timer = await manager.createTimer({ length: 1000, extra: {author: "someone"} });
|
|
26
|
-
* await changeExtra(timer, {author: "SUKEsann2000"});
|
|
41
|
+
* await manager.changeExtra(timer, {author: "SUKEsann2000"});
|
|
27
42
|
* // extra is changed and author will be "SUKEsann2000" instead of "someone"
|
|
28
43
|
*/
|
|
29
44
|
changeExtra(id: string, newExtra: Extra): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JSONLTimersManager.d.ts","sourceRoot":"","sources":["../../../src/TimersManager/JSONLTimersManager.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"JSONLTimersManager.d.ts","sourceRoot":"","sources":["../../../src/TimersManager/JSONLTimersManager.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AAGtE;;;;;;;GAOG;AACH,qBAAa,kBAAkB,CAAC,KAAK,SAAS,MAAM,CAAE,SAAQ,aAAa,CAAC,OAAO,EAAE,KAAK,CAAC;IAC1F,gBAAuB,eAAe,mBAAmB;IAEzD;;;;;;;;;OASG;WACiB,MAAM,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;IAkBjH;;;;OAIG;IACH,SAAS,aAAa,YAAY,EAAE,MAAM;IAK1C,UAAmB,WAAW,EAAE,gBAAgB,CAAC,KAAK,CAAC,CAAC;IAExD,UAAmB,IAAI,EAAE,OAAO,CAAoB;IAEpD;;;;;;;;;;;OAWG;IACU,WAAW,CACvB,EAAE,EAAE,MAAM,EACV,QAAQ,EAAE,KAAK,GACb,OAAO,CAAC,IAAI,CAAC;CAiBhB"}
|
|
@@ -1,23 +1,56 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import fs from "fs/promises";
|
|
3
|
+
import searchRoot from "../searchRoot.js";
|
|
1
4
|
import { TimersManager } from "./TimersManager.js";
|
|
2
5
|
import { JSONLTimersStore } from "../TimersStore/JSONLTimersStore.js";
|
|
3
6
|
import { throwMessage } from "../throwMessage.js";
|
|
4
7
|
/**
|
|
5
8
|
* JSONLTimersManager
|
|
6
9
|
* @description
|
|
7
|
-
* Manages timers stored in a
|
|
8
|
-
* (This is a abstract class)
|
|
10
|
+
* Manages timers stored in a JSONL file.
|
|
9
11
|
*
|
|
10
12
|
* - Timers are persisted in a file
|
|
11
13
|
* - Expired timers are detected by polling
|
|
12
14
|
*/
|
|
13
15
|
export class JSONLTimersManager extends TimersManager {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
static defaultFilename = ".timers.jsonl";
|
|
17
|
+
/**
|
|
18
|
+
* create
|
|
19
|
+
* @param {string | undefined} timerfile optional timer file path. If not provided, the default path will be used.
|
|
20
|
+
* @description Creates an instance of JSONLTimersManager. If the timer file does not exist, an empty file is created.
|
|
21
|
+
* @throws If file access or creation fails
|
|
22
|
+
* @example
|
|
23
|
+
* const manager = await JSONLTimersManager.create(); // Uses default timer file path
|
|
24
|
+
* const manager = await JSONLTimersManager.create("/path/to/timers.jsonl"); // Uses specified timer file path
|
|
25
|
+
* @returns Promise resolving to an instance of JSONLTimersManager
|
|
26
|
+
*/
|
|
27
|
+
static async create(timerfile) {
|
|
28
|
+
const rootDir = await searchRoot();
|
|
29
|
+
const filename = timerfile ?? this.defaultFilename;
|
|
30
|
+
const timerfiledir = path.isAbsolute(filename)
|
|
31
|
+
? filename
|
|
32
|
+
: path.resolve(rootDir, filename);
|
|
33
|
+
if (!timerfiledir.startsWith(rootDir)) {
|
|
34
|
+
throw new Error(throwMessage.FilePathinvalid);
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
await fs.access(timerfiledir);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
await fs.writeFile(timerfiledir, "");
|
|
41
|
+
}
|
|
42
|
+
return new this(timerfiledir);
|
|
17
43
|
}
|
|
18
|
-
|
|
19
|
-
|
|
44
|
+
/**
|
|
45
|
+
* constructor
|
|
46
|
+
* @param {string} timerfiledir resolved timer file path.
|
|
47
|
+
* @description Initializes the JSONLTimersManager instance.
|
|
48
|
+
*/
|
|
49
|
+
constructor(timerfiledir) {
|
|
50
|
+
super(timerfiledir);
|
|
51
|
+
this.TimersStore = new JSONLTimersStore(this.timerfiledir);
|
|
20
52
|
}
|
|
53
|
+
TimersStore;
|
|
21
54
|
type = "JSONL";
|
|
22
55
|
/**
|
|
23
56
|
* changeExtra
|
|
@@ -28,12 +61,11 @@ export class JSONLTimersManager extends TimersManager {
|
|
|
28
61
|
* @throws If timer with id not found or file operation fails
|
|
29
62
|
* @example
|
|
30
63
|
* const timer = await manager.createTimer({ length: 1000, extra: {author: "someone"} });
|
|
31
|
-
* await changeExtra(timer, {author: "SUKEsann2000"});
|
|
64
|
+
* await manager.changeExtra(timer, {author: "SUKEsann2000"});
|
|
32
65
|
* // extra is changed and author will be "SUKEsann2000" instead of "someone"
|
|
33
66
|
*/
|
|
34
67
|
async changeExtra(id, newExtra) {
|
|
35
68
|
return this.runExclusive(async () => {
|
|
36
|
-
this.TimersStore ??= await this.createTimersStore();
|
|
37
69
|
try {
|
|
38
70
|
const timers = await this.TimersStore.loadTimers();
|
|
39
71
|
const index = timers?.findIndex(t => t.id === id);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JSONLTimersManager.js","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,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD
|
|
1
|
+
{"version":3,"file":"JSONLTimersManager.js","sourceRoot":"","sources":["../../../src/TimersManager/JSONLTimersManager.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,MAAM,aAAa,CAAC;AAE7B,OAAO,UAAU,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD;;;;;;;GAOG;AACH,MAAM,OAAO,kBAAyC,SAAQ,aAA6B;IACnF,MAAM,CAAU,eAAe,GAAG,eAAe,CAAC;IAEzD;;;;;;;;;OASG;IACI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAgC,SAAkB;QAC3E,MAAM,OAAO,GAAG,MAAM,UAAU,EAAE,CAAC;QACnC,MAAM,QAAQ,GAAG,SAAS,IAAI,IAAI,CAAC,eAAe,CAAC;QACnD,MAAM,YAAY,GAAG,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC;YAC7C,CAAC,CAAC,QAAQ;YACV,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QAEn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C,CAAC;YAC3C,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACZ,MAAM,IAAI,KAAK,CAAC,YAAY,CAAC,WAAW,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;YACzD,CAAC;QACF,CAAC,CAAC,CAAC;IACJ,CAAC"}
|
|
@@ -4,15 +4,30 @@ import { PlainTextTimersStore } from "../TimersStore/PlainTextTimersStore.js";
|
|
|
4
4
|
* PlainTextTimersManager
|
|
5
5
|
* @description
|
|
6
6
|
* Manages timers stored in a PlainText file.
|
|
7
|
-
* (This is a abstract class)
|
|
8
7
|
*
|
|
9
8
|
* - Timers are persisted in a file
|
|
10
9
|
* - Expired timers are detected by polling
|
|
11
10
|
*/
|
|
12
11
|
export declare class PlainTextTimersManager extends TimersManager<"PlainText", object> {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
static readonly defaultFilename = ".timers";
|
|
13
|
+
/**
|
|
14
|
+
* create
|
|
15
|
+
* @param {string | undefined} timerfile optional timer file path. If not provided, the default path will be used.
|
|
16
|
+
* @description Creates an instance of PlainTextTimersManager. If the timer file does not exist, an empty file is created.
|
|
17
|
+
* @throws If file access or creation fails
|
|
18
|
+
* @example
|
|
19
|
+
* const manager = await PlainTextTimersManager.create(); // Uses default timer file path
|
|
20
|
+
* const manager = await PlainTextTimersManager.create("/path/to/.timers"); // Uses specified timer file path
|
|
21
|
+
* @returns Promise resolving to an instance of PlainTextTimersManager
|
|
22
|
+
*/
|
|
23
|
+
static create(timerfile?: string): Promise<PlainTextTimersManager>;
|
|
24
|
+
/**
|
|
25
|
+
* constructor
|
|
26
|
+
* @param {string} timerfiledir resolved timer file path.
|
|
27
|
+
* @description Initializes the PlainTextTimersManager instance.
|
|
28
|
+
*/
|
|
29
|
+
protected constructor(timerfiledir: string);
|
|
30
|
+
protected TimersStore: PlainTextTimersStore;
|
|
16
31
|
protected type: "PlainText";
|
|
17
32
|
}
|
|
18
33
|
//# sourceMappingURL=PlainTextTimersManager.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlainTextTimersManager.d.ts","sourceRoot":"","sources":["../../../src/TimersManager/PlainTextTimersManager.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"PlainTextTimersManager.d.ts","sourceRoot":"","sources":["../../../src/TimersManager/PlainTextTimersManager.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wCAAwC,CAAC;AAE9E;;;;;;;GAOG;AACH,qBAAa,sBAAuB,SAAQ,aAAa,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7E,gBAAuB,eAAe,aAAa;IAEnD;;;;;;;;;OASG;WACiB,MAAM,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,sBAAsB,CAAC;IAkB/E;;;;OAIG;IACH,SAAS,aAAa,YAAY,EAAE,MAAM;IAK1C,UAAmB,WAAW,EAAE,oBAAoB,CAAC;IAErD,UAAmB,IAAI,EAAE,WAAW,CAAwB;CAC5D"}
|
|
@@ -1,22 +1,56 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import fs from "fs/promises";
|
|
3
|
+
import searchRoot from "../searchRoot.js";
|
|
4
|
+
import { throwMessage } from "../throwMessage.js";
|
|
1
5
|
import { TimersManager } from "./TimersManager.js";
|
|
2
6
|
import { PlainTextTimersStore } from "../TimersStore/PlainTextTimersStore.js";
|
|
3
7
|
/**
|
|
4
8
|
* PlainTextTimersManager
|
|
5
9
|
* @description
|
|
6
10
|
* Manages timers stored in a PlainText file.
|
|
7
|
-
* (This is a abstract class)
|
|
8
11
|
*
|
|
9
12
|
* - Timers are persisted in a file
|
|
10
13
|
* - Expired timers are detected by polling
|
|
11
14
|
*/
|
|
12
15
|
export class PlainTextTimersManager extends TimersManager {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
static defaultFilename = ".timers";
|
|
17
|
+
/**
|
|
18
|
+
* create
|
|
19
|
+
* @param {string | undefined} timerfile optional timer file path. If not provided, the default path will be used.
|
|
20
|
+
* @description Creates an instance of PlainTextTimersManager. If the timer file does not exist, an empty file is created.
|
|
21
|
+
* @throws If file access or creation fails
|
|
22
|
+
* @example
|
|
23
|
+
* const manager = await PlainTextTimersManager.create(); // Uses default timer file path
|
|
24
|
+
* const manager = await PlainTextTimersManager.create("/path/to/.timers"); // Uses specified timer file path
|
|
25
|
+
* @returns Promise resolving to an instance of PlainTextTimersManager
|
|
26
|
+
*/
|
|
27
|
+
static async create(timerfile) {
|
|
28
|
+
const rootDir = await searchRoot();
|
|
29
|
+
const filename = timerfile ?? this.defaultFilename;
|
|
30
|
+
const timerfiledir = path.isAbsolute(filename)
|
|
31
|
+
? filename
|
|
32
|
+
: path.resolve(rootDir, filename);
|
|
33
|
+
if (!timerfiledir.startsWith(rootDir)) {
|
|
34
|
+
throw new Error(throwMessage.FilePathinvalid);
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
await fs.access(timerfiledir);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
await fs.writeFile(timerfiledir, "");
|
|
41
|
+
}
|
|
42
|
+
return new this(timerfiledir);
|
|
16
43
|
}
|
|
17
|
-
|
|
18
|
-
|
|
44
|
+
/**
|
|
45
|
+
* constructor
|
|
46
|
+
* @param {string} timerfiledir resolved timer file path.
|
|
47
|
+
* @description Initializes the PlainTextTimersManager instance.
|
|
48
|
+
*/
|
|
49
|
+
constructor(timerfiledir) {
|
|
50
|
+
super(timerfiledir);
|
|
51
|
+
this.TimersStore = new PlainTextTimersStore(this.timerfiledir);
|
|
19
52
|
}
|
|
53
|
+
TimersStore;
|
|
20
54
|
type = "PlainText";
|
|
21
55
|
}
|
|
22
56
|
//# sourceMappingURL=PlainTextTimersManager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlainTextTimersManager.js","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
|
|
1
|
+
{"version":3,"file":"PlainTextTimersManager.js","sourceRoot":"","sources":["../../../src/TimersManager/PlainTextTimersManager.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,MAAM,aAAa,CAAC;AAE7B,OAAO,UAAU,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wCAAwC,CAAC;AAE9E;;;;;;;GAOG;AACH,MAAM,OAAO,sBAAuB,SAAQ,aAAkC;IACtE,MAAM,CAAU,eAAe,GAAG,SAAS,CAAC;IAEnD;;;;;;;;;OASG;IACI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,SAAkB;QAC5C,MAAM,OAAO,GAAG,MAAM,UAAU,EAAE,CAAC;QACnC,MAAM,QAAQ,GAAG,SAAS,IAAI,IAAI,CAAC,eAAe,CAAC;QACnD,MAAM,YAAY,GAAG,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC;YAC7C,CAAC,CAAC,QAAQ;YACV,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QAEnC,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;YACvC,MAAM,IAAI,KAAK,CAAC,YAAY,CAAC,eAAe,CAAC,CAAC;QAC/C,CAAC;QACD,IAAI,CAAC;YACJ,MAAM,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;QAC/B,CAAC;QAAC,MAAM,CAAC;YACR,MAAM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