Handling Historical Events

An event that occurs prior to subscribing a listener is called a Historical Event to that listener. By default, listeners do not receive Historical Event. But you can allow a late-bound listener to handle Historicial Event by calling .acceptHistorical() on builder:

aggregator.get('some event').acceptHistorical().add(listener);

Below is an example:

// dispatches event once without listeners
aggregator.get('Price Changed').dispatch(12.00);

// subscribes listener
aggregator.get('Price Changed').acceptHistorical().add(price => {
    console.log('Received historical price: ' + price);
});

Run the code snippet, console output will look like:

Received historical price: 12.00

Limiting Event History

Event Aggregator allows you to limit number of historical event records. To specify the limit when creating Event Aggretator:

const HISTORICAL_LIMIT = 100;
const aggregator = event.createAggregator(HISTORICAL_LIMIT);

Special values:

  • -1 means no history. Used by default if there is no limit given.

  • Zero means unlimited history (be aware of memory leak).

By limiting history as in snippet above, there will be at maximum 100 most recent occurred events being stored in memory at a time. You can adjust the limit whenever you want. To do so:

aggregator.setHistoricalLimit(newLimit);

If current number of historical events is greater than the new limit, the historical stack will be truncated to the new limit.

To get current limit:

var currentLimit = aggregator.getHistoricalLimit();

To get current number of historical events:

var historicCount = aggregator.getHistoricalSize();

Deleting History

To delete history of a certain event:

aggregator.get('some event').deleteHistory();

To clear history of ALL events:

aggregator.clearHistory();

Last updated