Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@dhmk/utils

Package Overview
Dependencies
Maintainers
0
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dhmk/utils - npm Package Compare versions

Comparing version 4.4.1 to 5.0.0

esm/cancellable.d.ts

25

esm/fn.d.ts

@@ -1,2 +0,2 @@

import { PromiseType, AnyFunction, CancellablePromise } from "./types";
import { PromiseType } from "./types";
export declare const toPromise: <T>(fn: () => PromiseType<T>) => Promise<T>;

@@ -16,25 +16,2 @@ export declare type Deferred<T> = Promise<T> & {

export declare function g<T>(x: PromiseType<T>): Generator<unknown, T>;
export declare type CancellableFlow = {
readonly isCancelled: boolean;
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: 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 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 const cancellableState: () => {
q: <T>(fn: () => Promise<T>) => Promise<T>;
prev: CancellablePromise<void>;
};
export declare type CancellableState = ReturnType<typeof cancellableState>;
export declare function makeCancellable<T extends AnyFunction>(fn: (ctx: CancellableContext) => T, state?: {
q: <T_1>(fn: () => Promise<T_1>) => Promise<T_1>;
prev: CancellablePromise<void>;
}): (...args: Parameters<T>) => CancellablePromise<ReturnType<T>>;
export declare const cancellableEffect: (fn: (ctx: CancellableContext) => any) => () => any;

@@ -28,5 +28,3 @@ var __generator = (this && this.__generator) || function (thisArg, body) {

};
import { Cancelled, } from "./types";
import { noop } from "./std";
import { queue } from "./queue";
export var toPromise = function (fn) {

@@ -141,35 +139,2 @@ try {

}
export var runStep = function (g, arg, isError) {
return isError ? g.throw(arg) : g.next(arg);
};
export function flow(fn, run) {
var _this = this;
if (run === void 0) { run = runStep; }
var ctx = {
_g: undefined,
isCancelled: false,
cancel: function () {
ctx.isCancelled = true;
ctx._g.return(undefined);
},
};
var p = new Promise(function (res, rej) {
function step(arg, isError) {
try {
var _a = run(ctx._g, arg, isError), value = _a.value, done = _a.done;
if (done)
res(value);
else
Promise.resolve(value).then(function (x) { return step(x, false); }, function (e) { return step(e, true); });
}
catch (e) {
rej(e);
}
}
ctx._g = fn.call(_this, ctx);
step(undefined, false);
});
p.cancel = ctx.cancel;
return p;
}
export function pLimited(fn, limit) {

@@ -204,46 +169,1 @@ var stack = [];

}
export function cancellable(fn) {
var ctx = function (x) {
if (ctx.isCancelled)
return Promise.reject(Cancelled);
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)); }).catch(function (e) {
if (e === Cancelled)
return Cancelled;
else
throw e;
});
p.cancel = ctx.cancel;
return p;
}
export var cancellableState = function () { return ({
q: queue(),
prev: cancellable(function () { }),
}); };
export function makeCancellable(fn, state) {
if (state === void 0) { state = cancellableState(); }
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
state.prev.cancel();
return (state.prev = cancellable(function (checkCancel) {
return state.q(function () {
return Promise.resolve(checkCancel(0).then(function () { return fn(checkCancel).apply(void 0, args); }));
});
}));
};
}
export var cancellableEffect = function (fn) {
return cancellable(fn).cancel;
};

@@ -14,1 +14,2 @@ export * from "./error";

export * from "./ts-hacks";
export * from "./cancellable";

@@ -14,1 +14,2 @@ export * from "./error";

export * from "./ts-hacks";
export * from "./cancellable";

@@ -1,7 +0,2 @@

import { CancellableContext } from "./fn";
import { CancellablePromise } from "./types";
export declare function queue(): <T>(fn: () => Promise<T>) => Promise<T>;
export declare function createChain(create: (arg: unknown) => [task: Promise<unknown>, cancel: () => any]): (arg: unknown) => Promise<unknown>;
export declare function asyncChain(): <T>(fn: (ctx: CancellableContext) => Promise<T>) => CancellablePromise<T>;
export declare function flowChain(): <T>(fn: () => Generator<unknown, T>) => CancellablePromise<T>;
export declare function priorityQueue(q?: <T>(fn: () => Promise<T>) => Promise<T>): <T>(fn: () => Promise<T>, priority?: number) => Promise<T>;

