@softsky/utils 2.7.1 → 2.7.2

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
@@ -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: EventSource, resolveEvents: string[], rejectEvents?: string[]): Promise<T>;
126
130
  export {};
package/dist/control.js CHANGED
@@ -288,3 +288,21 @@ 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']) {
301
+ return new Promise((resolve, reject) => {
302
+ for (let index = 0; index < resolveEvents.length; index++)
303
+ // @ts-ignore
304
+ target.addEventListener(resolveEvents[index], resolve);
305
+ for (let index = 0; index < rejectEvents.length; index++)
306
+ target.addEventListener(rejectEvents[index], reject);
307
+ });
308
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softsky/utils",
3
- "version": "2.7.1",
3
+ "version": "2.7.2",
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.19"
24
+ "@types/bun": "^1.2.21"
25
25
  },
26
26
  "files": ["dist/**/*"]
27
27
  }