15
15
*/
16
16
17
17
import {
18
+ AuthEventKind ,
19
+ AuthRebindEvent ,
18
20
CoreStitchAppClient ,
21
+ CoreStitchServiceClient ,
19
22
CoreStitchServiceClientImpl ,
23
+ RebindEvent ,
20
24
StitchAppClientConfiguration ,
21
25
StitchAppClientInfo ,
22
26
StitchAppRequestClient
@@ -28,17 +32,22 @@ import StitchServiceClientImpl from "../../services/internal/StitchServiceClient
28
32
import StitchServiceClient from "../../services/StitchServiceClient" ;
29
33
import StitchAuthImpl from "../auth/internal/StitchAuthImpl" ;
30
34
import StitchBrowserAppRoutes from "../auth/internal/StitchBrowserAppRoutes" ;
35
+ import StitchAuth from "../auth/StitchAuth" ;
36
+ import StitchAuthListener from "../auth/StitchAuthListener" ;
37
+ import StitchUser from "../auth/StitchUser" ;
31
38
import StitchAppClient from "../StitchAppClient" ;
32
39
33
-
34
40
/** @hidden */
35
- export default class StitchAppClientImpl implements StitchAppClient {
41
+ export default class StitchAppClientImpl implements StitchAppClient ,
42
+ StitchAuthListener {
36
43
public readonly auth : StitchAuthImpl ;
37
44
38
45
private readonly coreClient : CoreStitchAppClient ;
39
46
private readonly info : StitchAppClientInfo ;
40
47
private readonly routes : StitchBrowserAppRoutes ;
41
48
49
+ private serviceClients : CoreStitchServiceClient [ ] ;
50
+
42
51
public constructor (
43
52
clientAppId : string ,
44
53
config : StitchAppClientConfiguration
@@ -64,42 +73,75 @@ export default class StitchAppClientImpl implements StitchAppClient {
64
73
this . info
65
74
) ;
66
75
this . coreClient = new CoreStitchAppClient ( this . auth , this . routes ) ;
76
+ this . serviceClients = [ ] ;
77
+ this . auth . addSynchronousAuthListener ( this ) ;
67
78
}
68
79
69
80
public getServiceClient < T > (
70
81
factory : ServiceClientFactory < T > | NamedServiceClientFactory < T > ,
71
82
serviceName ?: string
72
83
) : T {
73
84
if ( isServiceClientFactory ( factory ) ) {
74
- return factory . getClient (
75
- new CoreStitchServiceClientImpl ( this . auth , this . routes . serviceRoutes , "" ) ,
76
- this . info
85
+ const serviceClient = new CoreStitchServiceClientImpl (
86
+ this . auth , this . routes . serviceRoutes , ""
77
87
) ;
88
+ this . bindServiceClient ( serviceClient ) ;
89
+ return factory . getClient ( serviceClient , this . info ) ;
78
90
} else {
91
+ const serviceClient = new CoreStitchServiceClientImpl (
92
+ this . auth ,
93
+ this . routes . serviceRoutes ,
94
+ serviceName !
95
+ ) ;
96
+ this . bindServiceClient ( serviceClient ) ;
79
97
return factory . getNamedClient (
80
- new CoreStitchServiceClientImpl (
81
- this . auth ,
82
- this . routes . serviceRoutes ,
83
- serviceName !
84
- ) ,
98
+ serviceClient ,
85
99
this . info
86
100
) ;
87
101
}
88
102
}
89
103
90
104
public getGeneralServiceClient ( serviceName : string ) : StitchServiceClient {
105
+ const serviceClient = new CoreStitchServiceClientImpl (
106
+ this . auth ,
107
+ this . routes . serviceRoutes ,
108
+ serviceName
109
+ ) ;
110
+ this . bindServiceClient ( serviceClient ) ;
91
111
return new StitchServiceClientImpl (
92
- new CoreStitchServiceClientImpl (
93
- this . auth ,
94
- this . routes . serviceRoutes ,
95
- serviceName
96
- )
112
+ serviceClient
97
113
) ;
98
114
}
99
115
100
116
public callFunction ( name : string , args : any [ ] ) : Promise < any > {
101
117
return this . coreClient . callFunction ( name , args ) ;
102
118
}
119
+
120
+ // note: this is the only rebind event we care about for JS. if we add
121
+ // services in the future, or update existing services in such a way that
122
+ // they'll need to rebind on other types of events, those handlers should be
123
+ // added to this file.
124
+ public onActiveUserChanged (
125
+ _ : StitchAuth ,
126
+ currentActiveUser : StitchUser | undefined ,
127
+ previousActiveUser : StitchUser | undefined
128
+ ) {
129
+ this . onRebindEvent ( new AuthRebindEvent ( {
130
+ currentActiveUser,
131
+ kind : AuthEventKind . ActiveUserChanged ,
132
+ previousActiveUser
133
+ } ) )
134
+ }
135
+
136
+ private bindServiceClient ( coreStitchServiceClient : CoreStitchServiceClient ) {
137
+ this . serviceClients . push ( coreStitchServiceClient ) ;
138
+ }
139
+
140
+ private onRebindEvent ( rebindEvent : RebindEvent ) {
141
+ this . serviceClients . forEach ( serviceClient => {
142
+ serviceClient . onRebindEvent ( rebindEvent ) ;
143
+ } )
144
+ }
103
145
}
104
146
105
147
function isServiceClientFactory < T > (
0 commit comments