EventStream

class EventStream[E] extends EventSource[E, EventSubscriber[E]]

An event stream of type E dispatches events (of type E) to all functions of type (E) => Unit which were registered in the event stream as its subscribers. It doesn't have an internal state. It provides a handful of methods which enable the user to create new event streams by means of composing the old ones, filtering them, etc., in a way similar to how the user can operate on standard collections, as well as to interact with Scala futures, cancellable futures, and signals. Please note that by default an event stream is not able to receive events from the outside - that functionality belongs to SourceStream.

An event stream of type E dispatches events (of type E) to all functions of type (E) => Unit which were registered in the event stream as its subscribers. It doesn't have an internal state. It provides a handful of methods which enable the user to create new event streams by means of composing the old ones, filtering them, etc., in a way similar to how the user can operate on standard collections, as well as to interact with Scala futures, cancellable futures, and signals. Please note that by default an event stream is not able to receive events from the outside - that functionality belongs to SourceStream.

An event stream may also help in sending events from one execution context to another. For example, a source stream may receive an event in one execution context, but the function which consumes it is registered with another execution context specified. In that case the function won't be called immediately, but in a future executed in that execution context.

See also

ExecutionContext

Companion
object
class EventSource[E, EventSubscriber[E]]
class Object
trait Matchable
class Any

Value members

Concrete methods

final def collect[V](pf: PartialFunction[E, V]): EventStream[V]

Creates a new event stream of events of type V by applying a partial function which maps the original event of type E to an event of type V. If the partial function doesn't work for the emitted event, nothing will be emitted in the new event stream. Basically, it's filter + map.

Creates a new event stream of events of type V by applying a partial function which maps the original event of type E to an event of type V. If the partial function doesn't work for the emitted event, nothing will be emitted in the new event stream. Basically, it's filter + map.

Type Params
V

The type of the resulting event.

Value Params
pf

A partial function which for an original event of type E may produce an event of type V.

Returns

A new event stream of type V.

final def filter(f: E => Boolean): EventStream[E]

Creates a new EventStream[E] by filtering events emitted by the original one.

Creates a new EventStream[E] by filtering events emitted by the original one.

Value Params
f

A filtering function which for each event emitted by the original stream returns true or false. Only events for which f(event) returns true will be emitted in the resulting stream.

Returns

A new event stream emitting only filtered events.

final def flatMap[V](f: E => EventStream[V]): EventStream[V]

Creates a new EventStream[V] by mapping each event of the original EventStream[E] to a new event stream and switching to it. The usual use case is that the event from the original stream serves to make a decision which next stream we should be listening to. When another event is emitted by the original stream, we may stay listening to that second one, or change our previous decision.

Creates a new EventStream[V] by mapping each event of the original EventStream[E] to a new event stream and switching to it. The usual use case is that the event from the original stream serves to make a decision which next stream we should be listening to. When another event is emitted by the original stream, we may stay listening to that second one, or change our previous decision.

Type Params
V

The type of the resulting event stream.

Value Params
f

The function mapping each event of type E to an event stream of type V.

Returns

A new or already existing event stream to which we switch as the result of receiving the original event.

@inline
final def future(context: EventContext, executionContext: ExecutionContext): Future[E]

A shorthand for next which additionally unwraps the cancellable future

A shorthand for next which additionally unwraps the cancellable future

@inline
final def head: Future[E]

An alias to the future method.

An alias to the future method.

final def ifFalse(ev: E =:= Boolean): EventStream[Unit]

Assuming that the event emitted by the stream can be interpreted as a boolean, this method creates a new event stream of type Unit which emits unit events for each original event which is interpreted as false.

Assuming that the event emitted by the stream can be interpreted as a boolean, this method creates a new event stream of type Unit which emits unit events for each original event which is interpreted as false.

Returns

A new event stream of units.

final def ifTrue(ev: E =:= Boolean): EventStream[Unit]

Assuming that the event emitted by the stream can be interpreted as a boolean, this method creates a new event stream of type Unit which emits unit events for each original event which is interpreted as true.

