Skip to content

Commit 5fc182c

Browse files
committed
feat: Emit events when plugins are ready
1 parent dd3f57f commit 5fc182c

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

packages/cozy-flags/src/flag.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,12 @@ class FlagClientPlugin {
150150
async handleLogin() {
151151
await flag.initialize(this.client)
152152
this.resolveInitializing()
153+
this.client.emit('plugin:flag:login')
153154
}
154155

155156
async handleLogout() {
156157
flag.reset()
158+
this.client.emit('plugin:flag:logout')
157159
}
158160
}
159161

packages/cozy-flags/src/tests.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,15 @@ export default function testFlagAPI(flag) {
105105
const { client } = setup()
106106
client.isLogged = true
107107
client.registerPlugin(flag.plugin)
108+
const onLogin = jest.fn()
109+
client.on('plugin:flag:login', onLogin)
108110
await client.plugins.flags.initializing
109111
expect(flag('has_feature1')).toBe(true)
110112
expect(flag('has_feature2')).toBe(false)
111113
expect(flag('from_remote')).toBe(true)
112114
expect(flag('number_of_foos')).toBe(10)
113115
expect(flag('bar_config')).toEqual({ qux: 'quux' })
116+
expect(onLogin).toHaveBeenCalled()
114117
})
115118
})
116119

packages/cozy-realtime/src/RealtimePlugin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ class RealtimePlugin {
3030
this.realtime = new CozyRealtime({
3131
client: this.client
3232
})
33+
this.client.emit('plugin:realtime:login')
3334
}
3435

3536
handleLogout() {
3637
this.unsubscribeAll()
3738
this.realtime = null
39+
this.client.emit('plugin:realtime:logout')
3840
}
3941

4042
checkRealtimeInstance() {

packages/cozy-realtime/src/RealtimePlugin.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,23 @@ it('should expose the same API as CozyRealtime', () => {
2626
it('should login/logout correctly', async () => {
2727
client = new CozyClient({})
2828
client.registerPlugin(RealtimePlugin)
29+
30+
const onLogin = jest.fn()
31+
const onLogout = jest.fn()
32+
client.on('plugin:realtime:login', onLogin)
33+
client.on('plugin:realtime:logout', onLogout)
2934
expect(client.plugins.realtime.realtime).toBeNull()
3035
await client.login({
3136
uri: 'http://cozy.tools:8080',
3237
token: 'fake-token'
3338
})
3439
expect(client.plugins.realtime.realtime).not.toBeNull()
40+
expect(onLogin).toHaveBeenCalledTimes(1)
41+
expect(onLogout).toHaveBeenCalledTimes(0)
3542
await client.logout()
3643
expect(client.plugins.realtime.realtime).toBeNull()
44+
expect(onLogin).toHaveBeenCalledTimes(1)
45+
expect(onLogout).toHaveBeenCalledTimes(1)
3746
})
3847

3948
it('throws user friendly errors when trying to use the realtime while logged out', async () => {

0 commit comments

Comments
 (0)