Last active
August 29, 2015 14:24
-
-
Save hawkw/1f14f9eae3ed620a4773 to your computer and use it in GitHub Desktop.
Event
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
/////////////////////////////////////////////////////////////////////// | |
// TOP SECRET METEORCODE ENGINEERING BULLSHIT -- DO NOT STEAL // | |
// (c) Hawk Weisman, all rights reserved 'n' stuff // | |
/////////////////////////////////////////////////////////////////////// | |
type Payload: Map[String,Object] | |
abstract class Event ( | |
protected val payload: Payload, | |
protected var valid: Boolean = true | |
) extends (Payload => Unit) { | |
// validity stuff | |
def isValid: Boolean = valid | |
def invalidate: Unit { valid = false } | |
def prefunc(payload: Payload): Boolean = return payload.seen(this) | |
// this is where you write the onEvent behaviour, basically - the | |
// closure body becomes the apply() method | |
def apply(payload: Map[String, Object]): Unit | |
// onEvent applies the function to the payload (there might be a better way to do this) | |
def onEvent(): Unit = if (prefunc(payload)) this(payload) | |
// honestly we could just make it mix in abstract map and get all the nice map ops | |
def patchPayload(key: String, value: Object) = payload put (key, value) | |
} | |
class Fireball(payload: Payload) | |
extends Event(payload) { (payload) => | |
// do fireball stuff here | |
println(payload.toString) | |
} | |
object EventDemo extends App { | |
val aFireball = new Fireball(Map("some key" -> "some value")) | |
aFireball.onEvent() | |
} |
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
/////////////////////////////////////////////////////////////////////// | |
// TOP SECRET METEORCODE ENGINEERING BULLSHIT -- DO NOT STEAL // | |
// (c) Hawk Weisman, all rights reserved 'n' stuff // | |
/////////////////////////////////////////////////////////////////////// | |
// Suppose we wanted magic missiles to do special things | |
// when invalidated... | |
class MagicMissile (payload: Payload) | |
extends Event(payload) { | |
override def invalidate: Unit { | |
// do whatever it is we want to do here | |
println("I wanted to cast...Magic Missile!") | |
// we can even use super calls to get the parent class's behaviour | |
super.invalidate() | |
} | |
override def apply(payload: Payload): Unit { | |
(payload) => | |
// do whatever it is we would put in the function body | |
// here | |
println(payload.toString) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm reminded that in a real implementation, the
Event
class would also handle all the child event spawning and invalidating stuff, even though that seems grossly not FP. Maybe there's a way to do it with monads...