Last active
September 30, 2022 15:13
-
-
Save malerba118/7f58709a2b86a4215e3b382cca6ccbf7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PartSimulator { | |
terminals: TerminalSimulator[]; | |
terminalsByGroup: Record<number, TerminalSimulator[]>; | |
terminalsByName: Record<string, TerminalSimulator>; | |
constuctor(part: db.types.Part) { | |
this.terminals = this.constructor.terminals.map((terminalDefinition) => { | |
return new TerminalSimulator({ | |
...terminalDefinition, | |
connections: part.terminals[terminalDefinition.name].connections, | |
part: this, | |
}); | |
}); | |
this.terminalsByGroup = _.groupBy(this.terminals, "group"); | |
this.terminalsByName = _.keyBy(this.terminals, "name"); | |
} | |
} | |
class BreadboardSimulator extends PartSimulator { | |
static terminals = [ | |
{ | |
name: "positive.a.1", | |
group: 0, | |
}, | |
// ... | |
{ | |
name: "positive.a.64", | |
group: 0, | |
}, | |
{ | |
name: "a.1", | |
group: 1, | |
}, | |
{ | |
name: "b.1", | |
group: 1, | |
}, | |
{ | |
name: "c.1", | |
group: 1, | |
}, | |
{ | |
name: "d.1", | |
group: 1, | |
}, | |
{ | |
name: "e.1", | |
group: 1, | |
}, | |
]; | |
} | |
class TerminalSimulator { | |
name: string; | |
group: number; | |
connectionNames: string[]; | |
part: PartSimulator; | |
constructor(params) { | |
this.name = params.name; | |
this.group = params.group; | |
this.connectionNames = params.connections; | |
this.part = params.part; | |
} | |
get implicitConnections() { | |
return this.part.terminalsByGroup[this.group]; | |
} | |
get explicitConnections() { | |
return this.connectionNames.map( | |
(terminalName) => this.part.terminalsByName[terminalName] | |
); | |
} | |
get connections() { | |
return [...this.implicitConnections, ...this.explicitConnections]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment