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

Fix double subscription bug #19

Merged
merged 2 commits into from
Mar 13, 2018
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
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