ExecuteTrigger

This is for:

Developer

Example Implementation

execute-trigger.tsx

import {
  buildExecuteTrigger,
  SearchEngine,
  ExecuteTriggerParams,
  FunctionExecutionTrigger,
} from '@coveo/headless';
 
/**
 * This sample creates an instance of the headless execute trigger controller
 * and shows how to utilize the controller with the user defined functions.
 *
 * @param engine - a headless search engine instance
 * @returns An unsubscribe function
 */
export function bindExecuteTrigger(engine: SearchEngine) {
  const controller = buildExecuteTrigger(engine);
 
  const executeFunction = (execution: FunctionExecutionTrigger) => {
    const {functionName, params} = execution;
 
    if (functionName === 'log') {
      log(params);
    }
  };
 
  const log = (params: ExecuteTriggerParams) => {
    console.log('params: ', params);
  };
 
  const unsubscribe = controller.subscribe(() =>
    controller.state.executions.forEach((execution) =>
      executeFunction(execution)
    )
  );
  return unsubscribe;
}

The ExecuteTrigger controller handles Execute triggers from the query response. An Execute trigger query pipeline rule lets you define a custom JavaScript function to be executed in the frontend when a certain condition is met.

Methods

subscribe

Adds a callback that’s invoked on state change.

Parameters

  • listener: () => void

    A callback that’s invoked on state change.

Returns Unsubscribe: A function to remove the listener.

Attributes

state

The state of the ExecuteTrigger controller.

Properties

Initialize

buildExecuteTrigger

Creates a ExecuteTrigger controller instance. An execute trigger is configured in the Administration console, and used to execute a function in the browser when a certain condition is met.

Parameters

  • engine: SearchEngine

    The headless engine.

Returns ExecuteTrigger

FunctionExecutionTrigger

Properties

  • functionName: string

    The name of the function to execute.

  • params: (string | number | boolean)[]

    The parameters of the function to execute.

Unsubscribe

Call signatures

  • (): void;