Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Sources/AsyncDataLoader/DataLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public actor DataLoader<Key: Hashable & Sendable, Value: Sendable> {
queue.append((key: key, channel: channel))

if let executionPeriod = options.executionPeriod, !dispatchScheduled {
// This task must be detached in order to fulfill potential other keys
// even if the originally triggering key is cancelled.
Task.detached {
try await Task.sleep(nanoseconds: executionPeriod)
try await self.execute()
Expand All @@ -59,7 +61,7 @@ public actor DataLoader<Key: Hashable & Sendable, Value: Sendable> {
dispatchScheduled = true
}
} else {
Task.detached {
Task {
do {
let results = try await self.batchLoadFunction([key])

Expand Down Expand Up @@ -158,11 +160,7 @@ public actor DataLoader<Key: Hashable & Sendable, Value: Sendable> {

if cache[cacheKey] == nil {
let channel = Channel<Value, Error>()

Task.detached {
await channel.fulfill(value)
}

await channel.fulfill(value)
cache[cacheKey] = channel
}

Expand Down
Loading