-
Notifications
You must be signed in to change notification settings - Fork 537
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
off()
method to Listenable event interface (#23046)
## Description This exposes the `off()` method on the `Listenable` event interface, providing an additional way to unsubscribe from events. The current way of unsubscribing, via the deregistration function returned by `on()`, is still supported. Having both options available is meant to give customers options without being opinionated about the best pattern, as it depends on preference and use case. To prevent the two strategies from interfering with each other, it has also been made illegal to register the same listener more than once for the same event. It is now documented as throwing an error rather than as undefined behavior. This also adds a test for registering events with keys that are JS symbols and does some minor doc cleanup.
- Loading branch information
Showing
14 changed files
with
218 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
"fluid-framework": minor | ||
"@fluidframework/tree": minor | ||
--- | ||
--- | ||
"section": tree | ||
--- | ||
|
||
SharedTree event listeners that implement `Listenable` now allow deregistration of event listeners via an `off()` function. | ||
|
||
The ability to deregister events via a callback returned by `on()` remains the same. | ||
Both strategies will remain supported and consumers of SharedTree events may choose which method of deregistration they prefer in a given instance. | ||
|
||
```typescript | ||
// The new behavior | ||
function deregisterViaOff(view: TreeView<MySchema>): { | ||
const listener = () => { /* ... */ }; | ||
view.events.on("commitApplied", listener); // Register | ||
view.events.off("commitApplied", listener); // Deregister | ||
} | ||
|
||
// The existing behavior (still supported) | ||
function deregisterViaCallback(view: TreeView<MySchema>): { | ||
const off = view.events.on("commitApplied", () => { /* ... */ }); // Register | ||
off(); // Deregister | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.