Created
October 19, 2021 08:09
-
-
Save vikingosegundo/2554d4db3e7a96b2abceaa3779aff97b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func createLightingFeature(store : Store, | |
lightsStack: LightsStack, | |
output : @escaping Output) -> Input | |
{ | |
let loader = LightsLoader (lightsStack:lightsStack,store:store,responder:handle(output:output)) | |
let switcher = LightSwitcher (lightsStack:lightsStack,store:store,responder:handle(output:output)) | |
let valueSetter = LightValueSetter(lightsStack:lightsStack,store:store,responder:handle(output:output)) | |
let dimmer = Dimmer (lightsStack:lightsStack,store:store,responder:handle(output:output)) | |
let changer = LightChanger (lightsStack:lightsStack,store:store,responder:handle(output:output)) | |
func execute(command cmd: Message._Lighting) { | |
// |---------- Global Command ---------| request |- UseCase -| to |<-------- UseCase Request -------->| | |
if case let .load(lightsOrRooms) = cmd { request( loader, to:.load(.all(lightsOrRooms)) ) } | |
if case let .turn(light, onOrOff) = cmd { request( switcher, to:.turn(light, onOrOff ) ) } | |
if case let .apply(config, on:light) = cmd { request( valueSetter, to:.apply(config, on:light) ) } | |
if case let .increase(value,by:increase,on:light) = cmd { request( dimmer, to:.increase(value,by:increase,on:light) ) } | |
if case let .decrease(value,by:decrease,on:light) = cmd { request( dimmer, to:.decrease(value,by:decrease,on:light) ) } | |
if case let .change(.name(name), on:light) = cmd { request( changer, to:.change(.name(name), on:light) ) } | |
if case let .change(.display(interface),on:light) = cmd { request( changer, to:.change(.display(interface),on:light) ) } | |
} | |
return { // entry point | |
if case .lighting(let c) = $0 { execute(command:c) } // execute if lighting is command's recipient | |
} | |
} | |
// MARK: - UseCase response handler | |
private func handle(output: @escaping Output) -> ( LightSwitcher.Response) -> () { { process(response:$0,output:output) } } | |
private func handle(output: @escaping Output) -> (LightValueSetter.Response) -> () { { process(response:$0,output:output) } } | |
private func handle(output: @escaping Output) -> ( Dimmer.Response) -> () { { process(response:$0,output:output) } } | |
private func handle(output: @escaping Output) -> ( LightChanger.Response) -> () { { process(response:$0,output:output) } } | |
private func handle(output: @escaping Output) -> ( LightsLoader.Response) -> () { { process(response:$0,output:output) } } | |
private func process(response: LightSwitcher.Response, output: @escaping Output) { switch response { case let .light (light,.was(turned:onOrOff) ): output( .lighting(.turning (light, onOrOff,.succeeded )) ) }} | |
private func process(response:LightValueSetter.Response, output: @escaping Output) { switch response { case let .applying(values,on:light,succeededOrFaild): output( .lighting(.applying(values,on:light,succeededOrFaild)) ) }} | |
private func process(response: LightsLoader.Response, output: @escaping Output) { switch response { case let .loading (lightsOrRooms, succeededOrFaild): output( .lighting(.loading (lightsOrRooms, succeededOrFaild)) ) }} | |
private func process(response: Dimmer.Response, output: @escaping Output) { | |
switch response { | |
case let .increase(value,by:increment,on:light,succeededOrFaild): output( .lighting(.increasing(value,by:increment,on:light,succeededOrFaild)) ) | |
case let .decrease(value,by:increment,on:light,succeededOrFaild): output( .lighting(.decreasing(value,by:increment,on:light,succeededOrFaild)) ) | |
} | |
} | |
private func process(response: LightChanger.Response, output: @escaping Output) { | |
switch response { | |
case let .changing(.title(name), on:light,succeededOrFaild): output( .lighting(.changing(.name(name), on:light,succeededOrFaild)) ) | |
case let .changing(.display(interface),on:light, _ ): output( .lighting(.changing(.display(interface),on:light,.succeeded )) ) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment