Skip to content

Commit 5c1e7aa

Browse files
committed
Move scoped(to:) implementation to LoopBox.
1 parent 5ba9bf5 commit 5c1e7aa

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

Loop/LoopBox.swift

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ internal class ScopedLoopBox<RootState, RootEvent, ScopedState, ScopedEvent>: Lo
2626
override func send(_ event: ScopedEvent) {
2727
root.send(eventTransform(event))
2828
}
29+
30+
override func scoped<S, E>(
31+
to scope: KeyPath<ScopedState, S>,
32+
event: @escaping (E) -> ScopedEvent
33+
) -> LoopBoxBase<S, E> {
34+
return ScopedLoopBox<RootState, RootEvent, S, E>(
35+
root: self.root,
36+
value: value.appending(path: scope),
37+
event: { [eventTransform] in eventTransform(event($0)) }
38+
)
39+
}
2940
}
3041

3142
internal class RootLoopBox<State, Event>: LoopBoxBase<State, Event> {
@@ -38,7 +49,7 @@ internal class RootLoopBox<State, Event>: LoopBoxBase<State, Event> {
3849
private let token: Lifetime.Token
3950
private let input = Loop<State, Event>.Feedback.input
4051

41-
public override var producer: SignalProducer<State, Never> {
52+
override var producer: SignalProducer<State, Never> {
4253
SignalProducer { observer, lifetime in
4354
self.floodgate.withValue { initial, hasStarted -> Void in
4455
if hasStarted {
@@ -53,7 +64,7 @@ internal class RootLoopBox<State, Event>: LoopBoxBase<State, Event> {
5364
}
5465
}
5566

56-
public init(
67+
init(
5768
initial: State,
5869
reducer: @escaping (inout State, Event) -> Void,
5970
feedbacks: [Loop<State, Event>.Feedback],
@@ -75,6 +86,13 @@ internal class RootLoopBox<State, Event>: LoopBoxBase<State, Event> {
7586
}
7687
}
7788

89+
override func scoped<S, E>(
90+
to scope: KeyPath<State, S>,
91+
event: @escaping (E) -> Event
92+
) -> LoopBoxBase<S, E> {
93+
ScopedLoopBox(root: self, value: scope, event: event)
94+
}
95+
7896
func start() {
7997
floodgate.bootstrap()
8098
}
@@ -97,6 +115,13 @@ internal class LoopBoxBase<State, Event> {
97115
var producer: SignalProducer<State, Never> { subclassMustImplement() }
98116

99117
func send(_ event: Event) { subclassMustImplement() }
118+
119+
func scoped<S, E>(
120+
to scope: KeyPath<State, S>,
121+
event: @escaping (E) -> Event
122+
) -> LoopBoxBase<S, E> {
123+
subclassMustImplement()
124+
}
100125
}
101126

102127
@inline(never)

Loop/Public/Loop.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public final class Loop<State, Event> {
4747
event: @escaping (ScopedEvent) -> Event
4848
) -> Loop<ScopedState, ScopedEvent> {
4949
return Loop<ScopedState, ScopedEvent>(
50-
box: ScopedLoopBox(root: self.box, value: scope, event: event)
50+
box: box.scoped(to: scope, event: event)
5151
)
5252
}
5353
}

0 commit comments

Comments
 (0)