This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { serviceDb, type ServiceDoc } from "./dbs";
|
||||
|
||||
const SERVICE_DOC_ID = "_local/service:message-delivery-method-web-push";
|
||||
|
||||
export class ServiceSeqTracker {
|
||||
private serviceDoc: ServiceDoc;
|
||||
|
||||
async getSince() {
|
||||
this.serviceDoc = await serviceDb.get(SERVICE_DOC_ID).catch((error) => {
|
||||
if (error.name === "not_found") {
|
||||
return {
|
||||
_id: SERVICE_DOC_ID,
|
||||
lastSeq: "0",
|
||||
};
|
||||
}
|
||||
throw error; // Re-throw other errors
|
||||
});
|
||||
|
||||
this.serviceDoc.lastSeq = 0;
|
||||
|
||||
return this.serviceDoc.lastSeq;
|
||||
}
|
||||
|
||||
checkNextSeq(seq: number | string) {
|
||||
const next = parseInt(seq as string, 10);
|
||||
const current = parseInt(this.serviceDoc.lastSeq as string, 10);
|
||||
if (next < current) {
|
||||
throw new Error(
|
||||
`Sequence number is too old: ${seq} (${next} < ${current})`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
saveSeq(seq: number | string) {
|
||||
this.serviceDoc.lastSeq = seq;
|
||||
return serviceDb.put(this.serviceDoc);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user