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.
Files changed (42) hide show
  1. package/README.md +26 -22
  2. package/dist/cjs/TimersManager/JSONLTimersManager.d.ts +22 -7
  3. package/dist/cjs/TimersManager/JSONLTimersManager.d.ts.map +1 -1
  4. package/dist/cjs/TimersManager/JSONLTimersManager.js +44 -9
  5. package/dist/cjs/TimersManager/JSONLTimersManager.js.map +1 -1
  6. package/dist/cjs/TimersManager/PlainTextTimersManager.d.ts +19 -4
  7. package/dist/cjs/TimersManager/PlainTextTimersManager.d.ts.map +1 -1
  8. package/dist/cjs/TimersManager/PlainTextTimersManager.js +43 -6
  9. package/dist/cjs/TimersManager/PlainTextTimersManager.js.map +1 -1
  10. package/dist/cjs/TimersManager/TimersManager.d.ts +25 -26
  11. package/dist/cjs/TimersManager/TimersManager.d.ts.map +1 -1
  12. package/dist/cjs/TimersManager/TimersManager.js +39 -50
  13. package/dist/cjs/TimersManager/TimersManager.js.map +1 -1
  14. package/dist/cjs/searchRoot.d.ts +1 -1
  15. package/dist/cjs/searchRoot.d.ts.map +1 -1
  16. package/dist/cjs/searchRoot.js +13 -7
  17. package/dist/cjs/searchRoot.js.map +1 -1
  18. package/dist/cjs/throwMessage.d.ts +1 -0
  19. package/dist/cjs/throwMessage.d.ts.map +1 -1
  20. package/dist/cjs/throwMessage.js +1 -0
  21. package/dist/cjs/throwMessage.js.map +1 -1
  22. package/dist/esm/TimersManager/JSONLTimersManager.d.ts +22 -7
  23. package/dist/esm/TimersManager/JSONLTimersManager.d.ts.map +1 -1
  24. package/dist/esm/TimersManager/JSONLTimersManager.js +41 -9
  25. package/dist/esm/TimersManager/JSONLTimersManager.js.map +1 -1
  26. package/dist/esm/TimersManager/PlainTextTimersManager.d.ts +19 -4
  27. package/dist/esm/TimersManager/PlainTextTimersManager.d.ts.map +1 -1
  28. package/dist/esm/TimersManager/PlainTextTimersManager.js +40 -6
  29. package/dist/esm/TimersManager/PlainTextTimersManager.js.map +1 -1
  30. package/dist/esm/TimersManager/TimersManager.d.ts +25 -26
  31. package/dist/esm/TimersManager/TimersManager.d.ts.map +1 -1
  32. package/dist/esm/TimersManager/TimersManager.js +39 -47
  33. package/dist/esm/TimersManager/TimersManager.js.map +1 -1
  34. package/dist/esm/searchRoot.d.ts +1 -1
  35. package/dist/esm/searchRoot.d.ts.map +1 -1
  36. package/dist/esm/searchRoot.js +13 -7
  37. package/dist/esm/searchRoot.js.map +1 -1
  38. package/dist/esm/throwMessage.d.ts +1 -0
  39. package/dist/esm/throwMessage.d.ts.map +1 -1
  40. package/dist/esm/throwMessage.js +1 -0
  41. package/dist/esm/throwMessage.js.map +1 -1
  42. package/package.json +1 -1
package/README.md CHANGED
@@ -31,14 +31,14 @@ import { JSONLTimersManager } from 'eternal-timer';
31
31
 
