Tool to send cross-session opencode messages, including as request-response pattern
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

convert sensors to async generators

rektide e7c2a9a3 665b3136

+20 -16
+16 -9
src/sensor/mdns.ts
··· 4 4 export class MdnsSensor implements Sensor { 5 5 private browser?: Browser; 6 6 7 - async discover(timeout = 5000): Promise<Instance[]> { 8 - const instances: Instance[] = []; 7 + async *discover(timeout = 5000): AsyncGenerator<Instance> { 9 8 const bonjour = new Browser(); 9 + this.browser = bonjour; 10 10 11 - this.browser = bonjour; 11 + const timer = setTimeout(() => { 12 + this.stop(); 13 + }, timeout); 12 14 13 - return new Promise((resolve) => { 14 - setTimeout(() => { 15 - this.stop(); 16 - resolve(instances); 17 - }, timeout); 15 + try { 16 + const instances: Instance[] = []; 18 17 19 18 bonjour.find({ type: "http", protocol: "tcp" }, (service) => { 20 19 if (!service.name.includes("opencode")) { ··· 29 28 }); 30 29 } 31 30 }); 32 - }); 31 + 32 + await new Promise((resolve) => setTimeout(resolve, timeout)); 33 + 34 + for (const instance of instances) { 35 + yield instance; 36 + } 37 + } finally { 38 + clearTimeout(timer); 39 + } 33 40 } 34 41 35 42 stop(): void {
+3 -6
src/sensor/proc.ts
··· 80 80 } 81 81 82 82 export class ProcSensor implements Sensor { 83 - async discover(): Promise<Instance[]> { 83 + async *discover(): AsyncGenerator<Instance> { 84 84 const processes = await getOpenCodeProcesses(); 85 - const instances: Instance[] = []; 86 85 87 86 for (const proc of processes) { 88 87 const port = extractPortFromArgs(proc.cmdline); 89 - instances.push({ 88 + yield { 90 89 port: port ?? 4096, 91 90 pid: proc.pid, 92 91 cwd: proc.cwd, 93 92 source: "proc", 94 - }); 93 + }; 95 94 } 96 - 97 - return instances; 98 95 } 99 96 }
+1 -1
src/sensor/trait.ts
··· 7 7 } 8 8 9 9 export interface Sensor { 10 - discover(timeout?: number): Promise<Instance[]>; 10 + discover(timeout?: number): AsyncGenerator<Instance>; 11 11 stop?(): void; 12 12 }