Last active
December 19, 2015 03:59
-
-
Save Narigo/5894017 to your computer and use it in GitHub Desktop.
trying to typeclass
This file contains hidden or 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 EventBus(internal: JEventBus) { | |
| trait Sendable[T] { | |
| def send(address: String, message: T)(handler: Message[T] => Unit) | |
| def publish(address: String, message: T) | |
| } | |
| object Sendable { | |
| def instance[T](jSend: ((String, T, Handler[JMessage[T]]) => JEventBus), jPublish: (String, T) => JEventBus): Sendable[T] = new Sendable[T] { | |
| def publish(address: String, t: T) = { | |
| jPublish(address, t) | |
| } | |
| def send(address: String, t: T)(handler: Message[T] => Unit) = { | |
| jSend(address, t, convertFunctionToMessageHandler(handler)) | |
| } | |
| } | |
| } | |
| implicit val sendableString = Sendable.instance[String](internal.send(_, _, _), internal.publish(_, _)) | |
| implicit val sendableBoolean = Sendable.instance[Boolean](internal.send(_, _, _), internal.publish(_, _)) | |
| implicit val sendableBuffer = Sendable.instance[Buffer](internal.send(_, _, _), internal.publish(_, _)) | |
| implicit val sendableByte = Sendable.instance[Byte]((a, b, c) => internal.send(a, Byte.box(b), c), internal.publish(_, _)) | |
| implicit val sendableBinary = Sendable.instance[Array[Byte]](internal.send(_, _, _), internal.publish(_, _)) | |
| implicit val sendableChar = Sendable.instance[Char]((a, b, c) => internal.send(a, Char.box(b), c), internal.publish(_, _)) | |
| implicit val sendableDouble = Sendable.instance[Double](internal.send(_, _, _), internal.publish(_, _)) | |
| implicit val sendableFloat = Sendable.instance[Float]((a, b, c) => internal.send(a, Float.box(b), c), internal.publish(_, _)) | |
| implicit val sendableInt = Sendable.instance[Int]((a, b, c) => internal.send(a, Int.box(b), c), internal.publish(_, _)) | |
| implicit val sendableJsonArray = Sendable.instance[JsonArray](internal.send(_, _, _), internal.publish(_, _)) | |
| implicit val sendableJsonObject = Sendable.instance[JsonObject](internal.send(_, _, _), internal.publish(_, _)) | |
| implicit val sendableLong = Sendable.instance[Long]((a, b, c) => internal.send(a, Long.box(b), c), internal.publish(_, _)) | |
| implicit val sendableShort = Sendable.instance[Short]((a, b, c) => internal.send(a, Short.box(b), c), internal.publish(_, _)) | |
| def registerHandler[T <% Sendable[T]](address: String)(handler: EventBus.EventBusHandler[T], resultHandler: AsyncResult[Unit] => Unit = { ares => }): EventBus = { | |
| internal.registerHandler(address, handler.jHandler, voidAsyncHandler(resultHandler)) | |
| this | |
| } | |
| def registerLocalHandler[T <% Sendable[T]](address: String)(handler: EventBus.EventBusHandler[T]): EventBus = { | |
| internal.registerLocalHandler(address, handler.jHandler) | |
| this | |
| } | |
| def unregisterHandler[T <% Sendable[T]](address: String)(handler: EventBus.EventBusHandler[T], resultHandler: AsyncResult[Unit] => Unit = { ares => }) { | |
| handler.unRegisterMe(internal, address, resultHandler) | |
| } | |
| def send[T](address: String, message: T)(handler: Message[T] => Unit)(implicit ev: Sendable[T]): EventBus = { | |
| ev.send(address, message)(handler) | |
| this | |
| } | |
| def publish[T](address: String, message: T)(implicit ev: Sendable[T]): EventBus = { | |
| ev.publish(address, message) | |
| this | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment