@dhmk/utils
Advanced tools
Comparing version 2.4.0 to 2.4.1
@@ -5,3 +5,3 @@ declare const defaultEntitiesKey = "byId"; | ||
declare type DefaultIdsKey = typeof defaultIdsKey; | ||
export declare type NormalizedData<T, EntitiesKey extends string = DefaultEntitiesKey, IdsKey extends string = DefaultIdsKey> = Record<EntitiesKey, Record<string, T>> & Record<IdsKey, string[]>; | ||
export declare type NormalizedData<T, EntitiesKey extends string = DefaultEntitiesKey, IdsKey extends string = DefaultIdsKey> = Record<EntitiesKey, Record<string, T>> & Record<IdsKey, ReadonlyArray<string>>; | ||
declare type WithId = { | ||
@@ -12,3 +12,5 @@ [p: string]: any; | ||
declare type DataAdapter<T, E extends string, I extends string> = { | ||
getId(item: T): string; | ||
from(data: ReadonlyArray<T>): NormalizedData<T, E, I>; | ||
from(ids: ReadonlyArray<string>, byId: Record<string, T>): NormalizedData<T, E, I>; | ||
flatMap(context: NormalizedData<T, E, I>, fn: (item: T, index: number, id: string) => ReadonlyArray<T>): NormalizedData<T, E, I>; | ||
@@ -19,5 +21,14 @@ insert(context: NormalizedData<T, E, I>, index: number, ...items: ReadonlyArray<T>): NormalizedData<T, E, I>; | ||
update(context: NormalizedData<T, E, I>, where: string | ((item: T, index: number, id: string) => boolean), what: (item: T, index: number, id: string) => T): NormalizedData<T, E, I>; | ||
toArray(context: NormalizedData<T, E, I>): ReadonlyArray<T>; | ||
}; | ||
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 const fromIds: <T>(ids: ReadonlyArray<string>, byId: Record<string, T>) => T[]; | ||
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,7 @@ 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 var fromIds = function (ids, byId) { return ids.map(function (id) { return byId[id]; }); }; | ||
export function dataAdapter(getId, entitiesKey, idsKey) { | ||
@@ -19,7 +24,9 @@ if (getId === void 0) { getId = defaultId; } | ||
if (idsKey === void 0) { idsKey = defaultIdsKey; } | ||
function from(data) { | ||
function from(dataOrIds, cacheById) { | ||
var _a; | ||
var data = cacheById ? fromIds(dataOrIds, cacheById) : dataOrIds; | ||
var ids = cacheById ? dataOrIds : data.map(getId); | ||
return _a = {}, | ||
_a[entitiesKey] = objectFrom(data.map(function (x) { return [getId(x), x]; })), | ||
_a[idsKey] = data.map(getId), | ||
_a[entitiesKey] = byId(data, getId), | ||
_a[idsKey] = ids, | ||
_a; | ||
@@ -37,3 +44,6 @@ } | ||
} | ||
var atEnd = index === context[idsKey].length; | ||
var length = context[idsKey].length; | ||
if (!length) | ||
return from(items); | ||
var atEnd = index === length; | ||
if (atEnd) | ||
@@ -86,3 +96,7 @@ index--; | ||
} | ||
function toArray(context) { | ||
return fromIds(context[idsKey], context[entitiesKey]); | ||
} | ||
return { | ||
getId: getId, | ||
from: from, | ||
@@ -94,3 +108,4 @@ flatMap: flatMap, | ||
update: update, | ||
toArray: toArray, | ||
}; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { PromiseType } from "./types"; | ||
import { PromiseType, AnyFunction, CancellablePromise } from "./types"; | ||
export declare const toPromise: <T>(fn: () => PromiseType<T>) => Promise<T>; | ||
@@ -10,2 +10,3 @@ export declare type Deferred<T> = Promise<T> & { | ||
export declare const throttled: <T, A extends any[] = []>(fn: (...args: A) => PromiseType<T>, ms: number) => (...args: A) => Promise<T>; | ||
export declare const throttledAsync: <T, A extends any[] = []>(fn: (...args: A) => PromiseType<T>) => (...args: A) => Promise<T>; | ||
export declare const sleep: (ms: number) => Promise<void>; | ||
@@ -16,13 +17,17 @@ export declare function delayed<A extends unknown[], T>(fn: (...args: A) => T, ms: number): (...args: A) => 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; |
@@ -65,3 +65,2 @@ var __generator = (this && this.__generator) || function (thisArg, body) { | ||
export var throttled = function (fn, ms) { | ||
var muted = false; | ||
var p; | ||
@@ -73,9 +72,22 @@ return function () { | ||
} | ||
if (muted) | ||
return p; | ||
muted = true; | ||
setTimeout(function () { return (muted = false); }, ms); | ||
return (p = toPromise(function () { return fn.apply(void 0, args); })); | ||
if (!p) { | ||
p = toPromise(function () { return fn.apply(void 0, args); }); | ||
setTimeout(function () { return (p = undefined); }, ms); | ||
} | ||
return p; | ||
}; | ||
}; | ||
export var throttledAsync = function (fn) { | ||
var p; | ||
return function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
if (!p) { | ||
p = toPromise(function () { return fn.apply(void 0, args); }).finally(function () { return (p = undefined); }); | ||
} | ||
return p; | ||
}; | ||
}; | ||
export var sleep = function (ms) { | ||
@@ -190,16 +202,33 @@ return new Promise(function (res) { return setTimeout(res, ms); }); | ||
} | ||
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; | ||
}; |
@@ -41,7 +41,5 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return function (fn) { | ||
var p = deferred(); | ||
head = head.then(function () { | ||
p.resolve(toPromise(fn)); | ||
}); | ||
return p; | ||
var h = head; | ||
var d = (head = deferred()); | ||
return h.then(function () { return fn(); }).finally(function () { return d.resolve(undefined); }); | ||
}; | ||
@@ -48,0 +46,0 @@ } |
@@ -22,2 +22,5 @@ export declare class Tag<T> { | ||
}; | ||
export declare type CancellablePromise<T> = Promise<T> & { | ||
cancel(): any; | ||
}; | ||
export {}; |
@@ -5,3 +5,3 @@ declare const defaultEntitiesKey = "byId"; | ||
declare type DefaultIdsKey = typeof defaultIdsKey; | ||
export declare type NormalizedData<T, EntitiesKey extends string = DefaultEntitiesKey, IdsKey extends string = DefaultIdsKey> = Record<EntitiesKey, Record<string, T>> & Record<IdsKey, string[]>; | ||
export declare type NormalizedData<T, EntitiesKey extends string = DefaultEntitiesKey, IdsKey extends string = DefaultIdsKey> = Record<EntitiesKey, Record<string, T>> & Record<IdsKey, ReadonlyArray<string>>; | ||
declare type WithId = { | ||
@@ -12,3 +12,5 @@ [p: string]: any; | ||
declare type DataAdapter<T, E extends string, I extends string> = { | ||
getId(item: T): string; | ||
from(data: ReadonlyArray<T>): NormalizedData<T, E, I>; | ||
from(ids: ReadonlyArray<string>, byId: Record<string, T>): NormalizedData<T, E, I>; | ||
flatMap(context: NormalizedData<T, E, I>, fn: (item: T, index: number, id: string) => ReadonlyArray<T>): NormalizedData<T, E, I>; | ||
@@ -19,5 +21,14 @@ insert(context: NormalizedData<T, E, I>, index: number, ...items: ReadonlyArray<T>): NormalizedData<T, E, I>; | ||
update(context: NormalizedData<T, E, I>, where: string | ((item: T, index: number, id: string) => boolean), what: (item: T, index: number, id: string) => T): NormalizedData<T, E, I>; | ||
toArray(context: NormalizedData<T, E, I>): ReadonlyArray<T>; | ||
}; | ||
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 const fromIds: <T>(ids: ReadonlyArray<string>, byId: Record<string, T>) => T[]; | ||
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.fromIds = exports.byId = void 0; | ||
var std_1 = require("./std"); | ||
@@ -18,2 +18,9 @@ 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; | ||
var fromIds = function (ids, byId) { return ids.map(function (id) { return byId[id]; }); }; | ||
exports.fromIds = fromIds; | ||
function dataAdapter(getId, entitiesKey, idsKey) { | ||
@@ -23,7 +30,9 @@ if (getId === void 0) { getId = defaultId; } | ||
if (idsKey === void 0) { idsKey = defaultIdsKey; } | ||
function from(data) { | ||
function from(dataOrIds, cacheById) { | ||
var _a; | ||
var data = cacheById ? (0, exports.fromIds)(dataOrIds, cacheById) : dataOrIds; | ||
var ids = cacheById ? dataOrIds : data.map(getId); | ||
return _a = {}, | ||
_a[entitiesKey] = (0, std_1.objectFrom)(data.map(function (x) { return [getId(x), x]; })), | ||
_a[idsKey] = data.map(getId), | ||
_a[entitiesKey] = (0, exports.byId)(data, getId), | ||
_a[idsKey] = ids, | ||
_a; | ||
@@ -41,3 +50,6 @@ } | ||
} | ||
var atEnd = index === context[idsKey].length; | ||
var length = context[idsKey].length; | ||
if (!length) | ||
return from(items); | ||
var atEnd = index === length; | ||
if (atEnd) | ||
@@ -90,3 +102,7 @@ index--; | ||
} | ||
function toArray(context) { | ||
return (0, exports.fromIds)(context[idsKey], context[entitiesKey]); | ||
} | ||
return { | ||
getId: getId, | ||
from: from, | ||
@@ -98,4 +114,5 @@ flatMap: flatMap, | ||
update: update, | ||
toArray: toArray, | ||
}; | ||
} | ||
exports.dataAdapter = dataAdapter; |
@@ -1,2 +0,2 @@ | ||
import { PromiseType } from "./types"; | ||
import { PromiseType, AnyFunction, CancellablePromise } from "./types"; | ||
export declare const toPromise: <T>(fn: () => PromiseType<T>) => Promise<T>; | ||
@@ -10,2 +10,3 @@ export declare type Deferred<T> = Promise<T> & { | ||
export declare const throttled: <T, A extends any[] = []>(fn: (...args: A) => PromiseType<T>, ms: number) => (...args: A) => Promise<T>; | ||
export declare const throttledAsync: <T, A extends any[] = []>(fn: (...args: A) => PromiseType<T>) => (...args: A) => Promise<T>; | ||
export declare const sleep: (ms: number) => Promise<void>; | ||
@@ -16,13 +17,17 @@ export declare function delayed<A extends unknown[], T>(fn: (...args: A) => T, ms: number): (...args: A) => 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.throttledAsync = exports.throttled = exports.debounced = exports.deferred = exports.toPromise = void 0; | ||
var std_1 = require("./std"); | ||
@@ -72,3 +72,2 @@ var toPromise = function (fn) { | ||
var throttled = function (fn, ms) { | ||
var muted = false; | ||
var p; | ||
@@ -80,10 +79,24 @@ return function () { | ||
} | ||
if (muted) | ||
return p; | ||
muted = true; | ||
setTimeout(function () { return (muted = false); }, ms); | ||
return (p = (0, exports.toPromise)(function () { return fn.apply(void 0, args); })); | ||
if (!p) { | ||
p = (0, exports.toPromise)(function () { return fn.apply(void 0, args); }); | ||
setTimeout(function () { return (p = undefined); }, ms); | ||
} | ||
return p; | ||
}; | ||
}; | ||
exports.throttled = throttled; | ||
var throttledAsync = function (fn) { | ||
var p; | ||
return function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
if (!p) { | ||
p = (0, exports.toPromise)(function () { return fn.apply(void 0, args); }).finally(function () { return (p = undefined); }); | ||
} | ||
return p; | ||
}; | ||
}; | ||
exports.throttledAsync = throttledAsync; | ||
var sleep = function (ms) { | ||
@@ -206,17 +219,36 @@ return new Promise(function (res) { return setTimeout(res, ms); }); | ||
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; |
@@ -44,7 +44,5 @@ "use strict"; | ||
return function (fn) { | ||
var p = (0, fn_1.deferred)(); | ||
head = head.then(function () { | ||
p.resolve((0, fn_1.toPromise)(fn)); | ||
}); | ||
return p; | ||
var h = head; | ||
var d = (head = (0, fn_1.deferred)()); | ||
return h.then(function () { return fn(); }).finally(function () { return d.resolve(undefined); }); | ||
}; | ||
@@ -51,0 +49,0 @@ } |
@@ -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": "2.4.1", | ||
"description": "A collection of frequently used functions and primitives", | ||
@@ -25,3 +25,2 @@ "keywords": [ | ||
"version": "git add -A", | ||
"postversion": "git push && git push --tags", | ||
"clean": "rm -rf lib esm", | ||
@@ -28,0 +27,0 @@ "build": "yarn clean && tsc && tsc -m esnext --outDir esm", |
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
110232
2536