Skip to contents

A minimal publish/subscribe mechanism. Packages that finish a top-level fit, simulation or result call rxEventEmit(); listeners registered with rxEventListen() are told about it. Events are only delivered from the outermost operation: work done inside rxEventScope() (or between .rxEventEnter() and .rxEventExit()) emits nothing.

Usage

rxEventListen(id, fun)

rxEventUnlisten(id)

rxEventListeners()

rxEventDepth()

rxEventSeq()

rxEventScope(expr)

.rxEventEnter()

.rxEventExit(event = NULL, ..., fun = NULL)

.rxEventCall(call, fun = NULL)

rxEventEmit(event, ..., fun = NULL)

Arguments

id

A listener id; registering the same id again replaces it.

fun

For rxEventListen(), the listener, a function function(event, ...). For rxEventEmit(), .rxEventExit() and .rxEventCall(), the name of the emitting function, used to normalize a call payload field (see Details).

expr

Code to evaluate inside an operation scope.

event

The event name, e.g. "fitComplete" or "solveComplete".

...

Payload fields (named).

call

A call to normalize.

Value

rxEventListen(), rxEventUnlisten() and rxEventEmit() return NULL invisibly; rxEventListeners() returns the listener ids; rxEventDepth() the current depth; rxEventSeq() the number of events delivered in this process; rxEventScope() the value of expr.

Details

Listeners are called as fun(event, ...) with the event name and the payload fields as named arguments. A listener that errors gives a warning and never stops the other listeners or the caller. Events emitted while a listener runs are dropped.

A call payload field is passed through .rxEventCall(): its head becomes fun (when given; otherwise a function object in the head becomes `<fun>`). Arguments that are not language (values inlined by do.call()) become `<value>`, except single constants; when there are more than five such values they are all dropped and replaced by one `<...>` marker. A recorded call therefore never embeds a large object.

Author

Matthew L. Fidler

Examples

got <- NULL
rxEventListen("example", function(event, ...) got <<- event)
rxEventEmit("myEvent", value = 1)
got
#> [1] "myEvent"
rxEventScope(rxEventEmit("ignored"))
rxEventUnlisten("example")