@softsky/utils 2.7.1 → 2.7.3
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 +6 -0
- package/dist/control.d.ts +4 -0
- package/dist/control.js +21 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -204,6 +204,12 @@ semaphore.run(()=>{
|
|
|
204
204
|
---
|
|
205
205
|
__async function__ `withTimeout` - Add timeout to a promise
|
|
206
206
|
|
|
207
|
+
---
|
|
208
|
+
__function__ `promisify` - Promisify a function with callback
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
__function__ `promisifyEventSource` - Create a promise out of EventSource
|
|
212
|
+
|
|
207
213
|
---
|
|
208
214
|
|
|
209
215
|
|
package/dist/control.d.ts
CHANGED
|
@@ -123,4 +123,8 @@ export declare class Semaphore {
|
|
|
123
123
|
}
|
|
124
124
|
/** Add timeout to a promise */
|
|
125
125
|
export declare function withTimeout<T>(run: () => Promise<T>, ms: number): Promise<T>;
|
|
126
|
+
/** Promisify a function with callback */
|
|
127
|
+
export declare function promisify<A extends any[], R extends any[]>(handler: (...handlerArguments: [...A, (...results: R) => void]) => void): (...handlerArguments: A) => Promise<R extends [infer U] ? U : R>;
|
|
128
|
+
/** Create a promise out of EventSource */
|
|
129
|
+
export declare function promisifyEventSource<T = unknown>(target: object, resolveEvents: string[], rejectEvents?: string[], subName?: string): Promise<T>;
|
|
126
130
|
export {};
|
package/dist/control.js
CHANGED
|
@@ -288,3 +288,24 @@ export class Semaphore {
|
|
|
288
288
|
export async function withTimeout(run, ms) {
|
|
289
289
|
return Promise.race([run(), timeout(ms)]);
|
|
290
290
|
}
|
|
291
|
+
/** Promisify a function with callback */
|
|
292
|
+
export function promisify(handler) {
|
|
293
|
+
return (...handlerArguments) => new Promise((resolve) => {
|
|
294
|
+
handler(...handlerArguments, (...results) => {
|
|
295
|
+
resolve(results.length > 1 ? results : results[0]);
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
/** Create a promise out of EventSource */
|
|
300
|
+
export function promisifyEventSource(target, resolveEvents, rejectEvents = ['error'], subName = 'addEventListener') {
|
|
301
|
+
return new Promise((resolve, reject) => {
|
|
302
|
+
for (let index = 0; index < resolveEvents.length; index++)
|
|
303
|
+
// @ts-ignore
|
|
304
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
305
|
+
target[subName]?.(resolveEvents[index], resolve);
|
|
306
|
+
for (let index = 0; index < rejectEvents.length; index++)
|
|
307
|
+
// @ts-ignore
|
|
308
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
309
|
+
target[subName]?.(rejectEvents[index], reject);
|
|
310
|
+
});
|
|
311
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softsky/utils",
|
|
3
|
-
"version": "2.7.
|
|
3
|
+
"version": "2.7.3",
|
|
4
4
|
"description": "JavaScript/TypeScript utilities",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"homepage": "https://github.com/SoundOfTheSky/utils#readme",
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@softsky/configs": "^1.3.4",
|
|
24
|
-
"@types/bun": "^1.2.
|
|
24
|
+
"@types/bun": "^1.2.21"
|
|
25
25
|
},
|
|
26
26
|
"files": ["dist/**/*"]
|
|
27
27
|
}
|