npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

async-hooks-map

v1.2.0

Published

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

Downloads

335

Readme

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

// typescript
import { AsyncHookMap } from 'async-hooks-map';
// javascript
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 class AsyncHookMap<K=any, V=any>{

    /**
     * alias to asynchooks.executionAsyncId()
     *
     * @returns {number}
     * @memberof AsyncHookMap
     */
    executionAsyncId (): number {
        return this._asyncHooks.executionAsyncId()
    }

    /**
     * get the current AsyncMapNode
     *
     * @returns {AsyncMapNode<K, V>}
     * @memberof AsyncHookMap
     */
    current (): AsyncMapNode<K, V> 

    /**
     * add alias of AsyncNode, a AsyncNode can own multi names
     * 
     * @param {string} name 
     * @returns {this} 
     */
    alias (name: string): this

    /**
     * check the alias name
     * 
     * @param {string} name 
     * @returns {boolean} 
     */
    hasName (name: string): boolean 

    /** 
     * get parent AsyncMapNode
     * if name provided , return the named closest AsyncMapNode
     * this method will throw an error if there is no parent
     * 
     * @param {string} [name] 
     * @returns {AsyncMapNode<K, V>} 
     * @memberof AsyncStorageInterface
     */
    parent (name?: string): AsyncMapNode<K, V> | undefined

    /**
     * alias of parent
     * 
     * @param {string} name 
     * @returns {AsyncMapNode<K, V>} 
     * @memberof AsyncStorage
     */
    closest (name: string): AsyncMapNode<K, V>

    /**
     * get from AsyncStorage
     * 
     * @param {K} key 
     * @returns {(V | undefined)} 
     * @memberof AsyncStorage
     */
    get (key: K): V | undefined 

    /**
     * check key
     * @param key 
     * @param recursion check forefathers?
     */
    has (key: K, recursion = true): boolean

    /**
     * set value to current async AsyncNode
     * effect current AsyncNode and children
     * 
     * @param {K} key 
     * @param {V} value 
     * @returns {this} 
     * @memberof AsyncStorage
     */
    set (key: K, value: V): this

    /**
     * delete the value of current AsyncNode
     * 
     * @param {K} key 
     * @returns {boolean} 
     * @memberof AsyncStorage
     */
    delete (key: K): boolean

    /**
     * clear the current AsyncNode
     * 
     * @memberof AsyncStorage
     */
    clear (): void

    /**
     * print the async path
     *
     * @memberof AsyncHookMap
     */
    printPath (): void

    /**
     * get the distance of scope which has the key
     *
     * @param {K} key
     * @returns {number}
     * @memberof AsyncHookMap
     */
    distance (key: K): number
}
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