Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(di): use async_hook directly on @tsed/di
BREAKING CHANGE: use async_hook directly on @tsed/di. Remove @tsed/async_hook_context. Use @tsed/di to inject context.
- Loading branch information
Showing
23 changed files
with
84 additions
and
453 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {AsyncLocalStorage} from "async_hooks"; | ||
import {DIContext} from "../domain/DIContext"; | ||
|
||
let store: AsyncLocalStorage<DIContext>; | ||
|
||
export function getAsyncStore() { | ||
store = store || new AsyncLocalStorage(); | ||
return store; | ||
} | ||
|
||
export function getContext() { | ||
return store?.getStore(); | ||
} | ||
|
||
export function runInContext(ctx: DIContext, cb: any) { | ||
return getAsyncStore().run(ctx, cb); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {DIContext, DITest, Injectable, InjectContext, runInContext} from "@tsed/di"; | ||
import {expect} from "chai"; | ||
|
||
@Injectable() | ||
class MyService { | ||
@InjectContext() | ||
$ctx: DIContext; | ||
|
||
get() { | ||
return this.$ctx.id; | ||
} | ||
} | ||
|
||
describe("InjectContext", () => { | ||
beforeEach(() => DITest.create()); | ||
afterEach(() => DITest.reset()); | ||
|
||
it("should inject context", () => { | ||
const ctx = DITest.createDIContext(); | ||
const service = DITest.get<MyService>(MyService); | ||
|
||
const result = runInContext(ctx, () => service.get()); | ||
|
||
expect(result).to.equal("id"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.