Skip to content

"unload" events are counted incorrectly when using multiple devices #23

Open
@WofWca

Description

@WofWca

Reproduction

  1. Send the test app to "saved messages"
  2. Open-close it a couple of times. You will see beforeunload (or visibilitychange) event count go up.
  3. Open the app on another device. You will see that "webxdc.sendUpdate() worked..." is much higher than the corresponding event count.

This is because localStorage is per-device, and webxdc.setUpdateListener is shared.

The solution would be to add a unique ID for each device and only count webxdc updates from the current device in this piece of code:

webxdc-test/js/unload.js

Lines 14 to 48 in 72adcdc

window.anotherWebxdcUpdateListener = function (update) {
if (!update.payload?.isEventCounterUpdate) {
return;
}
const { eventName } = update.payload;
const newCount =
eventNameToNumWebxdcUpdatesFromIt[eventName] += 1;
updateWebxdcUpdatesFromListenerElement(eventName, newCount);
}
events.forEach(eventName => {
container.append(
h("strong", {}, eventName), " triggered ", h("strong", {id: eventName + "-counter"}, getInt(`docEvent.${eventName}`)), " times",
", webxdc.sendUpdate() worked ",
h("strong", { id: eventName + "-counter-sendUpdate" }, ''),
" times",
h("br"),
);
updateWebxdcUpdatesFromListenerElement(
eventName,
eventNameToNumWebxdcUpdatesFromIt[eventName]
);
window.addEventListener(eventName, () => {
let storageKey = `docEvent.${eventName}`;
let count = getInt(storageKey) + 1;
window.localStorage.setItem(storageKey, count);
document.getElementById(eventName + "-counter").innerHTML = count;
webxdc.sendUpdate({
payload: {
isEventCounterUpdate: true,
eventName: eventName,
}
}, `${eventName} event counter`);
});
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions