-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related to #2 --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/bluelibs/runner/issues/2?shareId=XXXX-XXXX-XXXX-XXXX).
- Loading branch information
1 parent
5847ae3
commit 2e993ea
Showing
4 changed files
with
66 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useContext, withContext } from '../context'; | ||
|
||
describe('ApplicationContext', () => { | ||
it('should create and use application context', async () => { | ||
const context = { user: { name: 'John Doe' }, roles: ['admin'], isLoggedIn: true }; | ||
|
||
const result = withContext(context, () => { | ||
const ctx = useContext(); | ||
return ctx; | ||
}); | ||
|
||
expect(result).toEqual(context); | ||
}); | ||
|
||
it('should handle nested contexts', async () => { | ||
const context1 = { user: { name: 'John Doe' }, roles: ['admin'], isLoggedIn: true }; | ||
const context2 = { user: { name: 'Jane Doe' }, roles: ['user'], isLoggedIn: false }; | ||
|
||
const result = withContext(context1, () => { | ||
const ctx1 = useContext(); | ||
expect(ctx1).toEqual(context1); | ||
|
||
return withContext(context2, () => { | ||
const ctx2 = useContext(); | ||
return ctx2; | ||
}); | ||
}); | ||
|
||
expect(result).toEqual(context2); | ||
}); | ||
|
||
it('should return undefined when no context is set', async () => { | ||
const result = useContext(); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
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,21 @@ | ||
import { AsyncLocalStorage } from 'node:async_hooks'; | ||
|
||
const asyncLocalStorage = new AsyncLocalStorage(); | ||
|
||
export type IApplicationContext = { | ||
user: User; | ||
roles: string[]; | ||
isLoggedIn: boolean; | ||
}; | ||
|
||
export function useContext(): IApplicationContext | undefined { | ||
return asyncLocalStorage.getStore(); | ||
} | ||
|
||
export function withContext<T>( | ||
context: IApplicationContext, | ||
fn: (...args: any[]) => T, | ||
...args: any[] | ||
): T { | ||
return asyncLocalStorage.run(context, fn, ...args); | ||
} |
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