Skip to main content

Custom Events

Custom events are freely defined by developers in service elements as needed and triggered in business logic code, providing sufficient flexibility for event-driven system development.

The hierarchical structure of custom event elements is Meta (events.Meta) → Type (events.NormalType) → Instance. Developers can quickly create custom event instance elements through JitAi's visual development tools.

Of course, developers can also create their own Type elements or modify the official events.NormalType element provided by JitAi in their own App to implement their own encapsulation.

Quick Start

Developers need to first define events in service elements, then create Custom Event instance elements to subscribe to events defined in services, and write event business logic code.

Creating Instance Elements

Directory Structure

Recommended Directory Structure
events/
├── MyCustomEvent/
│ ├── e.json
│ ├── inner.py (optional, when funcType is Inner)
│ └── __init__.py

e.json File

Event Element Definition File
{
"type": "events.NormalType",
"funcType": "Inner",
"asyncType": false,
"sender": "services.MyService.CustomEvent",
"title": "Test Custom Event",
"backendBundleEntry": ".",
"returnType": "None",
"backendEpath": "events/MyCustomEvent/element.pkg",
"extendType": "self"
}

sender: fullName of service element that declares the event.event name

Event Logic Code

When funcType is Inner, create inner.py file:

Internal Event Handler Function
from datatypes.Meta import datatypes

def customFunc(*args, **kwargs):
"""
Custom event handler function
"""
# Business logic implementation
print("Custom event triggered")
return True

Usage Example

Event Trigger Example
# sender: fullName of service element that declares the event.event name
app.event.publish(sender="services.MyService.CustomEvent",args=("paramValue"))

Element Configuration

e.json Configuration

Configuration ItemTypeRequiredDescription
typeStringYesFixed value: events.NormalType
senderStringYesEvent sender, format: service element fullName.event name
funcTypeStringYesFunction type: Global/Inner, default Global
funcStringConditionally requiredWhen funcType is Global, specify service function to call, value is element fullName.function name
asyncTypeBooleanNoWhether to execute asynchronously, default false
enableIntegerNoWhether enabled, 1 enabled/0 disabled, default 0
objModeBooleanNoObject mode, default false
returnTypeStringNoReturn value type, default None
titleStringNoEvent display name, default uses fullName
pathStringNoRelative path of element directory (example: events)
backendBundleEntryStringNoBackend bundle entry directory, relative path (example: .)
backendEpathStringNoBackend element bundle path (example: events/testCustomEvents/element.pkg)
extendTypeStringNoInheritance extension type (example: self)

Business Configuration File

When funcType is Inner, supports creating independent business logic files:

  • inner.py: Business logic file containing customFunc function
  • init.py: Package initialization file, imports business logic

Methods

call

Execute event handler function, record execution time.

Parameter Details

Parameter NameTypeCorresponding Native TypeRequiredDescription
argsVariable argumentstupleNoPositional arguments
kwargsKeyword argumentsdictNoNamed arguments

Return Value

Returns execution result of event handler function, type depends on specific business logic.

Usage Example

Event Call Example
# Get event instance
event = app.getElement("events.MyCustomEvent")

# Call event
result = event.call(
userId=123,
action="update",
data={"name": "张三", "email": "zhangsan@example.com"}
)

getSender

Get event sender identifier.

Return Value

Returns string type sender identifier, format: service element fullName.function name

Usage Example

Get Sender Example
event = app.getElement("events.MyCustomEvent")
sender = event.getSender()
print(f"Event sender: {sender}")

isValid

Check if event is valid, can be overridden in subclasses to implement custom validation logic.

Parameter Details

Parameter NameTypeCorresponding Native TypeRequiredDescription
argsVariable argumentstupleNoValidation parameters
kwargsKeyword argumentsdictNoValidation parameters

Return Value

Returns boolean value, True means event is valid, False means invalid.

Usage Example

Event Validity Check
event = app.getElement("events.MyCustomEvent")
if event.isValid(userId=123):
result = event.call(userId=123, action="process")

handleNode

Process event node, triggered before executing event function, can preprocess node and parameters.

Parameter Details

Parameter NameTypeCorresponding Native TypeRequiredDescription
nodeObjectobjectYesEvent node object
argsVariable argumentstupleNoOriginal parameters
kwargsKeyword argumentsdictNoOriginal parameters

Return Value

Returns tuple containing three elements: (processed node, processed args, processed kwargs)

Usage Example

Node Processing Example
# Override handleNode method in custom event class
def handleNode(self, node, *args, **kwargs):
# Add timestamp
kwargs['timestamp'] = datetime.now()

# Log record
print(f"Process event node: {node}, parameters: {args}, {kwargs}")

return node, args, kwargs

createTask

Create asynchronous task, used when event is configured for asynchronous execution.

Parameter Details

Parameter NameTypeCorresponding Native TypeRequiredDescription
taskParamsJitDictdictYesTask parameters, must be serializable
nodeIdStextstrYesNode ID that triggered the event
requestIdStextstrYesRequest ID, used for log positioning

Usage Example

Create Asynchronous Task Example
# Construct task parameters
taskParams = {
"userId": 123,
"action": "processData",
"data": {"key": "value"}
}

# Create asynchronous task
event.createTask(
taskParams=taskParams,
nodeId="node_001",
requestId="req_12345"
)

Properties

name

Complete name (fullName) of the event.

sender

Event sender identifier, format: service element fullName.function name

funcType

Function type, optional values:

  • Global: Call global service function
  • Inner: Call internal custom function

func

When funcType is Global, specifies service function name to call.

asyncType

Whether to execute asynchronously, boolean value.

enable

Whether event is enabled, 1 means enabled, 0 means disabled.

objMode

Object mode, when true, event parameters will be wrapped in obj object for passing.

callTime

Last time the event was called, datetime type.

Advanced Features

Asynchronous Event Processing

Configure asyncType to true to enable asynchronous event execution:

Asynchronous Event Configuration
{
"type": "events.NormalType",
"title": "Asynchronous Processing Event",
"sender": "services.DataProcessor.asyncProcess",
"funcType": "Global",
"func": "handleAsyncData",
"asyncType": true,
"enable": 1
}

Object Mode Parameter Passing

Enable objMode to wrap all parameters in obj object:

Object Mode Configuration
{
"type": "events.NormalType",
"title": "Object Mode Event",
"sender": "services.ObjectHandler.processObject",
"funcType": "Inner",
"objMode": true,
"enable": 1
}

Event Chain Calling

Event engine enables event chain calling and complex business logic:

Event Chain Calling Example
# Get event service
eventSvc = app.getElement("events.services.EventSvc")

# Trigger event chain
eventSvc.callEvent("events.DataValidation", data=inputData)
eventSvc.callEvent("events.DataProcessing", data=validatedData)
eventSvc.callEvent("events.DataStorage", data=processedData)

Custom Event Validation

Override isValid method to implement custom validation logic:

Custom Validation Logic
def customFunc(*args, **kwargs):
# Get user ID
userId = kwargs.get('userId')

# Business validation
if not userId or userId <= 0:
return False

# Permission check
userService = app.getElement("services.UserService")
if not userService.hasPermission(userId, "process_data"):
return False

# Execute business logic
print(f"User {userId} triggered data processing event")
return True
JitAI AssistantBeta
Powered by JitAI