GeezerVison is showing github.com — fix(app): performance improvements through event batching · f2dmarques/opencode@001b486
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/app/src/context/global-sdk.tsx +69-4Lines changed: 69 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { createOpencodeClient, type Event } from "@opencode-ai/sdk/v2/client" | 2 | 2 | import { createSimpleContext } from "@opencode-ai/ui/context" | 3 | 3 | import { createGlobalEmitter } from "@solid-primitives/event-bus" | 4 | | -import { onCleanup } from "solid-js" | | 4 | +import { batch, onCleanup } from "solid-js" | 5 | 5 | import { usePlatform } from "./platform" | 6 | 6 | import { useServer } from "./server" | 7 | 7 |
| @@ -19,14 +19,79 @@ export const { use: useGlobalSDK, provider: GlobalSDKProvider } = createSimpleCo | 19 | 19 | [key: string]: Event | 20 | 20 | }>() | 21 | 21 |
| | 22 | + type Queued = { directory: string; payload: Event } | | 23 | + | | 24 | + let queue: Array<Queued | undefined> = [] | | 25 | + const coalesced = new Map<string, number>() | | 26 | + let timer: ReturnType<typeof setTimeout> | undefined | | 27 | + let last = 0 | | 28 | + | | 29 | + const key = (directory: string, payload: Event) => { | | 30 | + if (payload.type === "session.status") return `session.status:${directory}:${payload.properties.sessionID}` | | 31 | + if (payload.type === "lsp.updated") return `lsp.updated:${directory}` | | 32 | + if (payload.type === "message.part.updated") { | | 33 | + const part = payload.properties.part | | 34 | + return `message.part.updated:${directory}:${part.messageID}:${part.id}` | | 35 | + } | | 36 | + } | | 37 | + | | 38 | + const flush = () => { | | 39 | + if (timer) clearTimeout(timer) | | 40 | + timer = undefined | | 41 | + | | 42 | + const events = queue | | 43 | + queue = [] | | 44 | + coalesced.clear() | | 45 | + if (events.length === 0) return | | 46 | + | | 47 | + last = Date.now() | | 48 | + batch(() => { | | 49 | + for (const event of events) { | | 50 | + if (!event) continue | | 51 | + emitter.emit(event.directory, event.payload) | | 52 | + } | | 53 | + }) | | 54 | + } | | 55 | + | | 56 | + const schedule = () => { | | 57 | + if (timer) return | | 58 | + const elapsed = Date.now() - last | | 59 | + timer = setTimeout(flush, Math.max(0, 16 - elapsed)) | | 60 | + } | | 61 | + | | 62 | + const stop = () => { | | 63 | + flush() | | 64 | + } | | 65 | + | 22 | 66 | void (async () => { | 23 | 67 | const events = await eventSdk.global.event() | | 68 | + let yielded = Date.now() | 24 | 69 | for await (const event of events.stream) { | 25 | | - emitter.emit(event.directory ?? "global", event.payload) | | 70 | + const directory = event.directory ?? "global" | | 71 | + const payload = event.payload | | 72 | + const k = key(directory, payload) | | 73 | + if (k) { | | 74 | + const i = coalesced.get(k) | | 75 | + if (i !== undefined) { | | 76 | + queue[i] = undefined | | 77 | + } | | 78 | + coalesced.set(k, queue.length) | | 79 | + } | | 80 | + queue.push({ directory, payload }) | | 81 | + schedule() | | 82 | + | | 83 | + if (Date.now() - yielded < 8) continue | | 84 | + yielded = Date.now() | | 85 | + await new Promise<void>((resolve) => setTimeout(resolve, 0)) | 26 | 86 | } | 27 | | - })().catch(() => undefined) | | 87 | + })() | | 88 | + .finally(stop) | | 89 | + .catch(() => undefined) | 28 | 90 |
| 29 | | - onCleanup(() => abort.abort()) | | 91 | + onCleanup(() => { | | 92 | + abort.abort() | | 93 | + stop() | | 94 | + }) | 30 | 95 |
| 31 | 96 | const platform = usePlatform() | 32 | 97 | const sdk = createOpencodeClient({ | |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/context/global-sync.tsx +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,7 +23,7 @@ import { Binary } from "@opencode-ai/util/binary" | 23 | 23 | import { retry } from "@opencode-ai/util/retry" | 24 | 24 | import { useGlobalSDK } from "./global-sdk" | 25 | 25 | import { ErrorPage, type InitError } from "../pages/error" | 26 | | -import { batch, createContext, useContext, onMount, type ParentProps, Switch, Match } from "solid-js" | | 26 | +import { batch, createContext, useContext, onCleanup, onMount, type ParentProps, Switch, Match } from "solid-js" | 27 | 27 | import { showToast } from "@opencode-ai/ui/toast" | 28 | 28 | import { getFilename } from "@opencode-ai/util/path" | 29 | 29 |
| @@ -212,7 +212,7 @@ function createGlobalSync() { | 212 | 212 | .catch((e) => setGlobalStore("error", e)) | 213 | 213 | } | 214 | 214 |
| 215 | | - globalSDK.event.listen((e) => { | | 215 | + const unsub = globalSDK.event.listen((e) => { | 216 | 216 | const directory = e.name | 217 | 217 | const event = e.details | 218 | 218 |
| @@ -404,6 +404,7 @@ function createGlobalSync() { | 404 | 404 | } | 405 | 405 | } | 406 | 406 | }) | | 407 | + onCleanup(unsub) | 407 | 408 |
| 408 | 409 | async function bootstrap() { | 409 | 410 | const health = await globalSDK.client.global | |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/context/local.tsx +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { createStore, produce, reconcile } from "solid-js/store" | 2 | | -import { batch, createMemo } from "solid-js" | | 2 | +import { batch, createMemo, onCleanup } from "solid-js" | 3 | 3 | import { filter, firstBy, flat, groupBy, mapValues, pipe, uniqueBy, values } from "remeda" | 4 | 4 | import type { FileContent, FileNode, Model, Provider, File as FileStatus } from "@opencode-ai/sdk/v2" | 5 | 5 | import { createSimpleContext } from "@opencode-ai/ui/context" | @@ -465,7 +465,7 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({ | 465 | 465 | const searchFilesAndDirectories = (query: string) => | 466 | 466 | sdk.client.find.files({ query, dirs: "true" }).then((x) => x.data!) | 467 | 467 |
| 468 | | - sdk.event.listen((e) => { | | 468 | + const unsub = sdk.event.listen((e) => { | 469 | 469 | const event = e.details | 470 | 470 | switch (event.type) { | 471 | 471 | case "file.watcher.updated": | @@ -475,6 +475,7 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({ | 475 | 475 | break | 476 | 476 | } | 477 | 477 | }) | | 478 | + onCleanup(unsub) | 478 | 479 |
| 479 | 480 | return { | 480 | 481 | node: async (path: string) => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/context/notification.tsx +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { createStore } from "solid-js/store" | | 2 | +import { onCleanup } from "solid-js" | 2 | 3 | import { createSimpleContext } from "@opencode-ai/ui/context" | 3 | 4 | import { useGlobalSDK } from "./global-sdk" | 4 | 5 | import { useGlobalSync } from "./global-sync" | @@ -54,7 +55,7 @@ export const { use: useNotification, provider: NotificationProvider } = createSi | 54 | 55 | }), | 55 | 56 | ) | 56 | 57 |
| 57 | | - globalSDK.event.listen((e) => { | | 58 | + const unsub = globalSDK.event.listen((e) => { | 58 | 59 | const directory = e.name | 59 | 60 | const event = e.details | 60 | 61 | const base = { | @@ -104,6 +105,7 @@ export const { use: useNotification, provider: NotificationProvider } = createSi | 104 | 105 | } | 105 | 106 | } | 106 | 107 | }) | | 108 | + onCleanup(unsub) | 107 | 109 |
| 108 | 110 | return { | 109 | 111 | ready, | |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/context/sdk.tsx +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { createOpencodeClient, type Event } from "@opencode-ai/sdk/v2/client" | 2 | 2 | import { createSimpleContext } from "@opencode-ai/ui/context" | 3 | 3 | import { createGlobalEmitter } from "@solid-primitives/event-bus" | | 4 | +import { onCleanup } from "solid-js" | 4 | 5 | import { useGlobalSDK } from "./global-sdk" | 5 | 6 | import { usePlatform } from "./platform" | 6 | 7 |
| @@ -20,9 +21,10 @@ export const { use: useSDK, provider: SDKProvider } = createSimpleContext({ | 20 | 21 | [key in Event["type"]]: Extract<Event, { type: key }> | 21 | 22 | }>() | 22 | 23 |
| 23 | | - globalSDK.event.on(props.directory, async (event) => { | | 24 | + const unsub = globalSDK.event.on(props.directory, (event) => { | 24 | 25 | emitter.emit(event.type, event) | 25 | 26 | }) | | 27 | + onCleanup(unsub) | 26 | 28 |
| 27 | 29 | return { directory: props.directory, client: sdk, event: emitter, url: globalSDK.url } | 28 | 30 | }, | |
|