@dhmk/utils
Advanced tools
Comparing version 2.4.0 to 3.0.0
@@ -18,4 +18,11 @@ declare const defaultEntitiesKey = "byId"; | ||
}; | ||
declare type ById = { | ||
<T extends { | ||
id: K; | ||
}, K extends string | number>(arr: T[]): Record<K, T>; | ||
<T, K extends string | number>(arr: T[], getId: (x: T) => K): Record<K, T>; | ||
}; | ||
export declare const byId: ById; | ||
export declare function dataAdapter<T extends WithId>(): DataAdapter<T, DefaultEntitiesKey, DefaultIdsKey>; | ||
export declare function dataAdapter<T, EntitiesKey extends string = DefaultEntitiesKey, IdsKey extends string = DefaultIdsKey>(getId: (item: T) => string, entitiesKey?: EntitiesKey, idsKey?: IdsKey): DataAdapter<T, EntitiesKey, IdsKey>; | ||
export {}; |
@@ -14,2 +14,6 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
var defaultId = function (x) { return x.id; }; | ||
export var byId = function (arr, getId) { | ||
if (getId === void 0) { getId = function (x) { return x.id; }; } | ||
return objectFrom(arr.map(function (x) { return [getId(x), x]; })); | ||
}; | ||
export function dataAdapter(getId, entitiesKey, idsKey) { | ||
@@ -22,3 +26,3 @@ if (getId === void 0) { getId = defaultId; } | ||
return _a = {}, | ||
_a[entitiesKey] = objectFrom(data.map(function (x) { return [getId(x), x]; })), | ||
_a[entitiesKey] = byId(data, getId), | ||
_a[idsKey] = data.map(getId), | ||
@@ -25,0 +29,0 @@ _a; |
@@ -1,2 +0,2 @@ | ||
import { PromiseType } from "./types"; | ||
import { PromiseType, AnyFunction, CancellablePromise } from "./types"; | ||
export declare const toPromise: <T>(fn: () => PromiseType<T>) => Promise<T>; | ||
@@ -15,13 +15,17 @@ export declare type Deferred<T> = Promise<T> & { | ||
export declare function g<T>(x: PromiseType<T>): Generator<unknown, T>; | ||
declare type Cancellable = { | ||
export declare type CancellableFlow = { | ||
readonly isCancelled: boolean; | ||
cancel(): void; | ||
}; | ||
export declare type Flow<T> = Promise<T> & { | ||
cancel(): void; | ||
}; | ||
export declare type Flow<T> = CancellablePromise<T>; | ||
export declare const runStep: (g: Generator, arg: any, isError: boolean) => IteratorResult<unknown, any>; | ||
export declare function flow<T>(this: any, fn: (ctx: Cancellable) => Generator<any, T>, run?: (g: Generator<unknown, any, unknown>, arg: any, isError: boolean) => IteratorResult<unknown, any>): Flow<T>; | ||
export declare function flow<T>(this: any, fn: (ctx: CancellableFlow) => Generator<any, T>, run?: (g: Generator<unknown, any, unknown>, arg: any, isError: boolean) => IteratorResult<unknown, any>): Flow<T>; | ||
export declare function pLimited<A extends unknown[] = any, T = any>(fn: (...args: A) => Promise<T>, limit: number): (...args: A) => Promise<T>; | ||
export declare function cancellable(fn: (checkCancelled: <T>(x: T) => T) => any, onCancel?: () => void): () => void; | ||
export {}; | ||
export declare type CancellableContext = { | ||
<T>(x: T): Promise<T>; | ||
readonly isCancelled: boolean; | ||
onCancel(cb: Function): any; | ||
}; | ||
export declare function cancellable<T>(fn: (ctx: CancellableContext) => T): CancellablePromise<T>; | ||
export declare function makeCancellable<T extends AnyFunction>(fn: (ctx: CancellableContext) => T): (...args: Parameters<T>) => CancellablePromise<ReturnType<T>>; | ||
export declare const cancellableEffect: (fn: (ctx: CancellableContext) => any) => () => any; |
@@ -188,16 +188,33 @@ var __generator = (this && this.__generator) || function (thisArg, body) { | ||
} | ||
export function cancellable(fn, onCancel) { | ||
if (onCancel === void 0) { onCancel = noop; } | ||
var isCancelled; | ||
fn(function (x) { | ||
if (isCancelled) | ||
export function cancellable(fn) { | ||
var ctx = function (x) { | ||
if (ctx.isCancelled) | ||
return new Promise(function () { }); | ||
// never resolves nor rejects | ||
else | ||
return x; | ||
}); | ||
}; | ||
ctx.isCancelled = false; | ||
ctx.cb = noop; | ||
ctx.onCancel = function (fn) { return (ctx.cb = fn); }; | ||
ctx.cancel = function () { | ||
ctx.isCancelled = true; | ||
ctx.cb(); | ||
}; | ||
var p = new Promise(function (resolve) { return resolve(fn(ctx)); }); | ||
p.cancel = ctx.cancel; | ||
return p; | ||
} | ||
export function makeCancellable(fn) { | ||
var prev = cancellable(function () { }); | ||
return function () { | ||
isCancelled = true; | ||
onCancel(); | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
prev.cancel(); | ||
return (prev = cancellable(function (ctx) { return fn(ctx).apply(void 0, args); })); | ||
}; | ||
} | ||
export var cancellableEffect = function (fn) { | ||
return cancellable(fn).cancel; | ||
}; |
@@ -22,2 +22,5 @@ export declare class Tag<T> { | ||
}; | ||
export declare type CancellablePromise<T> = Promise<T> & { | ||
cancel(): any; | ||
}; | ||
export {}; |
@@ -18,4 +18,11 @@ declare const defaultEntitiesKey = "byId"; | ||
}; | ||
declare type ById = { | ||
<T extends { | ||
id: K; | ||
}, K extends string | number>(arr: T[]): Record<K, T>; | ||
<T, K extends string | number>(arr: T[], getId: (x: T) => K): Record<K, T>; | ||
}; | ||
export declare const byId: ById; | ||
export declare function dataAdapter<T extends WithId>(): DataAdapter<T, DefaultEntitiesKey, DefaultIdsKey>; | ||
export declare function dataAdapter<T, EntitiesKey extends string = DefaultEntitiesKey, IdsKey extends string = DefaultIdsKey>(getId: (item: T) => string, entitiesKey?: EntitiesKey, idsKey?: IdsKey): DataAdapter<T, EntitiesKey, IdsKey>; | ||
export {}; |
@@ -12,3 +12,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.dataAdapter = void 0; | ||
exports.dataAdapter = exports.byId = void 0; | ||
var std_1 = require("./std"); | ||
@@ -18,2 +18,7 @@ var defaultEntitiesKey = "byId"; | ||
var defaultId = function (x) { return x.id; }; | ||
var byId = function (arr, getId) { | ||
if (getId === void 0) { getId = function (x) { return x.id; }; } | ||
return (0, std_1.objectFrom)(arr.map(function (x) { return [getId(x), x]; })); | ||
}; | ||
exports.byId = byId; | ||
function dataAdapter(getId, entitiesKey, idsKey) { | ||
@@ -26,3 +31,3 @@ if (getId === void 0) { getId = defaultId; } | ||
return _a = {}, | ||
_a[entitiesKey] = (0, std_1.objectFrom)(data.map(function (x) { return [getId(x), x]; })), | ||
_a[entitiesKey] = (0, exports.byId)(data, getId), | ||
_a[idsKey] = data.map(getId), | ||
@@ -29,0 +34,0 @@ _a; |
@@ -1,2 +0,2 @@ | ||
import { PromiseType } from "./types"; | ||
import { PromiseType, AnyFunction, CancellablePromise } from "./types"; | ||
export declare const toPromise: <T>(fn: () => PromiseType<T>) => Promise<T>; | ||
@@ -15,13 +15,17 @@ export declare type Deferred<T> = Promise<T> & { | ||
export declare function g<T>(x: PromiseType<T>): Generator<unknown, T>; | ||
declare type Cancellable = { | ||
export declare type CancellableFlow = { | ||
readonly isCancelled: boolean; | ||
cancel(): void; | ||
}; | ||
export declare type Flow<T> = Promise<T> & { | ||
cancel(): void; | ||
}; | ||
export declare type Flow<T> = CancellablePromise<T>; | ||
export declare const runStep: (g: Generator, arg: any, isError: boolean) => IteratorResult<unknown, any>; | ||
export declare function flow<T>(this: any, fn: (ctx: Cancellable) => Generator<any, T>, run?: (g: Generator<unknown, any, unknown>, arg: any, isError: boolean) => IteratorResult<unknown, any>): Flow<T>; | ||
export declare function flow<T>(this: any, fn: (ctx: CancellableFlow) => Generator<any, T>, run?: (g: Generator<unknown, any, unknown>, arg: any, isError: boolean) => IteratorResult<unknown, any>): Flow<T>; | ||
export declare function pLimited<A extends unknown[] = any, T = any>(fn: (...args: A) => Promise<T>, limit: number): (...args: A) => Promise<T>; | ||
export declare function cancellable(fn: (checkCancelled: <T>(x: T) => T) => any, onCancel?: () => void): () => void; | ||
export {}; | ||
export declare type CancellableContext = { | ||
<T>(x: T): Promise<T>; | ||
readonly isCancelled: boolean; | ||
onCancel(cb: Function): any; | ||
}; | ||
export declare function cancellable<T>(fn: (ctx: CancellableContext) => T): CancellablePromise<T>; | ||
export declare function makeCancellable<T extends AnyFunction>(fn: (ctx: CancellableContext) => T): (...args: Parameters<T>) => CancellablePromise<ReturnType<T>>; | ||
export declare const cancellableEffect: (fn: (ctx: CancellableContext) => any) => () => any; |
@@ -30,3 +30,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.cancellable = exports.pLimited = exports.flow = exports.runStep = exports.g = exports.disposable = exports.immediate = exports.delayed = exports.sleep = exports.throttled = exports.debounced = exports.deferred = exports.toPromise = void 0; | ||
exports.cancellableEffect = exports.makeCancellable = exports.cancellable = exports.pLimited = exports.flow = exports.runStep = exports.g = exports.disposable = exports.immediate = exports.delayed = exports.sleep = exports.throttled = exports.debounced = exports.deferred = exports.toPromise = void 0; | ||
var std_1 = require("./std"); | ||
@@ -204,17 +204,36 @@ var toPromise = function (fn) { | ||
exports.pLimited = pLimited; | ||
function cancellable(fn, onCancel) { | ||
if (onCancel === void 0) { onCancel = std_1.noop; } | ||
var isCancelled; | ||
fn(function (x) { | ||
if (isCancelled) | ||
function cancellable(fn) { | ||
var ctx = function (x) { | ||
if (ctx.isCancelled) | ||
return new Promise(function () { }); | ||
// never resolves nor rejects | ||
else | ||
return x; | ||
}); | ||
}; | ||
ctx.isCancelled = false; | ||
ctx.cb = std_1.noop; | ||
ctx.onCancel = function (fn) { return (ctx.cb = fn); }; | ||
ctx.cancel = function () { | ||
ctx.isCancelled = true; | ||
ctx.cb(); | ||
}; | ||
var p = new Promise(function (resolve) { return resolve(fn(ctx)); }); | ||
p.cancel = ctx.cancel; | ||
return p; | ||
} | ||
exports.cancellable = cancellable; | ||
function makeCancellable(fn) { | ||
var prev = cancellable(function () { }); | ||
return function () { | ||
isCancelled = true; | ||
onCancel(); | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
prev.cancel(); | ||
return (prev = cancellable(function (ctx) { return fn(ctx).apply(void 0, args); })); | ||
}; | ||
} | ||
exports.cancellable = cancellable; | ||
exports.makeCancellable = makeCancellable; | ||
var cancellableEffect = function (fn) { | ||
return cancellable(fn).cancel; | ||
}; | ||
exports.cancellableEffect = cancellableEffect; |
@@ -22,2 +22,5 @@ export declare class Tag<T> { | ||
}; | ||
export declare type CancellablePromise<T> = Promise<T> & { | ||
cancel(): any; | ||
}; | ||
export {}; |
{ | ||
"name": "@dhmk/utils", | ||
"version": "2.4.0", | ||
"version": "3.0.0", | ||
"description": "A collection of frequently used functions and primitives", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107577
2482