Skip to content

Instantly share code, notes, and snippets.

@hawkw
Forked from ArcticLight/Event.scala
Last active August 29, 2015 14:24
Show Gist options
  • Save hawkw/ed8f10d72ca950ac648b to your computer and use it in GitHub Desktop.
Save hawkw/ed8f10d72ca950ac648b to your computer and use it in GitHub Desktop.
///////////////////////////////////////////////////////////////////////
// TOP SECRET METEORCODE ENGINEERING BULLSHIT -- DO NOT STEAL //
// (c) Hawk Weisman, all rights reserved 'n' stuff //
///////////////////////////////////////////////////////////////////////
type Payload: Map[String,Object]
type Prefunc: Payload => Boolean
abstract class Event (
protected val payload: Payload,
protected var valid: Boolean = true
) extends Payload => Prefunc => Unit {
// validity stuff
def isValid: Boolean = valid
def invalidate: Unit { valid = false }
// this is where you write the onEvent behaviour, basically - the
// closure body becomes the apply() method
def apply(payload: Payload)(implicit prefunc: Prefunc = { _.seen(this) }): 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()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment