Skip to content

Commit

Permalink
fix: should return AsyncLocalStorage type
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 19, 2023
1 parent 79695af commit f9dae20
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { AsyncLocalStorage } from 'node:async_hooks';

export const kGALS = Symbol.for('gals#asyncLocalStorage');

export function getAsyncLocalStorage() {
export function getAsyncLocalStorage<T = any>(): AsyncLocalStorage<T> {
const g: any = globalThis;
if (!g[kGALS]) {
g[kGALS] = new AsyncLocalStorage();
g[kGALS] = new AsyncLocalStorage<T>();
}
return g[kGALS];
}
4 changes: 3 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { getAsyncLocalStorage, kGALS } from '../src/index.js';
describe('test/index.test.ts', () => {
it('should only create one als instance', () => {
const als1 = getAsyncLocalStorage();
const als2 = getAsyncLocalStorage();
const als2 = getAsyncLocalStorage<object>();
assert.equal(als1, als2);
assert.equal(als1, Reflect.get(globalThis, kGALS));
assert.equal(als2, Reflect.get(global, kGALS));
assert.equal(typeof als2.getStore, 'function');
assert.equal(als2.getStore(), undefined);
});
});

0 comments on commit f9dae20

Please sign in to comment.