Archived
1

rename project

This commit is contained in:
Conduitry 2017-06-24 16:30:25 -04:00
parent c40c877cb1
commit 7d9747a986
2 changed files with 27 additions and 27 deletions

View File

@ -1,6 +1,6 @@
# AsyncContexter
# Contexty
**AsyncContexter** is a very simple implementation of a "thread-local context"-esque concept for Node.js, based on asynchronous resources.
**Contexty** is a very simple implementation of a "thread-local context"-esque concept for Node.js, based on asynchronous resources.
For example: Early in handling an HTTP request, you can create a new context. Elsewhere in your code, you can retrieve the current context and get/set values on it. The context is preserved for the duration of that HTTP request, but is kept separate for different HTTP requests.
@ -10,51 +10,51 @@ A current nightly Node.js build, for now. My understanding is that the `async_ho
## Usage
Create an instance of `AsyncContexter` to create a "namespace" in which you want to share contexts. You should use the same `AsyncContexter` anywhere you want to have access to the same context. In many applications, you will only need a single `AsyncContexter` instance, which should be created *outside* your code which handles requests etc.
Create an instance of `Contexty` to create a "namespace" in which you want to share contexts. You should use the same `Contexty` anywhere you want to have access to the same context. In many applications, you will only need a single `Contexty` instance, which should be created *outside* your code which handles requests etc.
`let contexter = new AsyncContexter()`
`let contexty = new Contexty()`
When you want to create a new context, retrieve `contexter.new`. This is a getter which returns a new context (an object with `null` prototype). Store whatever you want on here. Later in the same or in a descendent asynchronous resource, the `contexter.current` getter will return that same context object.
When you want to create a new context, retrieve `contexty.new`. This is a getter which returns a new context (an object with `null` prototype). Store whatever you want on here. Later in the same or in a descendent asynchronous resource, the `contexty.current` getter will return that same context object.
Retrieving `contexter.new` when there is already an asynchronous context will create a new context with the old one as its prototype, so you have access to all the parent values, but new values you add to the context will not affect the parent context.
Retrieving `contexty.new` when there is already an asynchronous context will create a new context with the old one as its prototype, so you have access to all the parent values, but new values you add to the context will not affect the parent context.
## API
### `new AsyncContexter()`
### `new Contexty()`
Creates a new object to manage async contexts for a particular purpose.
### `AsyncContexter#new`
### `Contexty#new`
This getter creates and returns a new context. If a context already existed, the new context will be a child context. You can access values stored on the parent context, and any changes will no longer be accessible once you are out of this asynchronous call tree.
### `AsyncContexter#current`
### `Contexty#current`
This getter retrieves the context created by the appropriate asynchronously ancestral `AsyncContexter#new`.
This getter retrieves the context created by the appropriate asynchronously ancestral `Contexty#new`.
## Example
```javascript
let contexter = new AsyncContexter()
let contexty = new Contexty()
let counter = 0
async function test() {
contexter.new.foo = ++counter
console.log('A', contexter.current.foo)
contexty.new.foo = ++counter
console.log('A', contexty.current.foo)
await sleep(1000)
console.log('B', contexter.current.foo)
console.log('B', contexty.current.foo)
setTimeout(test2, 2000)
await sleep(4000)
console.log('C', contexter.current.foo)
console.log('C', contexty.current.foo)
}
function test2() {
console.log('D', contexter.current.foo)
contexter.new.foo = 'x'
console.log('E', contexter.current.foo)
console.log('D', contexty.current.foo)
contexty.new.foo = 'x'
console.log('E', contexty.current.foo)
sleep(1000).then(() => {
console.log('F', contexter.current.foo)
console.log('F', contexty.current.foo)
})
}
@ -94,4 +94,4 @@ C 3
Copyright (c) 2017 Conduitry
- [MIT](https://github.com/Conduitry/async-contexter/blob/master/LICENSE)
- [MIT](https://github.com/Conduitry/contexty/blob/master/LICENSE)

View File

@ -1,32 +1,32 @@
import { createHook, executionAsyncId } from 'async_hooks'
let asyncContexters = new Map()
let contexties = new Map()
createHook({
init(asyncId, type, triggerAsyncId) {
for (let contexts of asyncContexters.values()) {
for (let contexts of contexties.values()) {
contexts.set(asyncId, contexts.get(triggerAsyncId))
}
},
destroy(asyncId) {
for (let contexts of asyncContexters.values()) {
for (let contexts of contexties.values()) {
contexts.delete(asyncId)
}
},
}).enable()
export default class AsyncContexter {
export default class Contexty {
constructor() {
asyncContexters.set(this, new Map())
contexties.set(this, new Map())
}
get new() {
let asyncId = executionAsyncId()
let contexts = asyncContexters.get(this)
let contexts = contexties.get(this)
let context = Object.create(contexts.get(asyncId) || null)
contexts.set(asyncId, context)
return context
}
get current() {
return asyncContexters.get(this).get(executionAsyncId())
return contexties.get(this).get(executionAsyncId())
}
}