From 72c9ee93151d75b8fb7475f2108ca04245c86078 Mon Sep 17 00:00:00 2001 From: "Christian W. Damus" Date: Fri, 18 Oct 2024 15:16:50 -0400 Subject: [PATCH] Support removal of selection observers (#125) Because Perfetto instances can be reused in Sokatoa, we need to be able to remove selection observers that were added from the containing application context. Signed-off-by: Christian W. Damus --- ui/src/common/selection_observer.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ui/src/common/selection_observer.ts b/ui/src/common/selection_observer.ts index 0bd284ceba..5e4b231db9 100644 --- a/ui/src/common/selection_observer.ts +++ b/ui/src/common/selection_observer.ts @@ -26,7 +26,18 @@ export function onSelectionChanged( } } +// Add a selection-changed observer. +// Returns a function that removes the observer. export function addSelectionChangeObserver(observer: SelectionChangedObserver): - void { + () => void { selectionObservers.push(observer); + return () => removeSelectionChangeObserver(observer); +} + +function removeSelectionChangeObserver(observer: SelectionChangedObserver): + void { + const index = selectionObservers.indexOf(observer); + if (index >= 0) { + selectionObservers.splice(index, 1); + } }