This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Helpers to build common effects (#14)
- Loading branch information
Showing
3 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
RedUx/Source/Extensions/AnyAsyncSequenceable+Helpers.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Asynchrone | ||
|
||
|
||
extension AnyAsyncSequenceable { | ||
|
||
// MARK: Builders | ||
|
||
/// Create a simple effect that emits a single event. | ||
/// - Parameter closure: An async closure that returns an event. | ||
/// - Returns: A type erased async sequence. | ||
public static func effect(_ closure: @escaping () async -> Element) -> Self { | ||
AsyncStream { | ||
let event = await closure() | ||
$0.finish(with: event) | ||
}.eraseToAnyAsyncSequenceable() | ||
} | ||
|
||
/// Creates a fire and forget async sequence. This sequence will not emit any events | ||
/// and will finish as soon as the provided closure has been executed. | ||
/// - Parameter closure: An async closure. | ||
/// - Returns: A type erased async sequence. | ||
public static func fireAndForget(_ closure: @escaping () async -> Void) -> Self { | ||
AsyncStream { | ||
await closure() | ||
$0.finish() | ||
}.eraseToAnyAsyncSequenceable() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Asynchrone | ||
import XCTest | ||
@testable import RedUx | ||
|
||
|
||
final class AnyAsyncSequenceableExtensionTests: XCTestCase { | ||
func testEffectBuilder() async { | ||
let event = AppEvent.setValue("a") | ||
let sequence = AnyAsyncSequenceable.effect { | ||
event | ||
} | ||
|
||
let result = await sequence.collect() | ||
XCTAssertEqual(result, [event]) | ||
} | ||
|
||
func testFireAndForgetBuilder() async { | ||
let sequence = AnyAsyncSequenceable<AppEvent>.fireAndForget { } | ||
|
||
let result = await sequence.collect() | ||
XCTAssert(result.isEmpty) | ||
} | ||
} |