@@ -5,33 +5,27 @@ import { ModuleOptions } from './types'
5
5
6
6
export class Echo extends BaseEcho {
7
7
ctx : Context ;
8
- config : ModuleOptions ;
9
8
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 ) )
15
11
16
12
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 || { }
19
14
}
20
15
21
16
async init ( ) {
22
- this . options . auth = this . options . auth || { }
23
17
this . options . auth . headers = await this . getHeaders ( )
24
18
this . watchAuthState ( )
25
19
}
26
20
27
21
async getHeaders ( ) {
28
- let headers = this . config . auth . headers || { }
22
+ let headers : any = { }
29
23
30
24
if ( typeof headers === 'function' ) {
31
25
headers = await headers ( this . ctx )
32
26
}
33
27
34
- if ( this . config . authModule && this . ctx . app . $auth ) {
28
+ if ( this . options . authModule && this . ctx . app . $auth ) {
35
29
const strategy = this . ctx . app . $auth . strategy
36
30
37
31
if ( strategy . options . name === 'laravelSanctum' ) {
@@ -51,41 +45,39 @@ export class Echo extends BaseEcho {
51
45
}
52
46
53
47
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 )
56
50
}
57
51
58
52
super . connect ( )
59
53
60
- if ( this . config && this . config . onAfterConnect ) {
61
- await this . config . onAfterConnect ( )
54
+ if ( this . options . onAfterConnect ) {
55
+ await this . options . onAfterConnect ( ) . bind ( this )
62
56
}
63
57
}
64
58
65
59
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 )
68
62
}
69
63
70
64
super . disconnect ( )
71
65
72
- if ( this . config && this . config . onAfterDisconnect ) {
73
- await this . config . onAfterDisconnect ( )
66
+ if ( this . options . onAfterDisconnect ) {
67
+ await this . options . onAfterDisconnect ( ) . bind ( this )
74
68
}
75
69
}
76
70
77
71
watchAuthState ( ) {
78
- if ( this . config . authModule && this . ctx . app . $auth ) {
72
+ if ( this . options . authModule && this . ctx . app . $auth ) {
79
73
this . ctx . app . $auth . $storage . watchState ( 'loggedIn' , async ( loggedIn : boolean ) => {
80
74
this . options . auth . headers = await this . getHeaders ( )
81
75
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 ) {
85
77
await this . connect ( )
86
78
}
87
79
88
- if ( this . config . disconnectOnLogout && ! loggedIn && this . connector ) {
80
+ if ( this . options . disconnectOnLogout && ! loggedIn && this . connector ) {
89
81
await this . disconnect ( )
90
82
}
91
83
} ) . bind ( this )
0 commit comments