Assuming that the event emitted by the stream can be interpreted as a boolean, this method creates a new event stream of type Unit which emits unit events for each original event which is interpreted as true.

Returns

A new event stream of units.

final def map[V](f: E => V): EventStream[V]

Creates a new EventStream[V] by mapping events of the type E emitted by the original one.

Creates a new EventStream[V] by mapping events of the type E emitted by the original one.

Type Params
V

The type of the resulting event.

Value Params
f

The function mapping each event of type E into exactly one event of type V.

Returns

A new event stream of type V.

final def mapSync[V](f: E => Future[V]): EventStream[V]

Creates a new EventStream[V] by mapping events of the type E emitted by the original one.

Creates a new EventStream[V] by mapping events of the type E emitted by the original one.

Type Params
V

The type of the resulting event.

Value Params
f

A function which for a given event of the type E will return a future of the type V. If the future finishes with success, the resulting event of the type V will be emitted by the new stream. Two events, coming one after another, are guaranteed to be mapped in the same order even if the processing for the second event finishes before the processing for the first one.

Returns

A new event stream of type V.

final def next(context: EventContext, executionContext: ExecutionContext): CancellableFuture[E]

Produces a CancellableFuture which will finish when the next event is emitted in the parent event stream.

Produces a CancellableFuture which will finish when the next event is emitted in the parent event stream.

Value Params
context

Internally, the method creates a subscription to the event stream, and an EventContext can be provided to manage it. In practice it's rarely needed. The subscription will be destroyed when the returning future is finished or cancelled.

Returns

A cancellable future which will finish with the next event emitted by the event stream.

override def on(ec: ExecutionContext)(body: E => Unit)(eventContext: EventContext): Subscription

Registers a subscriber in a specified execution context and returns the subscription. An optional event context can also be provided by the user for managing the subscription instead of doing it manually. When an event is published in the event stream, the subscriber function will be called in the given execution context instead of the one of the publisher.

Registers a subscriber in a specified execution context and returns the subscription. An optional event context can also be provided by the user for managing the subscription instead of doing it manually. When an event is published in the event stream, the subscriber function will be called in the given execution context instead of the one of the publisher.

Value Params
body

A function which consumes the event

ec

An ExecutionContext in which the body function will be executed.

eventContext

An EventContext which will register the Subscription for further management (optional)

Returns

A Subscription representing the created connection between the event stream and the body function

See also
Definition Classes
override def onCurrent(body: E => Unit)(eventContext: EventContext): Subscription

Registers a subscriber which will always be called in the same execution context in which the event was published. An optional event context can be provided by the user for managing the subscription instead of doing it manually.

Registers a subscriber which will always be called in the same execution context in which the event was published. An optional event context can be provided by the user for managing the subscription instead of doing it manually.

Value Params
body

A function which consumes the event

eventContext

An EventContext which will register the Subscription for further management (optional)

Returns

A Subscription representing the created connection between the event stream and the body function

See also
Definition Classes
final def pipeTo(sourceStream: SourceStream[E])(ec: EventContext): Subscription

A shorthand for registering a subscriber function in this event stream which only purpose is to publish events emitted by this stream in a given SourceStream. The subscriber function will be called in the execution context of the original publisher.

A shorthand for registering a subscriber function in this event stream which only purpose is to publish events emitted by this stream in a given SourceStream. The subscriber function will be called in the execution context of the original publisher.

Value Params
ec

An EventContext which can be used to manage the subscription (optional).

sourceStream

The source stream in which events emitted by this stream will be published.

Returns

A new Subscription to this event stream.

See also
final def scan[V](zero: V)(f: (V, E) => V): EventStream[V]

Creates a new event stream of events of type V where each event is a result of applying a function which combines the previous result of type V with the original event of type E that triggers the emission of the new one.

Creates a new event stream of events of type V where each event is a result of applying a function which combines the previous result of type V with the original event of type E that triggers the emission of the new one.

Type Params
V

The type of the resulting event.

Value Params
f

