Skip to content

Instantly share code, notes, and snippets.

@naoty
Created August 22, 2014 07:14
Show Gist options
  • Save naoty/5a1df9eac96719948e14 to your computer and use it in GitHub Desktop.
Save naoty/5a1df9eac96719948e14 to your computer and use it in GitHub Desktop.
class Stream<T> {
var subscribers: [(T) -> Void] = []
func subscribe(subscriber: (T) -> Void) {
subscribers.append(subscriber)
}
func publish(message: T) {
for subscriber in subscribers {
subscriber(message)
}
}
}
let stringStream = Stream<String>()
stringStream.subscribe { message in
println("message received: \(message)")
}
stringStream.publish("test")
stringStream.publish("demo")
let intStream = Stream<Int>()
intStream.subscribe { integer in
println("integer received: \(integer)")
}
intStream.subscribe { integer in
println("number received: \(integer)")
}
intStream.publish(1)
intStream.publish(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment