Skip to content

Commit 8b5455a

Browse files
Merge pull request #15374 from Farenheith/patch-1
feat: supporting fine async storage control
2 parents 22cc29e + f981498 commit 8b5455a

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

packages/core/guards/guards-consumer.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ export class GuardsConsumer {
2020

2121
for (const guard of guards) {
2222
const result = guard.canActivate(context);
23+
if (typeof result === 'boolean') {
24+
if (!result) {
25+
return false;
26+
}
27+
continue;
28+
}
2329
if (await this.pickResult(result)) {
2430
continue;
2531
}

packages/core/test/guards/guards-consumer.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expect } from 'chai';
22
import { of } from 'rxjs';
33
import { GuardsConsumer } from '../../guards/guards-consumer';
4+
import { AsyncLocalStorage } from 'async_hooks';
45

56
describe('GuardsConsumer', () => {
67
let consumer: GuardsConsumer;
@@ -44,6 +45,34 @@ describe('GuardsConsumer', () => {
4445
expect(canActivate).to.be.true;
4546
});
4647
});
48+
describe('when sync guards initialize AsyncLocalStorages', () => {
49+
it('should keep local storages accessible', async () => {
50+
const storage1 = new AsyncLocalStorage<number>();
51+
const storage2 = new AsyncLocalStorage<number>();
52+
const canActivate = await consumer.tryActivate(
53+
[
54+
{
55+
canActivate: () => {
56+
storage1.enterWith(1);
57+
return true;
58+
},
59+
},
60+
{
61+
canActivate: () => {
62+
storage2.enterWith(2);
63+
return true;
64+
},
65+
},
66+
],
67+
[],
68+
{ constructor: null },
69+
null!,
70+
);
71+
expect(canActivate).to.be.true;
72+
expect(storage1.getStore()).to.equal(1);
73+
expect(storage2.getStore()).to.equal(2);
74+
});
75+
});
4776
});
4877
});
4978
describe('pickResult', () => {

0 commit comments

Comments
 (0)