Skip to content

Commit

Permalink
refactor @bugsnag/plugin-browser-session
Browse files Browse the repository at this point in the history
  • Loading branch information
AnastasiiaSvietlova authored and gingerbenw committed Dec 4, 2024
1 parent 9a4591e commit 8734597
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 10 deletions.
2 changes: 2 additions & 0 deletions packages/core/types/session.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ declare class Session {
public device?: Device;
public app?: App;

public _user?: User;

public getUser(): User;
public setUser(id?: string, email?: string, name?: string): void;
}
Expand Down
17 changes: 15 additions & 2 deletions packages/plugin-browser-session/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{
"name": "@bugsnag/plugin-browser-session",
"version": "8.1.1",
"main": "session.js",
"main": "dist/session.js",
"types": "dist/types/session.d.ts",
"exports": {
".": {
"types": "./dist/types/session.d.ts",
"default": "./dist/session.js",
"import": "./dist/session.mjs"
}
},
"description": "@bugsnag/js plugin to enable session tracking in browsers",
"homepage": "https://www.bugsnag.com/",
"repository": {
Expand All @@ -12,7 +20,7 @@
"access": "public"
},
"files": [
"*.js"
"dist"
],
"author": "Bugsnag",
"license": "MIT",
Expand All @@ -21,5 +29,10 @@
},
"peerDependencies": {
"@bugsnag/core": "^8.0.0"
},
"scripts": {
"build": "npm run build:npm",
"build:npm": "rollup --config rollup.config.npm.mjs",
"clean": "rm -rf dist/*"
}
}
6 changes: 6 additions & 0 deletions packages/plugin-browser-session/rollup.config.npm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import createRollupConfig from "../../.rollup/index.mjs";

export default createRollupConfig({
input: "src/session.ts",
external: ["@bugsnag/core/lib/es-utils/includes"]
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
const includes = require('@bugsnag/core/lib/es-utils/includes')
import { Client, Logger, Plugin, Session } from '@bugsnag/core'
import includes from '@bugsnag/core/lib/es-utils/includes'

module.exports = {
load: client => { client._sessionDelegate = sessionDelegate }
interface SessionDelegate {
startSession: (client: InternalClient, session: Session) => InternalClient
resumeSession: (client: { _session: any, _pausedSession: null, startSession: () => any}) => any
pauseSession: (client: { _pausedSession: any, _session: null }) => void
}

interface InternalClient extends Client {
_config: {
enabledReleaseStages: string[] | null
releaseStage: string
releaseStages: string[]
}
_delivery: any
_logger: Logger
_notifier: any
_session: Session
_sessionDelegate: SessionDelegate
_pausedSession: any
}

const plugin: Plugin = {
load: client => {
const internalClient = client as InternalClient
internalClient._sessionDelegate = sessionDelegate
}
}

const sessionDelegate = {
startSession: (client, session) => {
startSession: (client: InternalClient, session: Session) => {
const sessionClient = client
sessionClient._session = session
sessionClient._pausedSession = null
Expand All @@ -30,7 +54,7 @@ const sessionDelegate = {
})
return sessionClient
},
resumeSession: (client) => {
resumeSession: (client: { _session: any, _pausedSession: null, startSession: () => any }) => {
// Do nothing if there's already an active session
if (client._session) {
return client
Expand All @@ -47,8 +71,10 @@ const sessionDelegate = {
// Otherwise start a new session
return client.startSession()
},
pauseSession: (client) => {
pauseSession: (client: { _pausedSession: any, _session: null }) => {
client._pausedSession = client._session
client._session = null
}
}

export default plugin
2 changes: 1 addition & 1 deletion packages/plugin-browser-session/test/session.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import plugin from '../'
import plugin from '../src/session'
import Client, { EventDeliveryPayload } from '@bugsnag/core/client'
import EventWithInternals from '@bugsnag/core/event'

Expand Down
7 changes: 7 additions & 0 deletions packages/plugin-browser-session/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*.ts"],
"compilerOptions": {
"target": "ES2020"
}
}
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
"packages/plugin-node-device",
"packages/plugin-node-surrounding-code",
"packages/plugin-node-uncaught-exception",
"packages/plugin-browser-session",
"packages/browser"
],
"exclude": [
Expand Down

0 comments on commit 8734597

Please sign in to comment.