Modules@robinpath/event
event

@robinpath/event

0.1.1Node.jsPublic

Pub/sub event emitter for RobinPath automation workflows

Event

Pub/sub event system with named buses, listener management, history, and async waitFor

Package: @robinpath/event | Category: Infrastructure | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the event module when you need to:

  • Create a named event bus with an optional max listener limit -- Use event.create to perform this operation
  • Subscribe a listener to an event on a named bus -- Use event.on to perform this operation
  • Subscribe a one-time listener that automatically removes itself after firing -- Use event.once to perform this operation
  • Remove a listener, all listeners for an event, or all listeners on the bus -- Use event.off to perform this operation
  • Emit an event with optional data, notifying all subscribed listeners -- Use event.emit to perform this operation

Quick Reference

FunctionDescriptionReturns
createCreate a named event bus with an optional max listener limitThe event bus configuration
onSubscribe a listener to an event on a named busObject with event name and listener count
onceSubscribe a one-time listener that automatically removes itself after firingObject with event name and listener count
offRemove a listener, all listeners for an event, or all listeners on the busObject with event name and number removed
emitEmit an event with optional data, notifying all subscribed listenersObject with event name and number of listeners notified
listenerCountGet the number of listeners for a specific event or all events on a busNumber of registered listeners
eventNamesList all event names that have listeners on a busArray of event name strings
removeAllRemove all listeners from all events on a busObject with count of event types removed
historyGet the emission history for a bus, optionally filtered by event nameArray of {event, timestamp, data} objects
waitForWait for a specific event to be emitted, with a timeoutThe data emitted with the event
destroyDestroy a named event bus and free all resourcesTrue if the bus existed and was destroyed

Functions

create

Create a named event bus with an optional max listener limit

Module: event | Returns: object -- The event bus configuration

event.create "workflow" 50
ParameterTypeRequiredDescription
namestringNoEvent bus name (default: 'default')
maxListenersnumberNoMaximum listeners per event (default 100)

on

Subscribe a listener to an event on a named bus

Module: event | Returns: object -- Object with event name and listener count

event.on "workflow" "task.completed" $handler
ParameterTypeRequiredDescription
busstringYesEvent bus name
eventstringYesEvent name to listen for
listenerstringYesCallback function

once

Subscribe a one-time listener that automatically removes itself after firing

Module: event | Returns: object -- Object with event name and listener count

event.once "workflow" "done" $handler
ParameterTypeRequiredDescription
busstringYesEvent bus name
eventstringYesEvent name
listenerstringYesCallback function

off

Remove a listener, all listeners for an event, or all listeners on the bus

Module: event | Returns: object -- Object with event name and number removed

event.off "workflow" "task.completed"
ParameterTypeRequiredDescription
busstringYesEvent bus name
eventstringNoEvent name (omit to remove all)
listenerstringNoSpecific listener to remove (omit to remove all for event)

emit

Emit an event with optional data, notifying all subscribed listeners

Module: event | Returns: object -- Object with event name and number of listeners notified

event.emit "workflow" "task.completed" $taskData
ParameterTypeRequiredDescription
busstringYesEvent bus name
eventstringYesEvent name to emit
dataanyNoData to pass to listeners

listenerCount

Get the number of listeners for a specific event or all events on a bus

Module: event | Returns: number -- Number of registered listeners

event.listenerCount "workflow" "task.completed"
ParameterTypeRequiredDescription
busstringYesEvent bus name
eventstringNoEvent name (omit for total count)

eventNames

List all event names that have listeners on a bus

Module: event | Returns: array -- Array of event name strings

event.eventNames "workflow"
ParameterTypeRequiredDescription
busstringYesEvent bus name

removeAll

Remove all listeners from all events on a bus

Module: event | Returns: object -- Object with count of event types removed

event.removeAll "workflow"
ParameterTypeRequiredDescription
busstringYesEvent bus name

history

Get the emission history for a bus, optionally filtered by event name

Module: event | Returns: array -- Array of {event, timestamp, data} objects

event.history "workflow" "task.completed" 10
ParameterTypeRequiredDescription
busstringYesEvent bus name
eventstringNoFilter by event name (optional)
limitnumberNoMax entries to return (default 50)

waitFor

Wait for a specific event to be emitted, with a timeout

Module: event | Returns: any -- The data emitted with the event

event.waitFor "workflow" "task.completed" 5000
ParameterTypeRequiredDescription
busstringYesEvent bus name
eventstringYesEvent name to wait for
timeoutnumberNoTimeout in ms (default 30000)

destroy

Destroy a named event bus and free all resources

Module: event | Returns: boolean -- True if the bus existed and was destroyed

event.destroy "workflow"
ParameterTypeRequiredDescription
busstringYesEvent bus name

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Event name is requiredCheck the error message for details
Listener must be a functionCheck the error message for details
Max listeners (...) reached for event "..."Check the error message for details
@desc "Create and validate result"
do
  set $result as event.create "workflow" 50
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate enerCount

Retrieve all items and loop through them.

@desc "Listener count and iterate results"
do
  set $result as event.listenerCount "workflow" "task.completed"
  each $item in $result
    print $item
  end
enddo

2. Create a new item with create

Create a new resource and capture the result.

set $result as event.create "workflow" 50
print "Created: " + $result

3. Check before creating

List existing items and only create if needed.

@desc "Listener count and create"
do
  set $existing as event.listenerCount "workflow" "task.completed"
  if $existing == null
    event.create "workflow" 50
    print "Item created"
  else
    print "Item already exists"
  end
enddo

4. Multi-step Event workflow

Chain multiple event operations together.

@desc "Create, on, and more"
do
  set $r_create as event.create "workflow" 50
  set $r_on as event.on "workflow" "task.completed" $handler
  set $r_once as event.once "workflow" "done" $handler
  print "All operations complete"
enddo

5. Safe create with validation

Check results before proceeding.

@desc "Create and validate result"
do
  set $result as event.create "workflow" 50
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/event

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.6 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Category

devops