Skip to content

Commit

Permalink
refactor: 🏷️ update session types
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerbenw committed Dec 4, 2024
1 parent 5610953 commit bdeecba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
18 changes: 12 additions & 6 deletions packages/core/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@ interface SessionDeliveryPayload {
notifier?: Notifier
device?: Device
app?: App
sessions?: Session[]
sessions?: Array<{
id: string
startedAt: Date
user?: User
}>
}

interface Delivery {
sendEvent(payload: EventDeliveryPayload, cb: (err?: Error | null) => void): void
sendSession(session: SessionDeliveryPayload, cb: (err?: Error | null) => void): void
}

export interface SessionDelegate {
startSession: (client: ClientWithInternals, session: Session) => ClientWithInternals
pauseSession: (client: ClientWithInternals) => void
resumeSession: (client: ClientWithInternals) => ClientWithInternals
}

/**
* Extend the public type definitions with internal declarations.
*
Expand All @@ -58,11 +68,7 @@ export default class ClientWithInternals<T extends Config = Config> extends Clie
_session: Session | null
_pausedSession: Session | null

_sessionDelegate: {
startSession: (client: ClientWithInternals, session: Session) => ClientWithInternals
pauseSession: (client: ClientWithInternals) => void
resumeSession: (client: ClientWithInternals) => ClientWithInternals
}
_sessionDelegate: SessionDelegate

_addOnSessionPayload: (cb: (sessionPayload: Session) => void) => void

Expand Down
34 changes: 9 additions & 25 deletions packages/plugin-browser-session/src/session.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
import { Client, Logger, Plugin, Session } from '@bugsnag/core'
import { Plugin, Session } from '@bugsnag/core'
import ClientWithInternals, { Notifier } from '@bugsnag/core/client'
import includes from '@bugsnag/core/lib/es-utils/includes'

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
interface InternalClient extends ClientWithInternals {
_notifier?: Notifier
}

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

Expand All @@ -35,7 +19,7 @@ const sessionDelegate = {
sessionClient._pausedSession = null

// exit early if the current releaseStage is not enabled
if (sessionClient._config.enabledReleaseStages !== null && !includes(sessionClient._config.enabledReleaseStages, sessionClient._config.releaseStage)) {
if (sessionClient._config.enabledReleaseStages && !includes(sessionClient._config.enabledReleaseStages, sessionClient._config.releaseStage)) {
sessionClient._logger.warn('Session not sent due to releaseStage/enabledReleaseStages configuration')
return sessionClient
}
Expand All @@ -51,10 +35,10 @@ const sessionDelegate = {
user: session._user
}
]
})
}, () => {})
return sessionClient
},
resumeSession: (client: { _session: any, _pausedSession: null, startSession: () => any }) => {
resumeSession: (client: InternalClient) => {
// Do nothing if there's already an active session
if (client._session) {
return client
Expand All @@ -71,7 +55,7 @@ const sessionDelegate = {
// Otherwise start a new session
return client.startSession()
},
pauseSession: (client: { _pausedSession: any, _session: null }) => {
pauseSession: (client: InternalClient) => {
client._pausedSession = client._session
client._session = null
}
Expand Down

0 comments on commit bdeecba

Please sign in to comment.