Skip to content

Commit

Permalink
Merge pull request #20 from wlsdms0122/develop
Browse files Browse the repository at this point in the history
[RELEASE] Reducer/2.3.0
  • Loading branch information
wlsdms0122 authored Apr 5, 2024
2 parents a9d6883 + bc61263 commit 2544fcd
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
16 changes: 8 additions & 8 deletions Sources/Reducer/Reducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,15 @@ final class TaskBag<Item> {
}

// MARK: - Property
private var items = Set<TaskItem>()
private(set) var items = Set<TaskItem>()

// MARK: - Public
func store(_ item: TaskItem) {
items.insert(item)

let ref = UnsafeMutablePointer<Set<TaskItem>?>.allocate(capacity: 1)
ref.pointee = items

Task {
Task { @MainActor [weak self] in
await item.task.value
ref.pointee?.remove(item)
self?.items.remove(item)
}
}

Expand All @@ -65,6 +62,7 @@ final class TaskBag<Item> {
}
}

@MainActor
open class Reducer<R: Reduce>: ObservableObject, Mutable {
public typealias Action = R.Action
public typealias Mutation = R.Mutation
Expand Down Expand Up @@ -96,8 +94,10 @@ open class Reducer<R: Reduce>: ObservableObject, Mutable {

// MARK: - Lifecycle
open func mutate(_ mutation: Mutation) {
// Reduce state from mutation.
state = reduce(state: state, mutation: mutation)
Task { @MainActor in
// Reduce state from mutation.
state = reduce(state: state, mutation: mutation)
}
}

// MARK: - Public
Expand Down
1 change: 0 additions & 1 deletion Tests/MacroTests/ReduceMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ let testMacros: [String: Macro.Type] = [
"Reduce": ReduceMacro.self,
]

@MainActor
final class ReduceMacroTests: XCTestCase {
// MARK: - Property

Expand Down
1 change: 1 addition & 0 deletions Tests/ReducerTests/Model/CountIncreaseReduce.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by JSilver on 2023/03/07.
//

import Foundation
import Reducer

@Reduce
Expand Down
14 changes: 12 additions & 2 deletions Tests/ReducerTests/ReducerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import XCTest
@testable import Reducer
import Combine

@MainActor
final class ReducerTests: XCTestCase {
// MARK: - Property

Expand All @@ -23,6 +22,7 @@ final class ReducerTests: XCTestCase {
}

// MARK: - Test
@MainActor
func test_that_count_increases_when_receiving_increase_action() async throws {
// Given
let reducer = Reducer(
Expand All @@ -46,6 +46,7 @@ final class ReducerTests: XCTestCase {
XCTAssertEqual(result, [0, 1, 2])
}

@MainActor
func test_that_count_does_not_mutate_when_receiving_same_action_twice() async throws {
// Given
let reducer = Reducer(
Expand All @@ -69,6 +70,7 @@ final class ReducerTests: XCTestCase {
XCTAssertEqual(result, [0, 1])
}

@MainActor
func test_that_all_action_cancelled_when_reducer_deinit() async throws {
// Given
var reducer: Reducer<CountIncreaseReduce>? = Reducer(
Expand All @@ -94,6 +96,7 @@ final class ReducerTests: XCTestCase {
XCTAssertEqual(result, [0])
}

@MainActor
func test_that_count_increases_when_mutate_in_start() async throws {
// Given
let reducer = Reducer(TimerReduce(
Expand All @@ -110,6 +113,7 @@ final class ReducerTests: XCTestCase {
XCTAssertGreaterThan(result.last ?? 0, 0)
}

@MainActor
func test_that_count_increases_when_await_mutate_in_start() async throws {
// Given
let reducer = Reducer(AwaitStartReduce(
Expand All @@ -126,6 +130,7 @@ final class ReducerTests: XCTestCase {
XCTAssertEqual(result, [0, 1])
}

@MainActor
func test_that_reducer_should_be_able_to_assign_proxy_reduce() async throws {
// Given
var reducer = Reducer(CountIncreaseReduce(
Expand Down Expand Up @@ -165,7 +170,7 @@ final class ReducerTests: XCTestCase {
XCTAssertEqual(result, [0, 10])
}


@MainActor
func test_that_initial_count_is_100_when_proxy_set_initial_count() async throws {
// Given
let reducer = Reducer<CountIncreaseReduce>(proxy: .init(
Expand All @@ -184,6 +189,7 @@ final class ReducerTests: XCTestCase {
XCTAssertEqual(result, [100])
}

@MainActor
func test_that_count_increases_when_proxy_receiving_increase_action() async throws {
// Given
let reducer = Reducer<CountIncreaseReduce>(proxy: .init(
Expand Down Expand Up @@ -220,6 +226,7 @@ final class ReducerTests: XCTestCase {
XCTAssertEqual(result, [0, 1])
}

@MainActor
func test_that_count_increases_10_when_proxy_receiving_increase_action() async throws {
// Given
let reducer = Reducer<CountIncreaseReduce>(proxy: .init(
Expand Down Expand Up @@ -257,6 +264,7 @@ final class ReducerTests: XCTestCase {
XCTAssertEqual(result, [0, 10, 20])
}

@MainActor
func test_that_count_does_not_mutate_when_proxy_same_action_twice() async throws {
// Given
let reducer = Reducer<CountIncreaseReduce>(proxy: .init(
Expand Down Expand Up @@ -296,6 +304,7 @@ final class ReducerTests: XCTestCase {
XCTAssertEqual(result, [0, 1])
}

@MainActor
func test_that_count_increases_when_proxy_mutate_in_start() async throws {
// Given
var cancellable: AnyCancellable? = nil
Expand Down Expand Up @@ -327,6 +336,7 @@ final class ReducerTests: XCTestCase {
XCTAssertGreaterThan(result.last ?? 0, 0)
}

@MainActor
func test_that_reducer_can_assign_proxy_inherited_reduce() async throws {
// Given
let reducer = Reducer<CountIncreaseReduce>(
Expand Down
28 changes: 27 additions & 1 deletion Tests/ReducerTests/TaskBagTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import XCTest
@testable import Reducer
import Combine

@MainActor
final class TaskBagTests: XCTestCase {
// MARK: - Property

Expand All @@ -23,6 +22,33 @@ final class TaskBagTests: XCTestCase {
}

// MARK: - Test
func test_that_task_bag_remove_complete_task() async throws {
// Given
let taskBag = TaskBag<Int>()

taskBag.store(.init(
1,
with: Task {
try? await Task.sleep(nanoseconds: 1_000_000_000)
}
))
taskBag.store(.init(
2,
with: Task {
try? await Task.sleep(nanoseconds: 100_000_000)
}
))

let count = taskBag.items.count

// When
try await Task.sleep(nanoseconds: 500_000_000)

// Then
XCTAssertEqual(count, 2)
XCTAssertEqual(taskBag.items.count, 1)
}

func test_that_task_bag_cancel_all_task_when_deinit() async throws {
// Given
let expectation = expectation(description: "")
Expand Down

0 comments on commit 2544fcd

Please sign in to comment.