Skip to content

Commit

Permalink
Linter related fixes (#24)
Browse files Browse the repository at this point in the history
* swift lint autocorrect

* made awfully looking things less awful

* linter fixes

* swift lint rules tuning
  • Loading branch information
KazaiMazai authored Apr 11, 2024
1 parent 5d5cc20 commit b9e1f45
Show file tree
Hide file tree
Showing 21 changed files with 220 additions and 182 deletions.
51 changes: 25 additions & 26 deletions Sources/Puredux/Store/Core/CoreStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ typealias StoreID = UUID

final class CoreStore<State, Action> {
let id: StoreID = StoreID()

private static var queueLabel: String { "com.puredux.store" }

private var state: State

let dispatchQueue: DispatchQueue
private(set) var actionsInterceptor: ActionsInterceptor<Action>?
private let reducer: Reducer<State, Action>
private var observers: Set<Observer<State>> = []

init(queue: DispatchQueue,
actionsInterceptor: ActionsInterceptor<Action>?,
initialState: State,
initialState: State,
reducer: @escaping Reducer<State, Action>) {

self.dispatchQueue = queue
self.actionsInterceptor = actionsInterceptor
self.state = initialState
Expand All @@ -44,48 +44,48 @@ extension CoreStore {
}
}

//MARK: - StoreProtocol Conformance
// MARK: - StoreProtocol Conformance

extension CoreStore: StoreProtocol {
//MARK: - DispatchQueue
// MARK: - DispatchQueue

var queue: DispatchQueue {
dispatchQueue
}

//MARK: - Interceptor
// MARK: - Interceptor

func setInterceptor(_ interceptor: ActionsInterceptor<Action>) {
queue.async { [weak self] in
self?.setInterceptorSync(interceptor)
}
}

func setInterceptor(with handler: @escaping Interceptor<Action>) {
let interceptor = ActionsInterceptor(
storeId: self.id,
handler: handler,
dispatcher: { [weak self] in self?.dispatch($0) })

setInterceptor(interceptor)
}
}

func setInterceptorSync(_ interceptor: ActionsInterceptor<Action>) {
self.actionsInterceptor = interceptor
}

//MARK: - Subscribe
// MARK: - Subscribe

func unsubscribe(observer: Observer<State>) {
queue.async { [weak self] in
self?.syncUnsubscribe(observer: observer)
}
}

func subscribe(observer: Observer<State>) {
subscribe(observer: observer, receiveCurrentState: true)
}

func subscribe(observer: Observer<State>, receiveCurrentState: Bool = true) {
queue.async { [weak self] in
self?.syncSubscribe(observer: observer, receiveCurrentState: receiveCurrentState)
Expand All @@ -95,26 +95,25 @@ extension CoreStore: StoreProtocol {
func syncUnsubscribe(observer: Observer<State>) {
observers.remove(observer)
}

func syncSubscribe(observer: Observer<State>, receiveCurrentState: Bool) {
observers.insert(observer)
guard receiveCurrentState else { return }
send(self.state, to: observer)
}


//MARK: - Dispatch
// MARK: - Dispatch

func scopeAction(_ action: Action) -> ScopedAction<Action> {
ScopedAction(storeId: id, action: action)
}

func dispatch(scopedAction: ScopedAction<Action>) {
queue.async { [weak self] in
self?.syncDispatch(scopedAction: scopedAction)
}
}

func dispatch(_ action: Action) {
dispatch(scopedAction: scopeAction(action))
}
Expand All @@ -126,16 +125,16 @@ extension CoreStore: StoreProtocol {
}
}

//MARK: - Private
// MARK: - Private

private extension CoreStore {

func send(_ state: State, to observer: Observer<State>) {
observer.send(state) { [weak self] status in
guard status == .dead else {
return
}

self?.syncUnsubscribe(observer: observer)
}
}
Expand Down
53 changes: 26 additions & 27 deletions Sources/Puredux/Store/Core/StoreNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,42 @@ typealias RootStoreNode<State, Action> = StoreNode<VoidStore<Action>, State, Sta

final class StoreNode<ParentStore, LocalState, State, Action> where ParentStore: StoreProtocol,
ParentStore.Action == Action {

private let localStore: CoreStore<LocalState, Action>
private let parentStore: ParentStore

private let stateMapping: (ParentStore.State, LocalState) -> State
private var observers: Set<Observer<State>> = []

init(initialState: LocalState,
stateMapping: @escaping (ParentStore.State, LocalState) -> State,
parentStore: ParentStore,
reducer: @escaping Reducer<LocalState, Action>) {

self.stateMapping = stateMapping
self.parentStore = parentStore

localStore = CoreStore(
queue: parentStore.queue,
actionsInterceptor: nil,
initialState: initialState,
reducer: reducer
)

if let parentInterceptor = parentStore.actionsInterceptor {
let interceptor = ActionsInterceptor(
storeId: localStore.id,
handler: parentInterceptor.handler,
dispatcher: { [weak self] action in self?.dispatch(action) }
)

localStore.setInterceptorSync(interceptor)
}

localStore.syncSubscribe(observer: localObserver, receiveCurrentState: true)
parentStore.subscribe(observer: parentObserver, receiveCurrentState: true)
}



private lazy var parentObserver: Observer<ParentStore.State> = {
Observer(removeStateDuplicates: .neverEqual) { [weak self] parentState, complete in
guard let self else {
Expand All @@ -60,20 +59,20 @@ final class StoreNode<ParentStore, LocalState, State, Action> where ParentStore:
complete(.active)
}
}()

private lazy var localObserver: Observer<LocalState> = {
Observer(removeStateDuplicates: .neverEqual) { [weak self] _, complete in
guard let self else {
complete(.dead)
return
}

complete(.active)
}
}()
}

//MARK: - Root Store Init
// MARK: - Root Store Init

extension StoreNode where LocalState == State {
static func initRootStore(initialState: State,
Expand All @@ -92,24 +91,24 @@ extension StoreNode where LocalState == State {
dispatcher: { _ in }
),
initialState: Void(),
reducer: { _,_ in }
reducer: { _, _ in }
),
reducer: reducer
)
}
}

//MARK: - StoreProtocol Conformance
// MARK: - StoreProtocol Conformance

extension StoreNode: StoreProtocol {
var queue: DispatchQueue {
parentStore.queue
}

var actionsInterceptor: ActionsInterceptor<Action>? {
parentStore.actionsInterceptor
}

func syncDispatch(scopedAction: ScopedAction<Action>) {
localStore.syncDispatch(scopedAction: scopedAction)
parentStore.syncDispatch(scopedAction: scopedAction)
Expand All @@ -118,7 +117,7 @@ extension StoreNode: StoreProtocol {
func dispatch(_ action: Action) {
dispatch(scopedAction: localStore.scopeAction(action))
}

func dispatch(scopedAction: ScopedAction<Action>) {
queue.async { [weak self] in
self?.syncDispatch(scopedAction: scopedAction)
Expand All @@ -130,44 +129,44 @@ extension StoreNode: StoreProtocol {
self?.syncUnsubscribe(observer: observer)
}
}

func syncUnsubscribe(observer: Observer<State>) {
observers.remove(observer)
}

func subscribe(observer: Observer<State>) {
subscribe(observer: observer, receiveCurrentState: true)
}

func subscribe(observer: Observer<State>, receiveCurrentState: Bool) {
queue.async { [weak self] in
self?.syncSubscribe(observer: observer, receiveCurrentState: receiveCurrentState)
}
}

func syncSubscribe(observer: Observer<State>, receiveCurrentState: Bool) {
observers.insert(observer)

guard receiveCurrentState,
let parentState = parentObserver.state,
let localState = localObserver.state
else {
return
}

let state = stateMapping(parentState, localState)
send(state, to: observer)
}
}
//MARK: - Private

// MARK: - Private

private extension StoreNode {
func observe(_ parentState: ParentStore.State) {
guard let localState = localObserver.state else {
return
}

let state = stateMapping(parentState, localState)
observers.forEach { send(state, to: $0) }
}
Expand Down
22 changes: 11 additions & 11 deletions Sources/Puredux/Store/Core/StoreProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ import Foundation
protocol StoreProtocol<State, Action>: AnyObject & SyncStoreProtocol {
associatedtype Action
associatedtype State

func dispatch(_ action: Action)

var queue: DispatchQueue { get }

var actionsInterceptor: ActionsInterceptor<Action>? { get }

func unsubscribe(observer: Observer<State>)

func subscribe(observer: Observer<State>, receiveCurrentState: Bool)

func dispatch(scopedAction: ScopedAction<Action>)

func subscribe(observer: Observer<State>)
}

protocol SyncStoreProtocol<State, Action> {
associatedtype Action
associatedtype State

func syncUnsubscribe(observer: Observer<State>)

func syncSubscribe(observer: Observer<State>, receiveCurrentState: Bool)
Expand All @@ -43,7 +43,7 @@ extension StoreProtocol {
subscribe: { [weak self] in self?.subscribe(observer: $0) }
)
}

func stateStore() -> StateStore<State, Action> {
StateStore<State, Action>(storeObject: self)
}
Expand All @@ -56,7 +56,7 @@ extension StoreProtocol {
stateMapping: @escaping (Self.State, LocalState) -> ResultState,
qos: DispatchQoS,
reducer: @escaping Reducer<LocalState, Action>) -> any StoreProtocol<ResultState, Action> {

StoreNode<Self, LocalState, ResultState, Action>(
initialState: initialState,
stateMapping: stateMapping,
Expand All @@ -71,7 +71,7 @@ extension StoreProtocol {
initialState: LocalState,
stateMapping: @escaping (Self.State, LocalState) -> ResultState,
reducer: @escaping Reducer<LocalState, Action>) -> any StoreProtocol<ResultState, Action> {

StoreNode<Self, LocalState, ResultState, Action>(
initialState: initialState,
stateMapping: stateMapping,
Expand Down
2 changes: 1 addition & 1 deletion Sources/Puredux/Store/Deprecated/RootStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class RootStore<State, Action> {
reducer: @escaping Reducer<State, Action>) {

internalStore = CoreStore(
queue: queue.dispatchQueue,
queue: queue.dispatchQueue,
actionsInterceptor: nil,
initialState: initialState,
reducer: reducer)
Expand Down
Loading

0 comments on commit b9e1f45

Please sign in to comment.