@lzptec/ctx
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

@lzptec/ctx [WIP]

Dead simple context manager for Nodejs

import { AsyncCTX, AsyncContext, ContextToken, Context } from '@lzptec/ctx';

const token = new ContextToken<number>();
const tokenWithDefault = new ContextToken<number>(() => 123);
const context = new AsyncContext();

const printContextValue = () => {
    console.log(context.get(token));
};

const run = () => {
    await context.run(() => {
        printContextValue(); // Print undefined

        // Async Context
        context.set(token, 987);

        printContextValue(); // Print token value 987
    });

    // Outside Async Context 
    printContextValue(); // throw a ReferenceError
};

run();

Package Sidebar

Install

npm i @lzptec/ctx

Weekly Downloads

8

Version

1.0.0

License

MIT

Unpacked Size

16.5 kB

Total Files

8

Last publish

Collaborators

  • aposso