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

feat: Add scope inheritance #1343

Merged
merged 3 commits into from
Jun 1, 2018
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
3 changes: 2 additions & 1 deletion packages/core/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ export abstract class BaseClient<B extends Backend, O extends Options>
/**
* @inheritDoc
*/
public createScope(): Scope {
public createScope(parentScope?: Scope): Scope {
const newScope = new Scope();
newScope.setParentScope(parentScope);
newScope.setOnChange((scope: Scope) => {
this.getBackend().storeScope(scope);
});
Expand Down
18 changes: 16 additions & 2 deletions packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ export class Scope implements BaseScope {
* @param tags Tags context object to merge into current context.
*/
public setTag(key: string, value: string): void {
this.tags[key] = value;
this.tags = {
...this.tags,
[key]: value,
};
this.notifyListeners();
}

Expand All @@ -80,7 +83,10 @@ export class Scope implements BaseScope {
* @param extra Extra context object to merge into current context.
*/
public setExtra(key: string, extra: any): void {
this.extra[key] = extra;
this.extra = {
...this.extra,
[key]: extra,
};
this.notifyListeners();
}

Expand All @@ -93,6 +99,14 @@ export class Scope implements BaseScope {
this.notifyListeners();
}

/**
* Inherit values from the parent scope.
* @param scope
*/
public setParentScope(scope?: Scope): void {
Object.assign(this, scope);
}

/** Returns breadcrumbs. */
public getBreadcrumbs(): Breadcrumb[] {
return this.breadcrumbs;
Expand Down
62 changes: 62 additions & 0 deletions packages/core/test/lib/scope.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Scope } from '../../src/scope';

describe('Scope', () => {
test('fingerprint', () => {
const scope = new Scope();
scope.setFingerprint(['abcd']);
expect(scope.getFingerprint()).toEqual(['abcd']);
});

test('extra', () => {
const scope = new Scope();
scope.setExtra('a', 1);
expect(scope.getExtra()).toEqual({ a: 1 });
});

test('tags', () => {
const scope = new Scope();
scope.setTag('a', 'b');
expect(scope.getTags()).toEqual({ a: 'b' });
});

test('user', () => {
const scope = new Scope();
scope.setUser({ id: '1' });
expect(scope.getUser()).toEqual({ id: '1' });
});

test('breadcrumbs', () => {
const scope = new Scope();
scope.addBreadcrumb({ message: 'test' }, 100);
expect(scope.getBreadcrumbs()).toEqual([{ message: 'test' }]);
});

test('basic inheritance', () => {
const parentScope = new Scope();
parentScope.setExtra('a', 1);

const scope = new Scope();
scope.setParentScope(parentScope);
expect(parentScope.getExtra()).toEqual(scope.getExtra());
});

test('parent changed inheritance', () => {
const parentScope = new Scope();
const scope = new Scope();
scope.setParentScope(parentScope);
parentScope.setExtra('a', 2);
expect(scope.getExtra()).toEqual({});
expect(parentScope.getExtra()).toEqual({ a: 2 });
});

test('child override inheritance', () => {
const parentScope = new Scope();
parentScope.setExtra('a', 1);

const scope = new Scope();
scope.setParentScope(parentScope);
scope.setExtra('a', 2);
expect(parentScope.getExtra()).toEqual({ a: 1 });
expect(scope.getExtra()).toEqual({ a: 2 });
});
});
11 changes: 8 additions & 3 deletions packages/shim/src/shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ export class Shim {
*/
public pushScope(client?: any): void {
const usedClient = client || this.getCurrentClient();
// We want to clone the last scope and not create a new one
const stack = this.getStack();
const parentScope =
stack.length > 0 ? stack[stack.length - 1].scope : undefined;
this.getStack().push({
client: usedClient,
scope: this.createScope(usedClient),
scope: this.createScope(usedClient, parentScope),
type: 'local',
});
}
Expand Down Expand Up @@ -120,11 +124,12 @@ export class Shim {
* Obtains a new scope instance from the client.
*
* @param client A SDK client that implements `createScope`.
* @param parentScope Optional parent scope to inherit from.
* @returns The scope instance or an empty object on error.
*/
public createScope(client?: any): Scope | undefined {
public createScope(client?: any, parentScope?: Scope): Scope | undefined {
try {
return client && client.createScope();
return client && client.createScope(parentScope);
} catch {
return undefined;
}
Expand Down
7 changes: 6 additions & 1 deletion packages/shim/test/mocks/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export class ScopeMock implements Scope {
}

public setExtra(key: string, extra: any): void {
this.extra[key] = extra;
this.extra = {
...this.extra,
[key]: extra,
};
}

public setFingerprint(fingerprint: string[]): void {
Expand All @@ -26,6 +29,8 @@ export class ScopeMock implements Scope {

public clear(): void {
this.user = undefined;
this.extra = {};
this.fingerprint = undefined;
}
}

Expand Down