Skip to content

Commit 003752e

Browse files
fix: options
1 parent 4a246dc commit 003752e

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

src/runtime/echo.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,27 @@ import { ModuleOptions } from './types'
55

66
export class Echo extends BaseEcho {
77
ctx: Context;
8-
config: ModuleOptions;
98

10-
constructor (ctx: Context, config: Partial<ModuleOptions> = {}) {
11-
// when enabled authModule, start broadcaster with null
12-
// because laravel-echo auto connect https://github.com/laravel/echo/blob/master/src/echo.ts#L23
13-
// the connection should be made only when the user logs in
14-
super(defu(config.authModule && config.connectOnLogin ? { broadcaster: 'null' } : {}, config))
9+
constructor (ctx: Context, options: Partial<ModuleOptions> = {}) {
10+
super(defu((ctx.$config || {}).echo || {}, options))
1511

1612
this.ctx = ctx
17-
this.ctx.$config = this.ctx.$config || {} // fallback for Nuxt < 2.13
18-
this.config = defu(this.ctx.$config.echo || {}, { auth: { headers: {} } }, config)
13+
this.options.auth = this.options.auth || {}
1914
}
2015

2116
async init () {
22-
this.options.auth = this.options.auth || {}
2317
this.options.auth.headers = await this.getHeaders()
2418
this.watchAuthState()
2519
}
2620

2721
async getHeaders () {
28-
let headers = this.config.auth.headers || {}
22+
let headers: any = {}
2923

3024
if (typeof headers === 'function') {
3125
headers = await headers(this.ctx)
3226
}
3327

34-
if (this.config.authModule && this.ctx.app.$auth) {
28+
if (this.options.authModule && this.ctx.app.$auth) {
3529
const strategy = this.ctx.app.$auth.strategy
3630

3731
if (strategy.options.name === 'laravelSanctum') {
@@ -51,41 +45,39 @@ export class Echo extends BaseEcho {
5145
}
5246

5347
async connect () {
54-
if (this.config && this.config.onBeforeConnect) {
55-
await this.config.onBeforeConnect()
48+
if (this.options.onBeforeConnect) {
49+
await this.options.onBeforeConnect().bind(this)
5650
}
5751

5852
super.connect()
5953

60-
if (this.config && this.config.onAfterConnect) {
61-
await this.config.onAfterConnect()
54+
if (this.options.onAfterConnect) {
55+
await this.options.onAfterConnect().bind(this)
6256
}
6357
}
6458

6559
async disconnect () {
66-
if (this.config && this.config.onBeforeDisconnect) {
67-
await this.config.onBeforeDisconnect()
60+
if (this.options.onBeforeDisconnect) {
61+
await this.options.onBeforeDisconnect().bind(this)
6862
}
6963

7064
super.disconnect()
7165

72-
if (this.config && this.config.onAfterDisconnect) {
73-
await this.config.onAfterDisconnect()
66+
if (this.options.onAfterDisconnect) {
67+
await this.options.onAfterDisconnect().bind(this)
7468
}
7569
}
7670

7771
watchAuthState () {
78-
if (this.config.authModule && this.ctx.app.$auth) {
72+
if (this.options.authModule && this.ctx.app.$auth) {
7973
this.ctx.app.$auth.$storage.watchState('loggedIn', async (loggedIn: boolean) => {
8074
this.options.auth.headers = await this.getHeaders()
8175

82-
if (this.config.connectOnLogin && loggedIn) {
83-
// set broadcaster when user logged in
84-
this.options.broadcaster = this.config.broadcaster
76+
if (this.options.connectOnLogin && loggedIn) {
8577
await this.connect()
8678
}
8779

88-
if (this.config.disconnectOnLogout && !loggedIn && this.connector) {
80+
if (this.options.disconnectOnLogout && !loggedIn && this.connector) {
8981
await this.disconnect()
9082
}
9183
}).bind(this)

0 commit comments

Comments
 (0)