@isaac-platform/isaac-integration-sdk
Version:
A Typescript SDK for integrating with ISAAC
91 lines (90 loc) • 4.57 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { IsaacVariable } from "./variable.js";
export class variableController {
// TODO: Use knownVariables registry to track state of registration
constructor(isaacConn) {
this.knownVariables = [];
this.isaacConn = isaacConn;
}
/**
* GetVariables
* Returns an IsaacVariable object array of module related variables.
* ? Should I replace the includeAll parameter with a string to filter the list by provided module ID?
**/
GetVariables() {
return __awaiter(this, arguments, void 0, function* (includeAll = false) {
let variableDefinitionList = yield this.isaacConn.getRequest(`variables`);
let variableObjectList = [];
if (includeAll) {
variableDefinitionList.forEach((variableDef) => {
variableObjectList.push(new IsaacVariable(variableDef, this.isaacConn));
});
}
else {
variableDefinitionList.forEach((variableDef) => {
if (variableDef.subsystemExternalId == this.isaacConn.subsystemID) {
variableObjectList.push(new IsaacVariable(variableDef, this.isaacConn));
}
});
}
return variableObjectList;
});
}
/**
* Creates a new variable on the remote ISAAC system.
* @param newVariable is the definition of the variable to be added
* @returns new IsaacVariable object
*/
addVariable(newVariable) {
return __awaiter(this, void 0, void 0, function* () {
// Assign connection module ID if not overridden in definition.
if (!newVariable.hasOwnProperty('subsystemExternalId')) {
newVariable.subsystemExternalId = this.isaacConn.subsystemID;
}
let createdVariable = yield this.isaacConn.postRequest(`variables`, newVariable);
console.log(createdVariable);
return new IsaacVariable(createdVariable, this.isaacConn);
});
}
// Safely creates a new variable using the internal registry
// TODO: Improve functionality here. I need to rethink implementation of this sort of feature in the big picture.
registerVariable(newVariable) {
return __awaiter(this, void 0, void 0, function* () {
yield this.syncWithISAAC();
// Attempt to locate the existing variable by externalRef
let existingVariable = this.knownVariables.find((knownVariable) => {
return newVariable.externalRef === knownVariable.get().externalRef;
});
// If variable exists, do not add, return from registry.
if (existingVariable == undefined) {
console.log(`Creating variable ${newVariable.externalRef} for ${newVariable.subsystemExternalId || this.isaacConn.subsystemID}.`);
let index = this.knownVariables.push(yield this.addVariable(newVariable));
return this.knownVariables[index - 1];
// return new IsaacVariable(newVariable as IsaacVariableType, this.isaacConn)
}
else {
console.log(`Variable ${existingVariable.get().externalRef} already exists for ${existingVariable.get().subsystemExternalId}.`);
return existingVariable;
}
});
}
// Replaces known variable array with latest variables from ISAAC
// TODO: This might break events? TBH this is where my understanding of copy starts to get weird.
syncWithISAAC() {
return __awaiter(this, void 0, void 0, function* () {
console.log("Syncing ISAAC Variables");
this.knownVariables = yield this.GetVariables();
this.knownVariables.forEach(variable => {
console.log(variable.get());
});
});
}
}