UNPKG

@isaac-platform/isaac-integration-sdk

Version:

A Typescript SDK for integrating with ISAAC

87 lines (86 loc) 4.25 kB
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 { IsaacPlayable } from "./playable.js"; export class PlayableController { constructor(connection) { this.knownPlayables = []; this.isaacConn = connection; } /** * Retrieves list of playables * @returns Array of IsaacPlayables */ GetPlayables() { return __awaiter(this, void 0, void 0, function* () { console.log("ISAAC SDK - Getting playables"); let playableObjects = []; let playableList = yield this.isaacConn.getRequest(`playables?subsystemExternalId=${this.isaacConn.subsystemID}`); playableList.forEach((playable) => { // console.log(`Caching Playable Meta: ${playable.externalRef}`) playableObjects.push(new IsaacPlayable(playable, this.isaacConn)); }); return playableObjects; }); } GetKnownPlayables() { return this.knownPlayables; } GetPlayable(id) { return __awaiter(this, void 0, void 0, function* () { return yield this.isaacConn.getRequest(`playables/${id}`); }); } /** * Creates a new playable on the remote ISAAC system. * @param newPlayable is the definition of the variable to be added * @returns new IsaacPlayable object */ addPlayable(newPlayable) { return __awaiter(this, void 0, void 0, function* () { // Assign connection module ID if not overridden in definition. if (!newPlayable.hasOwnProperty('subsystemExternalId')) { newPlayable.subsystemExternalId = this.isaacConn.subsystemID; } let createdPlayable = yield this.isaacConn.postRequest(`playables`, newPlayable); console.log(createdPlayable); return new IsaacPlayable(createdPlayable, this.isaacConn); }); } // Safely creates a new playable using the internal registry // TODO: Improve functionality here. I need to rethink implementation of this sort of feature in the big picture. registerPlayable(newPlayable) { return __awaiter(this, void 0, void 0, function* () { yield this.syncWithISAAC(); // Attempt to locate the existing playable by externalRef let existingPlayable = this.knownPlayables.find((knownPlayable) => { return newPlayable.externalRef === knownPlayable.get().externalRef; }); // If playable exists, do not add, return from registry. if (existingPlayable == undefined) { console.log(`Creating playable ${newPlayable.externalRef} for ${newPlayable.subsystemExternalId || this.isaacConn.subsystemID}.`); let index = this.knownPlayables.push(yield this.addPlayable(newPlayable)); return this.knownPlayables[index - 1]; // return new IsaacVariable(newPlayable as IsaacVariableType, this.isaacConn) } else { console.log(`Playable ${existingPlayable.get().externalRef} already exists for ${existingPlayable.get().subsystemExternalId}.`); return existingPlayable; } }); } // Replaces known playable array with latest playables 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 Playables"); this.knownPlayables = yield this.GetPlayables(); }); } }