Skip to content

Commit

Permalink
Added missing docs (#95)
Browse files Browse the repository at this point in the history
* added docs for equating

* added docs for AnyStore

* added docs

* added docs

* updated async action docs

* updated UIStateObserver docs
  • Loading branch information
KazaiMazai authored Sep 9, 2024
1 parent 232d31a commit d149205
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Sources/Puredux/SideEffects/AsyncAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import Foundation
import Dispatch

/**
A protocol that defines an asynchronous action that can be dispatched and executed on a specific `DispatchQueue`.

Types conforming to `AsyncAction` represent actions that perform asynchronous tasks, such as network requests or long-running computations, and return a result upon completion.
*/
public protocol AsyncAction: Sendable {
associatedtype ResultAction
var dispatchQueue: DispatchQueue { get }
Expand Down
6 changes: 6 additions & 0 deletions Sources/Puredux/SideEffects/Effect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

import Foundation

/**
A structure representing a side effect that can be performed asynchronously.

`Effect` is used to encapsulate side effects or operations that can be performed outside the regular Store's data flow, like network requests, database updates, or other actions that are triggered in response to some event.

*/
public struct Effect: Sendable {
typealias Operation = @Sendable () -> Void
private let perform: Operation?
Expand Down
7 changes: 7 additions & 0 deletions Sources/Puredux/Store/AnyStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ typealias Subscribe<State> = @Sendable (_ observer: Observer<State>) -> Void

typealias ReferencedStore<State, Action> = ReferencedObject<AnyStoreObject<State, Action>>

/**
A type-erased store that conforms to the `Store` protocol. `AnyStore` allows for the abstraction of a specific store's state and actions, providing a general interface to interact with any store.

- Parameters:
- State: The type representing the state of the store, which must conform to `Sendable`.
- Action: The type representing actions that can be dispatched to the store, which must conform to `Sendable`.
*/
public struct AnyStore<State, Action>: Store where State: Sendable,
Action: Sendable {
let dispatchHandler: Dispatch<Action>
Expand Down
5 changes: 5 additions & 0 deletions Sources/Puredux/Store/DI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public struct Injected {
}
}

/**
A protocol that defines a key used for dependency injection.

Types conforming to `InjectionKey` provide a mechanism to inject dependencies into a `Injected` by associating a specific type of value (`Value`) with the key and providing default value
*/
public protocol InjectionKey {
/** The associated type representing the type of the dependency injection key's value. */
associatedtype Value
Expand Down
5 changes: 5 additions & 0 deletions Sources/Puredux/UIKit/UIKit+StoreBindings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ extension UIView: UIStateObserver { }

#endif

/**
A protocol that defines a UI state observer.

Types conforming to `UIStateObserver` can observe and respond to state changes in the user interface. The protocol requires conformance to `AnyObject`, meaning it can only be adopted by class types. Used for `UIViewController` & `UIView`.
*/
public protocol UIStateObserver: AnyObject { }

extension UIStateObserver {
Expand Down
63 changes: 63 additions & 0 deletions Sources/Puredux/Utils/Equating.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,88 @@

import Foundation

/**
A generic structure `Equating` that allows the comparison of two values of type `T` based on a custom predicate.

*/
public struct Equating<T>: Sendable {
public let predicate: @Sendable (T, T) -> Bool

/**
Initializes an `Equating` instance with a custom predicate.

- Parameter predicate: A custom comparison function that takes two values of type `T` and returns a Boolean value indicating whether they are equal.
*/
public init(predicate: @Sendable @escaping (T, T) -> Bool) {
self.predicate = predicate
}
}

public extension Equating {
/**
A static property that provides an `Equating` instance where no two values are never considered equal.

- Returns: An `Equating` instance that always returns `false` when comparing two values.
*/
static var neverEqual: Equating {
Equating { _, _ in false }
}

/**
A static property that provides an `Equating` instance where all values are always considered equal.

- Returns: An `Equating` instance that always returns `true` when comparing two values.
*/
static var alwaysEqual: Equating {
Equating { _, _ in true }
}

/**
A static method that creates an `Equating` instance based on an `Equatable` value derived from a function applied to values of type `T`.

- Parameter value: A function that maps a value of type `T` to a value of type `Value` that conforms to `Equatable`.
- Returns: An `Equating` instance that compares the results of the `value` function applied to two values of type `T`.
*/
static func equal<Value: Equatable>(value: @Sendable @escaping (T) -> Value) -> Equating<T> {
Equating {
value($0) == value($1)
}
}

/**
A static method that creates an `Equating` instance based on a `KeyPath` to an `Equatable` value within a value of type `T`.

- Parameter keyPath: A `KeyPath` from `T` to a `Value` that conforms to `Equatable`.
- Returns: An `Equating` instance that compares the values at the given `KeyPath` for two values of type `T`.
*/
static func keyPath<Value: Equatable>(_ keyPath: KeyPath<T, Value>) -> Equating<T> {
.equal(value: { $0[keyPath: keyPath]})
}
}

public extension Equating {
/**
A static method that combines multiple `Equating` instances from a sequence into a single `Equating` instance.

This method applies a logical AND (`&&`) between all `Equating` instances in the sequence, ensuring that all predicates must return `true` for the combined `Equating` instance to consider two values equal.

- Parameter sequence: A sequence of `Equating<T>` instances to be combined.
- Returns: A single `Equating<T>` instance that performs the AND operation on the predicates of the sequence.
*/
static func with<S: Sequence>(_ sequence: S) -> Equating<T> where S.Element == Equating<T> {
sequence.reduce(.alwaysEqual, &&)
}

/**
A static method that creates a new `Equating` instance by combining two existing `Equating` instances using a logical AND (`&&`).

The resulting `Equating` instance will only return `true` if both the `lhs` and `rhs` predicates return `true` for the given values.

- Parameters:
- lhs: The left-hand `Equating` instance.
- rhs: The right-hand `Equating` instance.
- Returns: A new `Equating<T>` instance that combines the predicates of `lhs` and `rhs` using a logical AND.
*/
static func && (lhs: Equating<T>, rhs: Equating<T>) -> Equating<T> {
Equating<T> {
lhs.predicate($0, $1) && rhs.predicate($0, $1)
Expand All @@ -48,6 +97,13 @@ public extension Equating {
}

public extension Equating where T: Equatable {
/**
A static property that creates a new `Equating` instance for types that conform to `Equatable`.

This `Equating` instance uses the default equality operator to compare two values of type `T`.

- Returns: An `Equating<T>` instance that compares two values using `Equatable`'s `==` operator.
*/
static var asEquatable: Equating {
Equating {
$0 == $1
Expand All @@ -56,6 +112,13 @@ public extension Equating where T: Equatable {
}

public extension Equating where T: Identifiable {
/**
A static property that creates an `Equating` instance for types that conform to `Identifiable`.

This `Equating` instance compares the `id` property of two values to determine if they are considered equal.

- Returns: An `Equating<T>` instance that compares two values based on their `id` property.
*/
static var asIdentifiable: Equating {
Equating {
$0.id == $1.id
Expand Down

0 comments on commit d149205

Please sign in to comment.