eternal-timer 2.3.0 → 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.
Files changed (86) hide show
  1. package/README.md +25 -13
  2. package/dist/cjs/TimersManager/JSONLTimersManager.cjs +25 -0
  3. package/dist/cjs/TimersManager/JSONLTimersManager.cjs.map +1 -0
  4. package/dist/cjs/TimersManager/JSONLTimersManager.d.ts +18 -0
  5. package/dist/cjs/TimersManager/JSONLTimersManager.d.ts.map +1 -0
  6. package/dist/cjs/TimersManager/PlainTextTimersManager.cjs +25 -0
  7. package/dist/cjs/TimersManager/PlainTextTimersManager.cjs.map +1 -0
  8. package/dist/cjs/TimersManager/PlainTextTimersManager.d.ts +18 -0
  9. package/dist/cjs/TimersManager/PlainTextTimersManager.d.ts.map +1 -0
  10. package/dist/cjs/TimersManager/TimersManager.cjs +211 -0
  11. package/dist/cjs/TimersManager/TimersManager.cjs.map +1 -0
  12. package/dist/cjs/TimersManager/TimersManager.d.ts +87 -0
  13. package/dist/cjs/TimersManager/TimersManager.d.ts.map +1 -0
  14. package/dist/cjs/TimersStore/JSONLTimersStore.cjs +50 -0
  15. package/dist/cjs/TimersStore/JSONLTimersStore.cjs.map +1 -0
  16. package/dist/cjs/TimersStore/JSONLTimersStore.d.ts +16 -0
  17. package/dist/cjs/TimersStore/JSONLTimersStore.d.ts.map +1 -0
  18. package/dist/cjs/TimersStore/PlainTextTimersStore.cjs +56 -0
  19. package/dist/cjs/TimersStore/PlainTextTimersStore.cjs.map +1 -0
  20. package/dist/cjs/TimersStore/PlainTextTimersStore.d.ts +16 -0
  21. package/dist/cjs/TimersStore/PlainTextTimersStore.d.ts.map +1 -0
  22. package/dist/cjs/TimersStore/TimersStore.cjs +44 -0
  23. package/dist/cjs/TimersStore/TimersStore.cjs.map +1 -0
  24. package/dist/cjs/TimersStore/TimersStore.d.ts +12 -0
  25. package/dist/cjs/TimersStore/TimersStore.d.ts.map +1 -0
  26. package/dist/cjs/index.cjs +2 -2
  27. package/dist/cjs/index.cjs.map +1 -1
  28. package/dist/cjs/index.d.ts +2 -2
  29. package/dist/cjs/index.d.ts.map +1 -1
  30. package/dist/cjs/types.d.ts +6 -3
  31. package/dist/cjs/types.d.ts.map +1 -1
  32. package/dist/esm/TimersManager/JSONLTimersManager.d.ts +18 -0
  33. package/dist/esm/TimersManager/JSONLTimersManager.d.ts.map +1 -0
  34. package/dist/esm/TimersManager/JSONLTimersManager.js +21 -0
  35. package/dist/esm/TimersManager/JSONLTimersManager.js.map +1 -0
  36. package/dist/esm/TimersManager/PlainTextTimersManager.d.ts +18 -0
  37. package/dist/esm/TimersManager/PlainTextTimersManager.d.ts.map +1 -0
  38. package/dist/esm/TimersManager/PlainTextTimersManager.js +21 -0
  39. package/dist/esm/TimersManager/PlainTextTimersManager.js.map +1 -0
  40. package/dist/esm/TimersManager/TimersManager.d.ts +87 -0
  41. package/dist/esm/TimersManager/TimersManager.d.ts.map +1 -0
  42. package/dist/esm/TimersManager/TimersManager.js +205 -0
  43. package/dist/esm/TimersManager/TimersManager.js.map +1 -0
  44. package/dist/esm/TimersStore/JSONLTimersStore.d.ts +16 -0
  45. package/dist/esm/TimersStore/JSONLTimersStore.d.ts.map +1 -0
  46. package/dist/esm/TimersStore/JSONLTimersStore.js +46 -0
  47. package/dist/esm/TimersStore/JSONLTimersStore.js.map +1 -0
  48. package/dist/esm/TimersStore/PlainTextTimersStore.d.ts +16 -0
  49. package/dist/esm/TimersStore/PlainTextTimersStore.d.ts.map +1 -0
  50. package/dist/esm/TimersStore/PlainTextTimersStore.js +52 -0
  51. package/dist/esm/TimersStore/PlainTextTimersStore.js.map +1 -0
  52. package/dist/esm/TimersStore/TimersStore.d.ts +12 -0
  53. package/dist/esm/TimersStore/TimersStore.d.ts.map +1 -0
  54. package/dist/esm/TimersStore/TimersStore.js +37 -0
  55. package/dist/esm/TimersStore/TimersStore.js.map +1 -0
  56. package/dist/esm/index.d.ts +2 -2
  57. package/dist/esm/index.d.ts.map +1 -1
  58. package/dist/esm/index.js +2 -2
  59. package/dist/esm/index.js.map +1 -1
  60. package/dist/esm/types.d.ts +6 -3
  61. package/dist/esm/types.d.ts.map +1 -1
  62. package/package.json +1 -1
  63. package/dist/cjs/JSONLTimersManager.cjs +0 -199
  64. package/dist/cjs/JSONLTimersManager.cjs.map +0 -1
  65. package/dist/cjs/JSONLTimersManager.d.ts +0 -63
  66. package/dist/cjs/JSONLTimersManager.d.ts.map +0 -1
  67. package/dist/cjs/PlainTextTimersManager.cjs +0 -200
  68. package/dist/cjs/PlainTextTimersManager.cjs.map +0 -1
  69. package/dist/cjs/PlainTextTimersManager.d.ts +0 -60
  70. package/dist/cjs/PlainTextTimersManager.d.ts.map +0 -1
  71. package/dist/cjs/TimersManager.cjs +0 -39
  72. package/dist/cjs/TimersManager.cjs.map +0 -1
  73. package/dist/cjs/TimersManager.d.ts +0 -75
  74. package/dist/cjs/TimersManager.d.ts.map +0 -1
  75. package/dist/esm/JSONLTimersManager.d.ts +0 -63
  76. package/dist/esm/JSONLTimersManager.d.ts.map +0 -1
  77. package/dist/esm/JSONLTimersManager.js +0 -192
  78. package/dist/esm/JSONLTimersManager.js.map +0 -1
  79. package/dist/esm/PlainTextTimersManager.d.ts +0 -60
  80. package/dist/esm/PlainTextTimersManager.d.ts.map +0 -1
  81. package/dist/esm/PlainTextTimersManager.js +0 -193
  82. package/dist/esm/PlainTextTimersManager.js.map +0 -1
  83. package/dist/esm/TimersManager.d.ts +0 -75
  84. package/dist/esm/TimersManager.d.ts.map +0 -1
  85. package/dist/esm/TimersManager.js +0 -32
  86. package/dist/esm/TimersManager.js.map +0 -1
