feat: supporting fine async storage control

This commit is contained in:
Thiago Oliveira Santos
2025-07-09 10:45:28 -03:00
committed by Thiago Santos
parent 138577e50a
commit f98149853e
2 changed files with 35 additions and 0 deletions

View File

@@ -20,6 +20,12 @@ export class GuardsConsumer {
for (const guard of guards) {
const result = guard.canActivate(context);
if (typeof result === 'boolean') {
if (!result) {
return false;
}
continue;
}
if (await this.pickResult(result)) {
continue;
}

View File

@@ -1,6 +1,7 @@
import { expect } from 'chai';
import { of } from 'rxjs';
import { GuardsConsumer } from '../../guards/guards-consumer';
import { AsyncLocalStorage } from 'async_hooks';
describe('GuardsConsumer', () => {
let consumer: GuardsConsumer;
@@ -44,6 +45,34 @@ describe('GuardsConsumer', () => {
expect(canActivate).to.be.true;
});
});
describe('when sync guards initialize AsyncLocalStorages', () => {
it('should keep local storages accessible', async () => {
const storage1 = new AsyncLocalStorage<number>();
const storage2 = new AsyncLocalStorage<number>();
const canActivate = await consumer.tryActivate(
[
{
canActivate: () => {
storage1.enterWith(1);
return true;
},
},
{
canActivate: () => {
storage2.enterWith(2);
return true;
},
},
],
[],
{ constructor: null },
null!,
);
expect(canActivate).to.be.true;
expect(storage1.getStore()).to.equal(1);
expect(storage2.getStore()).to.equal(2);
});
});
});
});
describe('pickResult', () => {