Skip to content

Commit

Permalink
Improve concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcferreira committed Sep 9, 2022
1 parent 062778f commit 51b8d7f
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions Sources/GIFImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,10 @@ public struct GIFImage: View {
}

private func handle(loop: Bool) {
guard loop else { return }
Task { await load() }
if loop { load() }
}

@Sendable
private func load() async {
@Sendable private func load() {
presentationTask?.cancel()
presentationTask = Task {
do {
Expand All @@ -97,14 +95,13 @@ public struct GIFImage: View {
try await action(source)
} while(self.loop)
} catch {
frame = errorImage ?? placeholder
await setFrame(errorImage ?? placeholder)
}
}
}

@Sendable
private func update(_ imageFrame: ImageFrame) async throws {
frame = RawImage.create(with: imageFrame.image)
@Sendable private func update(_ imageFrame: ImageFrame) async throws {
await setFrame(RawImage.create(with: imageFrame.image))
let calculatedInterval = imageFrame.interval ?? kDefaultGIFFrameInterval
let interval: Double
switch frameRate {
Expand All @@ -118,6 +115,11 @@ public struct GIFImage: View {
}
try await Task.sleep(nanoseconds: UInt64(interval * 1_000_000_000.0))
}

@MainActor
@Sendable private func setFrame(_ frame: RawImage) async {
self.frame = frame
}
}

struct GIFImage_Previews: PreviewProvider {
Expand Down

0 comments on commit 51b8d7f

Please sign in to comment.