Skip to content

Commit

Permalink
broadcast management
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Jan 28, 2025
1 parent 075eb33 commit 0dc1f67
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ export function SignalStateProvider({ children }) {
useSignalEffect(() => {
if (!service) return console.warn('could not access service');
const unsub = service.onData((evt) => {
const next = normalize(keys.value, evt.data);
batch(() => {
const next = normalize(keys.value, evt.data);
keys.value = next;
activity.value = normalizeItems(activity.value, evt.data);
});
Expand Down
2 changes: 2 additions & 0 deletions special-pages/pages/new-tab/app/activity/activity.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,14 @@ export class ActivityService {
* @return {Promise<import('../../types/new-tab.js').ConfirmBurnResponse>}
*/
confirmBurn(url) {
this.dataService.disableBroadcast();
return this.ntp.messaging.request('activity_confirmBurn', { url });
}
/**
* @param {string} url
*/
burnAnimationComplete(url) {
this.dataService.enableBroadcast();
this.dataService.update((old) => {
return {
...old,
Expand Down
17 changes: 16 additions & 1 deletion special-pages/pages/new-tab/app/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
export class Service {
eventTarget = new EventTarget();
DEBOUNCE_TIME_MS = 200;
_broadcast = true;
/**
* @param {object} props
* @param {() => Promise<Data>} [props.initial]
Expand Down Expand Up @@ -110,9 +111,20 @@ export class Service {
}
}

disableBroadcast() {
this._broadcast = false;
}

/**
*
*/
enableBroadcast() {
this._broadcast = true;
}

/**
* @param {Data} data
* @param {'initial' | 'subscription' | 'manual' | 'trigger-fetch'} source
* @param {'initial' | 'subscription' | 'manual' | 'trigger-fetch' | 'flush'} source
* @private
*/
_accept(data, source) {
Expand All @@ -124,13 +136,16 @@ export class Service {
// always cancel any existing debounced timers
this.clearDebounceTimer();

if (!this._broadcast) return;

// always broadcast the change on the event target
const dataEvent = new CustomEvent('data', {
detail: {
data: this.data,
source,
},
});

this.eventTarget.dispatchEvent(dataEvent);

// try to persist if the last try was 'manual' update
Expand Down

0 comments on commit 0dc1f67

Please sign in to comment.