@blockv/sdk
Version:
Allows web apps to display and interact with vatoms.
86 lines (60 loc) • 2.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _BLOCKvRegion = _interopRequireDefault(require("./BLOCKvRegion"));
var _DataObject = _interopRequireDefault(require("../DataObject"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* This region plugin provides access to a collection of vatoms identified by their IDs.
* The 'inventory' region is much mor reliable, so if you know that your vatoms are owned bby the current user,
* use the inventory region with a filter rather.
*
* TODO: Retry a few times
*
* To get an instance, call `DataPool.region('ids', ["id1", "id2"])`
*/
class VatomIDRegion extends _BLOCKvRegion.default {
/** Plugin ID */
static get id() {
return 'ids';
}
/** Constructor */
constructor(dataPool, ids) {
super(dataPool); // Store IDs. Sort them to keep our stateKey in check.
this.ids = ids.sort();
}
/** Our state key is the list of IDs */
get stateKey() {
return 'ids:' + this.ids.join(',');
}
/** Check if a region request matches our region */
matches(id, descriptor) {
// Check all filters match
if (id !== 'ids') return false;
if (!descriptor || descriptor.length !== this.ids.length) return false;
for (let i = 0; i < this.ids.length; i++) {
if (descriptor[i] !== this.ids[i]) {
return false;
}
} // Yes they do
return true;
}
/** Load current state from the server */
async load() {
// Pause websocket events
this.pauseMessages(); // Fetch data
let response = await this.dataPool.Blockv.client.request('POST', '/v1/user/vatom/get', {
ids: this.ids
}); // Add vatom to new objects list
let objects = [];
response.vatoms.map(v => new _DataObject.default('vatom', v.id, v)).forEach(f => objects.push(f)); // Add faces to new objects list
response.faces.map(f => new _DataObject.default('face', f.id, f)).forEach(f => objects.push(f)); // Add actions to new objects list
response.actions.map(a => new _DataObject.default('action', a.name, a)).forEach(a => objects.push(a)); // Add new objects
this.addObjects(objects); // Resume websocket messages
this.resumeMessages(); // Return array of IDs
return objects.map(o => o.id);
}
}
exports.default = VatomIDRegion;