@isaac-platform/isaac-integration-sdk
Version:
A Typescript SDK for integrating with ISAAC
124 lines (123 loc) • 5.6 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 { IsaacPlayable } from "./playable.js";
import isaacConnection from "../../controller/isaac.js";
export class PlayableController {
constructor() {
this.knownPlayables = [];
/**
* Retrieves list of playables
* @returns Array of IsaacPlayables
*/
this.GetPlayables = () => __awaiter(this, void 0, void 0, function* () {
try {
let playableObjects = [];
let playableList = yield isaacConnection.getRequest(`playables`, {
params: {
subsystemExternalId: isaacConnection.subsystemID,
}
});
playableList.forEach((playable) => {
playableObjects.push(new IsaacPlayable(playable));
});
return playableObjects;
}
catch (error) {
console.error('ISAAC SDK: Error getting playables.', error);
throw error;
}
});
/**
* Retrieves a list of subsystemId related playables marked to be cached.
* @return IsaacPlayable[] - Array of IsaacPlayable objects.
*/
this.getForceCachePlayables = () => __awaiter(this, void 0, void 0, function* () {
try {
let playableList = yield isaacConnection.getRequest(`playables`, {
params: {
subsystemExternalId: isaacConnection.subsystemID,
forceCached: true
}
});
// return a map of IsaacPlayable objects
return playableList.map((playable) => new IsaacPlayable(playable));
}
catch (error) {
console.error('ISAAC SDK: Error getting playables.', error);
throw error;
}
});
}
GetKnownPlayables() {
return this.knownPlayables;
}
GetPlayable(id) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield isaacConnection.getRequest(`playables/${id}`);
}
catch (error) {
console.error('ISAAC SDK: Error getting playable.', error);
throw error;
}
});
}
/**
* 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 = isaacConnection.subsystemID;
}
try {
const createdPlayable = yield isaacConnection.postRequest(`playables`, newPlayable);
return new IsaacPlayable(createdPlayable);
}
catch (error) {
console.log('ISAAC SDK: Error creating playable.', error);
throw error;
}
});
}
// 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 || isaacConnection.subsystemID}.`);
let index = this.knownPlayables.push(yield this.addPlayable(newPlayable));
return this.knownPlayables[index - 1];
// return new IsaacVariable(newPlayable as IsaacVariableType, isaacConnection)
}
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();
});
}
}