Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store Object Refactoring #7

Merged
merged 17 commits into from
Mar 31, 2024
Merged

Store Object Refactoring #7

merged 17 commits into from
Mar 31, 2024

Conversation

KazaiMazai
Copy link
Owner

  • Public StoreObject is marked as deprecated (only renaming required)
  • Moved actual StoreObject the Store internals.
  • Updated docs
  • Updated tests

This change allows to:

  • Keep a strong reference to the StoreNode and control its state lifecycle by doing that
  • Obtain a weak reference store when needed to guarantee any possibility of creating ref cycles
  • Do both things with just Store

This:

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

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

and this:

extension Store {
    func store() -> Store<State, Action> {
        switch storeType {
        case .storeObject(let referencedStore):
            return referencedStore.weakRefStore()
        case .store:
            return self
        }
    }
    
    static func referencedStore(dispatch: @escaping Dispatch,
                                subscribe: @escaping Subscribe) -> Store {
        
        Store(
            .storeObject(StoreObject(
                dispatch: dispatch,
                subscribe: subscribe)
            )
        )
    }
}

@KazaiMazai KazaiMazai self-assigned this Mar 30, 2024
@KazaiMazai KazaiMazai merged commit e5cb2db into main Mar 31, 2024
1 check passed
@KazaiMazai KazaiMazai deleted the StoreObject-Refactoring branch September 3, 2024 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant