@@ -126,18 +126,41 @@ public class StreamVideo: ObservableObject, @unchecked Sendable {
126
126
videoConfig: videoConfig,
127
127
tokenProvider: tokenProvider ?? { _ in } ,
128
128
pushNotificationsConfig: pushNotificationsConfig,
129
- environment: Environment ( )
129
+ environment: Environment ( ) ,
130
+ autoConnectOnInit: true
130
131
)
131
132
}
132
-
133
+
134
+ convenience init (
135
+ apiKey: String ,
136
+ user: User ,
137
+ token: UserToken ,
138
+ videoConfig: VideoConfig = VideoConfig ( ) ,
139
+ pushNotificationsConfig: PushNotificationsConfig = . default,
140
+ tokenProvider: UserTokenProvider ? = nil ,
141
+ autoConnectOnInit: Bool
142
+ ) {
143
+ self . init (
144
+ apiKey: apiKey,
145
+ user: user,
146
+ token: token,
147
+ videoConfig: videoConfig,
148
+ tokenProvider: tokenProvider ?? { _ in } ,
149
+ pushNotificationsConfig: pushNotificationsConfig,
150
+ environment: Environment ( ) ,
151
+ autoConnectOnInit: autoConnectOnInit
152
+ )
153
+ }
154
+
133
155
init (
134
156
apiKey: String ,
135
157
user: User ,
136
158
token: UserToken ,
137
159
videoConfig: VideoConfig = VideoConfig ( ) ,
138
160
tokenProvider: @escaping UserTokenProvider ,
139
161
pushNotificationsConfig: PushNotificationsConfig ,
140
- environment: Environment
162
+ environment: Environment ,
163
+ autoConnectOnInit: Bool
141
164
) {
142
165
self . apiKey = APIKey ( apiKey)
143
166
state = State ( user: user)
@@ -188,32 +211,12 @@ public class StreamVideo: ObservableObject, @unchecked Sendable {
188
211
coordinatorClient. middlewares. append ( anonymousAuth)
189
212
}
190
213
prefetchLocation ( )
191
- connectTask = Task {
192
- if user. type == . guest {
193
- do {
194
- try Task . checkCancellation ( )
195
- let guestInfo = try await loadGuestUserInfo ( for: user, apiKey: apiKey)
196
-
197
- self . state. user = guestInfo. user
198
- self . token = guestInfo. token
199
- self . tokenProvider = guestInfo. tokenProvider
200
214
201
- try Task . checkCancellation ( )
202
- try await self . connectUser ( isInitial: true )
203
- } catch {
204
- log. error ( " Error connecting as guest " , error: error)
205
- }
206
- } else if user. type != . anonymous {
207
- do {
208
- try Task . checkCancellation ( )
209
- try await self . connectUser ( isInitial: true )
210
- } catch {
211
- log. error ( error)
212
- }
213
- }
215
+ if autoConnectOnInit {
216
+ initialConnectIfRequired ( apiKey: apiKey)
214
217
}
215
218
}
216
-
219
+
217
220
deinit {
218
221
connectTask? . cancel ( )
219
222
connectTask = nil
@@ -418,7 +421,42 @@ public class StreamVideo: ObservableObject, @unchecked Sendable {
418
421
}
419
422
420
423
// MARK: - private
421
-
424
+
425
+ /// When initializing we perform an automatic connection attempt.
426
+ ///
427
+ /// - Important: This behaviour is only enabled for non-test environments. This is to reduce the
428
+ /// noise in logs and avoid unnecessary network operations with the backend.
429
+ private func initialConnectIfRequired( apiKey: String ) {
430
+ guard connectTask == nil else {
431
+ return
432
+ }
433
+
434
+ connectTask = Task {
435
+ if user. type == . guest {
436
+ do {
437
+ try Task . checkCancellation ( )
438
+ let guestInfo = try await loadGuestUserInfo ( for: user, apiKey: apiKey)
439
+
440
+ self . state. user = guestInfo. user
441
+ self . token = guestInfo. token
442
+ self . tokenProvider = guestInfo. tokenProvider
443
+
444
+ try Task . checkCancellation ( )
445
+ try await self . connectUser ( isInitial: true )
446
+ } catch {
447
+ log. error ( " Error connecting as guest " , error: error)
448
+ }
449
+ } else if user. type != . anonymous {
450
+ do {
451
+ try Task . checkCancellation ( )
452
+ try await self . connectUser ( isInitial: true )
453
+ } catch {
454
+ log. error ( error)
455
+ }
456
+ }
457
+ }
458
+ }
459
+
422
460
/// Creates a call controller, used for establishing and managing a call.
423
461
/// - Parameters:
424
462
/// - callType: the type of the call.
0 commit comments