32
32
  async function main() {
33
33
  // By default, timers are stored in '.timers.jsonl' in the project root.
34
- const manager = new JSONLTimersManager();
34
+ const manager = await JSONLTimersManager.create();
35
35
 
36
36
  // Create a timer (5 seconds) with a title and description
37
37
  const timerId = await manager.createTimer({length: 5000, extra: { title: 'My Timer', description: 'This is a test timer.' }});
38
38
  console.log('Timer created:', timerId);
39
39
 
40
40
  // Start monitoring for expired timers
41
- const interval = await manager.checkStart();
41
+ await manager.checkStart();
42
42
 
43
43
  // Listen for 'expired' events
44
44
  manager.on('expired', (timer) => {
@@ -50,8 +50,8 @@ async function main() {
50
50
  console.log('Active timers:', timers);
51
51
 
52
52
  // To stop monitoring, for example after 10 seconds
53
- setTimeout(() => {
54
- clearInterval(interval);
53
+ setTimeout(async () => {
54
+ await manager.checkStop();
55
55
  console.log('Stopped monitoring timers.');
56
56
  }, 10000);
57
57
  }
@@ -68,14 +68,14 @@ import { PlainTextTimersManager } from 'eternal-timer';
68
68
 
69
69
  async function main() {
70
70
  // By default, timers are stored in '.timers' in the project root.
71
- const manager = new PlainTextTimersManager();
71
+ const manager = await PlainTextTimersManager.create();
72
72
 
73
73
  // Create a timer (5 seconds)
74
74
  const timerId = await manager.createTimer(5000);
75
75
  console.log('Timer created:', timerId);
76
76
 
77
77
  // Start monitoring for expired timers
78
- const interval = await manager.checkStart();
78
+ await manager.checkStart();
79
79
 
80
80
  // Listen for 'expired' events
81
81
  manager.on('expired', (timer) => {
@@ -87,8 +87,8 @@ async function main() {
87
87
  console.log('Active timers:', timers);
88
88
 
89
89
  // Stop monitoring after a while
90
- setTimeout(() => {
91
- clearInterval(interval);
90
+ setTimeout(async () => {
91
+ await manager.checkStop();
92
92
  console.log('Stopped monitoring for timers.');
93
93
  }, 10000);
94
94
  }
@@ -100,10 +100,13 @@ main();
100
100
 
101
101
  ### `JSONLTimersManager`
102
102
 
103
- #### `constructor(timerfiledir?: string)`
104
- Creates a manager for timers stored in **JSON Lines** format.
103
+ #### `static create<Extra extends object = object>(timerfile?: string): Promise<JSONLTimersManager<Extra>>`
104
+ Creates an instance of `JSONLTimersManager`.
105
105
 
106
- - **`timerfiledir`** (optional, string): Path to the timer file. Defaults to `.timers.jsonl` in the project root.
106
+ - **`timerfile`** (optional, string): Path to the timer file. Defaults to `.timers.jsonl` in the project root.
107
+
108
+ > [!NOTE]
109
+ > The constructor is `protected`. Please use this `create` method to instantiate the manager.
107
110
 
108
111
  #### `changeExtra(id: string, newExtra: Extra): Promise<void>`
109
112
  Changes the `extra` data of an existing timer.
@@ -131,8 +134,8 @@ interface MyTimerExtra {
131
134
  }
132
135
 
133
136
  async function main() {
134
- // Specify MyTimerExtra as the type argument for JSONLTimersManager
135
- const manager = new JSONLTimersManager<MyTimerExtra>();
137
+ // Specify MyTimerExtra as the type argument for JSONLTimersManager.create
138
+ const manager = await JSONLTimersManager.create<MyTimerExtra>();
136
139
 
137
140
  // Create a timer with custom extra data
138
141
  const timerId = await manager.createTimer({
@@ -159,10 +162,13 @@ main();
159
162
 
160
163
  ### `PlainTextTimersManager`
161
164
 
162
- #### `constructor(timerfiledir?: string)`
163
- Creates a manager for timers stored in **plain-text** format.
165
+ #### `static create(timerfile?: string): Promise<PlainTextTimersManager>`
166
+ Creates an instance of `PlainTextTimersManager`.
167
+
168
+ - **`timerfile`** (optional, string): Path to the timer file. Defaults to `.timers` in the project root.
164
169
 
165
- - **`timerfiledir`** (optional, string): Path to the timer file. Defaults to `.timers` in the project root.
170
+ > [!NOTE]
171
+ > The constructor is `protected`. Please use this `create` method to instantiate the manager.
166
172
 
167
173
  ---
168
174
 
@@ -187,17 +193,15 @@ Creates a new timer and saves it to the file.
187
193
  **Examples:**
188
194
 
189
195
  ```javascript
190
- // For PlainTextTimersManager with a custom Extra type
191
196
  import { PlainTextTimersManager } from 'eternal-timer';
192
197
 
193
198
  // For PlainTextTimersManager
194
- const manager = new PlainTextTimersManager();
199
+ const manager = await PlainTextTimersManager.create();
195
200
  const newTimerId = await manager.createTimer(5000); // Create a 5-second timer
196
201
  console.log('Created PlainText timer with ID:', newTimerId);
197
202
  ```
198
203
 
199
204
  ```typescript
200
- // For JSONLTimersManager with a custom Extra type
201
205
  import { JSONLTimersManager } from 'eternal-timer';
202
206
 
203
207
  interface MyCustomExtra {
@@ -205,7 +209,7 @@ interface MyCustomExtra {
205
209
  userId: string;
206
210
  }
207
211
 
208
- const jsonlManager = new JSONLTimersManager<MyCustomExtra>();
212
+ const jsonlManager = await JSONLTimersManager.create<MyCustomExtra>();
209
213
  const jsonlTimerId = await jsonlManager.createTimer({
210
214
  length: 10000, // 10 seconds
211
215
  extra: {
@@ -382,12 +386,12 @@ You can choose between two storage formats by selecting the appropriate manager
382
386
  ### 1. JSON Lines (via `JSONLTimersManager`)
383
387
  This is the recommended format for storing rich metadata.
384
388
 
385
- - **Pros**: Allows for storing `title` and `description`. Improved memory efficiency due to line-by-line file reading.
389
+ - **Pros**: Allows for storing extra properties(e.g. title and description). Improved memory efficiency due to line-by-line file reading.
386
390
  - **Cons**: Involves JSON parsing, which may have a minor performance overhead.
387
391
  - **Default File**: `.timers.jsonl`
388
392
  - **Format**:
389
393
  ```json
390
- {"id":"...","start":1678886400000,"stop":1678886405000,"title":"My Timer","description":"..."}
394
+ {"id":"...","start":1678886400000,"stop":1678886405000,{"extra": { "title":"My Timer","description":"..." } }}
391
395
  ```
392
396
 
393
397
  ### 2. Plain Text (via `PlainTextTimersManager`)
@@ -3,16 +3,31 @@ import { JSONLTimersStore } from "../TimersStore/JSONLTimersStore.js";
3
3
  /**
4
4
  * JSONLTimersManager
5
5
  * @description
6
- * Manages timers stored in a JSONL file.
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 = object> extends TimersManager<"JSONL", Extra> {
13
- protected TimersStore: JSONLTimersStore<Extra> | null;
14
- protected getDefaultFilename(): string;
15
- protected createTimersStore(): Promise<JSONLTimersStore<Extra>>;
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":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AAGtE;;;;;;;;GAQG;AACH,qBAAa,kBAAkB,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,aAAa,CAAC,OAAO,EAAE,KAAK,CAAC;IACnG,UAAmB,WAAW,EAAE,gBAAgB,CAAC,KAAK,CAAC,GAAG,IAAI,CAAQ;cAEnD,kBAAkB,IAAI,MAAM;cAItB,iBAAiB,IAAI,OAAO,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;IAI9E,UAAmB,IAAI,EAAE,OAAO,CAAoB;IAEpD;;;;;;;;;;;OAWG;IACU,WAAW,CACvB,EAAE,EAAE,MAAM,EACV,QAAQ,EAAE,KAAK,GACb,OAAO,CAAC,IAAI,CAAC;CAkBhB"}
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,26 +1,62 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.JSONLTimersManager = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const promises_1 = __importDefault(require("fs/promises"));
9
+ const searchRoot_js_1 = __importDefault(require("../searchRoot.js"));
4
10
  const TimersManager_js_1 = require("./TimersManager.js");
5
11
  const JSONLTimersStore_js_1 = require("../TimersStore/JSONLTimersStore.js");
6
12
  const throwMessage_js_1 = require("../throwMessage.js");
7
13
  /**
8
14
  * JSONLTimersManager
9
15
  * @description
10
- * Manages timers stored in a JSONL file.
11
- * (This is a abstract class)
16
+ * Manages timers stored in a JSONL file.
12
17
  *
13
18
  * - Timers are persisted in a file
14
19
  * - Expired timers are detected by polling
15
20
  */
16
21
  class JSONLTimersManager extends TimersManager_js_1.TimersManager {
17
- TimersStore = null;
18
- getDefaultFilename() {
19
- return ".timers.jsonl";
22
+ static defaultFilename = ".timers.jsonl";
23
+ /**
24
+ * create
25
+ * @param {string | undefined} timerfile optional timer file path. If not provided, the default path will be used.
26
+ * @description Creates an instance of JSONLTimersManager. If the timer file does not exist, an empty file is created.
27
+ * @throws If file access or creation fails
28
+ * @example
29
+ * const manager = await JSONLTimersManager.create(); // Uses default timer file path
30
+ * const manager = await JSONLTimersManager.create("/path/to/timers.jsonl"); // Uses specified timer file path
31
+ * @returns Promise resolving to an instance of JSONLTimersManager
32
+ */
33
+ static async create(timerfile) {
34
+ const rootDir = await (0, searchRoot_js_1.default)();
35
+ const filename = timerfile ?? this.defaultFilename;
36
+ const timerfiledir = path_1.default.isAbsolute(filename)
37
+ ? filename
38
+ : path_1.default.resolve(rootDir, filename);
39
+ if (!timerfiledir.startsWith(rootDir)) {
40
+ throw new Error(throwMessage_js_1.throwMessage.FilePathinvalid);
41
+ }
42
+ try {
43
+ await promises_1.default.access(timerfiledir);
44
+ }
45
+ catch {
46
+ await promises_1.default.writeFile(timerfiledir, "");
47
+ }
48
+ return new this(timerfiledir);
20
49
  }
21
- async createTimersStore() {
22
- return new JSONLTimersStore_js_1.JSONLTimersStore(this.timerfiledir);
50
+ /**
51
+ * constructor
52
+ * @param {string} timerfiledir resolved timer file path.
53
+ * @description Initializes the JSONLTimersManager instance.
54
+ */
55
+ constructor(timerfiledir) {
56
+ super(timerfiledir);
57
+ this.TimersStore = new JSONLTimersStore_js_1.JSONLTimersStore(this.timerfiledir);
23
58
  }
59
+ TimersStore;
24
60
  type = "JSONL";
25
61
  /**
26
62
  * changeExtra
@@ -31,12 +67,11 @@ class JSONLTimersManager extends TimersManager_js_1.TimersManager {
31
67
  * @throws If timer with id not found or file operation fails
32
68
  * @example
33
69
  * const timer = await manager.createTimer({ length: 1000, extra: {author: "someone"} });
34
- * await changeExtra(timer, {author: "SUKEsann2000"});
70
+ * await manager.changeExtra(timer, {author: "SUKEsann2000"});
35
71
  * // extra is changed and author will be "SUKEsann2000" instead of "someone"
36
72
  */
37
73
  async changeExtra(id, newExtra) {
38
74
  return this.runExclusive(async () => {
39
- this.TimersStore ??= await this.createTimersStore();
40
75
  try {
41
76
  const timers = await this.TimersStore.loadTimers();
42
77
  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,yDAAmD;AACnD,4EAAsE;AACtE,wDAAkD;AAElD;;;;;;;;GAQG;AACH,MAAa,kBAAkD,SAAQ,gCAA6B;IAChF,WAAW,GAAmC,IAAI,CAAC;IAEnD,kBAAkB;QACpC,OAAO,eAAe,CAAC;IACxB,CAAC;IAEkB,KAAK,CAAC,iBAAiB;QACzC,OAAO,IAAI,sCAAgB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAChD,CAAC;IAEkB,IAAI,GAAY,OAAgB,CAAC;IAEpD;;;;;;;;;;;OAWG;IACI,KAAK,CAAC,WAAW,CACvB,EAAU,EACV,QAAe;QAEf,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;gBAEnD,MAAM,KAAK,GAAG,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;gBAClD,IAAI,KAAK,KAAK,CAAC,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,SAAS,EAAE,CAAC;oBACjD,MAAM,IAAI,KAAK,CAAC,8BAAY,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;gBAC5C,CAAC;gBAED,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,GAAG,QAAQ,CAAC;gBAC/B,MAAM,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;YAC3C,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACZ,MAAM,IAAI,KAAK,CA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AAI,CAAC,WAAW,GAAG,IAAI,sCAAgB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAC5D,CAAC;IAEkB,WAAW,CAA0B;IAErC,IAAI,GAAY,OAAgB,CAAC;IAEpD;;;;;;;;;;;OAWG;IACI,KAAK,CAAC,WAAW,CACvB,EAAU,EACV,QAAe;QAEf,OAAO,IAAI,CAAC,YAAY,CAAC,KAAK,IAAI,EAAE;YACnC,IAAI,CAAC;gBACJ,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,CAAC;gBAEnD,MAAM,KAAK,GAAG,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;gBAClD,IAAI,KAAK,KAAK,CAAC,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,SAAS,EAAE,CAAC;oBACjD,MAAM,IAAI,KAAK,CAAC,8BAAY,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;gBAC5C,CAAC;gBAED,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,GAAG,QAAQ,CAAC;gBAC/B,MAAM,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;YAC3C,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACZ,MAAM,IAAI,KAAK,CAAC,8BAAY,CAAC,WAAW,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;YACzD,CAAC;QACF,CAAC,CAAC,CAAC;IACJ,CAAC;;AA5EF,gDA6EC"}
@@ -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
- protected TimersStore: PlainTextTimersStore | null;
14
- protected getDefaultFilename(): string;
15
- protected createTimersStore(): Promise<PlainTextTimersStore>;
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":"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,EAAE,MAAM,CAAC;IAC7E,UAAmB,WAAW,EAAE,oBAAoB,GAAG,IAAI,CAAQ;cAEhD,kBAAkB,IAAI,MAAM;cAI/B,iBAAiB,IAAI,OAAO,CAAC,oBAAoB,CAAC;IAIlE,UAAmB,IAAI,EAAE,WAAW,CAAwB;CAC5D"}
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,25 +1,62 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.PlainTextTimersManager = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const promises_1 = __importDefault(require("fs/promises"));
9
+ const searchRoot_js_1 = __importDefault(require("../searchRoot.js"));
10
+ const throwMessage_js_1 = require("../throwMessage.js");
4
11
  const TimersManager_js_1 = require("./TimersManager.js");
5
12
  const PlainTextTimersStore_js_1 = require("../TimersStore/PlainTextTimersStore.js");
6
13
  /**
7
14
  * PlainTextTimersManager
8
15
  * @description
9
16
  * Manages timers stored in a PlainText file.
10
- * (This is a abstract class)
11
17
  *
12
18
  * - Timers are persisted in a file
13
19
  * - Expired timers are detected by polling
14
20
  */
15
21
  class PlainTextTimersManager extends TimersManager_js_1.TimersManager {
16
- TimersStore = null;
17
- getDefaultFilename() {
18
- return ".timers";
22
+ static defaultFilename = ".timers";
23
+ /**
24
+ * create
25
+ * @param {string | undefined} timerfile optional timer file path. If not provided, the default path will be used.
26
+ * @description Creates an instance of PlainTextTimersManager. If the timer file does not exist, an empty file is created.
27
+ * @throws If file access or creation fails
28
+ * @example
29
+ * const manager = await PlainTextTimersManager.create(); // Uses default timer file path
30
+ * const manager = await PlainTextTimersManager.create("/path/to/.timers"); // Uses specified timer file path
31
+ * @returns Promise resolving to an instance of PlainTextTimersManager
32
+ */
33
+ static async create(timerfile) {
34
+ const rootDir = await (0, searchRoot_js_1.default)();
35
+ const filename = timerfile ?? this.defaultFilename;
36
+ const timerfiledir = path_1.default.isAbsolute(filename)
37
+ ? filename
38
+ : path_1.default.resolve(rootDir, filename);
39
+ if (!timerfiledir.startsWith(rootDir)) {
40
+ throw new Error(throwMessage_js_1.throwMessage.FilePathinvalid);
41
+ }
42
+ try {
43
+ await promises_1.default.access(timerfiledir);
44
+ }
45
+ catch {
46
+ await promises_1.default.writeFile(timerfiledir, "");
47
+ }
48
+ return new this(timerfiledir);
19
49
  }
20
- async createTimersStore() {
21
- return new PlainTextTimersStore_js_1.PlainTextTimersStore(this.timerfiledir);
50
+ /**
51
+ * constructor
52
+ * @param {string} timerfiledir resolved timer file path.
53
+ * @description Initializes the PlainTextTimersManager instance.
54
+ */
55
+ constructor(timerfiledir) {
56
+ super(timerfiledir);
57
+ this.TimersStore = new PlainTextTimersStore_js_1.PlainTextTimersStore(this.timerfiledir);
22
58
  }
59
+ TimersStore;
23
60
  type = "PlainText";
24
61
  }
25
62
  exports.PlainTextTimersManager = PlainTextTimersManager;
@@ -1 +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,gCAAkC;IAC1D,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;IAEkB,IAAI,G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W,CAAuB;IAElC,IAAI,GAAgB,WAAoB,CAAC;;AA3C7D,wDA4CC"}
@@ -13,24 +13,21 @@ import { EventEmitter } from "../EventEmitter.js";
13
13
  export declare abstract class TimersManager<T extends StorageType, Extra extends object> extends EventEmitter<T, Extra> {
14
14
  protected readonly timerfiledir: string;
15
15
  protected checkLock: boolean;
16
- protected interval: NodeJS.Timeout | undefined;
16
+ protected static intervals: Map<string, NodeJS.Timeout>;
17
17
  protected running: boolean;
18
- protected TimersStore: TimersStore<T, Extra> | null;
18
+ protected abstract TimersStore: TimersStore<T, Extra>;
19
19
  private queue;
20
20
  protected runExclusive<T>(fn: () => Promise<T>): Promise<T>;
21
- protected abstract getDefaultFilename(): string;
22
- protected abstract createTimersStore(): Promise<TimersStore<T, Extra>>;
23
21
  protected abstract type: T;
24
22
  /**
25
- * constructor
26
- * @description Initializes the TimersManager instance. If the timer file does not exist, an empty file is created.
27
- * @param {string} [timerfile] (string, optional) Configuration timer file path and it is treated as the timer file path.
28
- * @throws If file access or creation fails
29
- * @example
30
- * const manager = new TimersManager(); // Uses default timer file path
31
- * const manager = new TimersManager("/path/to/timers.txt"); // Uses specified timer file path
32
- */
33
- constructor(timerfile?: string);
23
+ * constructor
24
+ * @param {string} timerfile timer file path.
25
+ * @description Initializes the TimersManager instance.
26
+ * @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.
27
+ * @example
28
+ * const manager = new TimersManager("/path/to/timers"); // Uses specified timer file path
29
+ */
30
+ protected constructor(timerfile: string);
34
31
  /**
35
32
  * createTimer
36
33
  * @description Creates a new timer.
@@ -39,11 +36,11 @@ export declare abstract class TimersManager<T extends StorageType, Extra extends
39
36
  * @throws If length is invalid (e.g. length < 0) or file operation fails
40
37
  * @example
41
38
  * // For PlainTextTimersManager
42
- * const manager = new PlainTextTimersManager();
39
+ * const manager = await PlainTextTimersManager.create();
43
40
  * const newTimerId = await manager.createTimer(5000); // Create a 5-second timer
44
41
  *
45
42
  * // For JSONLTimersManager
46
- * const jsonlManager = new JSONLTimersManager<{ title: string }>();
43
+ * const jsonlManager = await JSONLTimersManager.create<{ title: string }>();
47
44
  * const jsonlTimerId = await jsonlManager.createTimer({ length: 10000, extra: { title: "My JSONL Timer" } }); // Create a 10-second timer with extra data
48
45
  */
49
46
  createTimer(options: CreateTimerOptions<T, Extra>): Promise<string>;
@@ -64,7 +61,6 @@ export declare abstract class TimersManager<T extends StorageType, Extra extends
64
61
  * @returns Promise<void> that resolves when the loop has been started
65
62
  * @throws If file operation fails during checking
66
63
  * @example
67
- * const manager = new TimersManager();
68
64
  * await manager.checkStart(1000); // Check for expired timers every 1 second
69
65
  */
70
66
  checkStart(interval?: number): Promise<void>;
@@ -73,7 +69,6 @@ export declare abstract class TimersManager<T extends StorageType, Extra extends
73
69
  * @description Stops the timer checking loop.
74
70
  * @returns Promise resolving when the loop has been stopped
75
71
  * @example
76
- * const manager = new TimersManager();
77
72
  * await manager.checkStart(1000);
78
73
  * // ... later, to stop checking:
79
74
  * await manager.checkStop();
@@ -90,22 +85,26 @@ export declare abstract class TimersManager<T extends StorageType, Extra extends
90
85
  */
91
86
  get isBusy(): boolean;
92
87
  /**
93
- * showTimers
94
- * @description Retrieves all active timers.
95
- * @returns Array of `Timer` objects
96
- * @throws If file operation fails
97
- * @example
98
- * const timers = await manager.showTimers();
99
- * console.log(JSON.stringify(timers))
100
- */
88
+ * showTimers
89
+ * @description Retrieves all active timers.
90
+ * @returns Array of `Timer` objects
91
+ * @throws If file operation fails
92
+ * @example
93
+ * const timers = await manager.showTimers();
94
+ * console.log(JSON.stringify(timers));
95
+ */
101
96
  showTimers(): Promise<Timer<T, Extra>[]>;
102
97
  /**
103
- * adjustRemainingTime
98
+ * adjustRemainingTime
104
99
  * @description Adjusts the remaining time of a timer.
105
100
  * @param {string} id ID of the timer to modify
106
101
  * @param {number} delay Delay in milliseconds to add/subtract from the remaining time
107
102
  * @returns Promise resolving when the operation is complete
108
103
  * @throws If file operation fails
104
+ * @example
105
+ * const timer = await manager.createTimer(10000); // Create a 10-second timer
106
+ * await manager.adjustRemainingTime(timer, -2000); // Subtract 2 seconds from the remaining time (now 8 seconds left)
107
+ * await manager.adjustRemainingTime(timer, 3000); // Add 3 seconds to the remaining time (now 11 seconds left)
109
108
  */
110
109
  adjustRemainingTime(id: string, delay: number): Promise<void>;
111
110
  }
@@ -1 +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;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAGlD;;;;;;;;GAQG;AACH,8BAAsB,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,KAAK,SAAS,MAAM,CAAE,SAAQ,YAAY,CAAC,CAAC,EAAE,KAAK,CAAC;IAC9G,SAAS,CAAC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IACxC,SAAS,CAAC,SAAS,EAAE,OAAO,CAAS;IAErC,SAAS,CAAC,QAAQ,EAAE,MAAM,CAAC,OAAO,GAAG,SAAS,CAAC;IAC/C,SAAS,CAAC,OAAO,EAAE,OAAO,CAAS;IAEnC,SAAS,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC,EAAE,KAAK,CAAC,GAAG,IAAI,CAAQ;IAE3D,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,EAAE,KAAK,CAAC,CAAC;IACtE,S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