Skip to content

Commit

Permalink
Remove deprecated legacy for 2.0 (#79)
Browse files Browse the repository at this point in the history
* removing legacy and refactoring tests

* another round of legacy clean up

* cleaning up legacy

* removed actions interceptor

* clean up from legacy

* clean-up legacy

* legacy clean up

* refactoring

* removed legacy

* lint autocorrect

* minor fixes

* minor fixes
  • Loading branch information
KazaiMazai authored Sep 3, 2024
1 parent f3b9005 commit 8ace26f
Show file tree
Hide file tree
Showing 117 changed files with 1,412 additions and 5,569 deletions.
12 changes: 5 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// swift-tools-version:5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
import CompilerPluginSupport
Expand All @@ -11,13 +10,12 @@ let package = Package(
.macOS(.v10_15),
.tvOS(.v12),
.watchOS(.v7)

],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "Puredux",
targets: ["Puredux"]),
targets: ["Puredux"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0"),
Expand All @@ -38,17 +36,17 @@ let package = Package(
"PureduxMacros",
"PureduxMacrosPlugin"
]),

.testTarget(
name: "PureduxTests",
dependencies: ["Puredux"]),

.testTarget(
name: "PureduxMacrosTests",
dependencies: [
"PureduxMacros",
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax")
])
]
)
6 changes: 3 additions & 3 deletions Packages/PureduxMacros/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let package = Package(
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "PureduxMacros",
targets: ["PureduxMacros"]),
targets: ["PureduxMacros"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0")
Expand All @@ -27,9 +27,9 @@ let package = Package(
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax")
]),

.testTarget(
name: "PureduxMacrosTests",
dependencies: ["PureduxMacros"]),
dependencies: ["PureduxMacros"])
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ public struct InjectedStoreMacro: AccessorMacro, PeerMacro {
else {
return []
}

return [
"""
get { Self[\(raw: propertiesAttributes.keyName).self] }
""",
"""
set { Self[\(raw: propertiesAttributes.keyName).self] = newValue }
""",
"""
]
}

public static func expansion(
of node: SwiftSyntax.AttributeSyntax,
providingPeersOf declaration: some SwiftSyntax.DeclSyntaxProtocol,
in context: some SwiftSyntaxMacros.MacroExpansionContext) throws -> [SwiftSyntax.DeclSyntax] {

guard let variableDeclaration = declaration.as(VariableDeclSyntax.self),
let propertiesAttributes = variableDeclaration.propertiesAttributes()
else {
return []
}

return [
"""
enum \(raw: propertiesAttributes.keyName): InjectionKey {
Expand All @@ -57,7 +57,7 @@ public struct InjectedStoreMacro: AccessorMacro, PeerMacro {
struct PropertyAttributes {
let propertyName: String
let initializerClauseSyntax: InitializerClauseSyntax

var keyName: String { "\(propertyName.capitalized)Key" }
}

Expand All @@ -67,20 +67,20 @@ extension VariableDeclSyntax {
guard modifiers.first?.name.text != "static" else {
return nil
}

for binding in bindings {
guard let propertyName = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier.text,
let initializer = binding.initializer

else {
continue
}

return PropertyAttributes(
propertyName: propertyName,
initializerClauseSyntax: initializer
)

}
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Puredux/SideEffects/AnyCancellableEffect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import Foundation

class AnyCancellableEffect {
class EffectStateObserver { }

var observer: AnyObject = EffectStateObserver()

init(_ observer: AnyObject) {
self.observer = observer
}

init() {
self.observer = EffectStateObserver()
}

func cancel() {
observer = EffectStateObserver()
}
Expand Down
68 changes: 33 additions & 35 deletions Sources/Puredux/SideEffects/Effect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,74 +9,73 @@ import Foundation

struct Effect {
typealias Operation = () -> Void

private let perform: Operation?

var operation: Operation {
perform ?? { }
}

var canBeExecuted: Bool {
perform != nil
}

init(_ operation: @escaping () -> Void) {
perform = operation
}

private init(operation: Operation?) {
perform = operation
}



init(operation: @escaping () async -> Void) {
perform = {
Task { await operation() }
}
}

static let skip: Effect = Effect(operation: nil)
}

extension Effect {
struct State: Codable, Equatable, Hashable {
typealias ID = UUID

private(set) var id = ID()
private var state = InternalState()

var status: Status {
state.status
}

var isInProgress: Bool {
status.isInProgress
}

var isSuccess: Bool {
status.isSuccess
}

var isCancelled: Bool {
status.isCancelled
}

var isIdle: Bool {
status.isIdle
}

var isFailed: Bool {
status.isFailed
}

var error: Error? {
state.error
}

var currentAttempt: Int? {
state.currentAttempt?.attempt
}

var delay: TimeInterval? {
state.currentAttempt?.delay
}
Expand All @@ -86,19 +85,19 @@ extension Effect {
extension Effect.State {
static func running(maxAttempts: Int = 1,
delay: TimeInterval = .zero) -> Effect.State {

var effect = Effect.State()
effect.run(maxAttempts: maxAttempts, delay: delay)
return effect
}

static func idle() -> Effect.State {
Effect.State()
}
}

extension Effect.State {

enum Status: Int {
case none
case inProgress
Expand All @@ -112,56 +111,55 @@ extension Effect.State.Status {
var isInProgress: Bool {
self == .inProgress
}

var isSuccess: Bool {
self == .success
}

var isCancelled: Bool {
self == .cancelled
}

var isIdle: Bool {
self == .none
}

var isFailed: Bool {
self == .failure
}
}

extension Effect.State {

mutating func run(maxAttempts: Int = 1,
delay: TimeInterval = .zero) {

state.run(maxAttempts: maxAttempts, delay: delay)
}

mutating func restart(maxAttempts: Int = 1,
delay: TimeInterval = .zero) {

state.restart(maxAttempts: maxAttempts, delay: delay)
}

mutating func retryOrFailWith(_ error: Error?) {
state.retryOrFailWith(error)
}

mutating func cancel() {
state.cancel()
}

mutating func succeed() {
state.succeed()
}

mutating func reset() {
state.reset()
}

mutating func fail(_ error: Error?) {
state.fail(error)
}
}

20 changes: 10 additions & 10 deletions Sources/Puredux/SideEffects/EffectOperator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,38 @@ import Foundation
final class EffectOperator {
private(set) var executing: [Effect.State: DispatchWorkItem] = [:]
private(set) var isSynced = true

func run(_ inProgress: Bool,
on queue: DispatchQueue,
create: (Effect.State) -> Effect) {

let effect: Effect.State = inProgress ? .running() : .idle()
run(effect, on: queue, create: create)
}

func run(_ effect: Effect.State,
on queue: DispatchQueue,
create: (Effect.State) -> Effect) {

run([effect], on: queue, create: create)
}

func run<Effects>(_ effects: Effects,
on queue: DispatchQueue,
create: (Effect.State) -> Effect)

where Effects: Collection & Hashable, Effects.Element == Effect.State {

let effectsInProgress = effects.filter { $0.isInProgress }
let expectedToBeExecuting = Set(effectsInProgress)

executing.keys
.filter { !expectedToBeExecuting.contains($0) }
.forEach {
executing[$0]?.cancel()
executing[$0] = nil
}

effectsInProgress
.filter { !executing.keys.contains($0) }
.map { state in (state, create(state)) }
Expand All @@ -52,7 +52,7 @@ final class EffectOperator {
executing[state] = workItem
queue.asyncAfter(delay: state.delay, execute: workItem)
}

isSynced = expectedToBeExecuting.count == executing.count
}
}
Loading

0 comments on commit 8ace26f

Please sign in to comment.