Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/opencode/src/server/routes/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const EventRoutes = () =>
c.header("X-Accel-Buffering", "no")
c.header("X-Content-Type-Options", "nosniff")
return streamSSE(c, async (stream) => {
const q = new AsyncQueue<string | null>()
const q = new AsyncQueue<string | null>(1024)
let done = false

q.push(
Expand Down
2 changes: 1 addition & 1 deletion packages/opencode/src/server/routes/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const GlobalDisposedEvent = BusEvent.define("global.disposed", z.object({

async function streamEvents(c: Context, subscribe: (q: AsyncQueue<string | null>) => () => void) {
return streamSSE(c, async (stream) => {
const q = new AsyncQueue<string | null>()
const q = new AsyncQueue<string | null>(1024)
let done = false

q.push(
Expand Down
10 changes: 8 additions & 2 deletions packages/opencode/src/util/queue.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
export class AsyncQueue<T> implements AsyncIterable<T> {
private queue: T[] = []
private resolvers: ((value: T) => void)[] = []
private readonly capacity: number | undefined

constructor(capacity?: number) {
this.capacity = capacity
}

push(item: T) {
const resolve = this.resolvers.shift()
if (resolve) resolve(item)
else this.queue.push(item)
if (resolve) return resolve(item)
this.queue.push(item)
if (this.capacity !== undefined) while (this.queue.length > this.capacity) this.queue.shift()
}

async next(): Promise<T> {
Expand Down
Loading