Send events to JavaScript
An event contract lets C# push typed notifications to JavaScript without waiting for a response.
Declare the event
using Vidra.Bridge;
namespace MyApp;
public record SyncStatus(string State, int Completed);
[BridgeEventContract("sync")]
public interface ISyncEvents
{
[BridgeEvent("statusChanged")]
void StatusChanged(SyncStatus payload);
}
The source generator creates a typed SyncEvents.StatusChanged token in C#.
vidra-codegen creates the matching TypeScript subscription method.
Register the event
Register the generated event name with the bridge:
.UseVidra(dispatcher =>
{
dispatcher.RegisterEvents(
SyncEvents.StatusChanged.Contract,
SyncEvents.StatusChanged.Member);
})
Registration advertises the event in the app contract manifest.
Emit from C#
From a VidraPage, send the generated token and payload:
await Bridge.SendEventAsync(
SyncEvents.StatusChanged,
new SyncStatus("uploading", 42));
Code outside the page can receive an IJsCallbackChannel and call the same
typed API.
Subscribe from JavaScript
The generated proxy follows the on<EventName> convention:
import { sync } from "./generated/index.js";
const unsubscribe = sync.onStatusChanged((status) => {
console.log(status.state, status.completed);
});
// Later:
unsubscribe();
The handler receives a generated SyncStatus type. Returning the unsubscribe
function from a component cleanup prevents duplicate subscriptions.
Built-in events
Built-in modules expose events through the same generated API:
import { appWindow, connectivity } from "@vidra-dev/sdk";
const stopConnectivity = connectivity.onChanged((status) => {
console.log(status.access, status.profiles);
});
const stopResize = appWindow.onResized((windowInfo) => {
console.log(windowInfo.width, windowInfo.height);
});
See MAUI Essentials and Runtime events for available built-in events.