Skip to main content

Goal

Run a condition Function before a Trigger runs, and only execute the handler Function when the condition passes.

Steps

1. Register the handler and condition Functions

Register a normal handler Function and a condition Function that returns true or false.
Both Functions are declared in the same way. They only differ in how they are used.
conditions-functions.ts
import { registerWorker } from 'iii-sdk'

const iii = registerWorker(process.env.III_URL ?? 'ws://localhost:49134')

// condition
iii.registerFunction(
  { id: 'conditions::is-high-value' },
  async (event) => (event.new_value?.amount ?? 0) >= 1000,
)

// handler
iii.registerFunction(
  { id: 'orders::process-high-value' },
  async (event) => ({
    status_code: 200,
    body: { processed: true, amount: event.new_value?.amount ?? 0 },
  }),
)

2. Register triggers with the correct condition key

Create a Trigger and specify its condition with condition_function_id. Example (state trigger):
state-trigger-with-condition.ts
iii.registerTrigger({
  type: 'state',
  function_id: 'orders::process-high-value',
  config: {
    scope: 'orders',
    key: 'status',
    condition_function_id: 'conditions::is-high-value',
  },
})

Result

When the trigger fires, iii calls the condition Function first. The handler Function runs only when the condition passes.