Last active
November 22, 2021 15:38
-
-
Save vikingosegundo/79d9f3964086f34d6cffa4ab4c67c6d7 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
typealias Input = (Message) -> () | |
typealias Output = (Message) -> () | |
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 | |
// handling dimmer responses. other usecases omitted | |
private func handle(output: @escaping Output) -> (Dimmer.Response) -> () { { process(response:$0,output:output) } } | |
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)) ) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment