Events and Cancellation
Your application learns about edits, calculations and file operations through public events, rather than by inspecting private UI state. Use them for the behavior of your document and application. Workbook notifications and file-operation notifications have different lifecycles, and only cancelable before-events let you cancel an attempted change.
Subscribe to Workbook Events
The following code logs the source of each workbook change and a message when an export has completed. editor is the Spreadsheet of the browser editor, created as in JavaScript:
const unsubscribe = editor.workbook.onChange(change => {
console.log(change.source);
});
const offSaved = editor.workbook.events.on('saved', event => {
console.log('Export completed', event);
});
// Call unsubscribe() and offSaved() when the application no longer needs them.
The code works as follows:
- Workbook.onChange registers a listener for completed document changes. Each change names its source.
- Workbook.events returns the WorkbookEvents of the workbook, its typed subscriptions.
- WorkbookEvents.on registers a listener for the
savedevent and returns an idempotent unsubscribe function. unsubscribeandoffSavedare cleanup functions: subscription methods return them. Call them when your application no longer needs the listeners.
Use the actual typed WorkbookEventMap names for editing, mutation, cell interaction, calculation, loading and saving, progress and failures. events.once(), WorkbookEvents.once, supports a one-time listener.
How Before-Events and After-Events Work
- Cancellation. Only cancelable before-events honor cancellation.
- Success. A successful loaded or saved event occurs after the operation has produced its result.
- Failure. Failed and canceled operations report failure rather than success.
- Listener errors are reported separately and do not retroactively undo a completed successful transaction.
Cancel Long-Running Operations
Pass an AbortSignal to JavaScript operations that support it, and a CancellationToken to .NET operations that expose it. A request that is canceled while it waits for capacity is different from a completed export.
Warning
Unawaited Operations. Do not dispose an owner while an unawaited operation still uses it.
Handle Events in Razor and Angular
The adapters of the editor raise their own events:
- ASP.NET Core Razor. Initialization uses the bubbling DOM events
txsheetcontrolreadyandtxsheetcontrolerror, as in ASP.NET Core. - Angular. TXSheetControlComponent provides outputs for ready and error, workbook changes, the file lifecycle and progress, as in Angular.
These adapter events expose the same public concepts of the control and the workbook; they are not a second document model.
Keep Handlers Small
Keep event handlers small, and avoid recursive mutations without guards. Release subscriptions with the lifetime of the view.
Next Steps
- Lifetime and Concurrency: understand the ownership of documents and handles.
- Backend Services: process workbooks without a browser in .NET or Node.js.
- WorkbookEventMap: look up every typed workbook event in the event reference.
- Batch Updates, Undo and Redo: group edits so that one change notification covers them.