# Events and Cancellation

> Subscribe to workbook events with onChange and events.on, cancel before-events, pass AbortSignal or CancellationToken and handle Razor and Angular events.

- **Product:** TX Sheet Control
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Sheet Control > Spreadsheet Editor > Events and Cancellation
- **968 words**
- **Web URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/events/
- **LLMs URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/events/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/events/llms-full.txt

---

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](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/spreadsheet/llms-full.txt) of the browser editor, created as in [JavaScript](https://docs.textcontrol.com/tx-sheet-control/getting-started/spreadsheet-editor/javascript/llms-full.txt):

```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](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/onchange-method/llms-full.txt) registers a listener for completed document changes. Each change names its [source](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/workbookchange/source-property/llms-full.txt).
- [Workbook.events](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbook/events-property/llms-full.txt) returns the [WorkbookEvents](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbookevents/llms-full.txt) of the workbook, its typed subscriptions.
- [WorkbookEvents.on](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbookevents/on-method/llms-full.txt) registers a listener for the `saved` event and returns an idempotent unsubscribe function.
- `unsubscribe` and `offSaved` are cleanup functions: subscription methods return them. Call them when your application no longer needs the listeners.

Use the actual typed [WorkbookEventMap](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/workbookeventmap/llms-full.txt) names for editing, mutation, cell interaction, calculation, loading and saving, progress and failures. `events.once()`, [WorkbookEvents.once](https://docs.textcontrol.com/tx-sheet-control/reference/javascript-api/browser/workbookevents/once-method/llms-full.txt), 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 `txsheetcontrolready` and `txsheetcontrolerror`, as in [ASP.NET Core](https://docs.textcontrol.com/tx-sheet-control/getting-started/spreadsheet-editor/aspnet-core/llms-full.txt).
- **Angular.** [TXSheetControlComponent](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/angular/txsheetcontrolcomponent/llms-full.txt) provides outputs for ready and error, workbook changes, the file lifecycle and progress, as in [Angular](https://docs.textcontrol.com/tx-sheet-control/getting-started/spreadsheet-editor/angular/llms-full.txt).

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](https://docs.textcontrol.com/tx-sheet-control/document-processing/lifetime/llms-full.txt): understand the ownership of documents and handles.
- [Backend Services](https://docs.textcontrol.com/tx-sheet-control/document-processing/backend-services/llms-full.txt): process workbooks without a browser in .NET or Node.js.
- [WorkbookEventMap](https://docs.textcontrol.com/tx-sheet-control/reference/typescript-api/browser/workbookeventmap/llms-full.txt): look up every typed workbook event in the event reference.
- [Batch Updates, Undo and Redo](https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/undo-redo/llms-full.txt): group edits so that one change notification covers them.

---

## Nearby

- **Previous:** [Batch Updates, Undo and Redo](https://docs.textcontrol.com/tx-sheet-control/spreadsheet-editor/undo-redo/llms-full.txt)
- **Next:** [Spreadsheet Documents](https://docs.textcontrol.com/tx-sheet-control/spreadsheets/llms-full.txt)
