Options
All
  • Public
  • Public/Protected
  • All
Menu

Module events

Introduction

The CPI Node allows addons to dynamically control the application's flow and layout.

This is done by exposing events that the addons can subscribe to with an Interceptor and then perform custom logic within these interceptors.

Subscribing to events

Addons should subscribe to events within their exported load function. Every time the CPI Node is loaded the subcriptions are reset, and the addon needs to resubscribe in the load function.

Subscribing is done using the pepperi.events.intercept function which recieves 3 parameters:

  • The event key - What event the addon wants to subscribe to
  • A filter object - Only subscribe to certain events
  • The interceptor - The function that is call when this event happens

Subscribing to the RecalculateUIObject event of the UserHomePage UIObject

export async function load() {
     const filter = {
         UIObject: {
             Context: {
                 Name: 'UserHomePage'
             }
         }
     };
     pepperi.events.intercept('RecalculateUIObject', filter, async (data) => {
         const uiObject = data.UIObject;

         // do something with the UIObject
         for (const field of data.UIObject?.fields ?? []) {
             field.visible = Math.random() < 0.5;
         }
     });
}

Interceptors

Subscribing to events is done using the Interceptor function.

The CPI Node allows addons to control the application flow by subscribing to events.

Every event in the application is divided to 3 parts:

  • Before the main action
  • The main action
  • After the main action

Addons can intercept any event and implement it's main action, and/or perform some custom logic before and/or after the main action.

The Interceptor lifecycle

Interceptor

See Interceptor for expamle interceptors.

Type aliases

EventCallbackType

EventCallbackType: function

The event callback that close the event

Type declaration

    • (string: any): void
    • Parameters

      • string: any

      Returns void

EventKey

EventKey: typeof EventKeys[number] | string & object

IContextWithData

IContextWithData: IContext & object

The Interceptor function used to subscribe to events

param

data about the current invocation of the event

param

a middleware allowing the passing of control to the next interceptors

param

the main function implementation passed from previous interceptors

usage

Example #1:

Performing an action before the main action

In this case only the data parameter needs to be sent

const interceptor: Interceptor = (data) => {
     // perform you custom logic here
}
usage

Example #2:

Performing an action after the main action

const interceptor: Interceptor = (data, next, main) => {
     // first allow other interceptors & the main action to run
     await next(main);

     // perform you custom logic here
}
usage

Example #3:

Implementing the main action

const interceptor: Interceptor = (data, next, main) => {
     // first allow other interceptors & the main action to run
     await next(async () => {
         // implement the main logic here
     });
}

MainFunction

MainFunction: function | undefined | null

The main function definintion for interceptors

Variables

Const EventKeys

EventKeys: readonly "RecalculateUIObject"[] = ...

Generated using TypeDoc