package/README.md CHANGED
@@ -37,10 +37,8 @@ async function main() {
37
37
  console.log('Timer created:', timerId);
38
38
 
39
39
  // Monitor timers (executes when timer expires)
40
- const interval = manager.checkTimers(async (timer) => {
40
+ const interval = await manager.checkTimers(async (timer) => {
41
41
  console.log('Timer expired:', timer.id, timer.title);
42
- // Once the timer expires, you can remove it
43
- await manager.removeTimer(timer.id);
44
42
  });
45
43
 
46
44
  // Display all timers
@@ -73,9 +71,8 @@ async function main() {
73
71
  console.log('Timer created:', timerId);
74
72
 
75
73
  // Monitor timers
76
- const interval = manager.checkTimers(async (timer) => {
74
+ const interval = await manager.checkTimers(async (timer) => {
77
75
  console.log('Timer expired:', timer.id);
78
- await manager.removeTimer(timer.id);
79
76
  });
80
77
 
81
78
  // Display all timers
@@ -144,18 +141,22 @@ Removes a timer by its ID.
144
141
 
145
142
  **Throws:** An error if the timer with the specified ID is not found or if a file operation fails.
146
143
 
147
- ### `checkTimers(callback: (timer: Timer) => Promise<void>, interval?: number): NodeJS.Timeout`
144
+ ### `checkTimers(callback: (timer: Timer) => void | Promise<void>, interval?: number): Promise<NodeJS.Timeout>`
148
145
 
149
- Starts monitoring for expired timers at a specified interval. This function runs asynchronously and will not block execution.
146
+ Starts monitoring timers at the specified interval and invokes the provided
147
+ `callback` when a timer expires.
150
148
 
151
- When an expired timer is found, the provided `callback` function is invoked with the `timer` object. The function waits for the `callback`'s `Promise` to resolve before proceeding to the next check, preventing race conditions. Error handling has been added within the `callback` to prevent the application from crashing if an error occurs during timer processing.
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`.
152
152
 
153
- - **`callback`**: An async function that is called with the expired `timer` object.
154
- - **`interval`** (optional, number): The interval in milliseconds to check for expired timers. Defaults to `200ms`.
153
+ - **callback**: An async function invoked with the expired `timer`.
154
+ - **interval** *(optional, number)*: Interval in milliseconds for checking timers.
155
+ Defaults to `200`.
155
156
 
156
- **Returns:** A `NodeJS.Timeout` object that can be used with `clearInterval()` to stop monitoring.
157
+ **Returns:** A `NodeJS.Timeout` that can be passed to `clearInterval()` to stop monitoring.
157
158
 
158
- **Throws:** An error if a file operation fails during monitoring.
159
+ **Throws:** An error if a timer storage operation fails.
159
160
 
160
161
  ### `showTimers(): Promise<Timer[]>`
161
162
 
@@ -165,6 +166,17 @@ Retrieves all active timers.
165
166
 
166
167
  **Throws:** An error if a file operation fails.
167
168
 
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.
179
+
168
180
  ## Type Definition
169
181
 
170
182
  The `StorageType` has the following structure:
@@ -181,7 +193,7 @@ type Timer<T extends StorageType> = {
181
193
  stop: number;
182
194
  } & (T extends "JSONL"
183
195
  ? { title?: string; description?: string }
184
- : object);
196
+ : {});
185
197
  ```
186
198
 
187
199
  ## Scripts
@@ -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/Timers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QACzD,OAAO,IAAI,CAAC,YAAY,CAAC,KAAK,IAAI,EAAE;YACnC,IAAI,CAAC,WAAW,KAAK,MAAM,IAAI,CAAC,iBAAiB,EAAE,CAAC;YACpD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,CAAC;YAEnD,MAAM,KAAK,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;YACjD,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;gBAClB,MAAM,IAAI,KAAK,CAAC,iBAAiB,EAAE,YAAY,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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TimersManager.d.ts","sourceRoot":"","sources":["../../../src/TimersManager/TimersManager.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,kBAAkB,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAC1E,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAG5D;;;;;;;;GAQG;AACH,8BAAsB,aAAa,CAAC,CAAC,SAAS,WAAW;IACxD,SAAS,CAAC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IACxC,OAAO,CAAC,SAAS,CAAkB;IAEnC,SAAS,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,IAAI,CAAQ;IAEpD,OAAO,CAAC,KAAK,CAAoC;IACjD,OAAO,CAAC,YAAY;IAMpB,SAAS,CAAC,QAAQ,CAAC,kBAAkB,IAAI,MAAM;IAC/C,SAAS,CAAC,QAAQ,CAAC,iBAAiB,IAAI,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IAE/D;;;;;;;;QAQO;gBAEN,OAAO,CAAC,EAAE,MAAM;IAcjB;;;;;;;;;;OAUM;IACO,WAAW,CAAC,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;IA0BzE;;;;;;;;OAQM;IACO,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBnD;;;;;;;;;;;;;OAaM;IACO,WAAW,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,EAAE,QAAQ,GAAE,MAAY,GAAG,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC;IAuD9H;;;;;;;;OAQM;IACO,UAAU,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;IAQ9C;;;;;;;QAOO;IACM,mBAAmB,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAsB1E"}
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JSONLTimersStore = void 0;
4
+ const uuid_1 = require("uuid");
5
+ const TimersStore_js_1 = require("./TimersStore.cjs");
6
+ class JSONLTimersStore extends TimersStore_js_1.TimersStore {
7
+ constructor(timerfile) {
8
+ super(timerfile);
9
+ }
10
+ /**
11
+ * checkTimerfileSyntax
12
+ * @description Checks the syntax of the timer file.
13
+ * @param timers Array of timer objects to check
14
+ * @returns void
15
+ * @throws If syntax is invalid
16
+ */
17
+ async checkTimerfileSyntax(timers) {
18
+ const throwing = () => {
19
+ throw new Error(`Timer file's syntax is wrong`);
20
+ };
21
+ for (const timer of timers) {
22
+ if (!timer.id || !(0, uuid_1.validate)(timer.id))
23
+ throwing();
24
+ if (!timer.start || typeof timer.start !== "number")
25
+ throwing();
26
+ if (!timer.stop || typeof timer.stop !== "number")
27
+ throwing();
28
+ if (timer.start > timer.stop)
29
+ throwing();
30
+ if (timer.title && typeof timer.title !== "string")
31
+ throwing();
32
+ if (timer.description && typeof timer.description !== "string")
33
+ throwing();
34
+ }
35
+ }
36
+ toStringifyTimers(timers) {
37
+ if (timers.length === 0) {
38
+ return "";
39
+ }
40
+ return timers.map(t => JSON.stringify(t)).join("\n");
41
+ }
42
+ parseTimers(data) {
43
+ return data
44
+ .split(/\r?\n/)
45
+ .filter((line) => line.trim())
46
+ .map((line) => JSON.parse(line));
47
+ }
48
+ }
49
+ exports.JSONLTimersStore = JSONLTimersStore;
50
+ //# sourceMappingURL=JSONLTimersStore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JSONLTimersStore.js","sourceRoot":"","sources":["../../../src/TimersStore/JSONLTimersStore.ts"],"names":[],"mappings":";;;AAAA,+BAAgC;AAGhC,qDAA+C;AAE/C,MAAa,gBAAiB,SAAQ,4BAAoB;IAEzD,YACC,SAAiB;QAEjB,KAAK,CAAC,SAAS,CAAC,CAAC;IAClB,CAAC;IAED;;;;;;OAMG;IACgB,KAAK,CAAC,oBAAoB,CAAC,MAAwB;QACrE,MAAM,QAAQ,GAAG,GAAG,EAAE;YACrB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;QACjD,CAAC,CAAC;QACF,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC5B,IAAI,CAAC,KAAK,CAAC,EAAE,IAAI,CAAC,IAAA,eAAQ,EAAC,KAAK,CAAC,EAAE,CAAC;gBAAE,QAAQ,EAAE,CAAC;YACjD,IAAI,CAAC,KAAK,CAAC,KAAK,IAAI,OAAO,KAAK,CAAC,KAAK,KAAK,QAAQ;gBAAE,QAAQ,EAAE,CAAC;YAChE,IAAI,CAAC,KAAK,CAAC,IAAI,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ;gBAAE,QAAQ,EAAE,CAAC;YAC9D,IAAI,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI;gBAAE,QAAQ,EAAE,CAAC;YACzC,IAAI,KAAK,CAAC,KAAK,IAAI,OAAO,KAAK,CAAC,KAAK,KAAK,QAAQ;gBAAE,QAAQ,EAAE,CAAC;YAC/D,IAAI,KAAK,CAAC,WAAW,IAAI,OAAO,KAAK,CAAC,WAAW,KAAK,QAAQ;gBAAE,QAAQ,EAAE,CAAC;QAC5E,CAAC;IACF,CAAC;IAEe,iBAAiB,CAAC,MAAwB;QACzD,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACzB,OAAO,EAAE,CAAC;QACX,CAAC;QACD,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACtD,CAAC;IAEe,WAAW,CAAC,IAAY;QACvC,OAAO,IAAI;aACT,KAAK,CAAC,OAAO,CAAC;aACd,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;aAC7B,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAmB,CAAC,CAAC;IACrD,CAAC;CACD;AA1CD,4CA0CC"}
@@ -0,0 +1,16 @@
1
+ import type { Timer } from "../types.js";
2
+ import { TimersStore } from "./TimersStore.js";
3
+ export declare class JSONLTimersStore extends TimersStore<"JSONL"> {
4
+ constructor(timerfile: string);
5
+ /**
6
+ * checkTimerfileSyntax
7
+ * @description Checks the syntax of the timer file.
8
+ * @param timers Array of timer objects to check
9
+ * @returns void
10
+ * @throws If syntax is invalid
11
+ */
12
+ protected checkTimerfileSyntax(timers: Timer<"JSONL">[]): Promise<void>;
13
+ toStringifyTimers(timers: Timer<"JSONL">[]): string;
14
+ parseTimers(data: string): Timer<"JSONL">[];
15
+ }
16
+ //# sourceMappingURL=JSONLTimersStore.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JSONLTimersStore.d.ts","sourceRoot":"","sources":["../../../src/TimersStore/JSONLTimersStore.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,qBAAa,gBAAiB,SAAQ,WAAW,CAAC,OAAO,CAAC;gBAGxD,SAAS,EAAE,MAAM;IAKlB;;;;;;OAMG;cACsB,oBAAoB,CAAC,MAAM,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IActE,iBAAiB,CAAC,MAAM,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE,GAAG,MAAM;IAOnD,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,EAAE;CAM3D"}
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PlainTextTimersStore = void 0;
4
+ const uuid_1 = require("uuid");
5
+ const TimersStore_js_1 = require("./TimersStore.cjs");
6
+ class PlainTextTimersStore extends TimersStore_js_1.TimersStore {
7
+ constructor(timerfile) {
8
+ super(timerfile);
9
+ }
10
+ /**
11
+ * checkTimerfileSyntax
12
+ * @description Checks the syntax of the timer file.
13
+ * @param fileData
14
+ * @returns void
15
+ * @throws If syntax is invalid
16
+ */
17
+ async checkTimerfileSyntax(timers) {
18
+ const throwing = () => {
19
+ throw new Error(`Timer file's syntax is wrong`);
20
+ };
21
+ for (const timer of timers) {
22
+ if (Object.keys(timer).length !== 3)
23
+ throwing();
24
+ if (!(0, uuid_1.validate)(timer.id))
25
+ throwing();
26
+ if (isNaN(Number(timer.start)))
27
+ throwing();
28
+ if (isNaN(Number(timer.stop)))
29
+ throwing();
30
+ if (Number(timer.start) > Number(timer.stop))
31
+ throwing();
32
+ }
33
+ return;
34
+ }
35
+ toStringifyTimers(timers) {
36
+ if (timers.length === 0) {
37
+ return "";
38
+ }
39
+ return timers.map(timer => `${timer.id} ${timer.start} ${timer.stop}`).join("\n");
40
+ }
41
+ parseTimers(data) {
42
+ return data
43
+ .split(/\r?\n/)
44
+ .filter((line) => line.trim())
45
+ .map((line) => {
46
+ const [id, startStr, stopStr] = line.split(" ");
47
+ return {
48
+ id: id,
49
+ start: Number(startStr),
50
+ stop: Number(stopStr),
51
+ };
52
+ });
53
+ }
54
+ }
55
+ exports.PlainTextTimersStore = PlainTextTimersStore;
56
+ //# sourceMappingURL=PlainTextTimersStore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PlainTextTimersStore.js","sourceRoot":"","sources":["../../../src/TimersStore/PlainTextTimersStore.ts"],"names":[],"mappings":";;;AAAA,+BAAgC;AAGhC,qDAA+C;AAE/C,MAAa,oBAAqB,SAAQ,4BAAwB;IAEjE,YACC,SAAiB;QAEjB,KAAK,CAAC,SAAS,CAAC,CAAC;IAClB,CAAC;IAED;;;;;;OAMM;IACa,KAAK,CAAC,oBAAoB,CAAC,MAA4B;QACzE,MAAM,QAAQ,GAAG,GAAG,EAAE;YACrB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;QACjD,CAAC,CAAC;QACF,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC5B,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,KAAK,CAAC;gBAAE,QAAQ,EAAE,CAAC;YAChD,IAAI,CAAC,IAAA,eAAQ,EAAC,KAAK,CAAC,EAAE,CAAC;gBAAE,QAAQ,EAAE,CAAC;YACpC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;gBAAE,QAAQ,EAAE,CAAC;YAC3C,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;gBAAE,QAAQ,EAAE,CAAC;YAC1C,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;gBAAE,QAAQ,EAAE,CAAC;QAC1D,CAAC;QACD,OAAO;IACR,CAAC;IAEe,iBAAiB,CAAC,MAA4B;QAC7D,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACzB,OAAO,EAAE,CAAC;QACX,CAAC;QACD,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,EAAE,IAAI,KAAK,CAAC,KAAK,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACnF,CAAC;IAEe,WAAW,CAAC,IAAY;QACvC,OAAO,IAAI;aACT,KAAK,CAAC,OAAO,CAAC;aACd,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;aAC7B,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;YACb,MAAM,CAAC,EAAE,EAAE,QAAQ,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YAChD,OAAO;gBACN,EAAE,EAAE,EAAG;gBACP,KAAK,EAAE,MAAM,CAAC,QAAS,CAAC;gBACxB,IAAI,EAAE,MAAM,CAAC,OAAQ,CAAC;aACA,CAAC;QACzB,CAAC,CAAC,CAAC;IACL,CAAC;CACD;AAjDD,oDAiDC"}
@@ -0,0 +1,16 @@
1
+ import type { Timer } from "../types.js";
2
+ import { TimersStore } from "./TimersStore.js";
3
+ export declare class PlainTextTimersStore extends TimersStore<"PlainText"> {
4
+ constructor(timerfile: string);
5
+ /**
6
+ * checkTimerfileSyntax
7
+ * @description Checks the syntax of the timer file.
8
+ * @param fileData
9
+ * @returns void
10
+ * @throws If syntax is invalid
11
+ */
12
+ protected checkTimerfileSyntax(timers: Timer<"PlainText">[]): Promise<void>;
13
+ toStringifyTimers(timers: Timer<"PlainText">[]): string;
14
+ parseTimers(data: string): Timer<"PlainText">[];
15
+ }
16
+ //# sourceMappingURL=PlainTextTimersStore.d.ts.map