Release Vulpes OS Preview 26 with Gecko 161.0 and Tundra 0.2.0
This commit is contained in:
parent
3a1fba4efe
commit
e241a42d2f
1 changed files with 1305 additions and 331 deletions
|
|
@ -1,8 +1,10 @@
|
|||
// Local alarms survive host restarts. They cannot wake a powered-off PC.
|
||||
export class Alarms {
|
||||
constructor(identity, launch, storage, schedule) {
|
||||
constructor(identity, launch, storage, schedule, cancelTimer) {
|
||||
this.storage = storage;
|
||||
this.schedule = schedule;
|
||||
this.cancelTimer = cancelTimer;
|
||||
this.timer = null;
|
||||
this.identity = identity;
|
||||
this.launch = launch;
|
||||
this.listeners = new Map();
|
||||
|
|
@ -11,15 +13,25 @@ export class Alarms {
|
|||
}
|
||||
async init() {
|
||||
this.state = (await this.storage.load()) || { nextId: 1, records: [], pending: [] };
|
||||
this.timer = this.schedule(() => this.serial(() => this.tick()).catch(console.error), 1000);
|
||||
this.updateTimer();
|
||||
}
|
||||
updateTimer() {
|
||||
if (this.state.records.length || this.state.pending.length || this.pendingDeliverySave) {
|
||||
if (this.timer === null) this.timer = this.schedule(() => this.serial(() => this.tick()).catch(console.error), 1000);
|
||||
} else if (this.timer !== null) {
|
||||
this.cancelTimer?.(this.timer);
|
||||
this.timer = null;
|
||||
}
|
||||
}
|
||||
serial(fn) {
|
||||
const p = this.queue.then(fn);
|
||||
this.queue = p.catch(() => {});
|
||||
return p;
|
||||
}
|
||||
save() {
|
||||
return this.storage.save(this.state);
|
||||
async commit(next) {
|
||||
await this.storage.save(next);
|
||||
this.state = next;
|
||||
this.updateTimer();
|
||||
}
|
||||
cleanup(actor) {
|
||||
this.listeners.delete(actor);
|
||||
|
|
@ -42,11 +54,11 @@ export class Alarms {
|
|||
}));
|
||||
if (operation === 'add') {
|
||||
if (
|
||||
!Number.isFinite(date) ||
|
||||
!Number.isFinite(+new Date(date)) || !Number.isFinite(date) ||
|
||||
!['honorTimezone', 'ignoreTimezone'].includes(respectTimezone) ||
|
||||
!Array.isArray(local) ||
|
||||
local.length !== 7 ||
|
||||
!local.every(Number.isFinite)
|
||||
!local.every(Number.isInteger) || !Number.isFinite(+new Date(...local))
|
||||
)
|
||||
throw new Error('InvalidStateError');
|
||||
if (this.state.records.filter((r) => r.owner === app.id).length >= 256)
|
||||
|
|
@ -55,19 +67,18 @@ export class Alarms {
|
|||
if (encodedData.length > 65536) throw new Error('QuotaExceededError');
|
||||
const record = {
|
||||
owner: app.id,
|
||||
id: this.state.nextId++,
|
||||
id: this.state.nextId,
|
||||
date,
|
||||
respectTimezone,
|
||||
data: JSON.parse(encodedData),
|
||||
local,
|
||||
};
|
||||
this.state.records.push(record);
|
||||
await this.save();
|
||||
await this.commit({...this.state, nextId: record.id + 1, records: [...this.state.records, record]});
|
||||
return record.id;
|
||||
}
|
||||
if (operation === 'remove') {
|
||||
this.state.records = this.state.records.filter((r) => r.owner !== app.id || r.id !== id);
|
||||
await this.save();
|
||||
const keep = r => r.owner !== app.id || r.id !== id;
|
||||
await this.commit({...this.state, records: this.state.records.filter(keep), pending: this.state.pending.filter(keep)});
|
||||
return null;
|
||||
}
|
||||
if (operation === 'subscribe') {
|
||||
|
|
@ -92,9 +103,10 @@ export class Alarms {
|
|||
async tick() {
|
||||
const ready = this.state.records.filter((r) => this.due(r) <= Date.now());
|
||||
if (ready.length) {
|
||||
this.state.records = this.state.records.filter((r) => !ready.includes(r));
|
||||
this.state.pending.push(...ready.map((r) => ({ ...r, date: this.due(r) })));
|
||||
await this.save();
|
||||
await this.commit({...this.state,
|
||||
records: this.state.records.filter(r => !ready.includes(r)),
|
||||
pending: this.state.pending.concat(ready.map(r => ({...r, date: this.due(r)}))),
|
||||
});
|
||||
}
|
||||
let changed = false;
|
||||
for (const record of [...this.state.pending]) {
|
||||
|
|
@ -112,11 +124,19 @@ export class Alarms {
|
|||
} catch (_) {
|
||||
this.listeners.delete(receiver[0]);
|
||||
}
|
||||
} else if (Date.now() - (this.lastLaunch.get(record.owner) || 0) > 5000) {
|
||||
this.lastLaunch.set(record.owner, Date.now());
|
||||
} else {
|
||||
const now = Date.now();
|
||||
const previous = this.lastLaunch.get(record.owner);
|
||||
if (now >= previous && now - previous <= 5000) continue;
|
||||
this.lastLaunch.set(record.owner, now);
|
||||
this.launch(record.owner);
|
||||
}
|
||||
}
|
||||
if (changed) await this.save();
|
||||
if (changed) this.pendingDeliverySave = true;
|
||||
if (this.pendingDeliverySave) {
|
||||
await this.storage.save(this.state);
|
||||
this.pendingDeliverySave = false;
|
||||
this.updateTimer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue