Skip to content

Commit 3b51fba

Browse files
committed
chore(demo): increase retry attempts with backoff
1 parent 5cbe2c7 commit 3b51fba

6 files changed

Lines changed: 49 additions & 47 deletions

File tree

bun.lock

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/demo/src/services/OneSignalApiService.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ class OneSignalApiService {
8282
...extra,
8383
};
8484

85-
// Retry once on `invalid_player_ids` to absorb the brief race where the
85+
const maxAttempts = 3;
86+
87+
// Retry on `invalid_player_ids` to absorb the brief race where the
8688
// subscription has been created locally but is not yet visible to the
8789
// /notifications endpoint.
88-
for (let attempt = 0; attempt < 2; attempt++) {
90+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
8991
try {
9092
const response = await CapacitorHttp.post({
9193
url: 'https://onesignal.com/api/v1/notifications',
@@ -101,11 +103,10 @@ class OneSignalApiService {
101103
return false;
102104
}
103105

104-
const data = response.data as { errors?: { invalid_player_ids?: unknown } } | undefined;
105-
const invalidIds = data?.errors?.invalid_player_ids;
106+
const invalidIds = response.data?.errors?.invalid_player_ids;
106107
if (Array.isArray(invalidIds) && invalidIds.length > 0) {
107-
if (attempt === 0) {
108-
await new Promise((resolve) => setTimeout(resolve, 3_000));
108+
if (attempt < maxAttempts) {
109+
await new Promise((resolve) => setTimeout(resolve, 3_000 * attempt));
109110
continue;
110111
}
111112
console.error(

examples/demo_cap7/src/app/app.component.ts

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,42 @@ export class AppComponent {
9090
return;
9191
}
9292

93-
const response = await CapacitorHttp.post({
94-
url: 'https://onesignal.com/api/v1/notifications',
95-
headers: {
96-
Accept: 'application/vnd.onesignal.v1+json',
97-
'Content-Type': 'application/json',
98-
},
99-
data: {
100-
app_id: ONESIGNAL_APP_ID,
101-
include_subscription_ids: [subscriptionId],
102-
headings: { en: 'Simple Notification' },
103-
contents: { en: 'This is a simple push notification' },
104-
},
105-
});
106-
107-
if (response.status < 200 || response.status >= 300) {
108-
this.log(`Send failed (${response.status}): ${JSON.stringify(response.data)}`);
93+
const body = {
94+
app_id: ONESIGNAL_APP_ID,
95+
include_subscription_ids: [subscriptionId],
96+
headings: { en: 'Simple Notification' },
97+
contents: { en: 'This is a simple push notification' },
98+
};
99+
const maxAttempts = 3;
100+
101+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
102+
const response = await CapacitorHttp.post({
103+
url: 'https://onesignal.com/api/v1/notifications',
104+
headers: {
105+
Accept: 'application/vnd.onesignal.v1+json',
106+
'Content-Type': 'application/json',
107+
},
108+
data: body,
109+
});
110+
111+
if (response.status < 200 || response.status >= 300) {
112+
this.log(`Send failed (${response.status}): ${JSON.stringify(response.data)}`);
113+
return;
114+
}
115+
116+
const invalidIds = response.data?.errors?.invalid_player_ids;
117+
if (Array.isArray(invalidIds) && invalidIds.length > 0) {
118+
if (attempt < maxAttempts) {
119+
await new Promise((resolve) => setTimeout(resolve, 3_000 * attempt));
120+
continue;
121+
}
122+
this.log(`Send failed: invalid_player_ids ${JSON.stringify(invalidIds)}`);
123+
return;
124+
}
125+
126+
this.log(`Sent. response: ${JSON.stringify(response.data)}`);
109127
return;
110128
}
111-
112-
this.log(`Sent. response: ${JSON.stringify(response.data)}`);
113129
} catch (err) {
114130
this.log(`sendTestNotification error: ${String(err)}`);
115131
} finally {

examples/demo_pods/src/services/OneSignalApiService.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ class OneSignalApiService {
8282
...extra,
8383
};
8484

85-
// Retry once on `invalid_player_ids` to absorb the brief race where the
85+
const maxAttempts = 3;
86+
87+
// Retry on `invalid_player_ids` to absorb the brief race where the
8688
// subscription has been created locally but is not yet visible to the
8789
// /notifications endpoint.
88-
for (let attempt = 0; attempt < 2; attempt++) {
90+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
8991
try {
9092
const response = await CapacitorHttp.post({
9193
url: 'https://onesignal.com/api/v1/notifications',
@@ -101,11 +103,10 @@ class OneSignalApiService {
101103
return false;
102104
}
103105

104-
const data = response.data as { errors?: { invalid_player_ids?: unknown } } | undefined;
105-
const invalidIds = data?.errors?.invalid_player_ids;
106+
const invalidIds = response.data?.errors?.invalid_player_ids;
106107
if (Array.isArray(invalidIds) && invalidIds.length > 0) {
107-
if (attempt === 0) {
108-
await new Promise((resolve) => setTimeout(resolve, 3_000));
108+
if (attempt < maxAttempts) {
109+
await new Promise((resolve) => setTimeout(resolve, 3_000 * attempt));
109110
continue;
110111
}
111112
console.error(

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
"@capacitor/docgen": "^0.2.2",
6161
"@types/bun": "latest",
6262
"@vitest/coverage-v8": "^4.1.2",
63-
"happy-dom": "^20.9.0",
6463
"typescript": "^5.9.3",
6564
"vite-plus": "0.1.20"
6665
},

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default defineConfig({
3232
},
3333
test: {
3434
clearMocks: true,
35-
environment: 'happy-dom',
35+
environment: 'node',
3636
include: ['**/*.test.ts', '**/*.test.tsx'],
3737
coverage: {
3838
exclude: ['mocks/**', 'src/helpers.ts'],

0 commit comments

Comments
 (0)