Skip to content

Commit

Permalink
refactor: Use session callbacks for client platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
bengourley committed Dec 17, 2019
1 parent cf04b56 commit a5fc173
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
4 changes: 4 additions & 0 deletions packages/plugin-browser-device/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module.exports = {
userAgent: nav.userAgent
}

client.addOnSession(session => {
session.device = { ...session.device, ...device }
})

// add time just as the event is sent
client.addOnError((event) => {
event.device = {
Expand Down
21 changes: 21 additions & 0 deletions packages/plugin-browser-device/test/device.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,25 @@ describe('plugin: device', () => {
expect(payloads[0].events[0].device.locale).toBe(navigator.browserLanguage)
expect(payloads[0].events[0].device.userAgent).toBe(navigator.userAgent)
})

it('should add an onSession callback which captures device information', () => {
const client = new Client({ apiKey: 'API_KEY_YEAH' })
const payloads = []
client._sessionDelegate = {
startSession: (client, session) => {
client._delivery.sendSession(session)
}
}
client.use(plugin, navigator)

expect(client._cbs.s.length).toBe(1)

client._setDelivery(client => ({ sendSession: (payload) => payloads.push(payload) }))
client.startSession()

expect(payloads.length).toEqual(1)
expect(payloads[0].device).toBeDefined()
expect(payloads[0].device.locale).toBe(navigator.browserLanguage)
expect(payloads[0].device.userAgent).toBe(navigator.userAgent)
})
})
14 changes: 3 additions & 11 deletions packages/plugin-browser-session/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const sessionDelegate = {
return sessionClient
}

const payload = {
sessionClient._delivery.sendSession({
notifier: sessionClient._notifier,
device: {},
device: session.device,
app: { ...{ releaseStage }, ...sessionClient.app },
sessions: [
{
Expand All @@ -31,15 +31,7 @@ const sessionDelegate = {
user: sessionClient._user
}
]
}

const ignore = runSyncCallbacks(sessionClient._cbs.sp.slice(0), payload, 'onSessionPayload', sessionClient._logger)
if (ignore) {
sessionClient._logger.debug('Session not sent due to onSessionPayload callback')
return sessionClient
}

sessionClient._delivery.sendSession(payload)
})
return sessionClient
},
resumeSession: (client) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-expo-device/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ module.exports = {
}
}

client._addOnSessionPayload(sp => {
sp.device = { ...sp.device, ...device }
client.addOnSession(session => {
session.device = { ...session.device, ...device }
})

client.addOnError(event => {
Expand Down

0 comments on commit a5fc173

Please sign in to comment.