const updates = new Map();
const update = async (key, patch) => {
let current = updates.get(key)
if(!current) {
current = await env.KV.get(key, {
type: "json"
});
}
const next = { ...current, ...patch };
updates.set(key, next);
return next;
};
const flush = async () => {
for(const [key, value] of updates.entries()) {
await env.KV.put(key, JSON.stringify(value));
}
};
const key = "event-xxx";
const event = makeEvent();
// Store initial record that event occured
await update(key, event);
// Procedure 1
const {
complete,
data,
error
} = await proc1(); // (this can fail)
if (complete) {
await update(key, data);
} else {
throw error
};
// Procedure 2 (uses data returned from procedure 1)
const {
complete: complete2,
data: data2,
error: error2
} = await proc2(data); // (this can also fail)
if (complete2) {
await update(key, data2)
} else {
throw error2;
}