Skip to content

Commit

Permalink
Merge pull request #17 from yonekawa/fix_wait_for_invoke_twice
Browse files Browse the repository at this point in the history
Fix waitFor invokes same callback twice
  • Loading branch information
yonekawa committed Dec 15, 2015
2 parents 1f2a341 + 01a4724 commit 6506add
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
7 changes: 4 additions & 3 deletions SwiftFlux/Dispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,17 @@ public class DefaultDispatcher: Dispatcher {
private func startDispatching<T: Action>(type: T.Type) {
for (identifier, _) in self.callbacks {
guard let callback = self.callbacks[identifier] as? DispatchCallback<T> else { continue }
callback.status = DispatchStatus.Waiting
callback.status = .Waiting
}
}

private func invokeCallback<T: Action>(identifier: String, type: T.Type, result: Result<T.Payload, T.Error>) {
guard let callback = self.callbacks[identifier] as? DispatchCallback<T> else { return }
guard callback.status == .Waiting else { return }

callback.status = DispatchStatus.Pending
callback.status = .Pending
callback.handler(result)
callback.status = DispatchStatus.Handled
callback.status = .Handled
}
}

Expand Down
61 changes: 57 additions & 4 deletions SwiftFluxTests/DispatcherSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ class DispatcherSpec: QuickSpec {
override func spec() {
let dispatcher = DefaultDispatcher()

struct TestModel {
let name: String
}

describe("dispatch") {
var results = [String]()
var fails = [String]()
Expand Down Expand Up @@ -81,5 +77,62 @@ class DispatcherSpec: QuickSpec {
}
}
}

describe("waitFor") {
var results = [String]()
var callbacks = [String]()
var id1 = "";
var id2 = "";
var id3 = "";

beforeEach { () in
results = []
callbacks = []

id1 = dispatcher.register(DispatcherTestAction.self) { (result) in
switch result {
case .Success(let box):
dispatcher.waitFor([id2], type: DispatcherTestAction.self, result: result)
results.append("\(box.name)1")
default:
break
}
}
id2 = dispatcher.register(DispatcherTestAction.self) { (result) in
switch result {
case .Success(let box):
dispatcher.waitFor([id3], type: DispatcherTestAction.self, result: result)
results.append("\(box.name)2")
default:
break
}
}
id3 = dispatcher.register(DispatcherTestAction.self) { (result) in
switch result {
case .Success(let box):
results.append("\(box.name)3")
default:
break
}
}
callbacks.append(id1)
callbacks.append(id2)
callbacks.append(id3)
}

afterEach { () in
for id in callbacks {
dispatcher.unregister(id)
}
}

it("should wait for invoke callback") {
dispatcher.dispatch(DispatcherTestAction(), result: Result(value: DispatcherTestModel(name: "test")))
expect(results.count).to(equal(3))
expect(results[0]).to(equal("test3"))
expect(results[1]).to(equal("test2"))
expect(results[2]).to(equal("test1"))
}
}
}
}

0 comments on commit 6506add

Please sign in to comment.