Skip to content

Instantly share code, notes, and snippets.

@ArcticLight
Forked from hawkw/Event.scala
Last active August 29, 2015 14:24
Show Gist options
  • Save ArcticLight/a38ad86862d0d16cddc2 to your computer and use it in GitHub Desktop.
Save ArcticLight/a38ad86862d0d16cddc2 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]
abstract class Event (
protected val payload: Payload,
protected var valid: Boolean = true
) extends (Payload)(prefunc: (Payload) => Boolean = {
payload => return payload.seen(this)
}) => 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: 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()
}
@hawkw
Copy link

hawkw commented Jul 3, 2015

I think that the curried syntax may actually be

Payload => Prefunc => Boolean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment