Skip to content

Commit

Permalink
Fix for throttle logic. (#3075)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrandonw committed May 11, 2024
1 parent b7bf9fc commit 433a231
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ public final class FileStorageKey<Value: Codable & Sendable>: PersistenceKey, Se
self.isSetting.setValue(true)
try? self.storage.save(JSONEncoder().encode(value), self.url)
let workItem = DispatchWorkItem { [weak self] in
guard let self, let value = self.value.value else { return }
guard let self else { return }
defer {
self.value.setValue(nil)
self.workItem.setValue(nil)
}
guard let value = self.value.value
else { return }
self.isSetting.setValue(true)
try? self.storage.save(JSONEncoder().encode(value), self.url)
self.value.setValue(nil)
self.workItem.setValue(nil)
}
self.workItem.setValue(workItem)
if canListenForResignActive {
Expand Down
21 changes: 21 additions & 0 deletions Tests/ComposableArchitectureTests/FileStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ final class FileStorageTests: XCTestCase {
}
}

func testNoThrottling() throws {
let fileSystem = LockIsolated<[URL: Data]>([:])
let testScheduler = DispatchQueue.test
try withDependencies {
$0.defaultFileStorage = .inMemory(
fileSystem: fileSystem,
scheduler: testScheduler.eraseToAnyScheduler()
)
} operation: {
@Shared(.fileStorage(.fileURL)) var users = [User]()
try XCTAssertNoDifference(fileSystem.value.users(for: .fileURL), nil)

users.append(.blob)
try XCTAssertNoDifference(fileSystem.value.users(for: .fileURL), [.blob])

testScheduler.advance(by: .seconds(2))
users.append(.blobJr)
try XCTAssertNoDifference(fileSystem.value.users(for: .fileURL), [.blob, .blobJr])
}
}

func testWillResign() throws {
guard let willResignNotificationName else { return }

Expand Down

0 comments on commit 433a231

Please sign in to comment.