-
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.
- Loading branch information
Showing
20 changed files
with
862 additions
and
107 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
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
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
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
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
40 changes: 40 additions & 0 deletions
40
Sources/SimplexArchitecture/Internal/ActionTransition.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,40 @@ | ||
import Foundation | ||
|
||
struct ActionTransition<Reducer: ReducerProtocol> { | ||
struct State { | ||
let state: Reducer.Target.States? | ||
let reducerState: Reducer.ReducerState? | ||
} | ||
|
||
let previous: Self.State | ||
let next: Self.State | ||
let effect: SideEffect<Reducer> | ||
let effectContext: UUID | ||
let action: CombineAction<Reducer> | ||
|
||
init( | ||
previous: Self.State, | ||
next: Self.State, | ||
sideEffect: SideEffect<Reducer>, | ||
effectContext: UUID, | ||
for action: CombineAction<Reducer> | ||
) { | ||
self.previous = previous | ||
self.next = next | ||
effect = sideEffect | ||
self.effectContext = effectContext | ||
self.action = action | ||
} | ||
|
||
func toNextStateContainer(from target: Reducer.Target) -> StateContainer<Reducer.Target> { | ||
toStateContainer(from: target, state: next) | ||
} | ||
|
||
func toPreviousStateContainer(from target: Reducer.Target) -> StateContainer<Reducer.Target> { | ||
toStateContainer(from: target, state: previous) | ||
} | ||
|
||
private func toStateContainer(from target: Reducer.Target, state _: Self.State) -> StateContainer<Reducer.Target> { | ||
.init(target, states: next.state, reducerState: next.reducerState) | ||
} | ||
} |
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,7 @@ | ||
import Foundation | ||
|
||
extension Collection { | ||
subscript(safe index: Index) -> Element? { | ||
indices.contains(index) ? self[index] : nil | ||
} | ||
} |
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,5 @@ | ||
import Foundation | ||
|
||
enum EffectContext { | ||
@TaskLocal static var id: UUID? | ||
} |
37 changes: 37 additions & 0 deletions
37
Sources/SimplexArchitecture/Internal/Task+withEffectContext.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,37 @@ | ||
import Foundation | ||
|
||
extension Task where Failure == any Error { | ||
@discardableResult | ||
static func withEffectContext( | ||
priority: TaskPriority? = nil, | ||
@_inheritActorContext @_implicitSelfCapture operation: @Sendable @escaping () async throws -> Success | ||
) -> Self { | ||
if let _ = EffectContext.id { | ||
Self(priority: priority, operation: operation) | ||
} else { | ||
Self(priority: priority) { | ||
try await EffectContext.$id.withValue(UUID()) { | ||
try await operation() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension Task where Failure == Never { | ||
@discardableResult | ||
static func withEffectContext( | ||
priority: TaskPriority? = nil, | ||
@_inheritActorContext @_implicitSelfCapture operation: @Sendable @escaping () async -> Success | ||
) -> Self { | ||
if let _ = EffectContext.id { | ||
Self(priority: priority, operation: operation) | ||
} else { | ||
Self(priority: priority) { | ||
await EffectContext.$id.withValue(UUID()) { | ||
await operation() | ||
} | ||
} | ||
} | ||
} | ||
} |
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,25 @@ | ||
import Foundation | ||
import XCTestDynamicOverlay | ||
|
||
@propertyWrapper | ||
struct TestOnly<T> { | ||
private var _value: T | ||
|
||
var wrappedValue: T { | ||
_read { | ||
if !_XCTIsTesting { | ||
runtimeWarning("\(Self.self) is accessible only during Unit tests") | ||
} | ||
yield _value | ||
} | ||
set { | ||
if _XCTIsTesting { | ||
_value = newValue | ||
} | ||
} | ||
} | ||
|
||
init(wrappedValue: T) { | ||
_value = wrappedValue | ||
} | ||
} |
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
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
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
Oops, something went wrong.