Skip to content

Commit 867c731

Browse files
authored
feat(core): Add scope.getLastBreadcrumb() (#6495)
This is needed by replay, which can then stop using the private API `_breadcrumbs`.
1 parent 8ae1ce4 commit 867c731

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

packages/core/src/scope.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,13 @@ export class Scope implements ScopeInterface {
411411
return this;
412412
}
413413

414+
/**
415+
* @inheritDoc
416+
*/
417+
public getLastBreadcrumb(): Breadcrumb | undefined {
418+
return this._breadcrumbs[this._breadcrumbs.length - 1];
419+
}
420+
414421
/**
415422
* @inheritDoc
416423
*/

packages/replay/src/coreHandlers/handleScope.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import { createBreadcrumb } from '../util/createBreadcrumb';
55
let _LAST_BREADCRUMB: null | Breadcrumb = null;
66

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

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

2118
if (
22-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
23-
['fetch', 'xhr', 'sentry.event', 'sentry.transaction'].includes(newBreadcrumb.category) ||
24-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
25-
newBreadcrumb.category?.startsWith('ui.')
19+
newBreadcrumb.category &&
20+
(['fetch', 'xhr', 'sentry.event', 'sentry.transaction'].includes(newBreadcrumb.category) ||
21+
newBreadcrumb.category.startsWith('ui.'))
2622
) {
2723
return null;
2824
}

packages/replay/test/unit/coreHandlers/handleScope.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const mockHandleScope = HandleScope.handleScope as jest.MockedFunction<typeof Ha
88
it('returns a breadcrumb only if last breadcrumb has changed (unit)', function () {
99
const scope = {
1010
_breadcrumbs: [],
11+
getLastBreadcrumb() {
12+
return this._breadcrumbs[this._breadcrumbs.length - 1];
13+
},
1114
} as unknown as Scope;
1215

1316
function addBreadcrumb(breadcrumb: Breadcrumb) {

packages/types/src/scope.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ export interface Scope {
155155
*/
156156
addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): this;
157157

158+
/**
159+
* Get the last breadcrumb.
160+
*/
161+
getLastBreadcrumb(): Breadcrumb | undefined;
162+
158163
/**
159164
* Clears all currently set Breadcrumbs.
160165
*/

0 commit comments

Comments
 (0)