Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
WofWca opened this issue Aug 14, 2023 · 0 comments
Open

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

WofWca opened this issue Aug 14, 2023 · 0 comments

Comments

@WofWca
Copy link
Contributor

WofWca commented Aug 14, 2023

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`);
});
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant