@matrixai/contexts
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -1,5 +0,5 @@ | ||
import type { ContextTimed } from '../types'; | ||
type ContextRemaining<C> = Omit<C, keyof ContextTimed>; | ||
type ContextAndParameters<C, P extends Array<any>> = keyof ContextRemaining<C> extends never ? [Partial<ContextTimed>?, ...P] : [Partial<ContextTimed> & ContextRemaining<C>, ...P]; | ||
declare function setupTimedContext(delay: number, errorTimeoutConstructor: new () => Error, ctx: Partial<ContextTimed>): () => void; | ||
import type { ContextTimed, ContextTimedInput } from '../types'; | ||
type ContextRemaining<C> = Omit<C, keyof ContextTimedInput>; | ||
type ContextAndParameters<C, P extends Array<any>> = keyof ContextRemaining<C> extends never ? [Partial<ContextTimedInput>?, ...P] : [Partial<ContextTimedInput> & ContextRemaining<C>, ...P]; | ||
declare function setupTimedContext(delay: number, errorTimeoutConstructor: new () => Error, ctx: Partial<ContextTimedInput>): () => void; | ||
/** | ||
@@ -9,4 +9,4 @@ * Timed HOF | ||
*/ | ||
declare function timed<C extends ContextTimed, P extends Array<any>, R>(f: (ctx: C, ...params: P) => R, delay?: number, errorTimeoutConstructor?: new () => Error): (...params: ContextAndParameters<C, P>) => R; | ||
declare function timed<C extends ContextTimedInput, C_ extends ContextTimed, P extends Array<any>, R>(f: (ctx: C_, ...params: P) => R, delay?: number, errorTimeoutConstructor?: new () => Error): (...params: ContextAndParameters<C, P>) => R; | ||
export default timed; | ||
export { setupTimedContext }; |
@@ -50,7 +50,8 @@ "use strict"; | ||
// wrapper will not re-setup this property A relationship. | ||
if (ctx.timer === undefined && ctx.signal === undefined) { | ||
if ((ctx.timer === undefined || typeof ctx.timer === 'number') && | ||
ctx.signal === undefined) { | ||
const abortController = new AbortController(); | ||
const e = new errorTimeoutConstructor(); | ||
// Property A | ||
const timer = new timer_1.Timer(() => void abortController.abort(e), delay); | ||
const timer = new timer_1.Timer(() => void abortController.abort(e), ctx.timer ?? delay); | ||
abortController.signal.addEventListener('abort', () => { | ||
@@ -67,7 +68,8 @@ // Property B | ||
} | ||
else if (ctx.timer === undefined && ctx.signal instanceof AbortSignal) { | ||
else if ((ctx.timer === undefined || typeof ctx.timer === 'number') && | ||
ctx.signal instanceof AbortSignal) { | ||
const abortController = new AbortController(); | ||
const e = new errorTimeoutConstructor(); | ||
// Property A | ||
const timer = new timer_1.Timer(() => void abortController.abort(e), delay); | ||
const timer = new timer_1.Timer(() => void abortController.abort(e), ctx.timer ?? delay); | ||
const signalUpstream = ctx.signal; | ||
@@ -74,0 +76,0 @@ const signalHandler = () => { |
@@ -1,8 +0,8 @@ | ||
import type { ContextTimed } from '../types'; | ||
import type { ContextTimed, ContextTimedInput } from '../types'; | ||
import { PromiseCancellable } from '@matrixai/async-cancellable'; | ||
type ContextRemaining<C> = Omit<C, keyof ContextTimed>; | ||
type ContextAndParameters<C, P extends Array<any>> = keyof ContextRemaining<C> extends never ? [Partial<ContextTimed>?, ...P] : [Partial<ContextTimed> & ContextRemaining<C>, ...P]; | ||
declare function setupTimedCancellable<C extends ContextTimed, P extends Array<any>, R>(f: (ctx: C, ...params: P) => PromiseLike<R>, lazy: boolean, delay: number, errorTimeoutConstructor: (new () => Error) | undefined, ctx: Partial<ContextTimed>, args: P): PromiseCancellable<R>; | ||
declare function timedCancellable<C extends ContextTimed, P extends Array<any>, R>(f: (ctx: C, ...params: P) => PromiseLike<R>, lazy?: boolean, delay?: number, errorTimeoutConstructor?: new () => Error): (...params: ContextAndParameters<C, P>) => PromiseCancellable<R>; | ||
type ContextRemaining<C> = Omit<C, keyof ContextTimedInput>; | ||
type ContextAndParameters<C, P extends Array<any>> = keyof ContextRemaining<C> extends never ? [Partial<ContextTimedInput>?, ...P] : [Partial<ContextTimedInput> & ContextRemaining<C>, ...P]; | ||
declare function setupTimedCancellable<C extends ContextTimed, P extends Array<any>, R>(f: (ctx: C, ...params: P) => PromiseLike<R>, lazy: boolean, delay: number, errorTimeoutConstructor: new () => Error, ctx: Partial<ContextTimedInput>, args: P): PromiseCancellable<R>; | ||
declare function timedCancellable<C extends ContextTimedInput, C_ extends ContextTimed, P extends Array<any>, R>(f: (ctx: C_, ...params: P) => PromiseLike<R>, lazy?: boolean, delay?: number, errorTimeoutConstructor?: new () => Error): (...params: ContextAndParameters<C, P>) => PromiseCancellable<R>; | ||
export default timedCancellable; | ||
export { setupTimedCancellable }; |
@@ -30,3 +30,3 @@ "use strict"; | ||
const errors = __importStar(require("../errors")); | ||
function setupTimedCancellable(f, lazy, delay, errorTimeoutConstructor = errors.ErrorContextsTimedTimeOut, ctx, args) { | ||
function setupTimedCancellable(f, lazy, delay, errorTimeoutConstructor, ctx, args) { | ||
// There are 3 properties of timer and signal: | ||
@@ -53,7 +53,8 @@ // | ||
let teardownContext; | ||
if (ctx.timer === undefined && ctx.signal === undefined) { | ||
if ((ctx.timer === undefined || typeof ctx.timer === 'number') && | ||
ctx.signal === undefined) { | ||
abortController = new AbortController(); | ||
const e = new errorTimeoutConstructor(); | ||
// Property A | ||
const timer = new timer_1.Timer(() => void abortController.abort(e), delay); | ||
const timer = new timer_1.Timer(() => void abortController.abort(e), ctx.timer ?? delay); | ||
abortController.signal.addEventListener('abort', () => { | ||
@@ -70,7 +71,8 @@ // Property B | ||
} | ||
else if (ctx.timer === undefined && ctx.signal instanceof AbortSignal) { | ||
else if ((ctx.timer === undefined || typeof ctx.timer === 'number') && | ||
ctx.signal instanceof AbortSignal) { | ||
abortController = new AbortController(); | ||
const e = new errorTimeoutConstructor(); | ||
// Property A | ||
const timer = new timer_1.Timer(() => void abortController.abort(e), delay); | ||
const timer = new timer_1.Timer(() => void abortController.abort(e), ctx.timer ?? delay); | ||
const signalUpstream = ctx.signal; | ||
@@ -156,3 +158,3 @@ const signalHandler = () => { | ||
reject(signal.reason); | ||
}); | ||
}, { once: true }); | ||
} | ||
@@ -159,0 +161,0 @@ } |
@@ -8,2 +8,5 @@ import type { Timer } from '@matrixai/timer'; | ||
}; | ||
export type { ContextCancellable, ContextTimed }; | ||
type ContextTimedInput = ContextCancellable & { | ||
timer: Timer | number; | ||
}; | ||
export type { ContextCancellable, ContextTimed, ContextTimedInput }; |
@@ -0,1 +1,4 @@ | ||
import type { ResourceAcquire } from '@matrixai/resources'; | ||
import type { PromiseCancellableController } from '@matrixai/async-cancellable'; | ||
import { Timer } from '@matrixai/timer'; | ||
declare const AsyncFunction: Function; | ||
@@ -8,2 +11,12 @@ declare const GeneratorFunction: Function; | ||
declare function checkContextTimed(ctx: any, key: string | symbol, targetName: string): void; | ||
/** | ||
* Timer resource | ||
* Use it with `withF` or `withG`. | ||
*/ | ||
declare function timer<T = void>(handlerOrOpts?: ((signal: AbortSignal) => T | PromiseLike<T>) | { | ||
handler?: (signal: AbortSignal) => T | PromiseLike<T>; | ||
delay?: number; | ||
lazy?: boolean; | ||
controller?: PromiseCancellableController; | ||
}, delay?: number, lazy?: boolean, controller?: PromiseCancellableController): ResourceAcquire<Timer<T>>; | ||
declare function isPromiseLike(v: any): v is PromiseLike<unknown>; | ||
@@ -20,2 +33,2 @@ /** | ||
declare function isAsyncGenerator(v: any): v is AsyncGenerator<unknown>; | ||
export { AsyncFunction, GeneratorFunction, AsyncGeneratorFunction, contexts, getContextIndex, checkContextCancellable, checkContextTimed, isPromiseLike, isGenerator, isAsyncGenerator, }; | ||
export { AsyncFunction, GeneratorFunction, AsyncGeneratorFunction, contexts, getContextIndex, checkContextCancellable, checkContextTimed, timer, isPromiseLike, isGenerator, isAsyncGenerator, }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isAsyncGenerator = exports.isGenerator = exports.isPromiseLike = exports.checkContextTimed = exports.checkContextCancellable = exports.getContextIndex = exports.contexts = exports.AsyncGeneratorFunction = exports.GeneratorFunction = exports.AsyncFunction = void 0; | ||
exports.isAsyncGenerator = exports.isGenerator = exports.isPromiseLike = exports.timer = exports.checkContextTimed = exports.checkContextCancellable = exports.getContextIndex = exports.contexts = exports.AsyncGeneratorFunction = exports.GeneratorFunction = exports.AsyncFunction = void 0; | ||
const timer_1 = require("@matrixai/timer"); | ||
@@ -37,7 +37,30 @@ const AsyncFunction = (async () => { }).constructor; | ||
} | ||
if (ctx.timer !== undefined && !(ctx.timer instanceof timer_1.Timer)) { | ||
throw new TypeError(`\`${targetName}.${key.toString()}\` decorated \`@context\` parameter's \`timer\` property is not an instance of \`Timer\``); | ||
if (ctx.timer !== undefined && | ||
!(typeof ctx.timer === 'number' || ctx.timer instanceof timer_1.Timer)) { | ||
throw new TypeError(`\`${targetName}.${key.toString()}\` decorated \`@context\` parameter's \`timer\` property is not a number nor an instance of \`Timer\``); | ||
} | ||
} | ||
exports.checkContextTimed = checkContextTimed; | ||
/** | ||
* Timer resource | ||
* Use it with `withF` or `withG`. | ||
*/ | ||
function timer(handlerOrOpts, delay = 0, lazy = false, controller) { | ||
return async () => { | ||
let timer; | ||
if (typeof handlerOrOpts === 'function') { | ||
timer = new timer_1.Timer(handlerOrOpts, delay, lazy, controller); | ||
} | ||
else { | ||
timer = new timer_1.Timer(handlerOrOpts); | ||
} | ||
return [ | ||
async () => { | ||
timer.cancel(); | ||
}, | ||
timer, | ||
]; | ||
}; | ||
} | ||
exports.timer = timer; | ||
function isPromiseLike(v) { | ||
@@ -44,0 +67,0 @@ return v != null && typeof v.then === 'function'; |
{ | ||
"name": "@matrixai/contexts", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"author": "Roger Qiu", | ||
@@ -25,10 +25,13 @@ "description": "Asynchronous contexts", | ||
"dependencies": { | ||
"@matrixai/async-cancellable": "^1.0.6", | ||
"@matrixai/async-cancellable": "^1.1.1", | ||
"@matrixai/async-locks": "^4.0.0", | ||
"@matrixai/errors": "^1.1.7", | ||
"@matrixai/timer": "^1.1.0" | ||
"@matrixai/resources": "^1.1.5", | ||
"@matrixai/timer": "^1.1.1" | ||
}, | ||
"devDependencies": { | ||
"@swc/core": "^1.2.215", | ||
"@swc/core": "^1.3.62", | ||
"@swc/jest": "^0.2.26", | ||
"@types/jest": "^28.1.3", | ||
"@types/node": "^18.11.11", | ||
"@types/node": "^18.15.0", | ||
"@typescript-eslint/eslint-plugin": "^5.45.1", | ||
@@ -35,0 +38,0 @@ "@typescript-eslint/parser": "^5.45.1", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
84313
1048
5
20
+ Added@matrixai/async-locks@^4.0.0
+ Added@matrixai/resources@^1.1.5
+ Added@matrixai/async-locks@4.0.0(transitive)
+ Added@matrixai/resources@1.1.5(transitive)
Updated@matrixai/timer@^1.1.1