Skip to content

Commit

Permalink
Store DI docs (#55)
Browse files Browse the repository at this point in the history
* refactoring

* updated docs
  • Loading branch information
KazaiMazai authored Aug 29, 2024
1 parent b3eabe2 commit c22ccc9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 27 deletions.
25 changes: 0 additions & 25 deletions Sources/Puredux/Store/StoreActionsTransformations.swift

This file was deleted.

56 changes: 55 additions & 1 deletion Sources/Puredux/Store/StoreOf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,62 @@
// Created by Sergey Kazakov on 24/08/2024.
//

import Foundation
import SwiftUI


/**
A property wrapper that implements a Dependency Injection (DI) pattern by providing access to an injected instance of `StateStore`.

The `StoreOf` property wrapper is used to access and manage an instance of `StateStore` (or an optional `StateStore`) that is injected into the `Injected` type. This pattern facilitates dependency management by allowing components to retrieve dependencies directly through property wrappers.
- Note: This property wrapper can be initialized with key paths to injected `StateStore` or optional `StateStore` types.
- Parameter T: The type of the `StateStore` or optional `StateStore` being accessed.

Example usage:

```swift

// Use InjectEntry to inject the instance

extension Injected {
@InjectEntry var rootState = StateStore<AppRootState, Action>(AppRootState()) { state, action in
// Here is a reducer used to mutate the state
}
}


// Use @StoreOf property wrapper to obtain injected instance:

struct MyView: View {
@State @StoreOf(\.rootState)
var store: StateStore<AppRootState, Action>

var body: some View {
// ...
}
}

// Here is a more complicated Store configration example

struct AnotherView: View {
@State @ViewStore var store = { cancellable in
StoreOf(\.rootState) // resolves injected root state store
.with(LocalState()) { localState, action in // merged with local state store
// Here is a reducer used to mutate the local state
}
.onChangeEffect(cancellable) { state, dispatch in // cancellable side effects
Effect {
// ...
}
}
}

var body: some View {
// ...
}
}

```
*/
@propertyWrapper
public struct StoreOf<T> {
private let keyPath: WritableKeyPath<Injected, T>
Expand Down
1 change: 0 additions & 1 deletion Sources/Puredux/Store/StoreProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,3 @@ public protocol StoreProtocol<State, Action> {
func getStore() -> Store<State, Action>
}


19 changes: 19 additions & 0 deletions Sources/Puredux/Store/Transformations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,22 @@ public extension StoreProtocol {
}

// swiftlint:enable large_tuple identifier_name


//MARK: - Actions Transformations

extension Store {
func map<A>(_ transform: @escaping (A) -> Action) -> Store<State, A> {
Store<State, A>(
dispatcher: { action in dispatch(transform(action)) },
subscribe: subscribe
)
}
}

extension StateStore {

func map<A>(_ transform: @escaping (A) -> Action) -> Store<State, A> {
strongStore().map(transform)
}
}

0 comments on commit c22ccc9

Please sign in to comment.