Last active
June 6, 2025 23:06
-
-
Save jamwt/c621c2fc041065d6da75eea78af26ca7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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