@@ -65,9 +65,9 @@ class FirebaseDataConnect extends FirebasePluginPlatform {
6565 /// FirebaseAppCheck
6666 FirebaseAppCheck ? appCheck;
6767
68- /// Due to compatibility issues with grpc-web, we swap out the transport based on what platform the user is using .
69- /// For web, we use RestTransport. For mobile, we use GRPCTransport.
70- late DataConnectTransport transport;
68+ /// Transport for connecting to the Data Connect service .
69+ /// Routes between RestTransport and WebSocketTransport based on subscription status
70+ DataConnectTransport ? transport;
7171
7272 /// FirebaseAuth
7373 FirebaseAuth ? auth;
@@ -89,6 +89,9 @@ class FirebaseDataConnect extends FirebasePluginPlatform {
8989 /// Checks whether the transport has been properly initialized.
9090 @visibleForTesting
9191 void checkTransport () {
92+ if (transport != null ) {
93+ return ;
94+ }
9295 transportOptions ?? =
9396 TransportOptions ('firebasedataconnect.googleapis.com' , null , true );
9497 final rest = RestTransport (
@@ -137,7 +140,7 @@ class FirebaseDataConnect extends FirebasePluginPlatform {
137140 return QueryRef <Data , Variables >(
138141 this ,
139142 operationName,
140- transport,
143+ transport! ,
141144 dataDeserializer,
142145 _queryManager,
143146 varsSerializer,
@@ -154,10 +157,12 @@ class FirebaseDataConnect extends FirebasePluginPlatform {
154157 Variables ? vars,
155158 ) {
156159 checkTransport ();
160+ //initialize cache since mutations on a stream could result in subscribed query updates
161+ checkAndInitializeCache ();
157162 return MutationRef <Data , Variables >(
158163 this ,
159164 operationName,
160- transport,
165+ transport! ,
161166 dataDeserializer,
162167 varsSerializer,
163168 vars,
@@ -174,11 +179,12 @@ class FirebaseDataConnect extends FirebasePluginPlatform {
174179 String mappedHost = automaticHostMapping ? getMappedHost (host) : host;
175180 transportOptions = TransportOptions (mappedHost, port, isSecure);
176181
177- if (cacheManager != null ) {
178- // dispose and clean this up. it will get reinitialized for newer QueryRefs that target the emulator.
179- cacheManager? .dispose ();
180- cacheManager = null ;
181- }
182+ // dispose and clean this up. it will get reinitialized for newer QueryRefs that target the emulator.
183+ cacheManager? .dispose ();
184+ cacheManager = null ;
185+
186+ // transport will get reinitialized for newer QueryRefs that target the emulator.
187+ transport = null ;
182188 }
183189
184190 /// Currently cached DataConnect instances. Maps from app name to ConnectorConfigStr, DataConnect.
@@ -206,16 +212,13 @@ class FirebaseDataConnect extends FirebasePluginPlatform {
206212 return cachedInstances[app.name]! [connectorConfig.toJson ()]! ;
207213 }
208214
209- //TODO remove after testing since CS should be null by default
210- final resolvedCacheSettings = cacheSettings ?? CacheSettings ();
211-
212215 FirebaseDataConnect newInstance = FirebaseDataConnect (
213216 app: app,
214217 auth: auth,
215218 appCheck: appCheck,
216219 connectorConfig: connectorConfig,
217220 sdkType: sdkType,
218- cacheSettings: resolvedCacheSettings ,
221+ cacheSettings: cacheSettings ,
219222 );
220223 if (cachedInstances[app.name] == null ) {
221224 cachedInstances[app.name] = < String , FirebaseDataConnect > {};
0 commit comments