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

Commit

Permalink
Merge pull request #19 from haritowa/swift/4.0
Browse files Browse the repository at this point in the history
Fix double subscription bug
  • Loading branch information
inamiy authored Mar 13, 2018
2 parents 44f4999 + 0a692ef commit 7bbcb35
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Sources/Automaton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public final class Automaton<State, Input>
.map { input, fromState in
return (input, fromState, mapping(fromState, input)?.1)
}
.share(replay: 1)
.share(replay: 1, scope: .forever)

let successSignal = mappingSignal
.filterMap { input, fromState, effect in
Expand Down Expand Up @@ -110,7 +110,7 @@ public final class Automaton<State, Input>
return .just(.failure(input, fromState))
}
}
.share(replay: 1)
.share(replay: 1, scope: .forever)

replySignal
.flatMap(.merge) { reply -> Observable<State> in
Expand Down
27 changes: 26 additions & 1 deletion Tests/RxAutomatonTests/EffectMappingSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,32 @@ class EffectMappingSpec: QuickSpec
}

}


describe("Edge Invocation") {
var subscriptionsCount = 0

beforeEach {
subscriptionsCount = 0

// To reproduce the bug we need a plain cold observable
let loginOKProducer = Observable.just(AuthInput.loginOK)
.do(onSubscribe: { subscriptionsCount += 1 })

let mappings: [Automaton.EffectMapping] = [
.login | .loggedOut => .loggingIn | loginOKProducer
]

automaton = Automaton(state: .loggedOut, input: signal, mapping: reduce(mappings), strategy: .merge)
}

describe("loggedOut => .loggingIn") {
it("subscribes to testableLoginOKProducer only once") {
observer.onNext(.login)
expect(subscriptionsCount) == 1
}
}
}

describe("Func-based EffectMapping") {

beforeEach {
Expand Down

0 comments on commit 7bbcb35

Please sign in to comment.