Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Nov 24, 2023
1 parent f853bff commit 449d0df
Show file tree
Hide file tree
Showing 26 changed files with 150 additions and 443 deletions.
2 changes: 1 addition & 1 deletion Demo/VergeStoreDemoSwiftUI/PostView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct PostView: View {
let post: Entity.Post

private var user: Entity.User? {
session.store.primitiveState.db.entities.user.find(by: post.userID)
session.store.state.primitive.db.entities.user.find(by: post.userID)
}

var body: some View {
Expand Down
4 changes: 2 additions & 2 deletions Demo/VergeStoreDemoSwiftUI/UserDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ struct UserDetailView: View, Equatable {

private var posts: [Entity.Post] {

session.store.primitiveState.db.entities.post.find(in:
session.store.primitiveState.db.indexes.postIDsAuthorGrouped.orderedID(in: user.entityID)
session.store.state.primitive.db.entities.post.find(in:
session.store.state.primitive.db.indexes.postIDsAuthorGrouped.orderedID(in: user.entityID)
)
}

Expand Down
2 changes: 1 addition & 1 deletion Demo/VergeStoreDemoUIKit/Sources.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class RootStore: Store<RootState, RootActivity> {
DispatchQueue.main.async {

self.increment()
if self.primitiveState.count > 10 {
if self.state.primitive.count > 10 {
self.send(.bomb)
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Verge/Derived/Derived+Assign.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

extension DispatcherType {
extension StoreDriverType {

/**
Assigns a Store's state to a property of a store.
Expand All @@ -28,7 +28,7 @@ extension DispatcherType {
*/
public func assign(
queue: some TargetQueueType = .passthrough,
to binder: @escaping (Changes<State>) -> Void
to binder: @escaping (Changes<TargetStore.State>) -> Void
) -> StoreSubscription {
store.asStore().sinkState(queue: queue, receive: binder)
}
Expand All @@ -40,7 +40,7 @@ extension DispatcherType {
*/
public func assign(
queue: MainActorTargetQueue,
to binder: @escaping (Changes<State>) -> Void
to binder: @escaping (Changes<TargetStore.State>) -> Void
) -> StoreSubscription {
store.asStore().sinkState(queue: queue, receive: binder)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Verge/Store/DetachedDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

public final class DetachedDispatcher<State: Equatable, Activity, Scope: Equatable>: DispatcherType
public final class DetachedDispatcher<State: Equatable, Activity, Scope: Equatable>: StoreDriverType
{

public let store: Store<State, Activity>
Expand Down
12 changes: 1 addition & 11 deletions Sources/Verge/Store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public protocol StoreType<State>: AnyObject, Sendable, ObservableObject where Ob

func asStore() -> Store<State, Activity>

var primitiveState: State { get }
var state: Changes<State> { get }
}

Expand Down Expand Up @@ -80,7 +79,7 @@ actor Writer {
/// ```
/// You may use also `StoreWrapperType` to define State and Activity as inner types.
///
open class Store<State: Equatable, Activity>: EventEmitter<_StoreEvent<State, Activity>>, CustomReflectable, StoreType, DispatcherType, DerivedMaking, @unchecked Sendable {
open class Store<State: Equatable, Activity>: EventEmitter<_StoreEvent<State, Activity>>, CustomReflectable, StoreType, StoreDriverType, DerivedMaking, @unchecked Sendable {

public var scope: WritableKeyPath<State, State> = \State.self

Expand Down Expand Up @@ -268,15 +267,6 @@ extension Store {
public var objectDidChange: AnyPublisher<Changes<State>, Never> {
valuePublisher.dropFirst().eraseToAnyPublisher()
}


/// A current state.
///
/// It causes locking and unlocking with a bit cost.
/// It may cause blocking if any other is doing mutation or reading.
public var primitiveState: State {
state.primitive
}

/// Returns a current state with thread-safety.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,65 @@

import Foundation

// It would be renamed as StoreContextType
public protocol DispatcherType<Scope>: AnyObject where State == WrappedStore.State, Activity == WrappedStore.Activity {
@available(*, deprecated, renamed: "StoreDriverType")
public typealias DispatcherType<Scope> = StoreDriverType<Scope>

/**
A protocol that uses external Store inside and provides the functions.
```
final class MyViewModel: StoreDriverType {

struct State {
...
}

// If you don't need Activity, you can remove it.
enum Activity {
...
}

let store: Store<State, Activity>

init() {
self.store = .init(initialState: .init(), logger: nil)
}

}
```
*/
public protocol StoreDriverType<Scope>: ObservableObject {

associatedtype TargetStore: StoreType

associatedtype Scope: Equatable = TargetStore.State

var store: TargetStore { get }
var scope: WritableKeyPath<TargetStore.State, Scope> { get }

associatedtype WrappedStore: StoreType
associatedtype Scope: Equatable = WrappedStore.State

associatedtype State = WrappedStore.State
associatedtype Activity = WrappedStore.Activity

var store: WrappedStore { get }
var scope: WritableKeyPath<WrappedStore.State, Scope> { get }
var state: Changes<Scope> { get }
}

extension DispatcherType {
extension StoreDriverType {

public var objectWillChange: TargetStore.ObjectWillChangePublisher {
store.objectWillChange
}

/// A state that cut out from root-state with the scope key path.
public nonisolated var state: Changes<Scope> {
store.state.map { $0[keyPath: scope] }
}

public nonisolated var rootState: Changes<State> {
public nonisolated var rootState: Changes<TargetStore.State> {
return store.state
}
}

extension DispatcherType where Scope == State {
public var scope: WritableKeyPath<WrappedStore.State, WrappedStore.State> {
\WrappedStore.State.self
}
}
extension StoreDriverType where Scope == TargetStore.State {

extension DispatcherType where Scope == State {
public var scope: WritableKeyPath<TargetStore.State, TargetStore.State> {
\TargetStore.State.self
}

// MARK: - Subscribings

Expand All @@ -67,7 +94,7 @@ extension DispatcherType where Scope == State {
public func sinkState(
dropsFirst: Bool = false,
queue: some TargetQueueType,
receive: @escaping (Changes<State>) -> Void
receive: @escaping (Changes<TargetStore.State>) -> Void
) -> StoreSubscription {
store.asStore()._primitive_sinkState(dropsFirst: dropsFirst, queue: queue, receive: receive)
}
Expand All @@ -83,7 +110,7 @@ extension DispatcherType where Scope == State {
public func sinkState(
dropsFirst: Bool = false,
queue: MainActorTargetQueue = .mainIsolated(),
receive: @escaping @MainActor (Changes<State>) -> Void
receive: @escaping @MainActor (Changes<TargetStore.State>) -> Void
) -> StoreSubscription {
store.asStore()._mainActor_sinkState(dropsFirst: dropsFirst, queue: queue, receive: receive)
}
Expand All @@ -99,10 +126,10 @@ extension DispatcherType where Scope == State {
/// - Returns: A subscriber that performs the provided closure upon receiving values.
@_disfavoredOverload
public func sinkState<Accumulate>(
scan: Scan<Changes<State>, Accumulate>,
scan: Scan<Changes<TargetStore.State>, Accumulate>,
dropsFirst: Bool = false,
queue: some TargetQueueType,
receive: @escaping (Changes<State>, Accumulate) -> Void
receive: @escaping (Changes<TargetStore.State>, Accumulate) -> Void
) -> StoreSubscription {
store.asStore()._primitive_scan_sinkState(scan: scan, dropsFirst: dropsFirst, queue: queue, receive: receive)
}
Expand All @@ -118,10 +145,10 @@ extension DispatcherType where Scope == State {
/// - Returns: A subscriber that performs the provided closure upon receiving values.
@discardableResult
public func sinkState<Accumulate>(
scan: Scan<Changes<State>, Accumulate>,
scan: Scan<Changes<TargetStore.State>, Accumulate>,
dropsFirst: Bool = false,
queue: MainActorTargetQueue = .mainIsolated(),
receive: @escaping @MainActor (Changes<State>, Accumulate) -> Void
receive: @escaping @MainActor (Changes<TargetStore.State>, Accumulate) -> Void
) -> StoreSubscription {
store.asStore()._mainActor_scan_sinkState(scan: scan, dropsFirst: dropsFirst, queue: queue, receive: receive)
}
Expand All @@ -132,7 +159,7 @@ extension DispatcherType where Scope == State {
@_disfavoredOverload
public func sinkActivity(
queue: some TargetQueueType,
receive: @escaping (Activity) -> Void
receive: @escaping (TargetStore.Activity) -> Void
) -> StoreSubscription {

store.asStore()._primitive_sinkActivity(queue: queue, receive: receive)
Expand All @@ -144,7 +171,7 @@ extension DispatcherType where Scope == State {
/// - Returns: A subscriber that performs the provided closure upon receiving values.
public func sinkActivity(
queue: MainActorTargetQueue = .mainIsolated(),
receive: @escaping @MainActor (Activity) -> Void
receive: @escaping @MainActor (TargetStore.Activity) -> Void
) -> StoreSubscription {

store.asStore()._mainActor_sinkActivity(queue: queue) { activity in
Expand All @@ -157,7 +184,7 @@ extension DispatcherType where Scope == State {

}

extension DispatcherType {
extension StoreDriverType {

/**
Subscribe the state that scoped
Expand Down Expand Up @@ -254,7 +281,7 @@ extension DispatcherType {
/// - Parameter activity:
public func send(
_ name: String = "",
_ activity: WrappedStore.Activity,
_ activity: TargetStore.Activity,
_ file: StaticString = #file,
_ function: StaticString = #function,
_ line: UInt = #line
Expand All @@ -272,7 +299,7 @@ extension DispatcherType {
/// Send activity
/// - Parameter activity:
public func send(
_ activity: WrappedStore.Activity,
_ activity: TargetStore.Activity,
_ file: StaticString = #file,
_ function: StaticString = #function,
_ line: UInt = #line
Expand Down Expand Up @@ -316,7 +343,7 @@ extension DispatcherType {
_ function: StaticString = #function,
_ line: UInt = #line,
mutation: (inout InoutRef<Scope>) throws -> Result
) rethrows -> Result where Scope == WrappedStore.State {
) rethrows -> Result where Scope == TargetStore.State {
let trace = MutationTrace(
name: name,
file: file,
Expand All @@ -333,7 +360,7 @@ extension DispatcherType {
func _commit<Result>(
trace: MutationTrace,
mutation: (inout InoutRef<Scope>) throws -> Result
) rethrows -> Result where Scope == WrappedStore.State {
) rethrows -> Result where Scope == TargetStore.State {
return try store.asStore()._receive(
mutation: { ref, transaction -> Result in
ref.append(trace: trace)
Expand All @@ -342,13 +369,13 @@ extension DispatcherType {
)
}

public func detached<NewScope: Equatable>(from newScope: WritableKeyPath<WrappedStore.State, NewScope>)
-> DetachedDispatcher<WrappedStore.State, WrappedStore.Activity, NewScope> {
public func detached<NewScope: Equatable>(from newScope: WritableKeyPath<TargetStore.State, NewScope>)
-> DetachedDispatcher<TargetStore.State, TargetStore.Activity, NewScope> {
.init(store: store.asStore(), scope: newScope)
}

public func detached<NewScope: Equatable>(by appendingScope: WritableKeyPath<Scope, NewScope>)
-> DetachedDispatcher<WrappedStore.State, WrappedStore.Activity, NewScope> {
-> DetachedDispatcher<TargetStore.State, TargetStore.Activity, NewScope> {
.init(store: store.asStore(), scope: scope.appending(path: appendingScope))
}
}
20 changes: 10 additions & 10 deletions Sources/Verge/Store/StoreType+Assignee.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import Foundation

extension DispatcherType {
extension StoreDriverType {

public typealias Assignee<Value> = (Value) -> Void

Expand All @@ -38,7 +38,7 @@ extension DispatcherType {
```
*/
public func assignee<Value>(
_ keyPath: WritableKeyPath<State, Value>,
_ keyPath: WritableKeyPath<TargetStore.State, Value>,
dropsOutput: @escaping (Changes<Value>) -> Bool = { _ in false }
) -> Assignee<Changes<Value>> {
return { [weak self] value in
Expand All @@ -62,7 +62,7 @@ extension DispatcherType {
```
*/
public func assignee<Value>(
_ keyPath: WritableKeyPath<State, Value?>,
_ keyPath: WritableKeyPath<TargetStore.State, Value?>,
dropsOutput: @escaping (Changes<Value?>) -> Bool = { _ in false }
) -> Assignee<Changes<Value?>> {
return { [weak self] value in
Expand All @@ -86,7 +86,7 @@ extension DispatcherType {
```
*/
public func assignee<Value>(
_ keyPath: WritableKeyPath<State, Value?>,
_ keyPath: WritableKeyPath<TargetStore.State, Value?>,
dropsOutput: @escaping (Changes<Value?>) -> Bool = { _ in false }
) -> Assignee<Changes<Value>> {
return { [weak self] value in
Expand All @@ -102,7 +102,7 @@ extension DispatcherType {
Assignee to asign Changes object directly.
*/
public func assignee<Value>(
_ keyPath: WritableKeyPath<State, Value>
_ keyPath: WritableKeyPath<TargetStore.State, Value>
) -> Assignee<Value> {
return { [weak self] value in
self?.store.asStore().commit {
Expand All @@ -115,7 +115,7 @@ extension DispatcherType {
Assignee to asign Changes object directly.
*/
public func assignee<Value>(
_ keyPath: WritableKeyPath<State, Value?>
_ keyPath: WritableKeyPath<TargetStore.State, Value?>
) -> Assignee<Value?> {
return { [weak self] value in
self?.store.asStore().commit {
Expand All @@ -128,7 +128,7 @@ extension DispatcherType {
Assignee to asign Changes object directly.
*/
public func assignee<Value>(
_ keyPath: WritableKeyPath<State, Value?>
_ keyPath: WritableKeyPath<TargetStore.State, Value?>
) -> Assignee<Value> {
return { [weak self] value in
self?.store.asStore().commit {
Expand All @@ -150,7 +150,7 @@ extension DispatcherType {
```
*/
public func assignee<Value: Equatable>(
_ keyPath: WritableKeyPath<State, Value>
_ keyPath: WritableKeyPath<TargetStore.State, Value>
) -> Assignee<Changes<Value>> {
assignee(keyPath, dropsOutput: { !$0.hasChanges })
}
Expand All @@ -168,7 +168,7 @@ extension DispatcherType {
```
*/
public func assignee<Value: Equatable>(
_ keyPath: WritableKeyPath<State, Value?>
_ keyPath: WritableKeyPath<TargetStore.State, Value?>
) -> Assignee<Changes<Value?>> {
assignee(keyPath, dropsOutput: { !$0.hasChanges })
}
Expand All @@ -186,7 +186,7 @@ extension DispatcherType {
```
*/
public func assignee<Value: Equatable>(
_ keyPath: WritableKeyPath<State, Value?>
_ keyPath: WritableKeyPath<TargetStore.State, Value?>
) -> Assignee<Changes<Value>> {
assignee(keyPath, dropsOutput: { !$0.hasChanges })
}
Expand Down
Loading

0 comments on commit 449d0df

Please sign in to comment.