Created
August 22, 2014 07:14
-
-
Save naoty/5a1df9eac96719948e14 to your computer and use it in GitHub Desktop.
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
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