Skip to content

Commit

Permalink
Merge branch 'feature/gear-short-syntax'
Browse files Browse the repository at this point in the history
  • Loading branch information
twittemb committed Aug 24, 2020
2 parents 7729983 + e365278 commit 28d99bf
Show file tree
Hide file tree
Showing 15 changed files with 260 additions and 55 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
**version 0.20.0**:

* add a short syntax to build a feedback attached to a Gear

**version 0.19.0**:

* add helper constructors for Feedback to be able to pass dependencies
Expand Down
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,12 @@ let feedback = Feedback<State, Event>(attachedTo: gear, propagating: { (event: G
return nil
})

// or with the short syntax

let feedback = Feedback<State, Event>(attachedTo: gear, catching: .authorizationIssueHappened, emitting: .checkAuthorization)

...
// create the Check Authorization Spin with this feedback
// then, create the Check Authorization Spin with this feedback
...
```

Expand All @@ -519,13 +523,17 @@ At last we have to tell a feedback from the feature Spin how it will push events
let feedback = Feedback<State, Event>(attachedTo: gear, propagating: { (state: State) in
if state == .unauthorized {
// only the .unauthorized state should trigger en event in the Gear
return . authorizationIssueHappened
return .authorizationIssueHappened
}
return nil
})

// or with the short syntax

let feedback = Feedback<State, Event>(attachedTo: gear, catching: .unauthorized, propagating: .authorizationIssueHappened)

...
// create the Feature Spin with this feedback
// then, create the Feature Spin with this feedback
...
```

Expand Down Expand Up @@ -571,7 +579,7 @@ https://github.com/Spinners/Spin.Swift.git
Add the following entry to your Cartfile:

```
github "Spinners/Spin.Swift" ~> 0.19.0
github "Spinners/Spin.Swift" ~> 0.20.0
```

and then:
Expand All @@ -585,9 +593,9 @@ carthage update Spin.Swift
Add the following dependencies to your Podfile:

```
pod 'SpinReactiveSwift', '~> 0.19.0'
pod 'SpinCombine', '~> 0.19.0'
pod 'SpinRxSwift', '~> 0.19.0'
pod 'SpinReactiveSwift', '~> 0.20.0'
pod 'SpinCombine', '~> 0.20.0'
pod 'SpinRxSwift', '~> 0.20.0'
```

You should then be able to import SpinCommon (base implementation), SpinRxSwift, SpinReactiveSwift or SpinCombine
Expand Down
15 changes: 15 additions & 0 deletions Sources/Combine/Feedback.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,19 @@ where SchedulerTime: Strideable, SchedulerTime.Stride: SchedulerTimeIntervalConv

self.init(effect: effect, on: executer)
}

public init<Event>(attachedTo gear: Gear<Event>,
catching event: Event,
emitting loopEvent: EventStream.Value,
on executer: Executer? = nil) where Event: Equatable {
let emitFunction: (Event) -> EventStream .Value? = { gearEvent in
if event == gearEvent {
return loopEvent
}

return nil
}

self.init(attachedTo: gear, propagating: emitFunction, on: executer)
}
}
50 changes: 35 additions & 15 deletions Sources/Common/FeedbackDefinition+Default.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,26 @@ public extension FeedbackDefinition {
self.init(effect: effect, on: executer, applying: strategy)
}

init<GearType: GearDefinition>(attachedTo gear: GearType,
catching state: StateStream.Value,
propagating event: GearType.Event,
on executer: Executer? = nil,
applying strategy: ExecutionStrategy = Self.defaultExecutionStrategy) where StateStream.Value: Equatable {

let propagationFunction: (StateStream.Value) -> GearType.Event? = { feedbackState in
if feedbackState == state {
return event
}

return nil
}

self.init(attachedTo: gear,
propagating: propagationFunction,
on: executer,
applying: strategy)
}

init<Dep1>(effect: @escaping (Dep1, StateStream.Value) -> EventStream,
on executer: Executer? = nil,
applying strategy: ExecutionStrategy = Self.defaultExecutionStrategy,
Expand All @@ -220,31 +240,31 @@ public extension FeedbackDefinition {
}

init<Dep1, Dep2>(effect: @escaping (Dep1, Dep2, StateStream.Value) -> EventStream,
on executer: Executer? = nil,
applying strategy: ExecutionStrategy = Self.defaultExecutionStrategy,
dep1: Dep1,
dep2: Dep2) {
on executer: Executer? = nil,
applying strategy: ExecutionStrategy = Self.defaultExecutionStrategy,
dep1: Dep1,
dep2: Dep2) {
let partializedEffect = partial(effect, arg1: dep1, arg2: dep2, arg3: .undefined)
self.init(effect: partializedEffect, on: executer, applying: strategy)
}

init<Dep1, Dep2, Dep3>(effect: @escaping (Dep1, Dep2, Dep3, StateStream.Value) -> EventStream,
on executer: Executer? = nil,
applying strategy: ExecutionStrategy = Self.defaultExecutionStrategy,
dep1: Dep1,
dep2: Dep2,
dep3: Dep3) {
on executer: Executer? = nil,
applying strategy: ExecutionStrategy = Self.defaultExecutionStrategy,
dep1: Dep1,
dep2: Dep2,
dep3: Dep3) {
let partializedEffect = partial(effect, arg1: dep1, arg2: dep2, arg3: dep3, arg4: .undefined)
self.init(effect: partializedEffect, on: executer, applying: strategy)
}

init<Dep1, Dep2, Dep3, Dep4>(effect: @escaping (Dep1, Dep2, Dep3, Dep4, StateStream.Value) -> EventStream,
on executer: Executer? = nil,
applying strategy: ExecutionStrategy = Self.defaultExecutionStrategy,
dep1: Dep1,
dep2: Dep2,
dep3: Dep3,
dep4: Dep4) {
on executer: Executer? = nil,
applying strategy: ExecutionStrategy = Self.defaultExecutionStrategy,
dep1: Dep1,
dep2: Dep2,
dep3: Dep3,
dep4: Dep4) {
let partializedEffect = partial(effect, arg1: dep1, arg2: dep2, arg3: dep3, arg4: dep4, arg5: .undefined)
self.init(effect: partializedEffect, on: executer, applying: strategy)
}
Expand Down
15 changes: 15 additions & 0 deletions Sources/ReactiveSwift/Feedback.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,19 @@ public struct Feedback<State, Event>: FeedbackDefinition {

self.init(effect: effect, on: executer)
}

public init<Event>(attachedTo gear: Gear<Event>,
catching event: Event,
emitting loopEvent: EventStream.Value,
on executer: Executer? = nil) where Event: Equatable {
let emitFunction: (Event) -> EventStream .Value? = { gearEvent in
if event == gearEvent {
return loopEvent
}

return nil
}

self.init(attachedTo: gear, propagating: emitFunction, on: executer)
}
}
15 changes: 15 additions & 0 deletions Sources/RxSwift/Feedback.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,19 @@ public struct Feedback<State, Event>: FeedbackDefinition {

self.init(effect: effect, on: executer)
}

public init<Event>(attachedTo gear: Gear<Event>,
catching event: Event,
emitting loopEvent: EventStream.Value,
on executer: Executer? = nil) where Event: Equatable {
let emitFunction: (Event) -> EventStream .Value? = { gearEvent in
if event == gearEvent {
return loopEvent
}

return nil
}

self.init(attachedTo: gear, propagating: emitFunction, on: executer)
}
}
Loading

0 comments on commit 28d99bf

Please sign in to comment.