Eventslink

Programs can expose events. Other programs can subscribe to these events specifying a handler function. When an event is dispatched, all subscribed handlers will be called.

To subscribe to an event on one of your refs:

await ref.eventName.subscribe('handlerName');

export function handlerName({ sender, args }) {
    // Handle the event here
}

In the code above, sender is a ref to the node dispatching the event. If you subscribe to multiple events with the same handler, you can use sender to determine who's rising the event.

Subcriptions are idempotent, meaning that if you subscribe multiple times to the same ref, with the same handler, only the first subscription will actually be executed.

Subscribe/unsubscribelink

Programs can declare a subscribe and unsubscribe function for an event they expose. subscribe will be called when the first subscriber subscribes, conversely, unsubscribe will be called when the last subscriber unsubscribes.

This is useful for drivers that need to setup, say, a webhook. but only if someone is subscribed to an event. For example:

export const Type {
    eventName: {
        subscribe: () => {
            // Subscribe to webhook here
        },
        subscribe: () => {
            // Unsubscribe from webhook here
        }
    }
}
...