How to use @esfx/internal-guards - 10 common examples

To help you get started, we’ve selected a few @esfx/internal-guards examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github esfx / esfx / packages / cancelable / src / index.ts View on Github external
export function subscribe(cancelable: Cancelable | undefined, onSignaled: () => void) {
        if (!isMissing(cancelable) && !isCancelable(cancelable)) throw new TypeError("Cancelable expected: cancelable");
        if (cancelable === Cancelable.canceled) return Cancelable.canceled.subscribe(onSignaled);
        if (cancelable === Cancelable.none || isMissing(cancelable)) return Cancelable.none.subscribe(onSignaled);
        return cancelable[Cancelable.cancelSignal]().subscribe(onSignaled);
    }
github esfx / esfx / packages / disposable / src / index.ts View on Github external
export function from(resources: Iterable) {
        const disposablesArray: AsyncDisposable[] = [];
        for (const resource of resources) {
            if (!isMissing(resource) && !AsyncDisposable.hasInstance(resource) && !Disposable.hasInstance(resource)) {
                throw new TypeError("AsyncDisposable element expected: resources");
            }
            if (isMissing(resource)) continue;
            disposablesArray.push(toAsyncDisposable(resource));
        }
        return create(async () => {
            for (const disposable of disposablesArray) {
                await disposable[AsyncDisposable.asyncDispose]();
            }
        });
    }
github esfx / esfx / packages / cancelable / src / index.ts View on Github external
export function isSignaled(cancelable: Cancelable | undefined) {
        if (!isMissing(cancelable) && !isCancelable(cancelable)) throw new TypeError("Cancelable expected: cancelable");
        if (cancelable === Cancelable.canceled) return true;
        if (cancelable === Cancelable.none || isMissing(cancelable)) return false;
        return cancelable[Cancelable.cancelSignal]().signaled;
    }
github esfx / esfx / packages / async-canceltoken / src / index.ts View on Github external
static from(cancelable: Cancelable | null | undefined) {
        if (!isMissing(cancelable) && !Cancelable.hasInstance(cancelable)) {
            throw new TypeError("Cancelable exepected: cancelable");
        }
        if (cancelable === Cancelable.none || isMissing(cancelable)) {
            return CancelToken.none;
        }
        if (cancelable === Cancelable.canceled) {
            return CancelToken.canceled;
        }
        if (cancelable instanceof CancelToken) {
            return cancelable;
        }
        if (cancelSourcePrototype.isPrototypeOf(cancelable)) {
            return (cancelable as CancelSource).token;
        }
        const signal = cancelable[Cancelable.cancelSignal]();
        if (!canBeSignaled(signal)) {
github esfx / esfx / packages / cancelable / src / index.ts View on Github external
export function isSignaled(cancelable: Cancelable | undefined) {
        if (!isMissing(cancelable) && !isCancelable(cancelable)) throw new TypeError("Cancelable expected: cancelable");
        if (cancelable === Cancelable.canceled) return true;
        if (cancelable === Cancelable.none || isMissing(cancelable)) return false;
        return cancelable[Cancelable.cancelSignal]().signaled;
    }
github esfx / esfx / packages / async-semaphore / src / index.ts View on Github external
constructor(initialCount: number, maxCount?: number) {
        if (isMissing(maxCount)) maxCount = MAX_INT32;
        if (!isNumber(initialCount)) throw new TypeError("Number expected: initialCount.");
        if (!isNumber(maxCount)) throw new TypeError("Number expected: maxCount.");
        if ((initialCount |= 0) < 0) throw new RangeError("Argument out of range: initialCount.");
        if ((maxCount |= 0) < 1) throw new RangeError("Argument out of range: maxCount.");
        if (initialCount > maxCount) throw new RangeError("Argument out of range: initialCount.");

        this._currentCount = initialCount;
        this._maxCount = maxCount;
    }
github esfx / esfx / packages / disposable / src / index.ts View on Github external
export function from(resources: Iterable) {
        const disposablesArray: AsyncDisposable[] = [];
        for (const resource of resources) {
            if (!isMissing(resource) && !AsyncDisposable.hasInstance(resource) && !Disposable.hasInstance(resource)) {
                throw new TypeError("AsyncDisposable element expected: resources");
            }
            if (isMissing(resource)) continue;
            disposablesArray.push(toAsyncDisposable(resource));
        }
        return create(async () => {
            for (const disposable of disposablesArray) {
                await disposable[AsyncDisposable.asyncDispose]();
            }
        });
    }
github esfx / esfx / packages / async-barrier / src / index.ts View on Github external
constructor(participantCount: number, postPhaseAction?: (barrier: AsyncBarrier) => void | PromiseLike) {
        if (!isNumber(participantCount)) throw new TypeError("Number expected: participantCount.");
        if ((participantCount |= 0) < 0) throw new RangeError("Argument out of range: participantCount.");
        if (!isMissing(postPhaseAction) && !isFunction(postPhaseAction)) throw new TypeError("Function expected: postPhaseAction.");

        this._participantCount = participantCount;
        this._remainingParticipants = participantCount;
        this._postPhaseAction = postPhaseAction;
    }
github esfx / esfx / packages / async-countdown / src / index.ts View on Github external
add(count?: number): void {
        if (isMissing(count)) count = 1;
        if (!isNumber(count)) throw new TypeError("Number expected: count.");
        if ((count |= 0) <= 0) throw new RangeError("Argument out of range: count.");
        if (this._remainingCount === 0) throw new Error("The event is already signaled and cannot be incremented.");

        if (this._remainingCount > 0) {
            this._remainingCount += count;
        }
    }
github esfx / esfx / packages / reflect-metadata-compat / src / index.ts View on Github external
function decorateMember(decorators: (PropertyDecorator | MethodDecorator)[], target: object, propertyKey: PropertyKey, descriptor?: PropertyDescriptor): PropertyDescriptor | undefined {
        if (typeof propertyKey !== "symbol") propertyKey = "" + propertyKey;
        for (let i = decorators.length - 1; i >= 0; i--) {
            const decorator = decorators[i];
            const decorated = decorator(target, propertyKey, descriptor!);
            if (isDefined(decorated)) {
                if (!isObject(decorated)) throw new TypeError();
                descriptor = decorated;
            }
        }
        return descriptor;
    }

@esfx/internal-guards

This package provides internal utilities for @esfx and is not intended for use in user-code.

Apache-2.0
Latest version published 2 years ago

Package Health Score

51 / 100
Full package analysis

Similar packages