summaryrefslogtreecommitdiff
path: root/web-timeplot/src/data/source-registry.js
diff options
context:
space:
mode:
authorgrothedev <grothedev@gmail.com>2026-05-29 21:34:16 -0400
committergrothedev <grothedev@gmail.com>2026-05-29 21:34:16 -0400
commit27dc5849c3eaf4824d79938e7077abdbe2c82e24 (patch)
tree4a6e963d291132ad6f5a22841ea2404b60949366 /web-timeplot/src/data/source-registry.js
parent73d75835e18a33c7f6c1b09bbcef93b16a7a9bfa (diff)
updates from claude. need to review. archiving rust and cpp stuff, going completely TS
Diffstat (limited to 'web-timeplot/src/data/source-registry.js')
-rw-r--r--web-timeplot/src/data/source-registry.js85
1 files changed, 67 insertions, 18 deletions
diff --git a/web-timeplot/src/data/source-registry.js b/web-timeplot/src/data/source-registry.js
index 06f5895..917d06b 100644
--- a/web-timeplot/src/data/source-registry.js
+++ b/web-timeplot/src/data/source-registry.js
@@ -1,41 +1,90 @@
+import { CsvReplaySource } from './csv-replay-source.js';
import { SyntheticWaveSource } from './synthetic-wave-source.js';
+import { WebSocketSource } from './websocket-source.js';
export class SourceRegistry {
constructor(store, bus) {
this.store = store;
this.bus = bus;
- this.sources = new Map([
- ['synthetic-wave', new SyntheticWaveSource(store.getState().source)],
- ]);
- this.activeSource = this.sources.get(store.getState().source.activeId);
- this.activeSource.start(store.getState().time.plotTimeMs);
+ this.sources = new Map();
+ this.syncFromState();
}
syncFromState() {
const state = this.store.getState();
- const nextSource = this.sources.get(state.source.activeId);
+ const sourceEntries = Object.entries(state.sources);
+ const activeKeys = new Set(sourceEntries.map(([sourceKey]) => sourceKey));
- if (nextSource !== this.activeSource) {
- this.activeSource?.stop();
- this.activeSource = nextSource;
- this.activeSource?.start(state.time.plotTimeMs);
+ for (const [sourceKey, config] of sourceEntries) {
+ const existingSource = this.sources.get(sourceKey);
+
+ if (!existingSource) {
+ const nextSource = this.createSource(sourceKey, config);
+ this.sources.set(sourceKey, nextSource);
+ nextSource.start(state.time.plotTimeMs);
+ continue;
+ }
+
+ if (existingSource.sourceType !== config.type) {
+ existingSource.stop();
+ const replacementSource = this.createSource(sourceKey, config);
+ this.sources.set(sourceKey, replacementSource);
+ replacementSource.start(state.time.plotTimeMs);
+ continue;
+ }
+
+ existingSource.updateConfig(config);
}
- this.activeSource?.updateConfig(state.source);
+ for (const [sourceKey, source] of this.sources.entries()) {
+ if (!activeKeys.has(sourceKey)) {
+ source.stop();
+ this.sources.delete(sourceKey);
+ }
+ }
}
- update(currentPlotTimeMs) {
- if (!this.activeSource) {
- return;
+ createSource(sourceKey, config) {
+ switch (config.type) {
+ case 'csv-replay':
+ return new CsvReplaySource(config);
+ case 'websocket':
+ return new WebSocketSource(config, {
+ onStatusChange: (statusPatch) => {
+ this.store.setState((state) => ({
+ ...state,
+ sources: {
+ ...state.sources,
+ [sourceKey]: {
+ ...state.sources[sourceKey],
+ ...statusPatch,
+ },
+ },
+ }));
+ },
+ });
+ case 'synthetic-wave':
+ default:
+ return new SyntheticWaveSource(config);
}
+ }
- const points = this.activeSource.update(currentPlotTimeMs);
- for (const point of points) {
- this.bus.emit('data:point', point);
+ update(currentPlotTimeMs) {
+ for (const [sourceKey, source] of this.sources.entries()) {
+ const points = source.update(currentPlotTimeMs);
+ for (const point of points) {
+ this.bus.emit('data:point', {
+ ...point,
+ sourceId: sourceKey,
+ });
+ }
}
}
reset() {
- this.activeSource?.reset(this.store.getState().time.plotTimeMs);
+ const startTimeMs = this.store.getState().time.plotTimeMs;
+ for (const source of this.sources.values()) {
+ source.reset(startTimeMs);
+ }
}
}