Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed May 22, 2024
1 parent f0542e3 commit c98a88f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Sources/Verge/Store/StoreDriverType+Accumulator.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

extension StoreDriverType {

/**
Subscribes states and accumulates into components.
Against sink method, it does not use Changes object.
It allows to check if values has changed in the unit of accumulation, not Changes view.
*/
public func accumulate<T>(
queue: MainActorTargetQueue = .mainIsolated(),
@AccumulationSinkComponentBuilder<Scope> _ buildSubscription: @escaping @MainActor (consuming AccumulationBuilder<Scope>) -> _AccumulationSinkGroup<Scope, T>
Expand Down Expand Up @@ -33,6 +38,11 @@ extension StoreDriverType {

}

/**
Subscribes states and accumulates into components.
Against sink method, it does not use Changes object.
It allows to check if values has changed in the unit of accumulation, not Changes view.
*/
@_disfavoredOverload
public func accumulate<T>(
queue: some TargetQueueType,
Expand Down
59 changes: 57 additions & 2 deletions Tests/VergeTests/AccumulationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XCTest

final class AccumulationTests: XCTestCase {

func test() {
func test_main() {

let store = DemoStore()

Expand All @@ -13,7 +13,7 @@ final class AccumulationTests: XCTestCase {
let expForName = expectation(description: "name")
expForName.expectedFulfillmentCount = 2

let sub = store.accumulate { [weak self] in
let sub = store.accumulate(queue: .mainIsolated()) { [weak self] in

$0.ifChanged(\.count).do { value in
expForCount.fulfill()
Expand Down Expand Up @@ -64,6 +64,61 @@ final class AccumulationTests: XCTestCase {
let _ = sub
}

func test_background() {

let store = DemoStore()

let expForCount = expectation(description: "count")
expForCount.expectedFulfillmentCount = 2

let expForName = expectation(description: "name")
expForName.expectedFulfillmentCount = 2

let sub = store.accumulate(queue: .passthrough) { [weak self] in

$0.ifChanged(\.count).do { value in
expForCount.fulfill()
}

$0.ifChanged(\.name).do { value in
expForName.fulfill()
}

// checks for result builders
if let _ = self {
$0.ifChanged(\.name).do { value in
}
}

// checks for result builders
if true {
$0.ifChanged(\.name).do { value in
}
} else {
$0.ifChanged(\.name).do { value in
}
}

// checks for result builders
if true {
$0.ifChanged(\.name).do { value in
}
}

}

store.commit {
$0.count += 1
}

store.commit {
$0.name = "name"
}

wait(for: [expForCount, expForName], timeout: 1)

let _ = sub
}
}

@MainActor
Expand Down

0 comments on commit c98a88f

Please sign in to comment.