Last active
April 4, 2017 15:13
-
-
Save john-crossley/5f78c0a155cc7a80204e4d418a40ff6a to your computer and use it in GitHub Desktop.
This file contains 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
package jonnothebonno.me.veggiebeers.events | |
import jonnothebonno.me.veggiebeers.util.guard | |
typealias EventList = MutableList<EventType> | |
typealias EventObservers = MutableMap<EventDispatchObserver, EventList> | |
class EventDispatcher private constructor() : EventDispatcherContract { | |
val observers: EventObservers = mutableMapOf() | |
companion object { | |
val instance by lazy { EventDispatcher() } | |
} | |
override fun dispatchEvent(event: EventType) { | |
observers.forEach { key, value -> | |
if (value.contains(event)) { | |
key.didReceiveEvent(event) | |
} | |
} | |
} | |
override fun addObserver(observer: EventDispatchObserver, event: EventType) { | |
observers[observer].guard { observers[observer] = mutableListOf() } | |
observers[observer]?.add(event) | |
} | |
override fun removeObserver(observer: EventDispatchObserver) { | |
observers.remove(observer) | |
} | |
} |
This file contains 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
package jonnothebonno.me.veggiebeers.events | |
interface EventDispatcherContract { | |
fun dispatchEvent(event: EventType) | |
fun addObserver(observer: EventDispatchObserver, event: EventType) | |
fun removeObserver(observer: EventDispatchObserver) | |
} |
This file contains 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
package jonnothebonno.me.veggiebeers.events | |
interface EventDispatchObserver { | |
fun didReceiveEvent(event: EventType) | |
} |
This file contains 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
package jonnothebonno.me.veggiebeers.events | |
enum class EventType { | |
IAP_NOT_AVAILABLE | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment