Skip to content

Commit

Permalink
Update backgroundCommit (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Apr 28, 2024
1 parent b44a0f9 commit dadaed1
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 26 deletions.
27 changes: 1 addition & 26 deletions Sources/Verge/Store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ open class Store<State: Equatable, Activity>: EventEmitter<_StoreEvent<State, Ac

public let taskManager: TaskManagerActor = .init()

private let writer: Writer = .init()
let writer: Writer = .init()

// MARK: - Initializers

Expand Down Expand Up @@ -799,31 +799,6 @@ Latest Version (%d): (%@)

}

// MARK: - Mutation
extension Store {

/// Run Mutation that created inline
///
/// Throwable
public func backgroundCommit<Result>(
_ name: String = "",
_ file: StaticString = #file,
_ function: StaticString = #function,
_ line: UInt = #line,
mutation: (inout InoutRef<State>) throws -> Result
) async rethrows -> Result {

let result = try await writer.perform { [self] _ in
try self.commit(mutation: mutation)
}

await self.waitUntilAllEventConsumed()

return result
}

}

// MARK: - Storage Implementation
extension Store {

Expand Down
80 changes: 80 additions & 0 deletions Sources/Verge/Store/StoreDriverType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,86 @@ extension StoreDriverType {
)
}

/// Run Mutation that created inline
///
/// Throwable
public func backgroundCommit<Result>(
_ name: String = "",
_ file: StaticString = #file,
_ function: StaticString = #function,
_ line: UInt = #line,
mutation: (inout InoutRef<Scope>) throws -> Result
) async rethrows -> Result {

let result = try await store.asStore().writer.perform { [self] _ in
try self.commit(mutation: mutation)
}

await self.waitUntilAllEventConsumed()

return result
}

/// Run Mutation that created inline
///
/// Throwable
public func backgroundCommit<Result>(
_ name: String = "",
_ file: StaticString = #file,
_ function: StaticString = #function,
_ line: UInt = #line,
mutation: (inout InoutRef<Scope>, inout Transaction) throws -> Result
) async rethrows -> Result {

let result = try await store.asStore().writer.perform { [self] _ in
try self.commit(mutation: mutation)
}

await self.waitUntilAllEventConsumed()

return result
}

/// Run Mutation that created inline
///
/// Throwable
public func backgroundCommit<Result>(
_ name: String = "",
_ file: StaticString = #file,
_ function: StaticString = #function,
_ line: UInt = #line,
mutation: (inout InoutRef<Scope>) throws -> Result
) async rethrows -> Result where Scope == TargetStore.State {

let result = try await store.asStore().writer.perform { [self] _ in
try self.commit(mutation: mutation)
}

await self.waitUntilAllEventConsumed()

return result
}

/// Run Mutation that created inline
///
/// Throwable
public func backgroundCommit<Result>(
_ name: String = "",
_ file: StaticString = #file,
_ function: StaticString = #function,
_ line: UInt = #line,
mutation: (inout InoutRef<Scope>, inout Transaction) throws -> Result
) async rethrows -> Result where Scope == TargetStore.State {

let result = try await store.asStore().writer.perform { [self] _ in
try self.commit(mutation: mutation)
}

await self.waitUntilAllEventConsumed()

return result
}

public func detached<NewScope: Equatable>(from newScope: WritableKeyPath<TargetStore.State, NewScope>)
-> DetachedDispatcher<TargetStore.State, TargetStore.Activity, NewScope> {
.init(store: store.asStore(), scope: newScope)
Expand Down

0 comments on commit dadaed1

Please sign in to comment.