Skip to content

Commit

Permalink
Docs structure update (#90)
Browse files Browse the repository at this point in the history
* docs update for Docc

* updated docs

* updated docs

* docs update

* updated docs

* updated docs

* updated docs
  • Loading branch information
KazaiMazai authored Sep 8, 2024
1 parent 3184362 commit 06473d9
Show file tree
Hide file tree
Showing 18 changed files with 707 additions and 462 deletions.
2 changes: 1 addition & 1 deletion .spi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ version: 1
builder:
configs:
- documentation_targets: [Puredux]

scheme: Puredux
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,20 +260,29 @@ store.dispatch(FetchDataAction())

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:

- **Reducers background execution**: Offloads reducer execution to background queue to improve overall app performance.
- **Reducers background execution**: Offloads reducer logic to background threads to improve overall app performance.
- **State updates deduplication**: Minimizes redundant state updates, reducing unnecessary re-renders and improving processing efficiency.
- **Granular UI updates**: Ensures only the necessary parts of the UI are updated, enhancing responsiveness.
- **UI updates debouncing**: Prevents excessive UI updates by intelligently controlling the frequency of updates.
- **Two-step UI updates with background task offloading**: Heavy computational tasks are handled in the background, with UI updates executed in a structured two-step process to ensure smooth, lag-free interactions.
- **Granular UI updates**: Ensures only the necessary parts of the UI are updated, enhancing responsiveness.

### Reducers Background Execution
As your application and its features expand, you may encounter performance issues, such as reducers taking longer to execute or SwiftUI view
bodies refreshing more frequently than anticipated. This article highlights several common challenges when building features in Puredux and provides solutions to address them.

### Reducers Execution

Puredux is designed in a way that allows you to implement state and reducers without any dependencies.

This is done intentionally to offload all store work to the background without worrying much about data races, access synchronization, and so on, leaving the main thread exclusively for the UI.
This is done intentionally to be able to offload all stores reducers' work to the background without worrying much about data races, access
synchronization with dependencies.

As the result reducers operate in background leaving the main thread exclusively for the UI.


### QoS Tuning


So yes. Reducers are executed in bacckground. When creating the root store, you can choose
the quality of service for the queue it will operate on. It will define the whole store tree hierarchy.
When creating the root store, you can choose the quality of service for the queue it will operate on.

```swift
let store = StateStore(
Expand Down
12 changes: 12 additions & 0 deletions Sources/Puredux/Documentation.docc/Articles/Archive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Archive

Documentation archive

## Overview

Achived pieces of documentation for the old versions.

## Topics

- <doc:Documentation-v1.x>

183 changes: 183 additions & 0 deletions Sources/Puredux/Documentation.docc/Articles/GettingStarted.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# Getting Started

## Installation

### Swift Package Manager


Puredux is available through Swift Package Manager. To install it in Xcode 11.0 or later:

Select File > Swift Packages > Add Package Dependency...
Enter the Puredux repository URL:

```
https://github.com/KazaiMazai/Puredux
```

## Basics

At its core, Puredux follows a predictable state management pattern that consists of the following key components:

- State: A type that represents the entire application state or a portion of it.
- Actions: Events that describe possible changes in the system, which lead to state mutations.
- Reducer: A function that dictates how state changes in response to specific actions.
- Store: The central hub where:
- Initial state and reducers are defined.
- Actions are dispatched to trigger state changes.
- New state values are propagated to any observers or views.



```text
+-----------------------------------------+
| Store |
| |
| +-----------+ +-------------------+ |
| | Reducer |<--| Current State | |
New State | +-----------+ +-------------------+ | Actions
<-----------+ | A |<----------+
| | | | | A
| | V | | |
| | +-------------------+ | | |
| | | New State |------------+ | |
| | +-------------------+ | |
| | | |
| +-----------------------------------------+ |
| |
| |
| |
| +----------------+ +---+----+ |
V Observer | | Async Work | | |
+---------->| Side Effects |--------------->| Action |--------->|
| | | Result | | |
| +----------------+ +----+---+ |
| |
| +----------------+ +---+----+ |
V Observer | | User | | |
+---------->| UI |------------------>| Action |------>+
| | Interactions | |
+----------------+ +----+---+
```

## Store Definitions

Let's break down a typical store setup using Puredux.

**1. Define the Action Protocol**:

Actions in Puredux follow a protocol that ensures they can be handled uniformly.

```swift
protocol Action {
// Define specific actions in your app by conforming to this protocol
}
```

**2. Define the AppState**:

The application’s state can be represented by a struct, which will store the data relevant to your app.
The reduce method defines how the state will change in response to an action.


```swift
struct AppState {
// Define your app's state properties here

mutating func reduce(_ action: Action) {
// Logic for how the state should update when an action is dispatched
}
}
```
**3. Define the Store and Inject it**:

Using the root AppState, we create a store that integrates actions and the state.

The store’s role is to manage actions and apply the reducer function whenever an action is dispatched.

```swift

extension Injected {
@InjectEntry var root = StateStore<AppState, Action>(AppState()) { state, action in
state.reduce(action)
}
}

```

## SwiftUI Bindings

Puredux can seamlessly integrate with SwiftUI to manage both global app state and local states.

```swift

// View might need a local state that we don't need to share with the whole app

struct ViewState {
// ...

mutating func reduce(_ action: Action) {
// ...
}
}
```

In your SwiftUI VIew, you can combine the app's root store with local view-specific states.
This allows the view to respond dynamically to both app-level and view-level state changes.

```swift
struct ContentView: View {
// Combine the root store with local state
@State var store: StoreOf(\.root).with(ViewState()) { state, action in
state.reduce(action)
}

@State var viewState: (AppState, ViewState)?

var body: some View {
MyView(viewState)
.subscribe(store) { viewState = $0 }
.onAppear {
dispatch(SomeAction())
}
}
}

```

## UIKit Bindings

Puredux also supports UIKit, offering a similar approach for handling state and actions within UIViewController.

```swift

struct MyScreenState {
// ...

mutating func reduce(_ action: Action) {
// ...
}
}

class MyViewController: ViewController {
var store: StoreOf(\.root).with(MyScreenState()) { state, action in
state.reduce(action)
}

override func viewDidLoad() {
super.viewDidLoad()

subscribe(store) { [weak self] in
// Handle updates with the derived props
self?.updateUI(with: $0)
}
}

private func updateUI(with state: (AppState, MyScreenState)) {
// Update UI elements with the new view state
}
}

```

17 changes: 17 additions & 0 deletions Sources/Puredux/Documentation.docc/Articles/MigrationGuides.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Migration guides

Learn how to upgrade your application to the newest version of Puredux.

## Overview


> Important: Before following any particular migration guide be sure you have followed all the
> preceding migration guides.
## Topics

- <doc:Migrating-to-2.0.x>
- <doc:Migrating-to-1.9.x>
- <doc:Migrating-to-1.3.x>
- <doc:Migrating-to-1.2.x>
- <doc:Migrating-to-1.1.x>
Loading

0 comments on commit 06473d9

Please sign in to comment.