Fix various read sync inconsistencies
This commit is contained in:
parent
5075fa241f
commit
f5a3d4bc8a
3 changed files with 79 additions and 59 deletions
|
@ -1,30 +1,41 @@
|
|||
// Copyright 2021 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
/* eslint-disable no-restricted-syntax */
|
||||
|
||||
import * as Errors from '../types/errors';
|
||||
|
||||
type EntryType = Readonly<{
|
||||
value: number;
|
||||
callback(): void;
|
||||
}>;
|
||||
|
||||
export class StartupQueue {
|
||||
set: Set<string>;
|
||||
private readonly map = new Map<string, EntryType>();
|
||||
|
||||
items: Array<() => void>;
|
||||
|
||||
constructor() {
|
||||
this.set = new Set();
|
||||
this.items = [];
|
||||
}
|
||||
|
||||
add(id: string, f: () => void): void {
|
||||
if (this.set.has(id)) {
|
||||
public add(id: string, value: number, f: () => void): void {
|
||||
const existing = this.map.get(id);
|
||||
if (existing && existing.value >= value) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.items.push(f);
|
||||
this.set.add(id);
|
||||
this.map.set(id, { value, callback: f });
|
||||
}
|
||||
|
||||
flush(): void {
|
||||
const { items } = this;
|
||||
window.log.info('StartupQueue: Processing', items.length, 'actions');
|
||||
items.forEach(f => f());
|
||||
this.items = [];
|
||||
this.set.clear();
|
||||
public flush(): void {
|
||||
window.log.info('StartupQueue: Processing', this.map.size, 'actions');
|
||||
|
||||
const values = Array.from(this.map.values());
|
||||
this.map.clear();
|
||||
|
||||
for (const { callback } of values) {
|
||||
try {
|
||||
callback();
|
||||
} catch (error) {
|
||||
window.log.error(
|
||||
'StartupQueue: Failed to process item due to error',
|
||||
Errors.toLogFormat(error)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue