-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package app.wishingtree | ||
|
||
import dev.wishingtree.branch.keanu.eventbus.{EventBus, EventMessage, Subscriber} | ||
|
||
object IntEventBus extends EventBus[Int] | ||
|
||
object KeanuExample extends Subscriber[Int] { self => | ||
|
||
override def onMessage(msg: Int): Unit = | ||
println(s"Got Int = $msg") | ||
|
||
def main(args: Array[String]): Unit = { | ||
|
||
IntEventBus.subscribe(self, _.payload > 5) | ||
IntEventBus.subscribe(self, _.payload % 2 == 0) | ||
|
||
IntEventBus.subscribe((msg: Int) => println(s"Got message $msg")) | ||
|
||
IntEventBus.publish(EventMessage[Int]("tinyInts", 4)) | ||
IntEventBus.publish(EventMessage[Int]("tinyInts", 6)) | ||
IntEventBus.publish(EventMessage[Int]("tinyInts", 10)) | ||
IntEventBus.publish(EventMessage[Int]("tinyInts", 9)) | ||
|
||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
keanu/src/main/scala/dev/wishingtree/branch/keanu/eventbus/EventBus.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package dev.wishingtree.branch.keanu.eventbus | ||
|
||
import java.util | ||
import java.util.UUID | ||
import scala.collection.mutable | ||
import scala.collection.mutable.ArrayBuffer | ||
import scala.util.Try | ||
|
||
trait EventBus[T] { | ||
|
||
private case class Subscription( | ||
id: UUID, | ||
subscriber: Subscriber[T], | ||
filter: EventMessage[T] => Boolean | ||
) | ||
|
||
private val subscribers: mutable.ArrayBuffer[Subscription] = | ||
ArrayBuffer.empty | ||
|
||
def publish(msg: EventMessage[T]): Unit = synchronized { | ||
subscribers.foreach { sub => | ||
if sub.filter(msg) then Try(sub.subscriber.onMessage(msg.payload)) | ||
} | ||
} | ||
|
||
def subscribe(subscriber: Subscriber[T]): UUID = | ||
synchronized { | ||
val sub = Subscription(UUID.randomUUID(), subscriber, _ => true) | ||
subscribers.addOne(sub) | ||
sub.id | ||
} | ||
|
||
def subscribe( | ||
subscriber: Subscriber[T], | ||
filter: EventMessage[T] => Boolean | ||
): UUID = { | ||
synchronized { | ||
val sub = Subscription(UUID.randomUUID(), subscriber, filter) | ||
subscribers.addOne(sub) | ||
sub.id | ||
} | ||
} | ||
|
||
def unsubscribe(subscriber: Subscriber[T]): Unit = | ||
synchronized { | ||
subscribers.filterInPlace(sub => sub.subscriber != subscriber) | ||
} | ||
|
||
def unsubscribe(ids: UUID*): Unit = | ||
synchronized { | ||
subscribers.filterInPlace(sub => !ids.contains(sub.id)) | ||
} | ||
} | ||
|
3 changes: 3 additions & 0 deletions
3
keanu/src/main/scala/dev/wishingtree/branch/keanu/eventbus/EventMessage.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package dev.wishingtree.branch.keanu.eventbus | ||
|
||
case class EventMessage[T](topic: String, payload: T) |
5 changes: 5 additions & 0 deletions
5
keanu/src/main/scala/dev/wishingtree/branch/keanu/eventbus/Subscriber.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package dev.wishingtree.branch.keanu.eventbus | ||
|
||
trait Subscriber[T] { | ||
def onMessage(msg: T): Unit | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Keanu | ||
|
||
This module currently contains a simple *typed* EventBus. | ||
|
||
Have an object extend `EventBus[T]` for your type, e.g. | ||
|
||
```scala 3 | ||
object IntEventBus extends EventBus[Int] | ||
``` | ||
|
||
Then, you can have some implementation extend `Subsciber[T]` and subscribe, or pass in an anonymous implementation (e.g. | ||
`IntEventBus.subscribe((msg: Int) => println(s"Got message $msg"))`) |