@tmlmobilidade/utils 20260411.1248.30 → 20260418.1237.24

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.
@@ -1,6 +1,21 @@
1
+ import { type TimeSlot } from '@tmlmobilidade/dates';
2
+ interface RunOnIntervalOptions {
3
+ /**
4
+ * Interval in milliseconds between the end of one invocation
5
+ * and the start of the next. The first invocation happens immediately.
6
+ * @required
7
+ */
8
+ intervalMs: number | TimeSlot;
9
+ /**
10
+ * Whether to throw errors after logging them.
11
+ * If true, errors are logged and then re-thrown, stopping code execution.
12
+ * @default false
13
+ */
14
+ throwOnError?: boolean;
15
+ }
1
16
  /**
2
- * Runs a function immediately, then repeatedly on a fixed interval.
3
- * Errors are caught and logged per-invocation they do not halt scheduling.
4
- * Each invocation is fire-and-forget after the first awaited call.
17
+ * Runs an asynchronous function at regular intervals, ensuring that each invocation
18
+ * completes before the next one starts. Errors are logged and can optionally be re-thrown.
5
19
  */
6
- export declare function runOnInterval(fn: () => Promise<void>, intervalMs: number): Promise<void>;
20
+ export declare function runOnInterval(fn: () => Promise<void>, options: RunOnIntervalOptions): Promise<void>;
21
+ export {};
@@ -1,16 +1,32 @@
1
+ /* * */
2
+ import { TimeSlotMap } from '@tmlmobilidade/dates';
1
3
  /**
2
- * Runs a function immediately, then repeatedly on a fixed interval.
3
- * Errors are caught and logged per-invocation they do not halt scheduling.
4
- * Each invocation is fire-and-forget after the first awaited call.
4
+ * Runs an asynchronous function at regular intervals, ensuring that each invocation
5
+ * completes before the next one starts. Errors are logged and can optionally be re-thrown.
5
6
  */
6
- export async function runOnInterval(fn, intervalMs) {
7
+ export async function runOnInterval(fn, options) {
7
8
  //
9
+ //
10
+ // Validate and determine the interval in milliseconds,
11
+ // or throw an error if the provided interval is invalid.
12
+ let intervalMs;
13
+ if (typeof options.intervalMs === 'number')
14
+ intervalMs = options.intervalMs;
15
+ else if (!TimeSlotMap[options.intervalMs])
16
+ throw new Error(`Invalid TimeSlot: ${options.intervalMs}`);
17
+ else
18
+ intervalMs = TimeSlotMap[options.intervalMs];
19
+ //
20
+ // Define the runner function that will execute
21
+ // the provided function and schedule the next execution.
8
22
  const runner = async () => {
9
23
  try {
10
24
  await fn();
11
25
  }
12
26
  catch (error) {
13
27
  console.error('Error in runOnInterval:', error);
28
+ if (options.throwOnError)
29
+ throw error;
14
30
  }
15
31
  finally {
16
32
  setTimeout(runner, intervalMs);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/utils",
3
- "version": "20260411.1248.30",
3
+ "version": "20260418.1237.24",
4
4
  "author": {
5
5
  "email": "iso@tmlmobilidade.pt",
6
6
  "name": "TML-ISO"
@@ -47,7 +47,7 @@
47
47
  },
48
48
  "devDependencies": {
49
49
  "@tmlmobilidade/tsconfig": "*",
50
- "@types/node": "25.5.2",
50
+ "@types/node": "25.6.0",
51
51
  "resolve-tspaths": "0.8.23",
52
52
  "tsc-watch": "7.2.0",
53
53
  "typescript": "5.9.3"