The function which combines the previous result of type V with a new original event of type E to produce a new result of type V.

zero

The initial value of type V used to produce the first new event when the first original event comes in.

Returns

A new event stream of type V.

@inline
final def withFilter(f: E => Boolean): EventStream[E]

An alias for filter used in the for/yield notation.

An alias for filter used in the for/yield notation.

final def zip(stream: EventStream[E]): EventStream[E]

Creates a new event stream by merging the original stream with another one of the same type. The resulting stream will emit events coming from both sources.

Creates a new event stream by merging the original stream with another one of the same type. The resulting stream will emit events coming from both sources.

Value Params
stream

The other event stream of the same type of events.

Returns

A new event stream, emitting events from both original streams.

@inline
final def |(sourceStream: SourceStream[E])(ec: EventContext): Subscription

An alias for pipeTo.

An alias for pipeTo.

Inherited methods

Typically, a newly created event streams and signals are lazy in the sense that till there are no subscriptions to them, they will not execute any intermediate computations (e.g. assembled to it through maps, flatMaps, etc). After all, those computations would be ignored at the end. Only when a subscription is created, the computations are performed for the first time. disableAutowiring enforces those computations even if there are no subscribers. It can be useful if e.g. the computations perform side-effects or if it's important from the performance point of view to have the intermediate results ready when the subscriber is created.

Typically, a newly created event streams and signals are lazy in the sense that till there are no subscriptions to them, they will not execute any intermediate computations (e.g. assembled to it through maps, flatMaps, etc). After all, those computations would be ignored at the end. Only when a subscription is created, the computations are performed for the first time. disableAutowiring enforces those computations even if there are no subscribers. It can be useful if e.g. the computations perform side-effects or if it's important from the performance point of view to have the intermediate results ready when the subscriber is created.

Returns

The current instance, so that disableAutoworing can be chained with other method calls.

Inherited from
EventSource
@inline
final def foreach(body: E => Unit)(executionContext: ExecutionContext, eventContext: EventContext): Subscription

An alias for the on method with the default ExecutionContext.

An alias for the on method with the default ExecutionContext.

Inherited from
EventSource
@inline
final def hasSubscribers: Boolean

Checks if there are any subscribers registered in this EventRelay.

Checks if there are any subscribers registered in this EventRelay.

Returns

true if any subscribers are registered, false otherwise

Inherited from
EventSource
protected def notifySubscribers(call: EventSubscriber[E] => Unit): Unit

The class which implements this EventRelay can use this method to notify all the subscribers that a new event arrived.

The class which implements this EventRelay can use this method to notify all the subscribers that a new event arrived.

Value Params
call

A function that will perform some action on each subscriber

Inherited from
EventSource
def subscribe(subscriber: EventSubscriber[E]): Unit

Adds a new subscriber instance. The implementing class should handle notifying this subscriber when a new event arrives. If this is the first subscriber, and disableAutowiring wasn't called previous, this will trigger a call to onWire.

Adds a new subscriber instance. The implementing class should handle notifying this subscriber when a new event arrives. If this is the first subscriber, and disableAutowiring wasn't called previous, this will trigger a call to onWire.

Value Params
subscriber

An instance of a subscriber class, known to the class implementing this EventRelay

Inherited from
EventSource
def unsubscribe(subscriber: EventSubscriber[E]): Unit

Removes a previously registered subscriber instance. If this is the last subscriber, and disableAutowiring wasn't called previously, this will trigger a call to onUnwire.

Removes a previously registered subscriber instance. If this is the last subscriber, and disableAutowiring wasn't called previously, this will trigger a call to onUnwire.

Value Params
subscriber

An instance of a subscriber class, known to the class implementing this EventRelay

Inherited from
EventSource
def unsubscribeAll(): Unit

Empties the set of subscribers and calls unWire if disableAutowiring wasn't called before.

Empties the set of subscribers and calls unWire if disableAutowiring wasn't called before.

Inherited from
EventSource
@inline
final def wired: Boolean
Inherited from
EventSource