Skip to content

Commit

Permalink
feat: CozyRealtime handles login/logout
Browse files Browse the repository at this point in the history
Since the underlying instance of CozyRealtime can change, users should
not keep a handle on client.plugins.realtime.realtime
  • Loading branch information
ptbrowne committed Mar 25, 2020
1 parent acccb84 commit 44cbcbc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/cozy-realtime/src/RealtimePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,25 @@ class RealtimePlugin {
* @param {CozyClient} client A cozy-client instance
*/
constructor(client) {
this.realtime = new CozyRealtime({ client })
this.client = client
this.realtime = null
this.handleLogin = this.handleLogin.bind(this)
this.handleLogout = this.handleLogout.bind(this)
this.client.on('login', this.handleLogin)
this.client.on('logout', this.handleLogout)

if (client.isLogged) this.handleLogin()
}

handleLogin() {
this.realtime = new CozyRealtime({
client: this.client
})
}

handleLogout() {
this.unsubscribeAll()
this.realtime = null
}

/**
Expand Down
13 changes: 13 additions & 0 deletions packages/cozy-realtime/src/RealtimePlugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ it('should expose the same API as CozyRealtime', () => {
expect(client.plugins.realtime.unsubscribe).toBeInstanceOf(Function)
expect(client.plugins.realtime.unsubscribeAll).toBeInstanceOf(Function)
})

it('should login/logout correctly', async () => {
client = new CozyClient({})
client.registerPlugin(RealtimePlugin)
expect(client.plugins.realtime.realtime).toBeNull()
await client.login({
uri: 'http://cozy.tools:8080',
token: 'fake-token'
})
expect(client.plugins.realtime.realtime).not.toBeNull()
await client.logout()
expect(client.plugins.realtime.realtime).toBeNull()
})

0 comments on commit 44cbcbc

Please sign in to comment.