@isaac-platform/isaac-integration-sdk
Version:
A Typescript SDK for integrating with ISAAC
89 lines (88 loc) • 4.46 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 isaacConnection from "../controller/isaac.js";
import { IsaacVariable } from "./variable.js";
export class variableController {
// TODO: Use knownVariables registry to track state of registration
constructor() {
this.knownVariables = [];
/**
* 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?
**/
this.getVariables = () => __awaiter(this, void 0, void 0, function* () {
try {
let config = {
params: {
subsystemExternalId: isaacConnection.subsystemID,
}
};
let variableDefinitionList = yield isaacConnection.getRequest(`variables`, config);
let variableObjectList = [];
for (let definition of variableDefinitionList) {
variableObjectList.push(new IsaacVariable(definition));
}
return variableObjectList;
}
catch (error) {
console.error('ISAAC SDK: Error getting variables.', error);
throw error;
}
});
/**
* Creates a new variable on the remote ISAAC system.
* @param newVariable is the definition of the variable to be added
* @returns new IsaacVariable object
*/
this.addVariable = (newVariable) => __awaiter(this, void 0, void 0, function* () {
// Assign connection module ID if not overridden in definition.
if (!newVariable.hasOwnProperty('subsystemExternalId')) {
newVariable.subsystemExternalId = isaacConnection.subsystemID;
}
try {
const createdVariable = yield isaacConnection.postRequest(`variables`, newVariable);
console.log(createdVariable);
return new IsaacVariable(createdVariable);
}
catch (error) {
console.log('ISAAC SDK: Error creating variable.', error);
throw error;
}
});
// 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.
this.registerVariable = (newVariable) => __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 the new variable is not found in knownVariables, attempt to create it.
if (existingVariable == undefined) {
try {
const registeredVariable = yield this.addVariable(newVariable);
let index = this.knownVariables.push(registeredVariable);
return this.knownVariables[index - 1];
}
catch (error) {
console.log('ISAAC SDK: Error creating variable.', error);
throw error;
}
}
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.
this.syncWithISAAC = () => __awaiter(this, void 0, void 0, function* () {
this.knownVariables = yield this.getVariables();
});
}
}