Skip to content
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

WIP feat: Terminates realtime on client logout #548

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 12 additions & 1 deletion src/lib/realtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const APPS_DOCTYPE = 'io.cozy.apps'
/**
* Initialize realtime sockets
*
* Returns function that closes the realtime and unsubscribe handlers
*
* @private
* @param {object}
* @returns {Promise}
Expand All @@ -30,13 +32,22 @@ function initializeRealtime({ getApp, onCreate, onDelete, cozyClient }) {
}
}

let realtime
try {
const realtime = new CozyRealtime({ cozyClient })
realtime = new CozyRealtime({ cozyClient })
realtime.subscribe('created', APPS_DOCTYPE, handleAppCreation)
realtime.subscribe('deleted', APPS_DOCTYPE, handleAppRemoval)
} catch (error) {
console.warn(`Cannot initialize realtime in Cozy-bar: ${error.message}`)
}

return () => {
if (!realtime) {
return
}
realtime.unsubscribe('created', APPS_DOCTYPE, handleAppCreation)
realtime.unsubscribe('deleted', APPS_DOCTYPE, handleAppRemoval)
}
}

export default initializeRealtime
24 changes: 17 additions & 7 deletions src/lib/stack-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,23 @@ const getSettingsAppURL = function() {
* @returns {Promise}
*/
const init = function({ cozyClient: client, onCreate, onDelete }) {
cozyClient = client
if (!cozyClient.isLogged) return
initializeRealtime({
getApp,
onCreate,
onDelete,
cozyClient
cozyClient = client // Setting global variable
let terminateRealtime
const onLogin = () => {
terminateRealtime = initializeRealtime({
getApp,
onCreate,
onDelete,
cozyClient
})
}
if (cozyClient.isLogged) {
onLogin()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu ne tentes pas de réagir à l'événement de login s'il arrive plus tard ?

cozyClient.once('logout', () => {
if (terminateRealtime) {
terminateRealtime()
}
})
}

Expand Down
18 changes: 17 additions & 1 deletion test/lib/stack-client/stack-client.init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import CozyClient from 'cozy-client'
import initializeRealtime from 'lib/realtime'

jest.mock('lib/realtime')
initializeRealtime.mockResolvedValue(Promise.resolve())

const { init } = stack

Expand All @@ -30,6 +29,15 @@ describe('stack client', () => {
await init(params)
}

beforeEach(() => {
isLogged = undefined
isPublic = undefined
initializeRealtime.mockReset().mockImplementation(() => {
terminateRealtime = jest.fn()
return terminateRealtime
})
})

afterAll(() => {
jest.restoreAllMocks()
})
Expand All @@ -49,5 +57,13 @@ describe('stack client', () => {
expect(initializeRealtime).toHaveBeenCalled()
expect(initializeRealtime.mock.calls[0][0].cozyClient).toBe(cozyClient)
})

it('should terminate realtime if the user logs out', async () => {
isLogged = true
await setup()
expect(terminateRealtime).not.toHaveBeenCalled()
cozyClient.emit('logout')
expect(terminateRealtime).toHaveBeenCalled()
})
})
})