Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Refactor code by adding Effect, EffectQueue, Feedback #12

Merged
merged 1 commit into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ matrix:
- os: osx
language: generic
script:
- swift build
- REACTIVEAUTOMATON_SPM_TEST=1 swift test
- make build
- make test
env:
- JOB=swiftpm-mac

Expand All @@ -62,6 +62,6 @@ matrix:
before_install:
- eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/9f442512a46d7a2af7b850d65a7e9bd31edfb09b/swiftenv-install.sh)"
script:
- swift build
- REACTIVEAUTOMATON_SPM_TEST=1 swift test
- make build
- make test
env: JOB=swiftpm-linux
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build:
swift build

test:
REACTIVEAUTOMATON_SPM_TEST=1 swift test
42 changes: 31 additions & 11 deletions ReactiveAutomaton.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@
objects = {

/* Begin PBXBuildFile section */
1F00C32F1D5CB3D900CC3139 /* ReactiveCocoa+ThenValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F00C32E1D5CB3D900CC3139 /* ReactiveCocoa+ThenValue.swift */; };
1F0D0F141DC4DCF40083700F /* ReactiveSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1F0D0F131DC4DCF40083700F /* ReactiveSwift.framework */; };
1F0D0F161DC4DD3B0083700F /* Quick.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1F0D0F151DC4DD3B0083700F /* Quick.framework */; };
1F0D0F181DC4DD3E0083700F /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1F0D0F171DC4DD3E0083700F /* Nimble.framework */; };
1F1830A822AFFF220068EAB5 /* Feedback.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F1830A722AFFF220068EAB5 /* Feedback.swift */; };
1F1830AA22B005C60068EAB5 /* Automaton+Feedback.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F1830A922B005C50068EAB5 /* Automaton+Feedback.swift */; };
1F242B1E1D08770400647917 /* TerminatingSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F242AFE1D08766900647917 /* TerminatingSpec.swift */; };
1F24EB161CDD19240008028E /* ReactiveAutomaton.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1FAEBCF51CDCEF510046EA72 /* ReactiveAutomaton.framework */; };
1F24EB3D1CDD197B0008028E /* MappingSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F24EB3B1CDD197B0008028E /* MappingSpec.swift */; };
1F9602FF22AD08A80077EFCD /* EffectCancellationSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F9602FD22AD08360077EFCD /* EffectCancellationSpec.swift */; };
1F954C7822B088B8001891B7 /* InitialEffectSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F68CE9F22B087570005158C /* InitialEffectSpec.swift */; };
1F954C7B22B08BFD001891B7 /* FeedbackSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F954C7922B08AD7001891B7 /* FeedbackSpec.swift */; };
1F987E1C22AFE8340053AE8F /* EffectQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F987E1B22AFE8340053AE8F /* EffectQueue.swift */; };
1F987E1F22AFFAC10053AE8F /* EffectCancellationSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F987E1D22AFFA910053AE8F /* EffectCancellationSpec.swift */; };
1FA0AC441DE8AAC7007F01E0 /* StateFuncMappingSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA0AC431DE8AAC7007F01E0 /* StateFuncMappingSpec.swift */; };
1FAEBD041CDCEF940046EA72 /* ReactiveAutomaton.h in Headers */ = {isa = PBXBuildFile; fileRef = 1FAEBD021CDCEF940046EA72 /* ReactiveAutomaton.h */; settings = {ATTRIBUTES = (Public, ); }; };
1FAEBD061CDCEFD50046EA72 /* Automaton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FAEBD051CDCEFD50046EA72 /* Automaton.swift */; };
1FAEBD081CDCF04E0046EA72 /* Reply.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FAEBD071CDCF04E0046EA72 /* Reply.swift */; };
1FE8C50C22AF4B27002DEC22 /* Effect.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FE8C50B22AF4B27002DEC22 /* Effect.swift */; };
487C0F291D00311E0071666D /* Fixtures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 487C0F281D00311E0071666D /* Fixtures.swift */; };
487C0F4B1D0032D80071666D /* EffectMappingSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 487C0F4A1D0032D80071666D /* EffectMappingSpec.swift */; };
487C0F4D1D003B2E0071666D /* AnyMappingSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 487C0F4C1D003B2E0071666D /* AnyMappingSpec.swift */; };
Expand All @@ -37,10 +42,11 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
1F00C32E1D5CB3D900CC3139 /* ReactiveCocoa+ThenValue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ReactiveCocoa+ThenValue.swift"; sourceTree = "<group>"; };
1F0D0F131DC4DCF40083700F /* ReactiveSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = ReactiveSwift.framework; sourceTree = BUILT_PRODUCTS_DIR; };
1F0D0F151DC4DD3B0083700F /* Quick.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Quick.framework; sourceTree = BUILT_PRODUCTS_DIR; };
1F0D0F171DC4DD3E0083700F /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Nimble.framework; sourceTree = BUILT_PRODUCTS_DIR; };
1F1830A722AFFF220068EAB5 /* Feedback.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Feedback.swift; sourceTree = "<group>"; };
1F1830A922B005C50068EAB5 /* Automaton+Feedback.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Automaton+Feedback.swift"; sourceTree = "<group>"; };
1F242AFE1D08766900647917 /* TerminatingSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TerminatingSpec.swift; sourceTree = "<group>"; };
1F24E04E1CDD15980008028E /* Base.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Base.xcconfig; sourceTree = "<group>"; };
1F24E04F1CDD15980008028E /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Debug.xcconfig; sourceTree = "<group>"; };
Expand All @@ -52,7 +58,10 @@
1F24EB3A1CDD197B0008028E /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
1F24EB3B1CDD197B0008028E /* MappingSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MappingSpec.swift; sourceTree = "<group>"; };
1F4B96F622ACC1C200AC0BD1 /* Package.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = "<group>"; };
1F9602FD22AD08360077EFCD /* EffectCancellationSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EffectCancellationSpec.swift; sourceTree = "<group>"; };
1F68CE9F22B087570005158C /* InitialEffectSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InitialEffectSpec.swift; sourceTree = "<group>"; };
1F954C7922B08AD7001891B7 /* FeedbackSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FeedbackSpec.swift; sourceTree = "<group>"; };
1F987E1B22AFE8340053AE8F /* EffectQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EffectQueue.swift; sourceTree = "<group>"; };
1F987E1D22AFFA910053AE8F /* EffectCancellationSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EffectCancellationSpec.swift; sourceTree = "<group>"; };
1FA0AC431DE8AAC7007F01E0 /* StateFuncMappingSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StateFuncMappingSpec.swift; sourceTree = "<group>"; };
1FAEBCF51CDCEF510046EA72 /* ReactiveAutomaton.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = ReactiveAutomaton.framework; sourceTree = BUILT_PRODUCTS_DIR; };
1FAEBD011CDCEF940046EA72 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand All @@ -61,6 +70,7 @@
1FAEBD071CDCF04E0046EA72 /* Reply.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Reply.swift; sourceTree = "<group>"; };
1FAEBD091CDCF0990046EA72 /* Result.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Result.framework; sourceTree = BUILT_PRODUCTS_DIR; };
1FB0B4FA1E2254A900052073 /* LinuxMain.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LinuxMain.swift; sourceTree = "<group>"; };
1FE8C50B22AF4B27002DEC22 /* Effect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Effect.swift; sourceTree = "<group>"; };
487C0F281D00311E0071666D /* Fixtures.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Fixtures.swift; sourceTree = "<group>"; };
487C0F4A1D0032D80071666D /* EffectMappingSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EffectMappingSpec.swift; sourceTree = "<group>"; };
487C0F4C1D003B2E0071666D /* AnyMappingSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnyMappingSpec.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -125,12 +135,14 @@
children = (
487C0F271D00311E0071666D /* Fixtures */,
1F24EB3B1CDD197B0008028E /* MappingSpec.swift */,
487C0F4A1D0032D80071666D /* EffectMappingSpec.swift */,
1F9602FD22AD08360077EFCD /* EffectCancellationSpec.swift */,
487C0F4C1D003B2E0071666D /* AnyMappingSpec.swift */,
1FA0AC431DE8AAC7007F01E0 /* StateFuncMappingSpec.swift */,
48C03E751D4093610084B208 /* EffectMappingLatestSpec.swift */,
487C0F4C1D003B2E0071666D /* AnyMappingSpec.swift */,
1F242AFE1D08766900647917 /* TerminatingSpec.swift */,
487C0F4A1D0032D80071666D /* EffectMappingSpec.swift */,
48C03E751D4093610084B208 /* EffectMappingLatestSpec.swift */,
1F987E1D22AFFA910053AE8F /* EffectCancellationSpec.swift */,
1F68CE9F22B087570005158C /* InitialEffectSpec.swift */,
1F954C7922B08AD7001891B7 /* FeedbackSpec.swift */,
1F24EB3A1CDD197B0008028E /* Info.plist */,
);
path = ReactiveAutomatonTests;
Expand Down Expand Up @@ -162,9 +174,12 @@
children = (
1FAEBD021CDCEF940046EA72 /* ReactiveAutomaton.h */,
1FAEBD051CDCEFD50046EA72 /* Automaton.swift */,
1F1830A922B005C50068EAB5 /* Automaton+Feedback.swift */,
1FAEBD071CDCF04E0046EA72 /* Reply.swift */,
1FE8C50B22AF4B27002DEC22 /* Effect.swift */,
1F987E1B22AFE8340053AE8F /* EffectQueue.swift */,
1F1830A722AFFF220068EAB5 /* Feedback.swift */,
48B2D1131CED890D0095F175 /* Mapping+Helper.swift */,
1F00C32E1D5CB3D900CC3139 /* ReactiveCocoa+ThenValue.swift */,
1FAEBD011CDCEF940046EA72 /* Info.plist */,
);
path = Sources;
Expand Down Expand Up @@ -331,20 +346,25 @@
1F242B1E1D08770400647917 /* TerminatingSpec.swift in Sources */,
487C0F4B1D0032D80071666D /* EffectMappingSpec.swift in Sources */,
1FA0AC441DE8AAC7007F01E0 /* StateFuncMappingSpec.swift in Sources */,
1F9602FF22AD08A80077EFCD /* EffectCancellationSpec.swift in Sources */,
1F987E1F22AFFAC10053AE8F /* EffectCancellationSpec.swift in Sources */,
1F954C7822B088B8001891B7 /* InitialEffectSpec.swift in Sources */,
487C0F4D1D003B2E0071666D /* AnyMappingSpec.swift in Sources */,
487C0F291D00311E0071666D /* Fixtures.swift in Sources */,
48C03E951D4094270084B208 /* EffectMappingLatestSpec.swift in Sources */,
1F954C7B22B08BFD001891B7 /* FeedbackSpec.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
1FAEBCF01CDCEF510046EA72 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1FE8C50C22AF4B27002DEC22 /* Effect.swift in Sources */,
1FAEBD081CDCF04E0046EA72 /* Reply.swift in Sources */,
1F00C32F1D5CB3D900CC3139 /* ReactiveCocoa+ThenValue.swift in Sources */,
1F1830AA22B005C60068EAB5 /* Automaton+Feedback.swift in Sources */,
48B2D1141CED890D0095F175 /* Mapping+Helper.swift in Sources */,
1F987E1C22AFE8340053AE8F /* EffectQueue.swift in Sources */,
1F1830A822AFFF220068EAB5 /* Feedback.swift in Sources */,
1FAEBD061CDCEFD50046EA72 /* Automaton.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
44 changes: 44 additions & 0 deletions Sources/Automaton+Feedback.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import ReactiveSwift

extension Automaton
{
/// Initializer using `feedback` for injecting side-effects.
///
/// - Parameters:
/// - state: Initial state.
/// - input: `Signal<Input, Never>` that automaton receives.
/// - mapping: Simple `Mapping` that designates next state only (no additional effect).
/// - feedback: `Signal` transformer that performs side-effect and emits next input.
public convenience init(
state initialState: State,
inputs inputSignal: Signal<Input, Never>,
mapping: @escaping Mapping,
feedback: Feedback<Reply<State, Input>.Success, Input>
)
{
self.init(
state: initialState,
inputs: inputSignal,
makeSignals: { from -> MakeSignals in
let mapped = from
.map { input, fromState in
return (input, fromState, mapping(fromState, input))
}

let replies = mapped
.map { input, fromState, mapped -> Reply<State, Input> in
if let toState = mapped {
return .success((input, fromState, toState))
}
else {
return .failure((input, fromState))
}
}

let effects = feedback.transform(replies.filterMap { $0.success }).producer

return (replies, effects)
}
)
}
}
Loading