mirror of
https://github.com/DefinedNet/mobile_nebula.git
synced 2025-09-05 10:46:04 +00:00
Use an AsyncStream to avoid sending @MainActor
restricted AppDelegate into other isolation domains
This commit is contained in:
parent
df4c3a51b8
commit
a32d17705c
4 changed files with 28 additions and 14 deletions
|
@ -150,7 +150,7 @@ let statusString: [NEVPNStatus: String] = [
|
|||
]
|
||||
|
||||
// Represents a site that was pulled out of the system configuration
|
||||
class Site: Codable {
|
||||
class Site: Codable, @unchecked Sendable {
|
||||
// Stored in manager
|
||||
var name: String
|
||||
var id: String
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
import MobileNebula
|
||||
@preconcurrency import MobileNebula
|
||||
|
||||
enum APIClientError: Error {
|
||||
case invalidCredentials
|
||||
}
|
||||
|
||||
class APIClient {
|
||||
struct APIClient: Sendable {
|
||||
let apiClient: MobileNebulaAPIClient
|
||||
let json = JSONDecoder()
|
||||
|
||||
|
|
|
@ -25,17 +25,19 @@ func MissingArgumentError(message: String, details: Any?) -> FlutterError {
|
|||
) -> Bool {
|
||||
GeneratedPluginRegistrant.register(with: self)
|
||||
|
||||
dnUpdater.updateAllLoop { site in
|
||||
// Signal the site has changed in case the current site details screen is active
|
||||
let container = self.sites?.getContainer(id: site.id)
|
||||
if container != nil {
|
||||
// Update references to the site with the new site config
|
||||
container!.site = site
|
||||
container!.updater.update(connected: site.connected ?? false, replaceSite: site)
|
||||
}
|
||||
Task {
|
||||
for await site in dnUpdater.siteUpdates {
|
||||
// Signal the site has changed in case the current site details screen is active
|
||||
let container = self.sites?.getContainer(id: site.id)
|
||||
if container != nil {
|
||||
// Update references to the site with the new site config
|
||||
container!.site = site
|
||||
container!.updater.update(connected: site.connected ?? false, replaceSite: site)
|
||||
}
|
||||
|
||||
// Signal to the main screen to reload
|
||||
self.ui?.invokeMethod("refreshSites", arguments: nil)
|
||||
// Signal to the main screen to reload
|
||||
self.ui?.invokeMethod("refreshSites", arguments: nil)
|
||||
}
|
||||
}
|
||||
|
||||
guard let controller = window?.rootViewController as? FlutterViewController else {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import Foundation
|
||||
import os.log
|
||||
|
||||
class DNUpdater {
|
||||
class DNUpdater: @unchecked Sendable {
|
||||
private let apiClient = APIClient()
|
||||
private let timer = RepeatingTimer(timeInterval: 15 * 60) // 15 * 60 is 15 minutes
|
||||
private let log = Logger(subsystem: "net.defined.mobileNebula", category: "DNUpdater")
|
||||
|
@ -30,6 +30,18 @@ class DNUpdater {
|
|||
timer.resume()
|
||||
}
|
||||
|
||||
// Site updates provides an async/await alternative to `.updateAllLoop` that doesn't require a sendable closure.
|
||||
// https://developer.apple.com/documentation/swift/asyncstream
|
||||
var siteUpdates: AsyncStream<Site> {
|
||||
AsyncStream { continuation in
|
||||
self.updateAllLoop(onUpdate: { site in
|
||||
continuation.yield(site)
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func updateSingleLoop(site: Site, onUpdate: @Sendable @escaping (Site) -> Void) {
|
||||
timer.eventHandler = {
|
||||
self.updateSite(site: site, onUpdate: onUpdate)
|
||||
|
|
Loading…
Add table
Reference in a new issue