GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(app): performance improvements through event batching · f2dmarques/opencode@001b486

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/app/src/context
    • global-sdk.tsx
    • global-sync.tsx
    • local.tsx
    • notification.tsx
    • sdk.tsx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/app/src/context/global-sdk.tsx‎

Copy file name to clipboardExpand all lines: packages/app/src/context/global-sdk.tsx
+69-4Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createOpencodeClient, type Event } from "@opencode-ai/sdk/v2/client"
22
import { createSimpleContext } from "@opencode-ai/ui/context"
33
import { createGlobalEmitter } from "@solid-primitives/event-bus"
4-
import { onCleanup } from "solid-js"
4+
import { batch, onCleanup } from "solid-js"
55
import { usePlatform } from "./platform"
66
import { useServer } from "./server"
77

@@ -19,14 +19,79 @@ export const { use: useGlobalSDK, provider: GlobalSDKProvider } = createSimpleCo
1919
[key: string]: Event
2020
}>()
2121

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+
2266
void (async () => {
2367
const events = await eventSdk.global.event()
68+
let yielded = Date.now()
2469
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))
2686
}
27-
})().catch(() => undefined)
87+
})()
88+
.finally(stop)
89+
.catch(() => undefined)
2890

29-
onCleanup(() => abort.abort())
91+
onCleanup(() => {
92+
abort.abort()
93+
stop()
94+
})
3095

3196
const platform = usePlatform()
3297
const sdk = createOpencodeClient({
Collapse file

‎packages/app/src/context/global-sync.tsx‎

Copy file name to clipboardExpand all lines: packages/app/src/context/global-sync.tsx
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { Binary } from "@opencode-ai/util/binary"
2323
import { retry } from "@opencode-ai/util/retry"
2424
import { useGlobalSDK } from "./global-sdk"
2525
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"
2727
import { showToast } from "@opencode-ai/ui/toast"
2828
import { getFilename } from "@opencode-ai/util/path"
2929

@@ -212,7 +212,7 @@ function createGlobalSync() {
212212
.catch((e) => setGlobalStore("error", e))
213213
}
214214

215-
globalSDK.event.listen((e) => {
215+
const unsub = globalSDK.event.listen((e) => {
216216
const directory = e.name
217217
const event = e.details
218218

@@ -404,6 +404,7 @@ function createGlobalSync() {
404404
}
405405
}
406406
})
407+
onCleanup(unsub)
407408

408409
async function bootstrap() {
409410
const health = await globalSDK.client.global
Collapse file

‎packages/app/src/context/local.tsx‎

Copy file name to clipboardExpand all lines: packages/app/src/context/local.tsx
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createStore, produce, reconcile } from "solid-js/store"
2-
import { batch, createMemo } from "solid-js"
2+
import { batch, createMemo, onCleanup } from "solid-js"
33
import { filter, firstBy, flat, groupBy, mapValues, pipe, uniqueBy, values } from "remeda"
44
import type { FileContent, FileNode, Model, Provider, File as FileStatus } from "@opencode-ai/sdk/v2"
55
import { createSimpleContext } from "@opencode-ai/ui/context"
@@ -465,7 +465,7 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
465465
const searchFilesAndDirectories = (query: string) =>
466466
sdk.client.find.files({ query, dirs: "true" }).then((x) => x.data!)
467467

468-
sdk.event.listen((e) => {
468+
const unsub = sdk.event.listen((e) => {
469469
const event = e.details
470470
switch (event.type) {
471471
case "file.watcher.updated":
@@ -475,6 +475,7 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
475475
break
476476
}
477477
})
478+
onCleanup(unsub)
478479

479480
return {
480481
node: async (path: string) => {
Collapse file

‎packages/app/src/context/notification.tsx‎

Copy file name to clipboardExpand all lines: packages/app/src/context/notification.tsx
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createStore } from "solid-js/store"
2+
import { onCleanup } from "solid-js"
23
import { createSimpleContext } from "@opencode-ai/ui/context"
34
import { useGlobalSDK } from "./global-sdk"
45
import { useGlobalSync } from "./global-sync"
@@ -54,7 +55,7 @@ export const { use: useNotification, provider: NotificationProvider } = createSi
5455
}),
5556
)
5657

57-
globalSDK.event.listen((e) => {
58+
const unsub = globalSDK.event.listen((e) => {
5859
const directory = e.name
5960
const event = e.details
6061
const base = {
@@ -104,6 +105,7 @@ export const { use: useNotification, provider: NotificationProvider } = createSi
104105
}
105106
}
106107
})
108+
onCleanup(unsub)
107109

108110
return {
109111
ready,
Collapse file

‎packages/app/src/context/sdk.tsx‎

Copy file name to clipboardExpand all lines: packages/app/src/context/sdk.tsx
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createOpencodeClient, type Event } from "@opencode-ai/sdk/v2/client"
22
import { createSimpleContext } from "@opencode-ai/ui/context"
33
import { createGlobalEmitter } from "@solid-primitives/event-bus"
4+
import { onCleanup } from "solid-js"
45
import { useGlobalSDK } from "./global-sdk"
56
import { usePlatform } from "./platform"
67

@@ -20,9 +21,10 @@ export const { use: useSDK, provider: SDKProvider } = createSimpleContext({
2021
[key in Event["type"]]: Extract<Event, { type: key }>
2122
}>()
2223

23-
globalSDK.event.on(props.directory, async (event) => {
24+
const unsub = globalSDK.event.on(props.directory, (event) => {
2425
emitter.emit(event.type, event)
2526
})
27+
onCleanup(unsub)
2628

2729
return { directory: props.directory, client: sdk, event: emitter, url: globalSDK.url }
2830
},

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.