@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 +3 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/run-on-interval.d.ts +6 -0
- package/dist/run-on-interval.js +20 -0
- package/dist/singleton-proxy.d.ts +1 -1
- package/dist/singleton-proxy.js +1 -1
- package/package.json +1 -1
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
|
-
- **`
|
|
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 {
|
|
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 =
|
|
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
package/dist/index.js
CHANGED
|
@@ -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
|
|
6
|
+
export declare function asyncSingletonProxy<T extends object>(cls: {
|
|
7
7
|
getInstance: () => Promise<T>;
|
|
8
8
|
}): T;
|
package/dist/singleton-proxy.js
CHANGED
|
@@ -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
|
|
6
|
+
export function asyncSingletonProxy(cls) {
|
|
7
7
|
return new Proxy({}, {
|
|
8
8
|
get: function (_target, prop, receiver) {
|
|
9
9
|
return async (...args) => {
|