Skip to content

kayahr/scope

Repository files navigation

scope

GitHub | NPM | API Doc

Small, framework-independent ownership scopes for TypeScript.

@kayahr/scope provides disposable ownership scopes, cleanup registration, parent/child scope trees, and typed scope-local values.

Installation

npm install @kayahr/scope

TypeScript consumers currently need a compatible lib configuration including esnext.disposable.

Runtimes without native Symbol.dispose need a polyfill before importing @kayahr/scope, for example from core-js:

import "core-js/proposals/explicit-resource-management";

Basic Usage

Create a scope, activate it while constructing owned resources, and dispose it later as one unit:

import { createScope, onDispose } from "@kayahr/scope";

const scope = createScope();

scope.run(() => {
    const interval = setInterval(() => {
        console.log("tick");
    }, 1000);

    onDispose(() => {
        clearInterval(interval);
    });
});

// ...
scope.dispose();

createScope() creates a scope. Without an explicit parent, it uses the current active scope as parent. Pass null to create an explicit root scope. createScope(scope => ...) is shorthand for creating a scope and running a callback inside it. Only the synchronous execution of scope.run(...) or createScope(scope => ...) belongs to the scope. Work created after an await is outside that scope. If the callback returns a promise, that promise is returned as-is and is not awaited.

Documentation

About

Ownership scopes, cleanup callbacks, and typed scope-local values for TypeScript

Topics

Resources

License

Stars

Watchers

Forks

Contributors