Git Product home page Git Product logo

async-hooks-map's Introduction

NPM version node version npm download npm license

A Thread-local storage (TLS) like Map implementation, base on node async hooks, support nodejs & typescript

  • thread local support for nodejs & typescript

  • named scope & chain support , easily to get closest forefather scope

  • browser or lower version of node support if provided an async-hooks implementation with constructor

install

npm install async-hooks-map

import

const { AsyncHookMap } = require('async-hooks-map')

Usage

typescript:

    import { AsyncHookMap } from 'async-hooks-map'
    // import asyncHookMap from 'async-hooks-map'
    // import global instance which is lazy initialize
    // Object.defineProperty(exports, 'default', {
    //     get () {}
    // })

    const scope = new AsyncHookMap()

    Promise.resolve().then(() => {
        scope.set('aa', 'first')
        scope.alias('ccc')
        assert.equal(scope.get('aa'), 'first')
        return Promise.resolve().then(() => {
            assert(scope.has('aa'), 'should has the key')
            assert(!scope.has('not'), 'should not has the key')
            assert(!scope.has('aa', false), 'should not has the key in this scope')
            assert.equal(scope.get('aa'), 'first')
            scope.set('aa', 'second')
            assert.equal(scope.get('aa'), 'second')
        }).then(() => {
            assert.equal(scope.get('aa'), 'second')
            assert.equal(scope.closest('ccc').get('aa'), 'first')
            // 'root' as alias of 'ccc'
            assert.equal(scope.closest('root').get('aa'), 'first')
            scope.closest().delete('aa')
            // parent scope 'aa' has been delete, 'aa' will be first
            assert.equal(scope.get('aa'), 'first')
            scope.closest('ccc').set('bb', 'bb')
            assert.equal(scope.get('bb'), 'bb')
            scope.delete('bb')
            // can not be deleted ,because bb is set to "ccc" scope
            assert.equal(scope.get('bb'), 'bb')
        })
    })
})

Api:

export interface AsyncMapNode<K, V> {
    hasName (name: string): boolean
    alias (name: string): this
    parent (name?: string): AsyncMapNode<K, V> | undefined
    closest (name: string): AsyncMapNode<K, V>
    has (key: K, recurse?: boolean): boolean
    get (key: K): V | undefined
    set (key: K, value: V): this
    clear (): void
    delete (key: K): boolean
}

tips

  • closest(name:string) contains this and parent(name?:string) not closest will throw when cant find the scope and parent() will return undefined
  • A async scope can have multiple names
  • Top async scope is named 'root' by default

async-hooks-map's People

Contributors

xujif avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.