# async-hooks-map

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

Latest version **1.2.0** (published 2018-09-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install async-hooks-map
pnpm add async-hooks-map
yarn add async-hooks-map
bun add async-hooks-map
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2018-09-02 |
| First published | 2018-06-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=8.6.0 |
| Dependencies | 0 |
| Unpacked size | 57.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | i@xujif.com |
| Maintainers | xujif |
| Keywords | typescript, Thread-local storage (TLS) , thread local, async hooks, async hooks map |

## Links

- npm: https://www.npmjs.com/package/async-hooks-map
- Homepage: https://github.com/xujif/async-hooks-storage
- npm.io page: https://npm.io/package/async-hooks-map

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2018-09-02
- 1.1.1 — 2018-09-02
- 1.1.0 — 2018-06-11
- 1.0.4 — 2018-06-08
- 1.0.3 — 2018-06-06
- 1.0.1 — 2018-06-04
- 1.0.0 — 2018-06-04

## README

[![NPM version][npm-image]][npm-url]
[![node version][node-image]][node-url]
[![npm download][download-image]][download-url]
[![npm license][license-image]][download-url]
### 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
```javascript
// typescript
import { AsyncHookMap } from 'async-hooks-map';
// javascript
const { AsyncHookMap } = require('async-hooks-map')
```
## Usage

typescript: 
```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:
```typescript

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


[npm-image]: https://img.shields.io/npm/v/async-hooks-map.svg?style=flat-square
[npm-url]: https://npmjs.org/package/async-hooks-map
[travis-image]: https://img.shields.io/travis/https://github.com/xujif/async-hooks-map.svg?style=flat-square
[travis-url]: https://travis-ci.org/https://github.com/xujif/async-hooks-map
[coveralls-image]: https://img.shields.io/coveralls/https://github.com/xujif/async-hooks-map.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/https://github.com/xujif/async-hooks-map?branch=master
[david-image]: https://img.shields.io/david/https://github.com/xujif/async-hooks-map.svg?style=flat-square
[david-url]: https://david-dm.org/https://github.com/xujif/async-hooks-map
[node-image]: https://img.shields.io/badge/node.js-%3E=_8.6.0-green.svg?style=flat-square
[node-url]: http://nodejs.org/download/
[download-image]: https://img.shields.io/npm/dm/async-hooks-map.svg?style=flat-square
[download-url]: https://npmjs.org/package/async-hooks-map
[license-image]: https://img.shields.io/npm/l/async-hooks-map.svg

---
_Source: https://npm.io/package/async-hooks-map · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
