Skip to content

Commit b5a1e07

Browse files
fix: hooks
1 parent 96a5060 commit b5a1e07

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/runtime/echo.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,30 @@ export class Echo extends BaseEcho {
5050
return defu(headers, this.options.auth.headers)
5151
}
5252

53+
async connect () {
54+
if (this.config && this.config.onBeforeConnect) {
55+
await this.config.onBeforeConnect()
56+
}
57+
58+
super.connect()
59+
60+
if (this.config && this.config.onAfterConnect) {
61+
await this.config.onAfterConnect()
62+
}
63+
}
64+
65+
async disconnect () {
66+
if (this.config && this.config.onBeforeDisconnect) {
67+
await this.config.onBeforeDisconnect()
68+
}
69+
70+
super.disconnect()
71+
72+
if (this.config && this.config.onAfterDisconnect) {
73+
await this.config.onAfterDisconnect()
74+
}
75+
}
76+
5377
watchAuthState () {
5478
if (this.config.authModule && this.ctx.app.$auth) {
5579
this.ctx.app.$auth.$storage.watchState('loggedIn', async (loggedIn: boolean) => {
@@ -58,28 +82,11 @@ export class Echo extends BaseEcho {
5882
if (this.config.connectOnLogin && loggedIn) {
5983
// set broadcaster when user logged in
6084
this.options.broadcaster = this.config.broadcaster
61-
62-
if (this.config.onBeforeConnect) {
63-
await this.config.onBeforeConnect()
64-
}
65-
66-
this.connect()
67-
68-
if (this.config.onAfterConnect) {
69-
await this.config.onAfterConnect()
70-
}
85+
await this.connect()
7186
}
7287

7388
if (this.config.disconnectOnLogout && !loggedIn && this.connector) {
74-
if (this.config.onBeforeDisconnect) {
75-
await this.config.onBeforeDisconnect()
76-
}
77-
78-
this.disconnect()
79-
80-
if (this.config.onAfterDisconnect) {
81-
await this.config.onAfterDisconnect()
82-
}
89+
await this.disconnect()
8390
}
8491
}).bind(this)
8592
}

0 commit comments

Comments
 (0)