Skip to content

Instantly share code, notes, and snippets.

@orangy
Created June 3, 2014 19:01
Show Gist options
  • Save orangy/a382a041577ed113f365 to your computer and use it in GitHub Desktop.
Save orangy/a382a041577ed113f365 to your computer and use it in GitHub Desktop.
class Event<T> {
typealias subscriber = (T)->Void
var subscribers = subscriber[]()
func subscribe(body : subscriber) {
subscribers.append(body)
}
func fire(value : T) {
for s in subscribers {
s(value)
}
}
}
@assignment func += <T> (inout event: Event<T>, body: (T)->Void) {
event.subscribe(body)
}
var e = Event<Int>()
e += { value in println(value) }
e.fire(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment