Skip to content

Commit 00b6165

Browse files
committed
[Dependency: Injection mad composable] Environment in the store
- 전체적인 개념으로서 store는 environment와 거의 관련이 없다. - `Store` 의 사용자들은 오로지 state value를 꺼내오는 것과 action을 보내는 것만을 고려하면 된다.
1 parent ac96572 commit 00b6165

File tree

1 file changed

+11
-40
lines changed

1 file changed

+11
-40
lines changed

Core/Store.swift

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@
77

88
import Combine
99

10-
public final class Store<Value, Action, Environment>: ObservableObject {
10+
public final class Store<Value, Action>: ObservableObject {
1111

12-
public let reducer: Reducer<Value, Action, Environment>
13-
private let environment: Environment
12+
public let reducer: Reducer<Value, Action, Any>
13+
private let environment: Any
1414
@Published
1515
public private(set) var value: Value
1616

17-
public init(
17+
public init<Environment>(
1818
initialValue: Value,
1919
reducer: @escaping Reducer<Value, Action, Environment>,
2020
environment: Environment
2121
) {
2222

2323
self.value = initialValue
24-
self.reducer = reducer
24+
self.reducer = { value, action, environment in
25+
reducer(&value, action, environment as! Environment)
26+
}
2527
self.environment = environment
2628
}
2729

@@ -49,16 +51,17 @@ public final class Store<Value, Action, Environment>: ObservableObject {
4951
private var viewCancellableBag = Set<AnyCancellable>()
5052
public func view<LocalValue, LocalAction>(
5153
value toLocalValue: @escaping (Value) -> LocalValue,
52-
action toGlobalAction: @escaping (LocalAction) -> Action
54+
action toGlobalAction: @escaping (LocalAction) -> Action,
5355
) -> Store<LocalValue, LocalAction> {
5456

5557
let localStore = Store<LocalValue, LocalAction>(
5658
initialValue: toLocalValue(self.value),
57-
reducer: { localValue, localAction in
59+
reducer: { localValue, localAction, _ in
5860
self.send(toGlobalAction(localAction))
5961
localValue = toLocalValue(self.value)
6062
return []
61-
}
63+
},
64+
environment: self.environment
6265
)
6366
self.viewCancellableBag.insert(
6467
self.$value.sink { [weak localStore] (newValue) in
@@ -68,36 +71,4 @@ public final class Store<Value, Action, Environment>: ObservableObject {
6871
return localStore
6972
}
7073

71-
public func view<LocalAction>(
72-
_ f: @escaping (LocalAction) -> Action
73-
) -> Store<Value, LocalAction> {
74-
75-
return Store<Value, LocalAction>(
76-
initialValue: self.value) { (value, localAction) in
77-
78-
self.send(f(localAction))
79-
value = self.value
80-
return []
81-
}
82-
}
83-
84-
public func view<LocalValue>(
85-
_ f: @escaping (Value) -> LocalValue
86-
) -> Store<LocalValue, Action> {
87-
let localStore = Store<LocalValue, Action>(
88-
initialValue: f(self.value),
89-
reducer: { localValue, action in
90-
self.send(action)
91-
localValue = f(self.value)
92-
return []
93-
}
94-
)
95-
self.viewCancellableBag.insert(
96-
self.$value.sink { [weak localStore] (newValue) in
97-
localStore?.value = f(newValue)
98-
}
99-
)
100-
return localStore
101-
}
102-
10374
}

0 commit comments

Comments
 (0)