Skip to content

Commit

Permalink
Refine AsyncStore (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Nov 10, 2023
1 parent 2e80eeb commit 42d8c30
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
71 changes: 48 additions & 23 deletions Sources/Verge/Store/IsolatedStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,49 @@ public final class AsyncStore<State: StateType, Activity>: DerivedMaking, Sendab
self.backingStore = .init(initialState: initialState, storeOperation: .atomic(.init()), logger: nil, sanitizer: nil)
}

public func backgroundCommit<Result>(
mutation: (inout InoutRef<State>) throws -> Result
public func commit<Result>(
mutation: @Sendable (inout InoutRef<State>) throws -> Result
) async rethrows -> Result {

try await writer.perform { _ in
try backingStore._receive(mutation: { state, _ in try mutation(&state) })
}

}

public func backgroundCommit<Result>(
mutation: (inout InoutRef<State>, inout Transaction) throws -> Result
public func commit<Result>(
mutation: @Sendable (inout InoutRef<State>, inout Transaction) throws -> Result
) async rethrows -> Result {

try await writer.perform { _ in
try backingStore._receive(mutation: mutation)
}
}

/**
Adds an asynchronous task to perform.

Use this function to perform an asynchronous task with a lifetime that matches that of this store.
If this store is deallocated ealier than the given task finished, that asynchronous task will be cancelled.

Carefully use this function - If the task retains this store, it will continue to live until the task is finished.

- Parameters:
- key:
- mode:
- priority:
- action
- Returns: A Task for tracking given async operation's completion.
*/
@discardableResult
public func task<Return>(
key: ConcurrencyTaskManager.TaskKey = .distinct(),
mode: ConcurrencyTaskManager.TaskManagerActor.Mode = .dropCurrent,
priority: TaskPriority = .userInitiated,
@_inheritActorContext _ action: @Sendable @escaping () async throws -> Return
) -> Task<Return, Error> {
backingStore.task(key: key, mode: mode, priority: priority, action)
}

public func add(middleware: some StoreMiddlewareType<State>) {
backingStore.add(middleware: middleware)
}

/// Send activity
Expand Down Expand Up @@ -413,12 +438,12 @@ public protocol AsyncStoreDriverType {
var store: AsyncStore<State, Activity> { get }
var scope: WritableKeyPath<State, Scope> { get }

func backgroundCommit<Result>(
mutation: (inout InoutRef<State>) throws -> Result
func commit<Result>(
mutation: @Sendable (inout InoutRef<Scope>) throws -> Result
) async rethrows -> Result

func backgroundCommit<Result>(
mutation: (inout InoutRef<State>, inout Transaction) throws -> Result
func commit<Result>(
mutation: @Sendable (inout InoutRef<Scope>, inout Transaction) throws -> Result
) async rethrows -> Result
}

Expand Down Expand Up @@ -446,11 +471,11 @@ extension AsyncStoreDriverType {
/// Run Mutation that created inline
///
/// Throwable
public func backgroundCommit<Result>(
mutation: (inout InoutRef<Scope>) throws -> Result
public func commit<Result>(
mutation: @Sendable (inout InoutRef<Scope>) throws -> Result
) async rethrows -> Result {

return try await store.backgroundCommit { ref in
return try await store.commit { ref in
try ref.map(keyPath: scope, perform: mutation)
}

Expand All @@ -459,11 +484,11 @@ extension AsyncStoreDriverType {
/// Run Mutation that created inline
///
/// Throwable
public func backgroundCommit<Result>(
mutation: (inout InoutRef<Scope>, inout Transaction) throws -> Result
public func commit<Result>(
mutation: @Sendable (inout InoutRef<Scope>, inout Transaction) throws -> Result
) async rethrows -> Result {

return try await store.backgroundCommit { ref, transaction in
return try await store.commit { ref, transaction in
try ref.map(keyPath: scope, perform: {
try mutation(&$0, &transaction)
})
Expand Down Expand Up @@ -522,22 +547,22 @@ extension AsyncStoreDriverType where Scope == State {
/// Run Mutation that created inline
///
/// Throwable
public func backgroundCommit<Result>(
mutation: (inout InoutRef<Scope>) throws -> Result
public func commit<Result>(
mutation: @Sendable (inout InoutRef<Scope>) throws -> Result
) async rethrows -> Result {

return try await store.backgroundCommit(mutation: mutation)
return try await store.commit(mutation: mutation)

}

/// Run Mutation that created inline
///
/// Throwable
public func backgroundCommit<Result>(
mutation: (inout InoutRef<Scope>, inout Transaction) throws -> Result
public func commit<Result>(
mutation: @Sendable (inout InoutRef<Scope>, inout Transaction) throws -> Result
) async rethrows -> Result {

return try await store.backgroundCommit(mutation: mutation)
return try await store.commit(mutation: mutation)

}
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/VergeTests/Isolated/IsolatedStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ final class ActorIsolatedStoreTests: XCTestCase {

XCTAssertEqual(store.state.count, 0)

await store.backgroundCommit {
await store.commit {
$0.count = 1
}

Expand All @@ -131,7 +131,7 @@ final class ActorIsolatedStoreTests: XCTestCase {

XCTAssertEqual(store.state.count, 0)

await store.backgroundCommit {
await store.commit {
$0.count = 1
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/VergeTests/TransactionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class TransactionTests: XCTestCase {

let store = AsyncStore<DemoState, Never>(initialState: .init())

await store.backgroundCommit {
await store.commit {
$0.markAsModified()
$1[MyKey.self] = "first commit"
}
Expand Down

0 comments on commit 42d8c30

Please sign in to comment.