Skip to content

Commit

Permalink
Added DI section to readme (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
KazaiMazai authored Sep 18, 2024
1 parent 8f37de5 commit 592d6aa
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Develop an iOS app with a UDF Puredux engine using only clean UI, state, actions
- [Side Effects](#side-effects)
* [Async Actions](#async-actions)
* [State-Driven Side Effects](#state-driven-side-effects)
- [Dependency Injection](#dependency-injection)
* [Store Injection](#store-injection)
* [Dependency Injection](#dependency-injection-1)
- [Performance](#performance)
* [Reducers Execution](#reducers-execution)
* [QoS Tuning](#qos-tuning)
Expand Down Expand Up @@ -433,6 +436,68 @@ store.dispatch(.startJob)

A powerful feature of State-driven Side Effects is that their scope and lifetime are defined by the store they are connected to. This makes them especially beneficial in complex store hierarchies, such as app-level stores, feature-scoped stores, and per-screen stores, as their side effects automatically align with the lifecycle of each store.

## Dependency Injection

Puredux splits dependencies into two categories:

- Store Injection
- Dependency Injection

Although both are essentially dependencies, they are handled separately because they serve different purposes, and Puredux ensures they remain distinct.

- **Store Injection** is used to conveniently obtain store instances in the UI layer of the application.
- **Dependency Injection** is used inside the store's reducers to power the application's core logic.

### Store Injection

Use `@StoreEntry` in the `SharedStores` extension to inject the store instance:

```swift
extension SharedStores {
@StoreEntry var root = StateStore<AppRootState, Action>(....)
}


// The `@StoreOf` property wrapper can be used to obtain the injected store instance:

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

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

### Dependency Injection

Use `@DependencyEntry` in the `Dependencies` extension to inject the dependency instance:

```swift
extension Dependencies {
@DependencyEntry var now = { Date() }
}


// Then it can be used in the app reducer:

struct AppState {
private var currentTime: Date?

mutating func reduce(_ action: Action) {
switch action {
case let action as UpdateTime:
let now = Dependency[\.now]
currentTime = now()
default:
break
}
}
}

```

## Performance

Puredux offers a robust strategy for addressing the performance challenges commonly faced in iOS applications. It provides several key optimizations to enhance app responsiveness and efficiency, including:
Expand Down

0 comments on commit 592d6aa

Please sign in to comment.