@lf-lang/reactor-ts
Version:
A reactor-oriented programming framework in TypeScript
79 lines • 2.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Bank = void 0;
/**
* A bank of reactor instances.
*/
class Bank {
/**
* Array of reactor instances that constitute the bank.
*/
members = new Array();
/**
* A mapping from containing reactors to indices corresponding to the member
* of a contained bank that is currently being initialized (if there is one).
*/
static initializationMap = new Map();
/**
* Construct a new bank of given width on the basis of a given reactor class and a list of arguments.
* @param width the width of the bank
* @param cls the class to construct reactor instances of that will populate the bank
* @param parameters the arguments to pass into the constructor of the given reactor class
*/
constructor(container, width, cls, ...parameters) {
for (let i = 0; i < width; i++) {
Bank.initializationMap.set(container, i);
console.log(`Setting initializing to ${i}`);
this.members.push(Reflect.construct(cls, parameters, cls));
}
Bank.initializationMap.delete(container);
}
/**
* Return all reactor instances in this bank.
* @returns all reactor instances in this bank
*/
all() {
return this.members;
}
/**
* Return the reactor instance that corresponds to the given index.
* @param index index of the reactor instance inside this bank
* @returns the reactor instances that corresponds to the given index
*/
get(index) {
return this.members[index];
}
/**
* Return a list of ports selected across all bank members by the given lambda.
* @param selector lambda function that takes a reactor of type T and return a port of type P
* @returns a list of ports selected across all bank members by the given lambda
*/
port(selector) {
return this.all().reduce((acc, val) => acc.concat(selector(val)), new Array(0));
}
toString() {
return `bank(${this.members.length})`;
}
allWritable(ports) {
if (ports.length !== this.members.length) {
throw new Error("Length of ports does not match length of reactors.");
}
const result = new Array(ports.length);
for (let i = 0; i < ports.length; i++) {
result[i] = this.members[i].allWritable(ports[i]);
}
return result;
}
writable(ports) {
if (ports.length !== this.members.length) {
throw new Error("Length of ports does not match length of reactors.");
}
const result = new Array(ports.length);
for (let i = 0; i < ports.length; i++) {
result[i] = this.members[i].writable(ports[i]);
}
return result;
}
}
exports.Bank = Bank;
//# sourceMappingURL=bank.js.map