1 | # A cls session implementation use async_hooks
|
2 |
|
3 | Continuation Local Storage works like thread-local storage in threaded programming. This is a implementation of CLS using async_hooks instead of async-listener.
|
4 |
|
5 | It need running nodejs version >= 8.2.1.
|
6 |
|
7 | ## Example
|
8 |
|
9 | ``` js
|
10 | const Session = require('./index')
|
11 |
|
12 | const session = new Session()
|
13 |
|
14 | function timeout (id) {
|
15 | session.scope(() => {
|
16 | session.set('a', id)
|
17 | setTimeout(() => {
|
18 | const a = session.get('a')
|
19 | console.log(a)
|
20 | })
|
21 | })
|
22 | }
|
23 |
|
24 | timeout(1)
|
25 | timeout(2)
|
26 | timeout(3)
|
27 |
|
28 | // Output:
|
29 | // 1
|
30 | // 2
|
31 | // 3
|
32 | ```
|
33 |
|
34 | ## Class: Session
|
35 |
|
36 | ### session.scope(callback)
|
37 |
|
38 | ### session.set(key, value)
|
39 |
|
40 | ### session.get(key)
|
41 |
|
42 | ### session.context
|
43 |
|