Triggers Reference
This document provides a comprehensive reference for Triggers within the MSDN framework. Triggers are special functions that are automatically executed in response to specific events or conditions.
Understanding Triggers
Triggers offer a powerful mechanism for reacting to changes and events in your application without explicit manual invocation. They are particularly useful for:
- Implementing reactive behavior.
- Automating tasks based on system state.
- Enhancing user interface responsiveness.
- Ensuring data integrity and consistency.
Trigger Syntax
A trigger is defined using the trigger keyword, followed by the trigger name, the event it listens to, and the block of code to execute.
Basic Trigger Definition
trigger MyNewTrigger on EventName do {
// Code to execute when EventName occurs
console.log("MyNewTrigger was activated!");
}
Here's a breakdown of the syntax:
trigger: The keyword to declare a trigger.MyNewTrigger: The unique name of your trigger.on EventName: Specifies the event that will activate this trigger.do: Introduces the code block to be executed.{ ... }: Contains the statements that form the trigger's action.
Types of Events
Triggers can be associated with a variety of events, including:
- System Events: Events originating from the MSDN core or system services (e.g.,
SystemStart,SystemShutdown,DataChange). - Custom Events: Events defined and dispatched by your application logic.
- Lifecycle Events: Events related to the lifecycle of components or objects (e.g.,
ComponentLoad,ComponentUnload).
Trigger Context
Inside a trigger's execution block, you have access to a special context object that provides information about the event that fired the trigger. This context can vary depending on the event type.
Accessing Event Data
For many events, the context object will contain relevant data. For example, a DataChangeEvent might provide the new and old values of the data.
trigger DataUpdateLogger on DataChangeEvent do {
if (event.data.newValue != event.data.oldValue) {
console.log(`Data updated: ${event.data.key} changed from ${event.data.oldValue} to ${event.data.newValue}`);
}
}
Advanced Trigger Features
Conditional Triggers
You can add conditions to a trigger to control its execution more precisely. The trigger will only fire if the condition evaluates to true.
trigger HighLoadAlert on SystemLoadEvent if (event.loadPercentage > 90) do {
sendEmail("admin@example.com", "System High Load Alert", `Current load: ${event.loadPercentage}%`);
}
Trigger Prioritization
In scenarios where multiple triggers might respond to the same event, you can assign priorities to control the order of execution.
trigger CriticalTask on SystemEvent priority 100 do {
// Execute critical tasks first
}
trigger LoggingTask on SystemEvent priority 10 do {
// Execute logging tasks later
}
Trigger Debouncing and Throttling
For frequently occurring events, you might want to limit how often a trigger can execute. MSDN provides mechanisms for debouncing (executing only after a period of inactivity) and throttling (executing at most once per interval).
trigger DebouncedSave on UserInputEvent debounce 500ms do {
// Save user input after 500ms of no further input
saveUserData();
}
trigger ThrottledUpdate on FrameUpdateEvent throttle 16ms do {
// Update UI at a maximum of 60 FPS
updateUI();
}
Built-in Events
MSDN provides a set of predefined system events. Here are a few commonly used ones:
| Event Name | Description | Example Context Data |
|---|---|---|
SystemStart |
Fired when the MSDN runtime starts. | {} (empty object) |
DataChangeEvent |
Fired when a registered data point changes. | { key: string, oldValue: any, newValue: any } |
UserLoginEvent |
Fired when a user successfully logs in. | { userId: string, timestamp: Date } |
ConfigurationLoadedEvent |
Fired after application configuration is loaded. | { configPath: string } |
Creating Custom Events
You can define and dispatch your own custom events using the dispatchEvent function.
// Define a custom event
const MY_CUSTOM_EVENT = "MyApplicationEvent";
// Dispatch the event
dispatchEvent(MY_CUSTOM_EVENT, { message: "Something important happened!" });
// Define a trigger for the custom event
trigger CustomEventHandler on MY_CUSTOM_EVENT do {
console.log("Custom event received:", event.message);
}