fix(segmentation): Allow for debounced pub/sub events to be reliably cancelled when unsubscribed to. (#5092)

This commit is contained in:
Joe Boccanfuso authored and GitHub committed 2025-06-02 12:12:31 -04:00
1 parent a1fb7217bd
commit 38ed7ba744
5 files changed
+36 -9

No files matched your search

@@ -9,7 +9,11 @@ export function setupSegmentationDataModifiedHandler({
customizationService,
commandsManager,
}) {
const { unsubscribe } = segmentationService.subscribeDebounced(
// A flag to indicate if the event is unsubscribed to. This is important because
// the debounced callback does an await and in that period of time the event may have
// been unsubscribed.
let isUnsubscribed = false;
const { unsubscribe: debouncedUnsubscribe } = segmentationService.subscribeDebounced(
segmentationService.EVENTS.SEGMENTATION_DATA_MODIFIED,
async ({ segmentationId }) => {
const segmentation = segmentationService.getSegmentation(segmentationId);
@@ -42,7 +46,7 @@ export function setupSegmentationDataModifiedHandler({
readableText,
});
if (updatedSegmentation) {
if (!isUnsubscribed && updatedSegmentation) {
segmentationService.addOrUpdateSegmentation({
segmentationId,
segments: updatedSegmentation.segments,
@@ -52,6 +56,10 @@ export function setupSegmentationDataModifiedHandler({
1000
);
const unsubscribe = () => {
isUnsubscribed = true;
debouncedUnsubscribe();
};
return { unsubscribe };
}