Skip to content

Commit

Permalink
Update to Swift 6
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelesantos committed Oct 7, 2024
1 parent 4dda22e commit 6a4c3ec
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 43 deletions.
16 changes: 8 additions & 8 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.9
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -7,13 +7,13 @@ let package = Package(
name: "RefdsDesignPatterns",
defaultLocalization: "pt",
platforms: [
.iOS(.v17),
.macCatalyst(.v17),
.macOS(.v14),
.tvOS(.v17),
.watchOS(.v10),
.visionOS(.v1),
.driverKit(.v23)
.iOS(.v18),
.macCatalyst(.v18),
.macOS(.v15),
.tvOS(.v18),
.watchOS(.v11),
.visionOS(.v2),
.driverKit(.v24)
],
products: [
.library(
Expand Down
2 changes: 1 addition & 1 deletion Sources/RefdsRedux/RefdsReduxAction.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import Foundation

public protocol RefdsReduxAction {}
public protocol RefdsReduxAction: Sendable {}
11 changes: 5 additions & 6 deletions Sources/RefdsRedux/RefdsReduxMiddleware.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Foundation

public typealias RefdsReduxMiddlewareCompletion = (RefdsReduxAction) -> Void
public typealias RefdsReduxMiddleware<State> = (State, RefdsReduxAction, @escaping RefdsReduxMiddlewareCompletion) -> Void

public protocol RefdsReduxMiddlewareProtocol {
associatedtype State
var middleware: RefdsReduxMiddleware<State> { get }
public protocol RefdsReduxMiddleware {
func middleware<State: RefdsReduxState>(
state: State,
action: RefdsReduxAction
) -> AsyncStream<RefdsReduxAction>
}
10 changes: 5 additions & 5 deletions Sources/RefdsRedux/RefdsReduxReducer.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Foundation

public typealias RefdsReduxReducer<State> = (State, RefdsReduxAction) -> State

public protocol RefdsReduxReducerProtocol {
associatedtype State
var reduce: RefdsReduxReducer<State> { get }
public protocol RefdsReduxReducer {
func reduce<State: RefdsReduxState>(
state: State,
action: RefdsReduxAction
) -> State
}
2 changes: 1 addition & 1 deletion Sources/RefdsRedux/RefdsReduxState.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import Foundation

public protocol RefdsReduxState {}
public protocol RefdsReduxState: Sendable {}
44 changes: 22 additions & 22 deletions Sources/RefdsRedux/RefdsReduxStore.swift
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import Foundation
import SwiftUI

public final class RefdsReduxStore<State>: ObservableObject {
@MainActor
public class RefdsReduxStore<State: RefdsReduxState>: ObservableObject {
@Published public var state: State

public var middlewares: [RefdsReduxMiddleware<State>]
public var reducer: RefdsReduxReducer<State>

private let queue = DispatchQueue(
label: "refds.designPatterns.redux.middleware",
qos: .userInteractive,
attributes: .concurrent
)
public let reducer: RefdsReduxReducer
public let middlewares: [RefdsReduxMiddleware]

public init(
reducer: @escaping RefdsReduxReducer<State>,
state: State,
middlewares: [RefdsReduxMiddleware<State>] = []
reducer: RefdsReduxReducer,
middlewares: [RefdsReduxMiddleware] = []
) {
self._state = Published(initialValue: state)
self.reducer = reducer
self.state = state
self.middlewares = middlewares
}

public func dispatch(action: RefdsReduxAction) {
DispatchQueue.main.async {
withAnimation {
self.state = self.reducer(self.state, action)
}
}
public func dispatch(action: RefdsReduxAction) async {
let stateReduced = reducer.reduce(
state: state,
action: action
)

withAnimation(.easeInOut) { state = stateReduced }

self.middlewares.forEach { middleware in
queue.async {
middleware(self.state, action, self.dispatch)
for middleware in middlewares {
let actions = middleware.middleware(
state: stateReduced,
action: action
)

for await action in actions {
await dispatch(action: action)
}
}
}
Expand Down

0 comments on commit 6a4c3ec

Please sign in to comment.