Skip to content

Instantly share code, notes, and snippets.

@jamwt
Last active June 6, 2025 23:06
Show Gist options
  • Save jamwt/c621c2fc041065d6da75eea78af26ca7 to your computer and use it in GitHub Desktop.
Save jamwt/c621c2fc041065d6da75eea78af26ca7 to your computer and use it in GitHub Desktop.
jobs: defineTable({
order: v.incremental(),
}).index("by_order", ["order"]),
export const processJobs = mutation({
args: {
cursor: v.incremental(),
},
handler: async (ctx) => {
let cursor = args.cursor;
let nextBatch, waitToken;
do {
nextBatch, waitToken = await ctx.staleQuery("jobs")
.withIndex("by_order", (q) => q.gt(cursor))
.take(100);
// do thing with batch
cursor = nextBatch[nextBatch.length - 1].order;
// probably save cursor
} while (nextBatch.length);
await ctx.scheduler.runOnChange(waitToken, internal.mainLoop.processJobs, {cursor});
},
});
// then if there was some sort of "onLoad mutation"
const onLoad = mutation({
handler: (ctx) => {
// Probably load cursor from wahtever table
await ctx.scheduler.runAfter(0, internal.mainLoop.processJobs, {cursor});
}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment