@@ -38,38 +38,38 @@ registrations.
38
38
`@see org.puremvc.swift.multicore.patterns.command.MacroCommand MacroCommand`
39
39
*/
40
40
open class Controller : IController {
41
-
41
+
42
42
/// Local reference to View
43
43
internal var view : IView !
44
-
44
+
45
45
// Mapping of Notification names to factories that instanties and returns `ICommand` Class instances
46
46
internal var commandMap = [ String : ( ) -> ICommand ] ( )
47
-
47
+
48
48
// Concurrent queue for commandMap
49
49
// for speed and convenience of running concurrently while reading, and thread safety of blocking while mutating
50
50
internal let commandMapQueue = DispatchQueue ( label: " org.puremvc.controller.commandMapQueue " , attributes: DispatchQueue . Attributes. concurrent)
51
-
51
+
52
52
/// The Multiton Key for this app
53
53
internal private( set) var multitonKey : String
54
-
54
+
55
55
// The Multiton Controller instanceMap.
56
56
private static var instanceMap = [ String: IController] ( )
57
-
57
+
58
58
// instance Queue for thread safety
59
59
private static let instanceQueue = DispatchQueue ( label: " org.puremvc.controller.instanceQueue " , attributes: DispatchQueue . Attributes. concurrent)
60
-
60
+
61
61
/// Message constant
62
62
internal static let MULTITON_MSG = " Controller instance for this Multiton key already constructed! "
63
-
63
+
64
64
/**
65
65
Constructor.
66
-
66
+
67
67
This `IController` implementation is a Multiton,
68
68
so you should not call the constructor
69
69
directly, but instead call the static Factory method,
70
70
passing the unique key for this instance
71
71
`Controller.getInstance( multitonKey )`
72
-
72
+
73
73
@throws Error if instance for this Multiton key has already been constructed
74
74
*/
75
75
public init ( key: String ) {
@@ -78,17 +78,17 @@ open class Controller: IController {
78
78
Controller . instanceMap [ key] = self
79
79
initializeController ( )
80
80
}
81
-
81
+
82
82
/**
83
83
Initialize the Multiton `Controller` instance.
84
-
84
+
85
85
Called automatically by the constructor.
86
-
86
+
87
87
Note that if you are using a subclass of `View`
88
88
in your application, you should *also* subclass `Controller`
89
89
and override the `initializeController` method in the
90
90
following way:
91
-
91
+
92
92
// ensure that the Controller is talking to my IView implementation
93
93
override public func initializeController( ) {
94
94
view = MyView.getInstance(multitonKey) { MyView(key: self.multitonKey) }
@@ -97,10 +97,10 @@ open class Controller: IController {
97
97
open func initializeController( ) {
98
98
view = View . getInstance ( multitonKey) { key in View ( key: key) }
99
99
}
100
-
100
+
101
101
/**
102
102
`Controller` Multiton Factory method.
103
-
103
+
104
104
- parameter key: multitonKey
105
105
- parameter factory: reference that returns `IController`
106
106
- returns: the Multiton instance
@@ -113,18 +113,18 @@ open class Controller: IController {
113
113
}
114
114
return instanceMap [ key] !
115
115
}
116
-
116
+
117
117
/**
118
118
Register a particular `ICommand` class as the handler
119
119
for a particular `INotification`.
120
-
120
+
121
121
If an `ICommand` has already been registered to
122
122
handle `INotification`s with this name, it is no longer
123
123
used, the new `ICommand` is used instead.
124
-
124
+
125
125
The Observer for the new ICommand is only created if this the
126
126
first time an ICommand has been regisered for this Notification name.
127
-
127
+
128
128
- parameter notificationName: the name of the `INotification`
129
129
- parameter factory: reference that returns `ICommand`
130
130
*/
@@ -136,60 +136,59 @@ open class Controller: IController {
136
136
commandMap [ notificationName] = factory
137
137
}
138
138
}
139
-
139
+
140
140
/**
141
141
If an `ICommand` has previously been registered
142
142
to handle a the given `INotification`, then it is executed.
143
-
144
- - parameter note : an `INotification`
143
+
144
+ - parameter notification : an `INotification`
145
145
*/
146
146
open func executeCommand( _ notification: INotification ) {
147
- commandMapQueue. sync {
148
- if let factory = commandMap [ notification. name] {
149
- let commandInstance = factory ( )
150
- commandInstance. initializeNotifier ( multitonKey)
151
- commandInstance. execute ( notification)
152
- }
147
+ let factory = commandMapQueue. sync {
148
+ commandMap [ notification. name]
153
149
}
150
+
151
+ guard let command = factory ? ( ) else { return }
152
+
153
+ command. initializeNotifier ( multitonKey)
154
+ command. execute ( notification)
154
155
}
155
-
156
+
156
157
/**
157
158
Check if a Command is registered for a given Notification
158
-
159
+
159
160
- parameter notificationName:
160
161
- returns: whether a Command is currently registered for the given `notificationName`.
161
162
*/
162
163
open func hasCommand( _ notificationName: String ) -> Bool {
163
- var result = false
164
164
commandMapQueue. sync {
165
- result = commandMap [ notificationName] != nil
165
+ commandMap [ notificationName] != nil
166
166
}
167
- return result
168
167
}
169
-
168
+
170
169
/**
171
170
Remove a previously registered `ICommand` to `INotification` mapping.
172
-
171
+
173
172
- parameter notificationName: the name of the `INotification` to remove the `ICommand` mapping for
174
173
*/
175
174
open func removeCommand( _ notificationName: String ) {
176
- if hasCommand ( notificationName) {
177
- commandMapQueue. sync ( flags: . barrier) {
178
- view. removeObserver ( notificationName, notifyContext: self )
179
- commandMap. removeValue ( forKey: notificationName)
180
- }
175
+ commandMapQueue. sync ( flags: . barrier) {
176
+ guard commandMap [ notificationName] != nil else { return }
177
+ view. removeObserver ( notificationName, notifyContext: self )
178
+ commandMap. removeValue ( forKey: notificationName)
181
179
}
182
- }
183
180
181
+ }
182
+
184
183
/**
185
184
Remove an IController instance
186
-
185
+
187
186
- parameter multitonKey: of IController instance to remove
188
187
*/
189
188
open class func removeController( _ key: String ) {
190
189
instanceQueue. sync ( flags: . barrier) {
191
190
_ = instanceMap. removeValue ( forKey: key)
192
191
}
193
192
}
194
-
193
+
195
194
}
0 commit comments