How to use @esfx/disposable - 7 common examples

To help you get started, we’ve selected a few @esfx/disposable 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 / async-mutex / src / index.ts View on Github external
this._handle = undefined;
        }
    }

    // #region AsyncLockable
    [AsyncLockable.lock](cancelable?: Cancelable) {
        return this.lock(cancelable);
    }

    [AsyncLockable.unlock]() {
        this.unlock();
    }
    // #endregion AsyncLockable
}

const disposablePrototype: object = Object.getPrototypeOf(Disposable.create(() => {}));

const mutexLockHandlePrototype: object = {
    [AsyncLockable.lock](this: LockHandle, cancelable?: Cancelable) {
        return this.lock(cancelable);
    },
    [AsyncLockable.unlock](this: LockHandle) {
        return this.unlock();
    },
    [Disposable.dispose](this: LockHandle) {
        if (this.ownsLock) {
            this.unlock();
        }
    }
};

defineTag(mutexLockHandlePrototype, "MutexLockHandle");
github esfx / esfx / packages / async-canceltoken / src / index.ts View on Github external
}
    catch (e) {
        // HostReportError(e);
        setImmediate(() => { throw e; });
    }
}

function isSignaled(signal: CancelSignal) {
    return signal.signaled;
}

function canBeSignaled(signal: CancelSignal) {
    return signal !== Cancelable.none && (!(signal instanceof CancelToken) || signal.canBeSignaled);
}

const disposablePrototype: object = Object.getPrototypeOf(Disposable.create(() => { }));

const cancelSourcePrototype: object = {
    [Cancelable.cancelSignal](this: CancelSource) { return this.token; },
    [CancelableSource.cancel](this: CancelSource) { this.cancel(); },
};

Object.setPrototypeOf(cancelSourcePrototype, disposablePrototype);
defineTag(cancelSourcePrototype, "CancelSource");

function createCancelSource(links: CancelLinks | undefined): CancelSource {
    let state: "unsignaled" | "signaled" | "closed" = "unsignaled";
    let token: CancelToken | undefined;
    let subscriptions: LinkedList<() => void> | undefined;
    const source: CancelSource = Object.setPrototypeOf({
        get token() {
            return token || (token = createCancelToken({
github esfx / esfx / packages / async-mutex / src / index.ts View on Github external
[AsyncLockable.unlock]() {
        this.unlock();
    }
    // #endregion AsyncLockable
}

const disposablePrototype: object = Object.getPrototypeOf(Disposable.create(() => {}));

const mutexLockHandlePrototype: object = {
    [AsyncLockable.lock](this: LockHandle, cancelable?: Cancelable) {
        return this.lock(cancelable);
    },
    [AsyncLockable.unlock](this: LockHandle) {
        return this.unlock();
    },
    [Disposable.dispose](this: LockHandle) {
        if (this.ownsLock) {
            this.unlock();
        }
    }
};

defineTag(mutexLockHandlePrototype, "MutexLockHandle");
Object.setPrototypeOf(mutexLockHandlePrototype, disposablePrototype);

function createLockHandle(mutex: AsyncMutex): LockHandle {
    const handle: LockHandle = Object.setPrototypeOf({
        get mutex() {
            return mutex;
        },
        get ownsLock() {
            return mutex["_handle"] === handle;
github esfx / esfx / packages / async-readerwriterlock / src / index.ts View on Github external
Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

import { Tag, defineTag } from "@esfx/internal-tag";
import { WaitQueue } from "@esfx/async-waitqueue";
import { LockHandle, UpgradeableLockHandle, AsyncLockable } from "@esfx/async-lockable";
import { Cancelable } from "@esfx/cancelable";
import { Disposable } from "@esfx/disposable";

export { LockHandle, UpgradeableLockHandle } from "@esfx/async-lockable";

const disposablePrototype: object = Object.getPrototypeOf(Disposable.create(() => { }));

const lockHandlePrototype: object = {
    get mutex() {
        return this;
    },
    async [AsyncLockable.lock](this: LockHandle, cancelable?: Cancelable) {
        await this.lock(cancelable);
        return this;
    },
    [AsyncLockable.unlock](this: LockHandle) {
        this.unlock();
    },
    [Disposable.dispose](this: LockHandle) {
        if (this.ownsLock) {
            this.unlock();
        }
github esfx / esfx / packages / cancelable / src / index.ts View on Github external
http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

import { Disposable } from "@esfx/disposable";
import { isFunction, isMissing, isObject } from "@esfx/internal-guards";
import { defineTag } from "@esfx/internal-tag";
import { deprecateProperty } from "@esfx/internal-deprecate";

const disposablePrototype = Object.getPrototypeOf(Disposable.create(() => {}));

const cancelSubscriptionPrototype: Disposable = defineTag(Object.setPrototypeOf({
    [Disposable.dispose](this: CancelSubscription) {
        this.unsubscribe();
    },
}, disposablePrototype), "CancelSubscription");

function createCancelSubscription(unsubscribe: () => void): CancelSubscription {
    return Object.setPrototypeOf({
        unsubscribe() {
            unsubscribe();
        },
    }, cancelSubscriptionPrototype);
}

/**
github esfx / esfx / packages / cancelable / src / index.ts View on Github external
Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

import { Disposable } from "@esfx/disposable";
import { isFunction, isMissing, isObject } from "@esfx/internal-guards";
import { defineTag } from "@esfx/internal-tag";
import { deprecateProperty } from "@esfx/internal-deprecate";

const disposablePrototype = Object.getPrototypeOf(Disposable.create(() => {}));

const cancelSubscriptionPrototype: Disposable = defineTag(Object.setPrototypeOf({
    [Disposable.dispose](this: CancelSubscription) {
        this.unsubscribe();
    },
}, disposablePrototype), "CancelSubscription");

function createCancelSubscription(unsubscribe: () => void): CancelSubscription {
    return Object.setPrototypeOf({
        unsubscribe() {
            unsubscribe();
        },
    }, cancelSubscriptionPrototype);
}

/**
 * An object that can be canceled from an external source.
 */
export interface Cancelable {
github esfx / esfx / packages / async-readerwriterlock / src / index.ts View on Github external
export { LockHandle, UpgradeableLockHandle } from "@esfx/async-lockable";

const disposablePrototype: object = Object.getPrototypeOf(Disposable.create(() => { }));

const lockHandlePrototype: object = {
    get mutex() {
        return this;
    },
    async [AsyncLockable.lock](this: LockHandle, cancelable?: Cancelable) {
        await this.lock(cancelable);
        return this;
    },
    [AsyncLockable.unlock](this: LockHandle) {
        this.unlock();
    },
    [Disposable.dispose](this: LockHandle) {
        if (this.ownsLock) {
            this.unlock();
        }
    }
};

defineTag(lockHandlePrototype, "LockHandle");
Object.setPrototypeOf(lockHandlePrototype, disposablePrototype);

const readerPrototype: object = {};
defineTag(readerPrototype, "AsyncReaderWriterLockReader");
Object.setPrototypeOf(readerPrototype, lockHandlePrototype);

const writerPrototype: object = {};
defineTag(writerPrototype, "AsyncReaderWriterLockWriter");
Object.setPrototypeOf(writerPrototype, lockHandlePrototype);

@esfx/disposable

A low-level API for defining explicit resource management.

Apache-2.0
Latest version published 2 years ago

Package Health Score

53 / 100
Full package analysis

Similar packages