Created
December 9, 2021 17:51
-
-
Save orblazer/9c4f39c4539e9d7d02ff1c36194ab891 to your computer and use it in GitHub Desktop.
Programmig engine
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
import IO from './IO' | |
import Node from './Node' | |
export default class IfNode extends Node { | |
constructor() { | |
super() | |
this.inputs = [new IO(this, this.process.bind(this))] | |
this.outputs = [new IO(this), new IO(this)] | |
} | |
process(payload: unknown): void { | |
this.outputs[payload ? 0 : 1].emit(payload) | |
} | |
} |
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
import IO from './IO' | |
import Node from './Node' | |
export default class InputNode extends Node { | |
constructor() { | |
super() | |
this.outputs = [new IO(this)] | |
} | |
process(payload: boolean): void { | |
this.outputs[0].emit(payload) | |
} | |
} |
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
import type Node from './Node' | |
// eslint-disable-next-line @typescript-eslint/no-empty-function | |
function noop() {} | |
export type ReceiveCallback = (payload: unknown | null) => void | |
export default class IO { | |
protected readonly node!: Node | |
protected readonly onReceive!: ReceiveCallback | |
protected to: IO | null = null | |
constructor(node: Node, handler: ReceiveCallback = noop) { | |
this.node = node | |
this.onReceive = handler | |
} | |
/** | |
* Transmit an payload to connected IO | |
* @param payload The payload | |
*/ | |
emit(payload: unknown | null = null): void { | |
this.to?.onReceive(payload) | |
} | |
/** | |
* Connect two io (pass null for disconnect) | |
* @param target The io target | |
*/ | |
connect(target: IO | null): void { | |
this.to = target | |
} | |
} |
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
import type IO from './IO' | |
export default abstract class Node { | |
inputs: IO[] = [] | |
outputs: IO[] = [] | |
abstract process(payload: unknown): void | |
connect(outIndex: number, target: Node, targetInIndex = 0): void { | |
this.outputs[outIndex].connect(target.inputs[targetInIndex]) | |
} | |
} |
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
import IO from './IO' | |
import Node from './Node' | |
export default class PrintNode extends Node { | |
prefix = '' | |
constructor(prefix?: string) { | |
super() | |
this.inputs = [new IO(this, this.process.bind(this))] | |
if (typeof prefix === 'string') { | |
this.prefix = prefix | |
} | |
} | |
process(payload: unknown): void { | |
console.log(this.prefix, payload) | |
} | |
} |
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
import IfNode from './IfNode' | |
import InputNode from './InputNode' | |
import PrintNode from './PrintNode' | |
const input = new InputNode() | |
// Check if is true or false | |
const iff = new IfNode() | |
input.connect(0, iff) | |
// Print output | |
const printTrue = new PrintNode('[true]') | |
iff.connect(0, printTrue) | |
const printFalse = new PrintNode('[false]') | |
iff.connect(1, printFalse) | |
// Start flow | |
setInterval(() => input.process(Math.random() < 0.5), 1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment