Skip to content

Commit 3e21ffa

Browse files
committed
Merge pull request #1 from yonekawa/redesign_api
Use Action.Type to Dispatcher.register
2 parents 4f46564 + 218c8cb commit 3e21ffa

File tree

10 files changed

+54
-57
lines changed

10 files changed

+54
-57
lines changed

DemoApp/TodoAction.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TodoAction {
1515
typealias Payload = [Todo]
1616
func invoke() {
1717
let todos = [Todo(title: "List ToDo 1"), Todo(title: "List ToDo 2"), Todo(title: "List ToDo 3")]
18-
Dispatcher.dispatch(self, result: Result(value: todos))
18+
Dispatcher.dispatch(self.dynamicType, result: Result(value: todos))
1919
}
2020
}
2121

@@ -30,7 +30,7 @@ class TodoAction {
3030
}
3131

3232
func invoke() {
33-
Dispatcher.dispatch(self, result: Result(value: Todo(title: self.title)))
33+
Dispatcher.dispatch(self.dynamicType, result: Result(value: Todo(title: self.title)))
3434
}
3535
}
3636
}

DemoApp/TodoStore.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TodoStore : Store {
2828
}
2929

3030
init() {
31-
Dispatcher.register(TodoAction.List()) { (result) -> Void in
31+
Dispatcher.register(TodoAction.List.self) { (result) -> Void in
3232
switch result {
3333
case .Success(let box):
3434
self.todos = box.value
@@ -38,7 +38,7 @@ class TodoStore : Store {
3838
}
3939
}
4040

41-
Dispatcher.register(TodoAction.Create()) { (result) -> Void in
41+
Dispatcher.register(TodoAction.Create.self) { (result) -> Void in
4242
switch result {
4343
case .Success(let box):
4444
self.todos.insert(box.value, atIndex: 0)

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Circle CI](https://img.shields.io/circleci/project/yonekawa/SwiftFlux/master.svg?style=flat)](https://circleci.com/gh/yonekawa/SwiftFlux)
44
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
55

6-
SwiftFlux is an implementation of [Facebook's Flux architecure](https://facebook.github.io/flux/) for Swift.
6+
SwiftFlux is an implementation of [Facebook's Flux architecure](https://facebook.github.io/flux/) for Swift.
77
It provides concept of "one-way data flow" with **type-safe** modules by Swift language.
88

99
- Type of an action payload is inferred from the type of its action.
@@ -67,7 +67,7 @@ class TodoStore : Store {
6767
}
6868

6969
init() {
70-
Dispatcher.register(TodoAction.List()) { (result) -> Void in
70+
Dispatcher.register(TodoAction.List.self) { (result) -> Void in
7171
switch result {
7272
case .Success(let box):
7373
self.todo = box.value

SwiftFlux/Action.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ import Result
1111

1212
public protocol Action {
1313
typealias Payload
14-
func invoke() -> Void
1514
}

SwiftFlux/Dispatcher.swift

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,91 +13,89 @@ public class Dispatcher {
1313
private static let instance = Dispatcher()
1414
private var callbacks: Dictionary<String, AnyObject> = [:]
1515
private var _isDispatching = false
16-
private var lastId = 0
16+
private var lastDispatchIdentifier = 0
1717
private init() {}
1818
}
1919

20-
// Public interfaces
2120
extension Dispatcher {
2221
public var isDispathing: Bool {
2322
get {
2423
return self._isDispatching
2524
}
2625
}
2726

28-
public class func dispatch<T: Action>(action: T, result: Result<T.Payload, NSError>) {
27+
public class func dispatch<T: Action>(action: T.Type, result: Result<T.Payload, NSError>) {
2928
instance.dispatch(action, result: result)
3029
}
3130

32-
public class func register<T: Action>(action: T, handler: (Result<T.Payload, NSError>) -> Void) -> String {
31+
public class func register<T: Action>(action: T.Type, handler: (Result<T.Payload, NSError>) -> Void) -> String {
3332
return instance.register(action, handler: handler)
3433
}
3534

36-
public class func unregister(id: String) {
37-
instance.unregister(id)
35+
public class func unregister(identifier: String) {
36+
instance.unregister(identifier)
3837
}
3938

40-
public class func waitFor<T: Action>(ids: Array<String>, action: T, result: Result<T.Payload, NSError>) {
41-
instance.waitFor(ids, action: action, result: result)
39+
public class func waitFor<T: Action>(identifiers: Array<String>, action: T.Type, result: Result<T.Payload, NSError>) {
40+
instance.waitFor(identifiers, action: action, result: result)
4241
}
4342
}
4443

45-
// Private implementation
4644
extension Dispatcher {
47-
private func dispatch<T: Action>(action: T, result: Result<T.Payload, NSError>) {
45+
private func dispatch<T: Action>(action: T.Type, result: Result<T.Payload, NSError>) {
4846
objc_sync_enter(self)
4947

5048
self.startDispatching(action)
51-
for id in self.callbacks.keys {
52-
self.invokeCallback(id, action: action, result: result)
49+
for identifier in self.callbacks.keys {
50+
self.invokeCallback(identifier, action: action, result: result)
5351
}
5452
self.finishDispatching(action)
5553

5654
objc_sync_exit(self)
5755
}
5856

59-
private func register<T: Action>(action: T, handler: (Result<T.Payload, NSError>) -> Void) -> String {
60-
let nextId = "DISPATCH_CALLBACK_\(++self.lastId)"
61-
self.callbacks[nextId] = DispatchCallback<T>(action: action, handler: handler)
62-
return nextId
57+
private func register<T: Action>(action: T.Type, handler: (Result<T.Payload, NSError>) -> Void) -> String {
58+
let nextDispatchIdentifier = "DISPATCH_CALLBACK_\(++self.lastDispatchIdentifier)"
59+
self.callbacks[nextDispatchIdentifier] = DispatchCallback<T>(action: action, handler: handler)
60+
return nextDispatchIdentifier
6361
}
6462

65-
private func unregister(id: String) {
66-
self.callbacks.removeValueForKey(id)
63+
private func unregister(identifier: String) {
64+
self.callbacks.removeValueForKey(identifier)
6765
}
6866

69-
private func waitFor<T: Action>(ids: Array<String>, action: T, result: Result<T.Payload, NSError>) {
70-
for id in ids {
71-
if let callback = self.callbacks[id] as? DispatchCallback<T> {
67+
private func waitFor<T: Action>(identifiers: Array<String>, action: T.Type, result: Result<T.Payload, NSError>) {
68+
for identifier in identifiers {
69+
if let callback = self.callbacks[identifier] as? DispatchCallback<T> {
7270
switch callback.status {
7371
case .Handled:
7472
continue
7573
case .Pending:
7674
// Circular dependency detected while
7775
continue
7876
default:
79-
self.invokeCallback(id, action: action, result: result)
77+
self.invokeCallback(identifier, action: action, result: result)
8078
}
8179
}
8280
}
8381
}
8482

85-
private func startDispatching<T: Action>(action: T) {
83+
private func startDispatching<T: Action>(action: T.Type) {
8684
self._isDispatching = true
8785

88-
for (id, calllback) in self.callbacks {
89-
if let callback = self.callbacks[id] as? DispatchCallback<T> {
86+
for (identifier, calllback) in self.callbacks {
87+
if let callback = self.callbacks[identifier] as? DispatchCallback<T> {
9088
callback.status = DispatchStatus.Waiting
9189
}
9290
}
9391
}
9492

95-
private func finishDispatching<T: Action>(action: T) {
93+
private func finishDispatching<T: Action>(action: T.Type) {
9694
self._isDispatching = false
9795
}
9896

99-
private func invokeCallback<T: Action>(id: String, action: T, result: Result<T.Payload, NSError>) {
100-
if let callback = self.callbacks[id] as? DispatchCallback<T> {
97+
private func invokeCallback<T: Action>(identifier: String, action: T.Type, result: Result<T.Payload, NSError>) {
98+
if let callback = self.callbacks[identifier] as? DispatchCallback<T> {
10199
callback.status = DispatchStatus.Pending
102100
callback.handler(result)
103101
callback.status = DispatchStatus.Handled
@@ -106,12 +104,12 @@ extension Dispatcher {
106104
}
107105

108106
internal class DispatchCallback<T: Action> {
109-
let action: T
107+
let action: T.Type
110108
let handler: (Result<T.Payload, NSError>) -> Void
111109

112110
var status: DispatchStatus = DispatchStatus.Waiting
113111

114-
init(action: T, handler: (Result<T.Payload, NSError>) -> Void) {
112+
init(action: T.Type, handler: (Result<T.Payload, NSError>) -> Void) {
115113
self.action = action
116114
self.handler = handler
117115
}

SwiftFlux/EventEmitter.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@ import Foundation
1111
public class EventEmitter {
1212
private static let instance = EventEmitter()
1313
private var listenes: Dictionary<String, AnyObject> = [:]
14-
private var lastId = 0
14+
private var lastListenerIdentifier = 0
1515
private init() {}
1616
}
1717

1818
extension EventEmitter {
1919
public class func listen<T: Store>(store: T, event: T.Event, handler: () -> Void) -> String {
2020
return self.instance.listen(store, event: event, handler: handler)
2121
}
22-
22+
2323
public class func emit<T: Store>(store: T, event: T.Event) {
2424
self.instance.emit(store, event: event)
2525
}
26-
27-
public class func unlisten(id: String) {
28-
self.instance.unlisten(id)
26+
27+
public class func unlisten(identifier: String) {
28+
self.instance.unlisten(identifier)
2929
}
3030
}
3131

3232
extension EventEmitter {
3333
private func listen<T: Store>(store: T, event: T.Event, handler: () -> Void) -> String {
34-
let nextId = "EVENT_LISTENER_\(++lastId)"
35-
self.listenes[nextId] = EventListener<T>(store: store, event: event, handler: handler)
36-
return nextId
34+
let nextListenerIdentifier = "EVENT_LISTENER_\(++lastListenerIdentifier)"
35+
self.listenes[nextListenerIdentifier] = EventListener<T>(store: store, event: event, handler: handler)
36+
return nextListenerIdentifier
3737
}
38-
38+
3939
private func emit<T: Store>(store: T, event: T.Event) {
4040
for (key, value) in self.listenes {
4141
if let listener = value as? EventListener<T> {
@@ -45,9 +45,9 @@ extension EventEmitter {
4545
}
4646
}
4747
}
48-
49-
private func unlisten(id: String) {
50-
self.listenes.removeValueForKey(id)
48+
49+
private func unlisten(identifier: String) {
50+
self.listenes.removeValueForKey(identifier)
5151
}
5252
}
5353

SwiftFlux/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>0.0.1</string>
18+
<string>0.0.2</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

SwiftFlux/Store.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
import Foundation
1010

1111
public protocol Store {
12-
typealias Event: Hashable
12+
typealias Event: Equatable
1313
}

SwiftFluxTests/DispatcherSpec.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ class DispatcherSpec: QuickSpec {
3333
fails = []
3434
callbacks = []
3535

36-
let id1 = Dispatcher.register(TestAction()) { (result) in
36+
let id1 = Dispatcher.register(TestAction.self) { (result) in
3737
switch result {
3838
case .Success(let box):
3939
results.append("\(box.value.name)1")
4040
case .Failure(let box):
4141
fails.append("fail")
4242
}
4343
}
44-
let id2 = Dispatcher.register(TestAction()) { (result) in
44+
let id2 = Dispatcher.register(TestAction.self) { (result) in
4545
switch result {
4646
case .Success(let box):
4747
results.append("\(box.value.name)2")
@@ -61,7 +61,7 @@ class DispatcherSpec: QuickSpec {
6161

6262
context("when action succeeded") {
6363
it("should dispatch to registered callback handlers") {
64-
Dispatcher.dispatch(TestAction(), result: Result(value: TestModel(name: "test")))
64+
Dispatcher.dispatch(TestAction.self, result: Result(value: TestModel(name: "test")))
6565
expect(results.count).to(equal(2))
6666
expect(fails.isEmpty).to(beTruthy())
6767
expect(results).to(contain("test1", "test2"))
@@ -70,7 +70,7 @@ class DispatcherSpec: QuickSpec {
7070

7171
context("when action failed") {
7272
it("should dispatch to registered callback handlers") {
73-
Dispatcher.dispatch(TestAction(), result: Result(error: NSError()))
73+
Dispatcher.dispatch(TestAction.self, result: Result(error: NSError()))
7474
expect(fails.count).to(equal(2))
7575
expect(results.isEmpty).to(beTruthy())
7676
}

SwiftFluxTests/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
<key>CFBundlePackageType</key>
1616
<string>BNDL</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.0</string>
18+
<string>0.0.2</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>
22-
<string>1</string>
22+
<string>$(CURRENT_PROJECT_VERSION)</string>
2323
</dict>
2424
</plist>

0 commit comments

Comments
 (0)