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.

implement CacheSensor to aggregate and deduplicate sensors

rektide fb64ad1b ee6951e7

+54
+53
src/sensor/cache.ts
··· 1 + import { Instance, Sensor } from "./trait.js"; 2 + 3 + export interface CacheOptions { 4 + sensors: Iterable<Sensor>; 5 + } 6 + 7 + export class CacheSensor implements Sensor { 8 + public sensors: Sensor[]; 9 + private instances: Instance[] = []; 10 + 11 + constructor(options: CacheOptions) { 12 + this.sensors = Array.from(options.sensors); 13 + } 14 + 15 + async *discover(timeout?: number): AsyncGenerator<Instance> { 16 + const seen = new Set<number>(); 17 + 18 + for (const instance of this.instances) { 19 + if (instance.pid !== undefined) { 20 + seen.add(instance.pid); 21 + } 22 + } 23 + 24 + for (const instance of this.instances) { 25 + yield instance; 26 + } 27 + 28 + for (const sensor of this.sensors) { 29 + for await (const instance of sensor.discover(timeout)) { 30 + if (instance.pid !== undefined && seen.has(instance.pid)) { 31 + continue; 32 + } 33 + 34 + if (instance.pid !== undefined) { 35 + seen.add(instance.pid); 36 + } 37 + 38 + this.instances.push(instance); 39 + yield instance; 40 + } 41 + } 42 + } 43 + 44 + getInstances(): Instance[] { 45 + return [...this.instances]; 46 + } 47 + 48 + stop(): void { 49 + for (const sensor of this.sensors) { 50 + sensor.stop?.(); 51 + } 52 + } 53 + }
+1
src/sensor/index.ts
··· 1 1 export { Instance, Sensor } from "./trait.js"; 2 2 export { MdnsSensor } from "./mdns.js"; 3 3 export { ProcSensor } from "./proc.js"; 4 + export { CacheSensor, type CacheOptions } from "./cache.js";