Skip to content

feat(core): Add scope.getLastBreadcrumb() #6495

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

Merged
merged 1 commit into from
Dec 12, 2022
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
7 changes: 7 additions & 0 deletions packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@ export class Scope implements ScopeInterface {
return this;
}

/**
* @inheritDoc
*/
public getLastBreadcrumb(): Breadcrumb | undefined {
return this._breadcrumbs[this._breadcrumbs.length - 1];
}

/**
* @inheritDoc
*/
Expand Down
12 changes: 4 additions & 8 deletions packages/replay/src/coreHandlers/handleScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { createBreadcrumb } from '../util/createBreadcrumb';
let _LAST_BREADCRUMB: null | Breadcrumb = null;

export function handleScope(scope: Scope): Breadcrumb | null {
// TODO: remove ignores here
// @ts-ignore using private val
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const newBreadcrumb = scope._breadcrumbs[scope._breadcrumbs.length - 1];
const newBreadcrumb = scope.getLastBreadcrumb();

// Listener can be called when breadcrumbs have not changed, so we store the
// reference to the last crumb and only return a crumb if it has changed
Expand All @@ -19,10 +16,9 @@ export function handleScope(scope: Scope): Breadcrumb | null {
_LAST_BREADCRUMB = newBreadcrumb;

if (
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
['fetch', 'xhr', 'sentry.event', 'sentry.transaction'].includes(newBreadcrumb.category) ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
newBreadcrumb.category?.startsWith('ui.')
newBreadcrumb.category &&
(['fetch', 'xhr', 'sentry.event', 'sentry.transaction'].includes(newBreadcrumb.category) ||
newBreadcrumb.category.startsWith('ui.'))
) {
return null;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/replay/test/unit/coreHandlers/handleScope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const mockHandleScope = HandleScope.handleScope as jest.MockedFunction<typeof Ha
it('returns a breadcrumb only if last breadcrumb has changed (unit)', function () {
const scope = {
_breadcrumbs: [],
getLastBreadcrumb() {
return this._breadcrumbs[this._breadcrumbs.length - 1];
},
} as unknown as Scope;

function addBreadcrumb(breadcrumb: Breadcrumb) {
Expand Down
5 changes: 5 additions & 0 deletions packages/types/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ export interface Scope {
*/
addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): this;

/**
* Get the last breadcrumb.
*/
getLastBreadcrumb(): Breadcrumb | undefined;

/**
* Clears all currently set Breadcrumbs.
*/
Expand Down