Skip to content

Commit

Permalink
Referenced object store refactoring (#12)
Browse files Browse the repository at this point in the history
* updated StoreProtocol to inherit from AnyObject

* updated referenced store to use StoreNode directly

* minor refactoring
  • Loading branch information
KazaiMazai authored Apr 2, 2024
1 parent a8bb586 commit 61057ff
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 53 deletions.
16 changes: 1 addition & 15 deletions Sources/Puredux/Store/Core/StoreNodeExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,7 @@ extension StoreNode where LocalState == State {
)
}
}

extension StoreNode {
func weakRefStore() -> Store<State, Action> {
Store(dispatch: { [weak self] in self?.dispatch($0) },
subscribe: { [weak self] in self?.subscribe(observer: $0) }
)
}

func strongRefStore() -> Store<State, Action> {
Store.referencedStore(dispatch: { [self] in self.dispatch($0) },
subscribe: { [self] in self.subscribe(observer: $0) }
)
}
}


extension StoreNode {

func createChildStore<NodeState, ResultState>(initialState: NodeState,
Expand Down
18 changes: 17 additions & 1 deletion Sources/Puredux/Store/Core/StoreProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

protocol StoreProtocol {
protocol StoreProtocol<State, Action>: AnyObject {
associatedtype Action
associatedtype State

Expand All @@ -23,3 +23,19 @@ protocol StoreProtocol {

func dispatch(scopedAction: ScopedAction<Action>)
}

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

func weakRefStore() -> Store<State, Action> {
Store(dispatch: { [weak self] in self?.dispatch($0) },
subscribe: { [weak self] in self?.subscribe(observer: $0) }
)
}

func strongRefStore() -> Store<State, Action> {
Store<State, Action>(storeObject: self)
}
}
6 changes: 5 additions & 1 deletion Sources/Puredux/Store/Core/VoidStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

struct VoidStore<Action>: StoreProtocol {
final class VoidStore<Action>: StoreProtocol {
let currentState: Void = Void()
let actionsInterceptor: ActionsInterceptor<Action>?

Expand All @@ -18,4 +18,8 @@ struct VoidStore<Action>: StoreProtocol {
func subscribe(observer: Observer<()>, receiveCurrentState: Bool) { }

func dispatch(scopedAction: ScopedAction<Action>) { }

init(actionsInterceptor: ActionsInterceptor<Action>? = nil) {
self.actionsInterceptor = actionsInterceptor
}
}
43 changes: 7 additions & 36 deletions Sources/Puredux/Store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,11 @@ extension Store {
return self
}
}

static func referencedStore(dispatch: @escaping Dispatch,
subscribe: @escaping Subscribe) -> Store {

Store(
.storeObject(StoreObject(
dispatch: dispatch,
subscribe: subscribe)
)
)
}

extension Store {
init(storeObject: any StoreProtocol<State, Action>) {
self.storeType = .storeObject(storeObject)
}
}

Expand All @@ -153,35 +148,11 @@ private extension Store {
}
}


private extension Store {
enum StoreType {
case storeObject(StoreObject)
case storeObject(any StoreProtocol<State, Action>)
case store(Dispatch, Subscribe)
}

final class StoreObject {
private let dispatch: Dispatch
private let subscribe: Subscribe

func dispatch(_ action: Action) {
dispatch(action)
}

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

init(dispatch: @escaping Dispatch,
subscribe: @escaping Subscribe) {
self.dispatch = dispatch
self.subscribe = subscribe
}

func weakRefStore() -> Store<State, Action> {
Store(dispatch: { [weak self] in self?.dispatch($0) },
subscribe: { [weak self] in self?.subscribe(observer: $0) }
)
}
}
}

0 comments on commit 61057ff

Please sign in to comment.