@tmlmobilidade/utils 20260226.1419.7 → 20260302.1416.42

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 CHANGED
@@ -65,7 +65,7 @@ Permission checking utilities for role-based access control.
65
65
 
66
66
  Utility for creating proxies around async singleton classes.
67
67
 
68
- - **`AsyncSingletonProxy`** - Creates a proxy that delays method access until singleton instance is initialized
68
+ - **`asyncSingletonProxy`** - Creates a proxy that delays method access until singleton instance is initialized
69
69
 
70
70
  ### Query Validation
71
71
 
@@ -211,7 +211,7 @@ const params = validateQueryParams(req.query, schema);
211
211
  ### Singleton Proxy
212
212
 
213
213
  ```typescript
214
- import { AsyncSingletonProxy } from '@tmlmobilidade/utils';
214
+ import { asyncSingletonProxy } from '@tmlmobilidade/utils';
215
215
 
216
216
  class Database {
217
217
  private static instance: Database | null = null;
@@ -229,7 +229,7 @@ class Database {
229
229
  }
230
230
 
231
231
  // Create proxy that handles async initialization
232
- const db = AsyncSingletonProxy(Database);
232
+ const db = asyncSingletonProxy(Database);
233
233
  await db.query('SELECT * FROM users'); // Automatically waits for initialization
234
234
  ```
235
235
 
package/dist/index.d.ts CHANGED
@@ -4,5 +4,6 @@ export * from './generic/index.js';
4
4
  export * from './http.js';
5
5
  export * from './objects/index.js';
6
6
  export * from './permissions.js';
7
+ export * from './run-on-interval.js';
7
8
  export * from './singleton-proxy.js';
8
9
  export * from './validate-query-params.js';
package/dist/index.js CHANGED
@@ -4,5 +4,6 @@ export * from './generic/index.js';
4
4
  export * from './http.js';
5
5
  export * from './objects/index.js';
6
6
  export * from './permissions.js';
7
+ export * from './run-on-interval.js';
7
8
  export * from './singleton-proxy.js';
8
9
  export * from './validate-query-params.js';
@@ -0,0 +1,6 @@
1
+ /**
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.
5
+ */
6
+ export declare function runOnInterval(fn: () => Promise<void>, intervalMs: number): () => void;
@@ -0,0 +1,20 @@
1
+ /**
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.
5
+ */
6
+ export function runOnInterval(fn, intervalMs) {
7
+ let intervalId = null;
8
+ const execute = () => fn().catch((error) => {
9
+ console.error('[runOnInterval] Unhandled error in scheduled function:', error);
10
+ });
11
+ // Await the first run before starting the interval so startup errors surface
12
+ void execute().then(() => {
13
+ intervalId = setInterval(() => void execute(), intervalMs);
14
+ });
15
+ // Return a cleanup function to cancel scheduling
16
+ return () => {
17
+ if (intervalId !== null)
18
+ clearInterval(intervalId);
19
+ };
20
+ }
@@ -3,6 +3,6 @@
3
3
  * @param cls - A class with a static `getInstance` method that returns a promise resolving to the class instance.
4
4
  * @returns A proxy object that intercepts method calls and ensures the instance is initialized before invoking them.
5
5
  */
6
- export declare function AsyncSingletonProxy<T extends object>(cls: {
6
+ export declare function asyncSingletonProxy<T extends object>(cls: {
7
7
  getInstance: () => Promise<T>;
8
8
  }): T;
@@ -3,7 +3,7 @@
3
3
  * @param cls - A class with a static `getInstance` method that returns a promise resolving to the class instance.
4
4
  * @returns A proxy object that intercepts method calls and ensures the instance is initialized before invoking them.
5
5
  */
6
- export function AsyncSingletonProxy(cls) {
6
+ export function asyncSingletonProxy(cls) {
7
7
  return new Proxy({}, {
8
8
  get: function (_target, prop, receiver) {
9
9
  return async (...args) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/utils",
3
- "version": "20260226.1419.7",
3
+ "version": "20260302.1416.42",
4
4
  "author": {
5
5
  "email": "iso@tmlmobilidade.pt",
6
6
  "name": "TML-ISO"