0.0.10 • Published 5 years ago

node-async-context v0.0.10

Weekly downloads
54
License
MIT
Repository
github
Last release
5 years ago

node 异步上下文容器

容器 Api

create() (应用启动时只需要调用一次, 重复调用会抛出异常)

创建异步上下文容器

    const asyncContext = require('node-async-context')
    process.context = asyncContext.create()

实例 Api

set(key, val)

设置当前异步上下文的值

    process.context.set('demoKey', 'demoValue')

get(key)

获取当前异步上下文的值

    const value = process.context.get('demoKey')

del(key)

删除当前异步上下文的值

    process.context.del('demoKey')

destory() (在请求结束前(尾中间件)务必调用销毁,否则一些异步类型(如Promise)会引起内存泄漏)

销毁当前异步上下文

    process.context.destroy()

getByAsyncId (不常用)

获取当前异步上下文

    const ah = require('async_hooks')

    const asyncId = ah.executionAsyncId()
    const currentContext = process.context.getByAsyncId(asyncId)

setByAsyncId (不常用)

设置当前异步上下文

    const ah = require('async_hooks')

    const asyncId = ah.executionAsyncId()
    process.context.setByAsyncId(asyncId, { demoKey: 'demoValue' })

destroyByAsyncId (不常用)

销毁当前异步上下文

    const ah = require('async_hooks')

    const asyncId = ah.executionAsyncId()
    process.context.destroyByAsyncId(asyncId)

run(cb) (其中 cb 是回调函数)

运行一个新的异步上下文

    const express = require('express')
    const app = express()

    app.use((req, res, next) => {
        process.context.run(next)
    })
0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago