Skip to content

Commit

Permalink
Only increment widget on widgetclick or push
Browse files Browse the repository at this point in the history
  • Loading branch information
amandabaker committed May 21, 2024
1 parent bfa5a49 commit 8e46aed
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions widgets/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,25 @@ const kIncrementTemplate = {
version: '1.5',
};

const incrementData = async () => {
// Accepts a bool |increment| indicating whether the value should be
// incremented when getting refreshed data.
const getIncrementData = async (increment) => {
// get the stored count
const count = await getAndIncrementCount();
let count;
if (increment) {
count = await getAndIncrementCount();
} else {
count = await getCount();
}
return { count };
};

const incrementPayload = async () => {
// Accepts a bool |increment| indicating whether the value should be
// incremented when getting a refreshed payload.
const getIncrementPayload = async (increment) => {
return {
template: JSON.stringify(kIncrementTemplate),
data: JSON.stringify(await incrementData()),
data: JSON.stringify(await getIncrementData(increment)),
};
};

Expand Down Expand Up @@ -88,7 +97,7 @@ const incrementWidgetclick = async () => {
self.addEventListener('widgetclick', (event) => {
logWidgetEvent('widgetclick', Date.now());
if (event.action === kIncrementActionVerb) {
event.waitUntil(updateDefaultWidget());
event.waitUntil(updateDefaultWidget(true));
} else if (event.action === kOpenAppActionVerb) {
event.waitUntil(openApp());
}
Expand All @@ -109,7 +118,7 @@ self.addEventListener('widgetinstall', (event) => {
return;
}

event.waitUntil(updateDefaultWidget());
event.waitUntil(updateDefaultWidget(false));

});

Expand All @@ -120,7 +129,7 @@ self.addEventListener('widgetuninstall', (event) => {
return;
}

event.waitUntil(updateDefaultWidget());
event.waitUntil(updateDefaultWidget(false));
});

self.addEventListener('widgetresume', (event) => {
Expand All @@ -130,7 +139,7 @@ self.addEventListener('widgetresume', (event) => {
return;
}

event.waitUntil(updateDefaultWidget());
event.waitUntil(updateDefaultWidget(false));
});


Expand Down Expand Up @@ -240,8 +249,10 @@ const updateByInstanceId = async (instanceId, payload) => {
}
};

const updateDefaultWidget = async () => {
await updateByTag('increment', await incrementPayload());
// Accepts a bool |increment| indicating whether the count should be
// incremented when updating the widget.
const updateDefaultWidget = async (increment) => {
await updateByTag('increment', await getIncrementPayload(increment));
};

const updateTicTacToeWidget = async (event) => {
Expand Down Expand Up @@ -307,7 +318,7 @@ self.addEventListener(
"push",
(event) => {
// Treat a push as an increment.
updateDefaultWidget();
updateDefaultWidget(true);
},
false,
);

0 comments on commit 8e46aed

Please sign in to comment.