Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): handle nested context correctly with UseCls decorator #119

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/core/src/lib/cls-initializers/use-cls.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CLS_ID } from '../cls.constants';
import { ClsModule } from '../cls.module';
import { ClsService } from '../cls.service';
import { UseCls } from './use-cls.decorator';
import { ClsServiceManager } from '../cls-service-manager';

@Injectable()
class TestClass {
Expand Down Expand Up @@ -38,6 +39,7 @@ class TestClass {
return {
id: this.cls.getId(),
value: this.cls.get('value'),
inheritedValue: this.cls.get('inheritedValue'),
};
}
}
Expand All @@ -61,6 +63,20 @@ describe('@UseCls', () => {
value: 'something',
});
});

it('should handle nested context', async () => {
const cls = ClsServiceManager.getClsService();
await cls.run(async () => {
cls.set('inheritedValue', 'other');
const result = await testClass.startContext('something');
expect(result).toEqual({
id: 'the-id',
value: 'something',
inheritedValue: 'other',
});
});
});

it('calls id generator and setup and uses correct this', async () => {
const result = await testClass.startContextWithIdAndSetup(
'something else',
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/cls.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ export class ClsService<S extends ClsStore = ClsStore> {
let options: ClsContextOptions;
let callback: () => any;
if (typeof optionsOrCallback === 'object') {
options = optionsOrCallback;
options = { ...new ClsContextOptions(), ...optionsOrCallback };
Papooch marked this conversation as resolved.
Show resolved Hide resolved
callback = maybeCallback;
} else {
options = { ...new ClsContextOptions(), ...optionsOrCallback };
options = new ClsContextOptions();
Papooch marked this conversation as resolved.
Show resolved Hide resolved
callback = optionsOrCallback;
}
if (!this.isActive()) return this.runWith({} as S, callback);
Expand Down
Loading