trait Event[+T] extends AnyRef
Events are emitters of values. They can be processed as if they were streams. Here's how to create one that emits Ints.
val ints = Event[Int]()
To listen to an Event stream for values, register a handler.
val you = ints.respond { n => println(s"$n for you") }
Handlers in Event-speak are called Witnesses.
And, respond is equivalent to register(Witness { ... })
, so we can also
write:
val me = ints.register(Witness { n => println(s"$n for me") })
Registration is time-sensitive. Witnesses registered to an Event stream can't observe past values. In this way, Events are instantaneous. When the stream emits a value, it fans out to every Witness; if there are no Witnesses to that communication, whatever value it carried is lost.
Events emit values, okay, but who decides what is emitted? The implementor of an Event decides!
But there's another way: you can create an Event that is also a Witness.
Remember, Witnesses are simply handlers that can receive a value. Their type
is T => Unit
. So an Event that you can send values to is also a Witness.
The ints
Event we created above is one of these. Watch:
scala> ints.notify(1) 1 for you 1 for me
We can also stop listening. This is important for resource cleanup.
scala> me.close() scala> ints.notify(2) 2 for you
In relation to Vars, Events are instantaneous values, defined only at particular instants in time; whereas Vars are defined at all times. It is possible to view Events as the discrete counterpart to Var's continuous nature.
Note: There is a Java-friendly API for this trait: com.twitter.util.AbstractEvent.
- Self Type
- Event[T]
- Alphabetic
- By Inheritance
- Event
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Abstract Value Members
Concrete Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def build[U >: T, That <: Seq[U]](implicit factory: Factory[U, That]): Event[That]
Progressively build a Seq of events using the passed-in builder.
Progressively build a Seq of events using the passed-in builder. A value containing the current version of the collection is notified for each incoming event.
- def buildAny[That](implicit factory: Factory[T, That]): Event[That]
Progressively build any collection of events using the passed-in builder.
Progressively build any collection of events using the passed-in builder. A value containing the current version of the collection is notified for each incoming event.
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- def collect[U](f: PartialFunction[T, U]): Event[U]
Build a new Event by applying the given function to each value observed.
Build a new Event by applying the given function to each value observed. Event values for which the partial function
f
does not apply are dropped; other values are transformed byf
. - def dedup: Event[T]
Builds a new Event by keeping only the Events where the previous and current values are not
==
to each other. - def dedupWith(eq: (T, T) => Boolean): Event[T]
Build a new Event by keeping only those Event values where the equality predicate
eq
applied to the current and new values does not match. - def diff[CC[_], U](implicit arg0: Diffable[CC], toCC: <:<[T, CC[U]]): Event[Diff[CC, U]]
The Event that stores the difference between successive updates to the parent event.
The Event that stores the difference between successive updates to the parent event. This can be used to perform incremental computation on large data structures.
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def filter(p: (T) => Boolean): Event[T]
Build a new Event by keeping only those Event values that match the predicate
p
. - def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- def foldLeft[U](z: U)(f: (U, T) => U): Event[U]
Build a new Event by incrementally accumulating over events, starting with value
z
.Build a new Event by incrementally accumulating over events, starting with value
z
. Each intermediate aggregate is notified to the derived event. - final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def joinLast[U](other: Event[U]): Event[(T, U)]
Join two events into a new Event which notifies a tuple of the last value in each underlying event.
- def map[U](f: (T) => U): Event[U]
Build a new Event by transforming each new event value with
f
. - def merge[U >: T](other: Event[U]): Event[U]
Merge two events; the resulting event interleaves events from this and
other
. - def mergeMap[U](f: (T) => Event[U]): Event[U]
The Event which merges the events resulting from
f
applied to each element in this Event. - final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- def patch[CC[_], U](implicit arg0: Diffable[CC], ev: <:<[T, Diff[CC, U]]): Event[CC[U]]
Patch up an Event of differences (like those produced by Event.diff) into an Event that reflects the current version of a data structure.
Patch up an Event of differences (like those produced by Event.diff) into an Event that reflects the current version of a data structure. That is:
(event: Event[CC[T]]).diff.patch
is equivalent toevent
- final def respond(s: (T) => Unit): Closable
Observe this event with function
s
.Observe this event with function
s
. Equivalent toregister(Witness(s))
. - def select[U](other: Event[U]): Event[Either[T, U]]
Merge two Events of different types.
- def sliding(n: Int): Event[Seq[T]]
Build a new Event representing a sliding window of at-most
n
.Build a new Event representing a sliding window of at-most
n
. Each event notified by the parent are added to a queue of size at-mostn
. This queue is in turn notified to register of the returned event. - final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def take(howmany: Int): Event[T]
An event which consists of the first
howmany
values in the parent Event. - def toFuture(): Future[T]
A Future which is satisfied by the first value observed.
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- def zip[U](other: Event[U]): Event[(T, U)]
Merge two event streams in lock-step, combining corresponding event values.
Merge two event streams in lock-step, combining corresponding event values.
- Note
This can be dangerous! Since the implementation needs to queue outstanding Event-values from the slower producer, if one Event outpaces another, this queue can grow in an unbounded fashion.