Skip to content

Commit bcc68c3

Browse files
committed
fix: make demo prepare actions sequential
1 parent 4f73db7 commit bcc68c3

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

demo/app/home/home-page.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,39 @@ export function onNavigatingTo(args: NavigatedData) {
2121

2222
page.bindingContext = new HomeViewModel();
2323

24+
let preparing = true;
2425
let locationSubscription: Subscription;
25-
2626
Application.on(Application.resumeEvent, () => {
27-
if (!_preparing) {
27+
if (!preparing) {
2828
printCurrentLocation().catch((err) => {
2929
console.error(`Could not print current location: ${err}`);
30-
});
31-
printLocationUpdates()
30+
}).then(() => printLocationUpdates()
3231
.then((subscription) => (locationSubscription = subscription))
3332
.catch(
3433
(err) =>
35-
`An error occurred while getting location updates: ${err}`
36-
);
34+
`An error occurred while getting location updates: ${err}`)
35+
).then(() => listenToActivityChanges());
3736
}
38-
listenToActivityChanges();
3937
});
4038

4139
Application.on(Application.suspendEvent, () => {
42-
if (locationSubscription) {
43-
locationSubscription.unsubscribe();
40+
if (!preparing) {
41+
if (locationSubscription) {
42+
locationSubscription.unsubscribe();
43+
}
44+
stopListeningToChanges();
4445
}
45-
stopListeningToChanges();
4646
});
4747

4848
printCurrentLocation().catch((err) => {
4949
console.error(`Could not print current location: ${err}`);
50-
});
51-
printLocationUpdates()
50+
}).then(() => printLocationUpdates()
5251
.then((subscription) => (locationSubscription = subscription))
5352
.catch(
5453
(err) => `An error occurred while getting location updates: ${err}`
55-
);
56-
listenToActivityChanges(true);
54+
)
55+
).then(() => listenToActivityChanges(true))
56+
.then(() => preparing = false);
5757
}
5858

5959
async function printCurrentLocation() {
@@ -90,14 +90,16 @@ async function printLocationUpdates(): Promise<Subscription> {
9090
});
9191
}
9292

93-
export function listenToActivityChanges(addListener = false) {
94-
activityRecognizers.forEach((recognizerType) => {
95-
listenToActivityChangesFor(recognizerType, addListener).catch((err) => {
93+
export async function listenToActivityChanges(addListener = false) {
94+
for (const recognizerType of activityRecognizers) {
95+
try {
96+
await listenToActivityChangesFor(recognizerType, addListener);
97+
} catch (err) {
9698
console.error(
9799
`An error occurred while listening to ${recognizerType} res activity changes: ${JSON.stringify(err)}`
98100
);
99-
});
100-
});
101+
}
102+
}
101103
}
102104

103105
function stopListeningToChanges() {

0 commit comments

Comments
 (0)