@@ -37,46 +37,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

};
import { deferred, toPromise, cancellable, flow, } from "./fn";
import { Cancelled } from "./types";
import { deferred, toPromise } from "./fn";
export function queue() {
var head = Promise.resolve();
var lock = Promise.resolve();
return function (fn) {
var h = head;
var d = (head = deferred());
return h.then(function () { return fn(); }).finally(function () { return d.resolve(undefined); });
var prevLock = lock;
var nextLock = (lock = deferred());
return prevLock.then(function () { return fn(); }).finally(nextLock.resolve);
};
}
export function createChain(create) {
var q = queue();
var p;
var cancel;
var prev = { cancelled: false };
return function (arg) {
prev.cancelled = true;
cancel === null || cancel === void 0 ? void 0 : cancel();
var cc = (prev = { cancelled: false });
return q(function () {
var _a;
if (cc.cancelled)
return Promise.resolve(Cancelled);
_a = create(arg), p = _a[0], cancel = _a[1];
return p;
});
};
}
// const canChain = createChain((fn: any) => {
// const f = fn();
// return [f, f.cancel.bind(f)];
// });
export function asyncChain() {
return createChain(function (fn) {
var c = cancellable(fn);
return [c, c.cancel.bind(c)];
});
}
export function flowChain() {
return createChain(function (fn) {
var f = flow(fn);
return [f, f.cancel.bind(f)];
});
}
export function priorityQueue(q) {

@@ -83,0 +48,0 @@ if (q === void 0) { q = queue(); }

@@ -22,9 +22,1 @@ export declare class Tag<T> {

};
declare class CancelledResult {
}
export declare type Cancelled = CancelledResult;
export declare const Cancelled: CancelledResult;
export declare type CancellablePromise<T> = Promise<T | Cancelled> & {
cancel(): any;
};
export {};

@@ -1,6 +0,1 @@

var CancelledResult = /** @class */ (function () {
function CancelledResult() {
}
return CancelledResult;
}());
export var Cancelled = new CancelledResult();
export {};

@@ -1,2 +0,2 @@

import { PromiseType, AnyFunction, CancellablePromise } from "./types";
import { PromiseType } from "./types";
export declare const toPromise: <T>(fn: () => PromiseType<T>) => Promise<T>;

@@ -16,25 +16,2 @@ export declare type Deferred<T> = Promise<T> & {

export declare function g<T>(x: PromiseType<T>): Generator<unknown, T>;
export declare type CancellableFlow = {
readonly isCancelled: boolean;
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: 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 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 const cancellableState: () => {
q: <T>(fn: () => Promise<T>) => Promise<T>;
prev: CancellablePromise<void>;
};
export declare type CancellableState = ReturnType<typeof cancellableState>;
export declare function makeCancellable<T extends AnyFunction>(fn: (ctx: CancellableContext) => T, state?: {
q: <T_1>(fn: () => Promise<T_1>) => Promise<T_1>;
prev: CancellablePromise<void>;
}): (...args: Parameters<T>) => CancellablePromise<ReturnType<T>>;
export declare const cancellableEffect: (fn: (ctx: CancellableContext) => any) => () => any;

@@ -30,6 +30,4 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.cancellableEffect = exports.makeCancellable = exports.cancellableState = 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 types_1 = require("./types");
exports.pLimited = 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");
var queue_1 = require("./queue");
var toPromise = function (fn) {

@@ -154,37 +152,2 @@ try {

exports.g = g;
var runStep = function (g, arg, isError) {
return isError ? g.throw(arg) : g.next(arg);
};
exports.runStep = runStep;
function flow(fn, run) {
var _this = this;
if (run === void 0) { run = exports.runStep; }
var ctx = {
_g: undefined,
isCancelled: false,
cancel: function () {
ctx.isCancelled = true;
ctx._g.return(undefined);
},
};
var p = new Promise(function (res, rej) {
function step(arg, isError) {
try {
var _a = run(ctx._g, arg, isError), value = _a.value, done = _a.done;
if (done)
res(value);
else
Promise.resolve(value).then(function (x) { return step(x, false); }, function (e) { return step(e, true); });
}
catch (e) {
rej(e);
}
}
ctx._g = fn.call(_this, ctx);
step(undefined, false);
});
p.cancel = ctx.cancel;
return p;
}
exports.flow = flow;
function pLimited(fn, limit) {

@@ -220,50 +183,1 @@ var stack = [];

exports.pLimited = pLimited;
function cancellable(fn) {
var ctx = function (x) {
if (ctx.isCancelled)
return Promise.reject(types_1.Cancelled);
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)); }).catch(function (e) {
if (e === types_1.Cancelled)
return types_1.Cancelled;
else
throw e;
});
p.cancel = ctx.cancel;
return p;
}
exports.cancellable = cancellable;
var cancellableState = function () { return ({
q: (0, queue_1.queue)(),
prev: cancellable(function () { }),
}); };
exports.cancellableState = cancellableState;
function makeCancellable(fn, state) {
if (state === void 0) { state = (0, exports.cancellableState)(); }
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
state.prev.cancel();
return (state.prev = cancellable(function (checkCancel) {
return state.q(function () {
return Promise.resolve(checkCancel(0).then(function () { return fn(checkCancel).apply(void 0, args); }));
});
}));
};
}
exports.makeCancellable = makeCancellable;
var cancellableEffect = function (fn) {
return cancellable(fn).cancel;
};
exports.cancellableEffect = cancellableEffect;

@@ -14,1 +14,2 @@ export * from "./error";

export * from "./ts-hacks";
export * from "./cancellable";

@@ -26,1 +26,2 @@ "use strict";

__exportStar(require("./ts-hacks"), exports);
__exportStar(require("./cancellable"), exports);

@@ -1,7 +0,2 @@

import { CancellableContext } from "./fn";
import { CancellablePromise } from "./types";
export declare function queue(): <T>(fn: () => Promise<T>) => Promise<T>;
export declare function createChain(create: (arg: unknown) => [task: Promise<unknown>, cancel: () => any]): (arg: unknown) => Promise<unknown>;
export declare function asyncChain(): <T>(fn: (ctx: CancellableContext) => Promise<T>) => CancellablePromise<T>;
export declare function flowChain(): <T>(fn: () => Generator<unknown, T>) => CancellablePromise<T>;
export declare function priorityQueue(q?: <T>(fn: () => Promise<T>) => Promise<T>): <T>(fn: () => Promise<T>, priority?: number) => Promise<T>;

@@ -39,51 +39,13 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.priorityQueue = exports.flowChain = exports.asyncChain = exports.createChain = exports.queue = void 0;
exports.priorityQueue = exports.queue = void 0;
var fn_1 = require("./fn");
var types_1 = require("./types");
function queue() {
var head = Promise.resolve();
var lock = Promise.resolve();
return function (fn) {
var h = head;
var d = (head = (0, fn_1.deferred)());
return h.then(function () { return fn(); }).finally(function () { return d.resolve(undefined); });
var prevLock = lock;
var nextLock = (lock = (0, fn_1.deferred)());
return prevLock.then(function () { return fn(); }).finally(nextLock.resolve);
};
}
exports.queue = queue;
function createChain(create) {
var q = queue();
var p;
var cancel;
var prev = { cancelled: false };
return function (arg) {
prev.cancelled = true;
cancel === null || cancel === void 0 ? void 0 : cancel();
var cc = (prev = { cancelled: false });
return q(function () {
var _a;
if (cc.cancelled)
return Promise.resolve(types_1.Cancelled);
_a = create(arg), p = _a[0], cancel = _a[1];
return p;
});
};
}
exports.createChain = createChain;
// const canChain = createChain((fn: any) => {
// const f = fn();
// return [f, f.cancel.bind(f)];
// });
function asyncChain() {
return createChain(function (fn) {
var c = (0, fn_1.cancellable)(fn);
return [c, c.cancel.bind(c)];
});
}
exports.asyncChain = asyncChain;
function flowChain() {
return createChain(function (fn) {
var f = (0, fn_1.flow)(fn);
return [f, f.cancel.bind(f)];
});
}
exports.flowChain = flowChain;
function priorityQueue(q) {

@@ -90,0 +52,0 @@ if (q === void 0) { q = queue(); }

@@ -22,9 +22,1 @@ export declare class Tag<T> {

};
declare class CancelledResult {
}
export declare type Cancelled = CancelledResult;
export declare const Cancelled: CancelledResult;
export declare type CancellablePromise<T> = Promise<T | Cancelled> & {
cancel(): any;
};
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cancelled = void 0;
var CancelledResult = /** @class */ (function () {
function CancelledResult() {
}
return CancelledResult;
}());
exports.Cancelled = new CancelledResult();
{
"name": "@dhmk/utils",
"version": "4.4.1",
"version": "5.0.0",
"description": "A collection of frequently used functions and primitives",

@@ -5,0 +5,0 @@ "keywords": [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc