mirror of
https://github.com/binwiederhier/ntfy.git
synced 2024-12-14 11:47:33 +00:00
Make async for loops performant using Promise.all
This commit is contained in:
parent
c16da26780
commit
9056d68fc9
2 changed files with 37 additions and 39 deletions
|
@ -21,15 +21,16 @@ class Poller {
|
||||||
async pollAll() {
|
async pollAll() {
|
||||||
console.log(`[Poller] Polling all subscriptions`);
|
console.log(`[Poller] Polling all subscriptions`);
|
||||||
const subscriptions = await subscriptionManager.all();
|
const subscriptions = await subscriptionManager.all();
|
||||||
for (const s of subscriptions) {
|
|
||||||
try {
|
await Promise.all(
|
||||||
// TODO(eslint): Switch to Promise.all
|
subscriptions.map(async (s) => {
|
||||||
// eslint-disable-next-line no-await-in-loop
|
try {
|
||||||
await this.poll(s);
|
await this.poll(s);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(`[Poller] Error polling ${s.id}`, e);
|
console.log(`[Poller] Error polling ${s.id}`, e);
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async poll(subscription) {
|
async poll(subscription) {
|
||||||
|
|
|
@ -5,13 +5,12 @@ class SubscriptionManager {
|
||||||
/** All subscriptions, including "new count"; this is a JOIN, see https://dexie.org/docs/API-Reference#joining */
|
/** All subscriptions, including "new count"; this is a JOIN, see https://dexie.org/docs/API-Reference#joining */
|
||||||
async all() {
|
async all() {
|
||||||
const subscriptions = await db.subscriptions.toArray();
|
const subscriptions = await db.subscriptions.toArray();
|
||||||
await Promise.all(
|
return Promise.all(
|
||||||
subscriptions.map(async (s) => {
|
subscriptions.map(async (s) => ({
|
||||||
// eslint-disable-next-line no-param-reassign
|
...s,
|
||||||
s.new = await db.notifications.where({ subscriptionId: s.id, new: 1 }).count();
|
new: await db.notifications.where({ subscriptionId: s.id, new: 1 }).count(),
|
||||||
})
|
}))
|
||||||
);
|
);
|
||||||
return subscriptions;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(subscriptionId) {
|
async get(subscriptionId) {
|
||||||
|
@ -40,33 +39,31 @@ class SubscriptionManager {
|
||||||
console.log(`[SubscriptionManager] Syncing subscriptions from remote`, remoteSubscriptions);
|
console.log(`[SubscriptionManager] Syncing subscriptions from remote`, remoteSubscriptions);
|
||||||
|
|
||||||
// Add remote subscriptions
|
// Add remote subscriptions
|
||||||
const remoteIds = []; // = topicUrl(baseUrl, topic)
|
const remoteIds = await Promise.all(
|
||||||
for (let i = 0; i < remoteSubscriptions.length; i += 1) {
|
remoteSubscriptions.map(async (remote) => {
|
||||||
const remote = remoteSubscriptions[i];
|
const local = await this.add(remote.base_url, remote.topic, false);
|
||||||
// TODO(eslint): Switch to Promise.all
|
const reservation = remoteReservations?.find((r) => remote.base_url === config.base_url && remote.topic === r.topic) || null;
|
||||||
// eslint-disable-next-line no-await-in-loop
|
|
||||||
const local = await this.add(remote.base_url, remote.topic, false);
|
await this.update(local.id, {
|
||||||
const reservation = remoteReservations?.find((r) => remote.base_url === config.base_url && remote.topic === r.topic) || null;
|
displayName: remote.display_name, // May be undefined
|
||||||
// TODO(eslint): Switch to Promise.all
|
reservation, // May be null!
|
||||||
// eslint-disable-next-line no-await-in-loop
|
});
|
||||||
await this.update(local.id, {
|
|
||||||
displayName: remote.display_name, // May be undefined
|
return local.id;
|
||||||
reservation, // May be null!
|
})
|
||||||
});
|
);
|
||||||
remoteIds.push(local.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove local subscriptions that do not exist remotely
|
// Remove local subscriptions that do not exist remotely
|
||||||
const localSubscriptions = await db.subscriptions.toArray();
|
const localSubscriptions = await db.subscriptions.toArray();
|
||||||
for (let i = 0; i < localSubscriptions.length; i += 1) {
|
|
||||||
const local = localSubscriptions[i];
|
await Promise.all(
|
||||||
const remoteExists = remoteIds.includes(local.id);
|
localSubscriptions.map(async (local) => {
|
||||||
if (!local.internal && !remoteExists) {
|
const remoteExists = remoteIds.includes(local.id);
|
||||||
// TODO(eslint): Switch to Promise.all
|
if (!local.internal && !remoteExists) {
|
||||||
// eslint-disable-next-line no-await-in-loop
|
await this.remove(local.id);
|
||||||
await this.remove(local.id);
|
}
|
||||||
}
|
})
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateState(subscriptionId, state) {
|
async updateState(subscriptionId, state) {
|
||||||
|
|
Loading…
Reference in a new issue