Skip to content

Commit

Permalink
refactor: move ProgramInfo to each class
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaRedHand committed Feb 1, 2025
1 parent fd7cc27 commit 989f920
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
27 changes: 23 additions & 4 deletions src/programs/echo_sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ import { Ticker } from "pixi.js";
import { DeviceId } from "../types/graphs/datagraph";
import { sendPacket } from "../types/packet";
import { ProgramBase } from "./program_base";
import { ViewGraph } from "../types/graphs/viewgraph";
import { ProgramInfo } from "../graphics/renderables/device_info";

function adjacentDevices(viewgraph: ViewGraph, srcId: DeviceId) {
const adjacentDevices = viewgraph
.getAdjacentDeviceIds(srcId)
.map((id) => ({ value: id.toString(), text: `Device ${id}` }));

return adjacentDevices;
}

export abstract class EchoSender extends ProgramBase {
protected dstId: DeviceId;

protected _parseInputs(inputs: string[]): void {
if (inputs.length !== 1) {
console.error(
ProgramBase.PROGRAM_NAME +
" requires 1 input. " +
inputs.length +
" were given.",
"Program requires 1 input. " + inputs.length + " were given.",
);
return;
}
Expand All @@ -29,6 +36,12 @@ export class SingleEcho extends EchoSender {
}

protected _stop() {}

static getProgramInfo(viewgraph: ViewGraph, srcId: DeviceId): ProgramInfo {
const programInfo = new ProgramInfo(this.PROGRAM_NAME);
programInfo.withDropdown("Destination", adjacentDevices(viewgraph, srcId));
return programInfo;
}
}

const DEFAULT_ECHO_DELAY_MS = 250;
Expand All @@ -55,4 +68,10 @@ export class EchoServer extends EchoSender {
protected _stop() {
Ticker.shared.remove(this.tick, this);
}

static getProgramInfo(viewgraph: ViewGraph, srcId: DeviceId): ProgramInfo {
const programInfo = new ProgramInfo(this.PROGRAM_NAME);
programInfo.withDropdown("Destination", adjacentDevices(viewgraph, srcId));
return programInfo;
}
}
27 changes: 7 additions & 20 deletions src/programs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ interface ProgramConstructor {
new (viewgraph: ViewGraph, srcId: DeviceId, inputs: string[]): Program;
}

// List of all programs
// List of all programs.
// Each one has to:
// - Implement the Program interface
// - Have a static readonly PROGRAM_NAME property
// - Have a constructor with the signature (viewgraph, srcId, inputs)
// - Have a getProgramInfo static method
const programList = [SingleEcho, EchoServer];

// Map of program name to program constructor
Expand All @@ -51,25 +56,7 @@ export function getProgramList(
viewgraph: ViewGraph,
srcId: DeviceId,
): ProgramInfo[] {
const adjacentDevices = viewgraph
.getDeviceIds()
.filter((adjId) => adjId !== srcId)
.map((id) => ({ value: id.toString(), text: `Device ${id}` }));

const programList = [];

{
const programInfo = new ProgramInfo(SingleEcho.PROGRAM_NAME);
programInfo.withDropdown("Destination", adjacentDevices);
programList.push(programInfo);
}
{
const programInfo = new ProgramInfo(EchoServer.PROGRAM_NAME);
programInfo.withDropdown("Destination", adjacentDevices);
programList.push(programInfo);
}

return programList;
return programList.map((p) => p.getProgramInfo(viewgraph, srcId));
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/programs/program_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { ViewGraph } from "../types/graphs/viewgraph";
* Provides a basic structure for programs to be run.
*/
export abstract class ProgramBase implements Program {
static readonly PROGRAM_NAME: string;

protected viewgraph: ViewGraph;
protected srcId: DeviceId;

Expand All @@ -23,7 +21,7 @@ export abstract class ProgramBase implements Program {

run(signalStop: () => void) {
if (this.signalStop) {
console.error(ProgramBase.PROGRAM_NAME + " already running");
console.error("Program already running");
return;
}
this.signalStop = signalStop;
Expand All @@ -33,7 +31,7 @@ export abstract class ProgramBase implements Program {

stop(): void {
// This function could be useful
console.debug(ProgramBase.PROGRAM_NAME + " stopping");
console.debug("Program stopping");
this._stop();
}

Expand Down
4 changes: 4 additions & 0 deletions src/types/graphs/viewgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ export class ViewGraph {
return Array.from(this.devices.keys());
}

getAdjacentDeviceIds(id: DeviceId): DeviceId[] {
return this.getDeviceIds().filter((adjId) => adjId !== id);
}

// Get the number of devices in the graph
getDeviceCount(): number {
return this.devices.size;
Expand Down

0 comments on commit 989f920

Please sign in to comment.