Skip to content

Commit ed338af

Browse files
committed
update
optional multitons
1 parent c4b5e84 commit ed338af

21 files changed

+173
-173
lines changed

Sources/PureMVC/core/Controller.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ open class Controller: IController {
105105
- parameter factory: reference that returns `IController`
106106
- returns: the Multiton instance
107107
*/
108-
open class func getInstance(_ key:String, factory: (String) -> IController) -> IController {
108+
open class func getInstance(_ key:String, factory: (String) -> IController) -> IController? {
109109
instanceQueue.sync(flags: .barrier) {
110110
if instanceMap[key] == nil {
111111
instanceMap[key] = factory(key)
112112
}
113113
}
114-
return instanceMap[key]!
114+
return instanceMap[key]
115115
}
116116

117117
/**

Sources/PureMVC/core/Model.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ open class Model: IModel {
8888
- parameter factory: reference that returns `IModel`
8989
- returns: the instance returned by the passed closure
9090
*/
91-
open class func getInstance(_ key: String, factory: (String) -> IModel) -> IModel {
91+
open class func getInstance(_ key: String, factory: (String) -> IModel) -> IModel? {
9292
instanceQueue.sync(flags: .barrier) {
9393
if instanceMap[key] == nil {
9494
instanceMap[key] = factory(key)
9595
}
9696
}
97-
return instanceMap[key]!
97+
return instanceMap[key]
9898
}
9999

100100
/**

Sources/PureMVC/core/View.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ open class View: IView {
9393
- parameter factory: reference that returns `IView`
9494
- returns: the Multiton instance returned by executing the passed closure
9595
*/
96-
open class func getInstance(_ key: String, factory: (String) -> IView) -> IView {
96+
open class func getInstance(_ key: String, factory: (String) -> IView) -> IView? {
9797
instanceQueue.sync(flags: .barrier) {
9898
if instanceMap[key] == nil {
9999
instanceMap[key] = factory(key)
100100
}
101101
}
102-
return instanceMap[key]!
102+
return instanceMap[key]
103103
}
104104

105105
/**

Sources/PureMVC/patterns/command/MacroCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ open class MacroCommand: Notifier, ICommand {
6262
public func addSubCommand(closure: () -> ICommand) {
6363
addSubCommand( { FirstCommand() } );
6464
addSubCommand( { SecondCommand() } );
65-
addSubCommand( ) { ThirdCommand() }; //or by using a trailing closure
65+
addSubCommand { ThirdCommand() }; // or by using a trailing closure
6666
}
6767

6868
Note that *SubCommands* may be any closure returning `ICommand`

Sources/PureMVC/patterns/facade/Facade.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ open class Facade: IFacade {
8080
- parameter factory: reference that returns `IFacade`
8181
- returns: the Multiton instance of the `IFacade`
8282
*/
83-
open class func getInstance(_ key: String, factory: (String) -> IFacade) -> IFacade {
83+
open class func getInstance(_ key: String, factory: (String) -> IFacade) -> IFacade? {
8484
instanceMapQueue.sync(flags: .barrier) {
8585
if instanceMap[key] == nil {
8686
instanceMap[key] = factory(key)
8787
}
8888
}
89-
return instanceMap[key]!
89+
return instanceMap[key]
9090
}
9191

9292
/**

Sources/PureMVC/patterns/mediator/Mediator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ open class Mediator: Notifier, IMediator {
3434
- parameter name: the mediator name
3535
- parameter viewComponent: viewComponent instance
3636
*/
37-
public init(name: String?=nil, viewComponent: AnyObject?=nil) {
37+
public init(name: String? = nil, viewComponent: AnyObject? = nil) {
3838
self.name = name ?? Mediator.NAME
3939
self.viewComponent = viewComponent
4040
}

Sources/PureMVC/patterns/observer/Notifier.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ open class Notifier : INotifier {
5151
internal var multitonKey: String?
5252

5353
/// Reference to the Facade Multiton
54-
open lazy var facade:IFacade = {
54+
open lazy var facade: IFacade? = {
5555
assert(multitonKey != nil, Notifier.MULTITON_MSG)
5656

5757
// returns instance mapped to multitonKey if it exists otherwise defaults to Facade
@@ -74,7 +74,7 @@ open class Notifier : INotifier {
7474
- parameter type: the type of the notification (optional)
7575
*/
7676
open func sendNotification(_ notificationName: String, body: Any, type: String) {
77-
facade.sendNotification(notificationName, body: body, type: type)
77+
facade?.sendNotification(notificationName, body: body, type: type)
7878
}
7979

8080
/**
@@ -87,7 +87,7 @@ open class Notifier : INotifier {
8787
- parameter body: the body of the notification (optional)
8888
*/
8989
open func sendNotification(_ notificationName: String, body: Any) {
90-
facade.sendNotification(notificationName, body: body)
90+
facade?.sendNotification(notificationName, body: body)
9191
}
9292

9393
/**
@@ -99,7 +99,7 @@ open class Notifier : INotifier {
9999
- parameter notificationName: the name of the notification to send
100100
*/
101101
open func sendNotification(_ notificationName: String) {
102-
facade.sendNotification(notificationName)
102+
facade?.sendNotification(notificationName)
103103
}
104104

105105
/**

Sources/PureMVC/patterns/proxy/Proxy.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ open class Proxy: Notifier, IProxy {
3636
public var data: Any?
3737

3838
/// Constructor
39-
public init(name: String?=nil, data: Any?=nil) {
39+
public init(name: String? = nil, data: Any? = nil) {
4040
self.name = name ?? Proxy.NAME
4141
self.data = data
4242
}

Tests/PureMVCTests/core/ControllerExtend.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ControllerExtend: Controller {
1616
view = View.getInstance(multitonKey) { key in ViewExtend(key: key) }
1717
}
1818

19-
public class func getInstance(key: String) -> IController {
19+
public class func getInstance(key: String) -> IController? {
2020
return Controller.getInstance(key) { key in ControllerExtend(key: key) }
2121
}
2222

Tests/PureMVCTests/core/ControllerTest.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ControllerTest: XCTestCase {
3333
*/
3434
func testGetInstance() {
3535
// Test Factory Method
36-
let controller: IController = Controller.getInstance("ControllerTestKey1") { key in Controller(key: key) }
36+
let controller: IController? = Controller.getInstance("ControllerTestKey1") { key in Controller(key: key) }
3737

3838
// test assertions
3939
XCTAssertNotNil(controller as? Controller, "Expecting instance not nil")
@@ -54,8 +54,8 @@ class ControllerTest: XCTestCase {
5454
*/
5555
func testRegisterAndExecuteCommand() {
5656
// Create the controller, register the ControllerTestCommand to handle 'ControllerTest' notes
57-
let controller: IController = Controller.getInstance("ControllerTestKey1") { key in Controller(key: key) }
58-
controller.registerCommand("ControllerTest", factory: {ControllerTestCommand()})
57+
let controller: IController? = Controller.getInstance("ControllerTestKey1") { key in Controller(key: key) }
58+
controller?.registerCommand("ControllerTest", factory: {ControllerTestCommand()})
5959

6060
// Create a 'ControllerTest' note
6161
let vo: ControllerTestVO = ControllerTestVO(input: 12)
@@ -64,7 +64,7 @@ class ControllerTest: XCTestCase {
6464
// Tell the controller to execute the Command associated with the note
6565
// the ControllerTestCommand invoked will multiply the vo.input value
6666
// by 2 and set the result on vo.result
67-
controller.executeCommand(note)
67+
controller?.executeCommand(note)
6868

6969
// test assertions
7070
XCTAssertTrue(vo.result == 24, "Expecting vo.result == 24")
@@ -79,8 +79,8 @@ class ControllerTest: XCTestCase {
7979
*/
8080
func testRegisterAndRemoveCommand() {
8181
// Create the controller, register the ControllerTestCommand to handle 'ControllerTest' notes
82-
let controller: IController = Controller.getInstance("ControllerTestKey3") { key in Controller(key: key) }
83-
controller.registerCommand("ControllerRemoveTest", factory: {ControllerTestCommand()})
82+
let controller: IController? = Controller.getInstance("ControllerTestKey3") { key in Controller(key: key) }
83+
controller?.registerCommand("ControllerRemoveTest", factory: {ControllerTestCommand()})
8484

8585
// Create a 'ControllerTest' note
8686
let vo = ControllerTestVO(input: 12)
@@ -89,7 +89,7 @@ class ControllerTest: XCTestCase {
8989
// Tell the controller to execute the Command associated with the note
9090
// the ControllerTestCommand invoked will multiply the vo.input value
9191
// by 2 and set the result on vo.result
92-
controller.executeCommand(note)
92+
controller?.executeCommand(note)
9393

9494
// test assertions
9595
XCTAssertTrue(vo.result == 24, "Expecting vo.result == 24")
@@ -98,12 +98,12 @@ class ControllerTest: XCTestCase {
9898
vo.result = 0
9999

100100
// Remove the Command from the Controller
101-
controller.removeCommand("ControllerRemoveTest")
101+
controller?.removeCommand("ControllerRemoveTest")
102102

103103
// Tell the controller to execute the Command associated with the
104104
// note. This time, it should not be registered, and our vo result
105105
// will not change
106-
controller.executeCommand(note)
106+
controller?.executeCommand(note)
107107

108108
// test assertions
109109
XCTAssertTrue(vo.result == 0, "Expecting vo.result == 0")
@@ -115,16 +115,16 @@ class ControllerTest: XCTestCase {
115115
func testHasCommand() {
116116
// register the ControllerTestCommand to handle 'hasCommandTest' notes
117117
let controller = Controller.getInstance("ControllerTestKey4") { key in Controller(key: key) }
118-
controller.registerCommand("hasCommandTest", factory: {ControllerTestCommand()})
118+
controller?.registerCommand("hasCommandTest", factory: {ControllerTestCommand()})
119119

120120
// test that hasCommand returns true for hasCommandTest notifications
121-
XCTAssertTrue(controller.hasCommand("hasCommandTest"), "Expecting controller.hasCommand('hasCommandTest') == true")
121+
XCTAssertTrue(controller?.hasCommand("hasCommandTest") == true, "Expecting controller.hasCommand('hasCommandTest') == true")
122122

123123
// Remove the Command from the Controller
124-
controller.removeCommand("hasCommandTest")
124+
controller?.removeCommand("hasCommandTest")
125125

126126
// test that hasCommand returns false for hasCommandTest notifications
127-
XCTAssertTrue(controller.hasCommand("hasCommandTest") == false, "Expecting controller.hasCommand('hasCommandTest') == false")
127+
XCTAssertTrue(controller?.hasCommand("hasCommandTest") == false, "Expecting controller.hasCommand('hasCommandTest') == false")
128128
}
129129

130130
/**
@@ -141,13 +141,13 @@ class ControllerTest: XCTestCase {
141141
func testReregisterAndExecuteCommand() {
142142
// Fetch the controller, register the ControllerTestCommand2 to handle 'ControllerTest2' notes
143143
let controller = Controller.getInstance("ControllerTestKey5") { key in Controller(key: key) }
144-
controller.registerCommand("ControllerTest2", factory: {ControllerTestCommand2()})
144+
controller?.registerCommand("ControllerTest2", factory: {ControllerTestCommand2()})
145145

146146
// Remove the Command from the Controller
147-
controller.removeCommand("ControllerTest2")
147+
controller?.removeCommand("ControllerTest2")
148148

149149
// Re-register the Command with the Controller
150-
controller.registerCommand("ControllerTest2", factory: {ControllerTestCommand2()})
150+
controller?.registerCommand("ControllerTest2", factory: {ControllerTestCommand2()})
151151

152152
// Create a 'ControllerTest2' note
153153
let vo = ControllerTestVO(input: 12)
@@ -157,14 +157,14 @@ class ControllerTest: XCTestCase {
157157
let view = View.getInstance("ControllerTestKey5") { key in View(key: key) }
158158

159159
// send the Notification
160-
view.notifyObservers(note)
160+
view?.notifyObservers(note)
161161

162162
// test assertions
163163
// if the command is executed once the value will be 24
164164
XCTAssertTrue(vo.result == 24, "Expecting vo.result == 24")
165165

166166
// Prove that accumulation works in the VO by sending the notification again
167-
view.notifyObservers(note)
167+
view?.notifyObservers(note)
168168

169169
// if the command is executed twice the value will be 48
170170
XCTAssertTrue(vo.result == 48, "Expecting vo.result == 48")

Tests/PureMVCTests/core/ModelExtend.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ModelExtend: Model {
1212

1313
public var resource: Resource?
1414

15-
public class func getInstance(key: String) -> IModel {
15+
public class func getInstance(key: String) -> IModel? {
1616
return Model.getInstance(key) { key in ModelExtend(key: key) }
1717
}
1818

Tests/PureMVCTests/core/ModelTest.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ModelTest: XCTestCase {
2626

2727
func testGetInstance() {
2828
// Test Factory Method
29-
let model: IModel = Model.getInstance("ModelTestKey4") { key in Model(key: key) }
29+
let model: IModel? = Model.getInstance("ModelTestKey4") { key in Model(key: key) }
3030

3131
// test assertions
3232
XCTAssertNotNil(model as? Model, "Expecting instance not null")
@@ -42,9 +42,9 @@ class ModelTest: XCTestCase {
4242
*/
4343
func testRegisterAndRetrieveProxy() {
4444
// register a proxy and retrieve it.
45-
let model: IModel = Model.getInstance("ModelTestKey2") { key in Model(key: key) }
46-
model.registerProxy(Proxy(name: "colors", data: ["red", "green", "blue"]))
47-
let proxy: Proxy = model.retrieveProxy("colors") as! Proxy
45+
let model: IModel? = Model.getInstance("ModelTestKey2") { key in Model(key: key) }
46+
model?.registerProxy(Proxy(name: "colors", data: ["red", "green", "blue"]))
47+
let proxy: Proxy = model?.retrieveProxy("colors") as! Proxy
4848
let data: Array<String> = proxy.data as! Array<String>
4949

5050
// test assertions
@@ -57,10 +57,10 @@ class ModelTest: XCTestCase {
5757

5858
//Test with tuple
5959
func testRegisterAndRetrieveProxy2() {
60-
let model: IModel = Model.getInstance("ModelTestKey2") { key in Model(key: key) }
61-
model.registerProxy(Proxy(name: "tuple", data: (1, "abc", false)))
60+
let model: IModel? = Model.getInstance("ModelTestKey2") { key in Model(key: key) }
61+
model?.registerProxy(Proxy(name: "tuple", data: (1, "abc", false)))
6262

63-
let proxy: Proxy = model.retrieveProxy("tuple") as! Proxy
63+
let proxy: Proxy = model?.retrieveProxy("tuple") as! Proxy
6464
let data: (Int, String, Bool) = proxy.data as! (Int, String, Bool)
6565

6666
XCTAssertTrue(data.0 == 1, "Expecting data.0 == 1")
@@ -73,18 +73,18 @@ class ModelTest: XCTestCase {
7373
*/
7474
func testRegisterAndRemoveProxy() {
7575
// register a proxy, remove it, then try to retrieve it
76-
let model: IModel = Model.getInstance("ModelTestKey3") { key in Model(key: key) }
76+
let model: IModel? = Model.getInstance("ModelTestKey3") { key in Model(key: key) }
7777
let proxy: IProxy = Proxy(name: "sizes", data: ["7", "13", "21"])
78-
model.registerProxy(proxy)
78+
model?.registerProxy(proxy)
7979

8080
// remove the proxy
81-
let removedProxy = model.removeProxy("sizes")
81+
let removedProxy = model?.removeProxy("sizes")
8282

8383
// assert that we removed the appropriate proxy
8484
XCTAssertTrue(removedProxy!.name == "sizes", "Expecting removedProxy.getProxyName() == 'sizes'")
8585

8686
// ensure that the proxy is no longer retrievable from the model
87-
let nilProxy = model.retrieveProxy("sizes")
87+
let nilProxy = model?.retrieveProxy("sizes")
8888

8989
// test assertions
9090
XCTAssertTrue(nilProxy == nil, "Expecting proxy is nil")
@@ -95,38 +95,38 @@ class ModelTest: XCTestCase {
9595
*/
9696
func testHasProxy() {
9797
// register a proxy
98-
let model: IModel = Model.getInstance("ModelTestKey4") { key in Model(key: key) }
98+
let model: IModel? = Model.getInstance("ModelTestKey4") { key in Model(key: key) }
9999
let proxy: IProxy = Proxy(name: "aces", data: ["clubs", "spades", "hearts", "diamonds"])
100-
model.registerProxy(proxy)
100+
model?.registerProxy(proxy)
101101

102102
// assert that the model.hasProxy method returns true
103103
// for that proxy name
104-
XCTAssertTrue(model.hasProxy("aces"), "Expecting model.hasProxy('aces') == true")
104+
XCTAssertTrue(model?.hasProxy("aces") == true, "Expecting model.hasProxy('aces') == true")
105105

106106
// remove the proxy
107-
_ = model.removeProxy("aces")
107+
_ = model?.removeProxy("aces")
108108

109109
// assert that the model.hasProxy method returns false
110110
// for that proxy name
111-
XCTAssertTrue(model.hasProxy("aces") == false, "Expecting model.hasProxy('aces') == false")
111+
XCTAssertTrue(model?.hasProxy("aces") == false, "Expecting model.hasProxy('aces') == false")
112112
}
113113

114114
/**
115115
Tests that the Model calls the onRegister and onRemove methods
116116
*/
117117
func testOnRegisterAndOnRemove() {
118118
// Get a Multiton View instance
119-
let model: IModel = Model.getInstance("ModelTestKey4") { key in Model(key: key) }
119+
let model: IModel? = Model.getInstance("ModelTestKey4") { key in Model(key: key) }
120120

121121
// Create and register the test mediator
122122
let proxy: IProxy = ModelTestProxy()
123-
model.registerProxy(proxy)
123+
model?.registerProxy(proxy)
124124

125125
// assert that onRegsiter was called, and the proxy responded by setting its data accordingly
126126
XCTAssertTrue((proxy.data as! String) == ModelTestProxy.ON_REGISTER_CALLED, "Expecting proxy.data == ModelTestProxy.ON_REGISTER_CALLED")
127127

128128
// Remove the component
129-
_ = model.removeProxy(ModelTestProxy.NAME)
129+
_ = model?.removeProxy(ModelTestProxy.NAME)
130130

131131
// assert that onRemove was called, and the proxy responded by setting its data accordingly
132132
XCTAssertTrue((proxy.data as! String) == ModelTestProxy.ON_REMOVE_CALLED, "Expecting proxy.data == ModelTestProxy.ON_REMOVE_CALLED \(String(describing: proxy.data))")

Tests/PureMVCTests/core/ViewExtend.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ViewExtend: View {
1212

1313
public var resource: Resource?
1414

15-
public class func getInstance(key: String) -> IView {
15+
public class func getInstance(key: String) -> IView? {
1616
return View.getInstance(key) { key in ViewExtend(key: key) }
1717
}
1818

0 commit comments

Comments
 (0)