Creating Workbook Plugins – Publishing events to the Workbook

This is part 4 in the series about creating Workbook plugins for Profitbase InVision.

Part 1 : The Basics
Part 2 : Reading data using the Sql Script Web Service API
Part 3 : Writing data using the Sql Script Web Service API
Part 4 : Publishing events to the Workbook (this one)
Part 5 : Conditional execution of GenericAction

Download : Source code and sample database

In this post, we’ll walk through how to publish events from a plugin, so that other components in the Workbook can react and do their thing.

You should download the source code from the link above to follow along as I explain the parts of the code that’s relevant to understanding how to publish events from within a plugin.

Publishing events to the Workbook from a plugin

When the user has submitted a new incident, we want to raise an event so that the charts showing the incident causes and affected starship types are updated, like shown below.

Sienar

In order to do this, we simply need to call the raisePluginEvent method on the PluginCompoent base class that we inherit from, and then the event can be handled by other component Actions (such as LoadData, SaveData, ExecuteExpression etc). We can even pass along some data that can be used by the event subscribers handling the event.

In the code, we raise the event when a new incident has been successfully submitted. We talked about how a new incident is submitted in Part 3, so I won’t repeat that here, but if you look in the source code below, there’s a call to raiseNewIncidentSubmitted when a new incident has been submitted through the Web Service.

/**
         * Submits a new incident by calling a SQL Script through the SQL Script Web Service API.
         * When a new script has been successfully submitted, an event is raised through the Event Dispatcher so that
         * it can be handled by any other Workbook component.
         */
        public submitNewIncident() {
            this.applicationContext.getService('httpClients')
                .getSqlScriptHttpClient()
                .execute(this.SUBMIT_INCIDENT_SCRIPTID, this.createSubmitNewIncidentRequestMessage())
                .then(success => {
                      this.raiseNewIncidentSubmitted();
                }, error => {
                      this.reportError(error);      
                }).finally(() => {
                    
                });
        }                
                
        /**
         * Raises an event through the Event Dispatcher, notifying other Workbook components that a new incident
         * has been submitted.
         */
        private raiseNewIncidentSubmitted() : void {
            this.raisePluginEvent({
                messageCode : 'INCIDENT_SUBMITTED'
            });            
        }

raiseNewIncidentSubmitted simply calls the raisePluginEvent method on the PluginComponent base class and passes along an event data object. The event data object can be used by the event handlers (for example a LoadData action) through the standard @Event.Data property call. In this example, you would be able to examine the message code by accessing @Event.Data.messageCode in an Action Group condition or an Action instruction to determine what to do based on the value of messageCode.

Next step

Part 5 : Conditional execution of GenericAction