-
Notifications
You must be signed in to change notification settings - Fork 0
Chat web socket client #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
laevandus
merged 20 commits into
chat-json-encoding-decoding
from
chat-web-socket-client
Nov 5, 2025
Merged
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b3935ce
Event batcher changes for chat
laevandus 761ba03
Change InternetConnection for chat
laevandus b862ef3
Change BackgroundTaskScheduler for chat
laevandus 68c45f8
Make it open for mocks
laevandus 8b9ce2c
Revert "Make it open for mocks"
laevandus 2888b55
Change ConnectionStatus for chat
laevandus 3416182
Use background task scheduler from chat
laevandus 341f04b
Make WebSocketEngineError public
laevandus 27e3ce6
WebSocketClient support for chat
laevandus ac7eb6d
Reconnection from offline
laevandus 5e6cc78
Fix reconnection after expired token
laevandus 72f0edc
Revert "Reconnection from offline"
laevandus 9b64137
No need to have these public
laevandus 9634642
Fix tests
laevandus e58ea40
Revert some of the changes for chat
laevandus 18fd9a0
Revert some more
laevandus badf37d
Clean up ErrorPayload
laevandus 58f33b4
Update docs
laevandus 19ceaab
Add more comments
laevandus fb91dc2
Fix attachments target
laevandus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,16 +9,12 @@ public protocol BackgroundTaskScheduler: Sendable { | |
| /// It's your responsibility to finish previously running task. | ||
| /// | ||
| /// Returns: `false` if system forbid background task, `true` otherwise | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved scheduler implementation from chat to core since there were additional fixes and the main actor annotations made it too hard to fix tests since using |
||
| @MainActor | ||
| func beginTask(expirationHandler: (@Sendable () -> Void)?) -> Bool | ||
| @MainActor | ||
| func beginTask(expirationHandler: (@MainActor () -> Void)?) -> Bool | ||
| func endTask() | ||
| @MainActor | ||
| func startListeningForAppStateUpdates( | ||
| onEnteringBackground: @escaping () -> Void, | ||
| onEnteringForeground: @escaping () -> Void | ||
| ) | ||
| @MainActor | ||
| func stopListeningForAppStateUpdates() | ||
|
|
||
| var isAppActive: Bool { get } | ||
|
|
@@ -28,37 +24,48 @@ public protocol BackgroundTaskScheduler: Sendable { | |
| import UIKit | ||
|
|
||
| public class IOSBackgroundTaskScheduler: BackgroundTaskScheduler, @unchecked Sendable { | ||
| private let applicationStateAdapter = StreamAppStateAdapter() | ||
|
|
||
| private lazy var app: UIApplication? = // We can't use `UIApplication.shared` directly because there's no way to convince the compiler | ||
| private lazy var app: UIApplication? = { | ||
| // We can't use `UIApplication.shared` directly because there's no way to convince the compiler | ||
| // this code is accessible only for non-extension executables. | ||
| UIApplication.value(forKeyPath: "sharedApplication") as? UIApplication | ||
| }() | ||
|
|
||
| /// The identifier of the currently running background task. `nil` if no background task is running. | ||
| private var activeBackgroundTask: UIBackgroundTaskIdentifier? | ||
| private let queue = DispatchQueue(label: "io.getstream.IOSBackgroundTaskScheduler", target: .global()) | ||
|
|
||
| public var isAppActive: Bool { applicationStateAdapter.state == .foreground } | ||
|
|
||
| public init() {} | ||
|
|
||
| public var isAppActive: Bool { | ||
| StreamConcurrency.onMain { | ||
| self.app?.applicationState == .active | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
| public func beginTask(expirationHandler: (@Sendable () -> Void)?) -> Bool { | ||
| activeBackgroundTask = app?.beginBackgroundTask { [weak self] in | ||
| self?._endTask() | ||
| expirationHandler?() | ||
| public func beginTask(expirationHandler: (@MainActor () -> Void)?) -> Bool { | ||
| // Only a single task is allowed at the same time | ||
| endTask() | ||
|
|
||
| guard let app else { return false } | ||
| let identifier = app.beginBackgroundTask { [weak self] in | ||
| self?.endTask() | ||
| StreamConcurrency.onMain { | ||
| expirationHandler?() | ||
| } | ||
| } | ||
| queue.sync { | ||
| self.activeBackgroundTask = identifier | ||
| } | ||
| return activeBackgroundTask != .invalid | ||
| return identifier != .invalid | ||
| } | ||
|
|
||
| @MainActor | ||
| public func endTask() { | ||
| _endTask() | ||
| } | ||
|
|
||
| private func _endTask() { | ||
| if let activeTask = activeBackgroundTask { | ||
| app?.endBackgroundTask(activeTask) | ||
| activeBackgroundTask = nil | ||
| guard let app else { return } | ||
| queue.sync { | ||
| if let identifier = self.activeBackgroundTask { | ||
| self.activeBackgroundTask = nil | ||
| app.endBackgroundTask(identifier) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -69,8 +76,10 @@ public class IOSBackgroundTaskScheduler: BackgroundTaskScheduler, @unchecked Sen | |
| onEnteringBackground: @escaping () -> Void, | ||
| onEnteringForeground: @escaping () -> Void | ||
| ) { | ||
| self.onEnteringForeground = onEnteringForeground | ||
| self.onEnteringBackground = onEnteringBackground | ||
| queue.sync { | ||
| self.onEnteringForeground = onEnteringForeground | ||
| self.onEnteringBackground = onEnteringBackground | ||
| } | ||
|
|
||
| NotificationCenter.default.addObserver( | ||
| self, | ||
|
|
@@ -88,8 +97,10 @@ public class IOSBackgroundTaskScheduler: BackgroundTaskScheduler, @unchecked Sen | |
| } | ||
|
|
||
| public func stopListeningForAppStateUpdates() { | ||
| onEnteringForeground = {} | ||
| onEnteringBackground = {} | ||
| queue.sync { | ||
| self.onEnteringForeground = {} | ||
| self.onEnteringBackground = {} | ||
| } | ||
|
|
||
| NotificationCenter.default.removeObserver( | ||
| self, | ||
|
|
@@ -105,19 +116,17 @@ public class IOSBackgroundTaskScheduler: BackgroundTaskScheduler, @unchecked Sen | |
| } | ||
|
|
||
| @objc private func handleAppDidEnterBackground() { | ||
| onEnteringBackground() | ||
| let callback = queue.sync { onEnteringBackground } | ||
| callback() | ||
| } | ||
|
|
||
| @objc private func handleAppDidBecomeActive() { | ||
| onEnteringForeground() | ||
| let callback = queue.sync { onEnteringForeground } | ||
| callback() | ||
| } | ||
|
|
||
| deinit { | ||
| Task { @MainActor [activeBackgroundTask, app] in | ||
| if let activeTask = activeBackgroundTask { | ||
| app?.endBackgroundTask(activeTask) | ||
| } | ||
| } | ||
| endTask() | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.