-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix crash when getOnGoingDownloads is called early
Sometimes `DownloadManager.getOnGoingDownloads` may be called earlier than `DownloadManager.start`. This result in a crash. This change ensures that getOnGoingDownloads awaits the initialization.
- Loading branch information
1 parent
4f1a562
commit b9d4088
Showing
5 changed files
with
111 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// AsyncInitializer.swift | ||
// | ||
// | ||
// Created by Mohamed Afifi on 2023-11-26. | ||
// | ||
|
||
public struct AsyncInitializer { | ||
// MARK: Lifecycle | ||
|
||
public init() { | ||
var continuation: AsyncStream<Void>.Continuation! | ||
let stream = AsyncStream<Void> { continuation = $0 } | ||
self.continuation = continuation | ||
self.stream = stream | ||
} | ||
|
||
// MARK: Public | ||
|
||
public private(set) var initialized = false | ||
|
||
public mutating func initialize() { | ||
initialized = true | ||
continuation.finish() | ||
} | ||
|
||
public func awaitInitialization() async { | ||
if initialized { | ||
return | ||
} | ||
// Wait until the stream finishes | ||
for await _ in stream {} | ||
} | ||
|
||
// MARK: Private | ||
|
||
private let continuation: AsyncStream<Void>.Continuation | ||
private let stream: AsyncStream<Void> | ||
} |
This file contains 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 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 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 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