Skip to content

Commit 87b5dfa

Browse files
feat: add hooks before/after connect/disconnect
1 parent 7a5e78a commit 87b5dfa

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

src/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const DEFAULTS: ModuleOptions = {
1616

1717
const nuxtModule: Module<ModuleOptions> = function (moduleOptions) {
1818
this.nuxt.hook('builder:extendPlugins', (plugins: NuxtOptionsPlugin[]) => {
19-
const options: ModuleOptions = defu(
19+
const options = defu(
2020
this.options.echo || {},
2121
moduleOptions,
2222
DEFAULTS

src/runtime/echo.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ModuleOptions } from './types'
55

66
export class Echo extends BaseEcho {
77
ctx: Context;
8-
config: Partial<ModuleOptions>;
8+
config: ModuleOptions;
99

1010
constructor (ctx: Context, config: Partial<ModuleOptions> = {}) {
1111
// when enabled authModule, start broadcaster with null
@@ -58,11 +58,28 @@ export class Echo extends BaseEcho {
5858
if (this.config.connectOnLogin && loggedIn) {
5959
// set broadcaster when user logged in
6060
this.options.broadcaster = this.config.broadcaster
61+
62+
if (this.config.onBeforeConnect) {
63+
await this.config.onBeforeConnect()
64+
}
65+
6166
this.connect()
67+
68+
if (this.config.onAfterConnect) {
69+
await this.config.onAfterConnect()
70+
}
6271
}
6372

6473
if (this.config.disconnectOnLogout && !loggedIn && this.connector) {
74+
if (this.config.onBeforeDisconnect) {
75+
await this.config.onBeforeDisconnect()
76+
}
77+
6578
this.disconnect()
79+
80+
if (this.config.onAfterDisconnect) {
81+
await this.config.onAfterDisconnect()
82+
}
6683
}
6784
}).bind(this)
6885
}

src/runtime/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ declare module '@nuxt/types' {
1010
$echo: Echo;
1111
}
1212

13+
// Nuxt 2.14+
1314
interface NuxtConfig {
1415
echo?: Partial<ModuleOptions>
1516
}
1617

18+
// Nuxt 2.9 - 2.13
1719
interface Configuration {
1820
echo?: Partial<ModuleOptions>
1921
}

src/runtime/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ export interface ModuleOptions extends EchoOptions {
88
connectOnLogin?: boolean,
99
disconnectOnLogout?: boolean,
1010
optionsPath?: string,
11+
onBeforeConnect?: Function,
12+
onAfterConnect?: Function,
13+
onBeforeDisconnect?: Function,
14+
onAfterDisconnect?: Function,
1115
}

0 commit comments

Comments
 (0)