idea-toolbox
Version:
IDEA's utility functions
108 lines (107 loc) • 3.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeltaCount = exports.DeltaResources = exports.DeltaNext = exports.Delta = exports.DeltaRecord = void 0;
const resource_model_1 = require("./resource.model");
/**
* A record for the Delta mechanism.
* It shows the latest state of a particular resource element.
*
* Table: `xxx_teamsResources_delta`.
*
* Indexes:
* - `teamResource-timestamp-index`; LSI, includes: deleted, element.
* - `teamResource-timestamp-count`; LSI, includes: deleted.
*/
class DeltaRecord extends resource_model_1.Resource {
load(x) {
super.load(x);
this.teamResource = this.clean(x.teamResource, String);
this.id = this.clean(x.id, String);
this.timestamp = this.clean(x.timestamp, Number);
if (x.deleted)
this.deleted = this.clean(x.deleted, Boolean);
if (x.element)
this.element = x.element || {};
}
}
exports.DeltaRecord = DeltaRecord;
/**
* A structure to manage the delta (changes since a timestamp) of a set of resources.
*/
class Delta extends resource_model_1.Resource {
load(x) {
super.load(x);
this.since = this.clean(x.since, Number, 0);
if (x.next)
this.next = this.clean(x.next, String);
this.resources = this.cleanArray(x.resources, String);
this.records = {};
if (x.records)
this.resources.forEach(r => this.cleanArray(x.records[r], y => new DeltaRecord(y)));
}
/**
* Set the records of a resource to the delta.
*/
setRecordsOfResource(records, resource) {
if (!this.resources.some(r => r === resource))
this.resources.push(resource);
this.records[resource] = records;
}
}
exports.Delta = Delta;
/**
* The structure to have the next page of a previous delta request.
*/
class DeltaNext extends resource_model_1.Resource {
load(x) {
super.load(x);
this.resources = this.cleanArray(x.resources, String);
this.keys = {};
if (x.keys)
this.resources.forEach(r => (this.keys[r] = x.keys[r] || null));
}
/**
* Add the keys of a resource to the DeltaNext.
*/
addKeyOfResource(key, resource) {
if (!this.resources.some(r => r === resource))
this.resources.push(resource);
this.keys[resource] = key;
}
/**
* Remove a resource from the structure.
*/
removeResource(resource) {
delete this.keys[resource];
this.resources.splice(this.resources.indexOf(resource), 1);
}
/**
* Whether the DeltaNext is needed; it depends if there are still resources to be managed.
*/
isNeeded() {
return this.resources.length;
}
}
exports.DeltaNext = DeltaNext;
/**
* The resources supporting the delta mechanism.
* Create a similar structure when implementing the delta in a project.
*/
var DeltaResources;
(function (DeltaResources) {
DeltaResources["RESOURCE"] = "RESOURCE";
})(DeltaResources || (exports.DeltaResources = DeltaResources = {}));
/**
* A structure to have an overview of the number of elements for each resource.
*/
class DeltaCount extends resource_model_1.Resource {
load(x) {
super.load(x);
this.since = this.clean(x.since, Number, 0);
this.resources = this.cleanArray(x.resources, String);
this.count = {};
if (x.count)
this.resources.forEach(r => this.clean(x.count[r], Number));
}
}
exports.DeltaCount = DeltaCount;