@barchart/common-js
Version:
Library of common JavaScript utilities
243 lines (237 loc) • 6.63 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Model_exports = {};
__export(Model_exports, {
default: () => Model
});
module.exports = __toCommonJS(Model_exports);
var assert = __toESM(require("./../lang/assert.js"));
var is = __toESM(require("./../lang/is.js"));
var import_Disposable = __toESM(require("./../lang/Disposable.js"));
var import_Event = __toESM(require("./../messaging/Event.js"));
class Model extends import_Disposable.default {
#propertyNames;
#transactionCommit;
#transactionOpen;
#transactionData;
#trackerOpen;
#trackerData;
#sequence;
/**
* @param {string[]} propertyNames
* @param {object=} propertyObservers
* @param {object=} equalityPredicates
*/
constructor(propertyNames, propertyObservers, equalityPredicates) {
super();
this.#propertyNames = propertyNames;
this.#transactionCommit = new import_Event.default(this);
this.#transactionOpen = false;
this.#transactionData = null;
this.#trackerOpen = false;
this.#trackerData = null;
this.#sequence = 0;
const observers = propertyObservers || {};
const predicates = equalityPredicates || {};
for (let i = 0; i < this.#propertyNames.length; i++) {
const propertyName = propertyNames[i];
this.#createProperty(propertyName, observers[propertyName] || emptyFunction, predicates[propertyName] || checkEquals);
}
}
/**
* @public
*/
beginTransaction() {
if (this.#transactionOpen) {
return;
}
this.#transactionOpen = true;
}
/**
* @public
*/
endTransaction() {
if (!this.#transactionOpen) {
return;
}
if (this.disposed) {
return;
}
this.#transactionOpen = false;
if (this.#transactionData !== null) {
this._formatTransactionData(this.#transactionData);
this.#transactionData.sequence = this.#sequence++;
if (this.#trackerOpen) {
this.#trackerData = this.#trackerData || {};
for (let propertyName in this.#transactionData) {
this.#trackerData[propertyName] = this.#transactionData[propertyName];
}
}
this.#transactionCommit.fire(this.#transactionData);
this.#transactionData = null;
}
}
/**
* @protected
* @param {object} transactionData
*/
_formatTransactionData(transactionData) {
return;
}
/**
* @public
* @param {Function} processor
*/
executeTransaction(processor) {
assert.argumentIsRequired(processor, "processor", Function);
this.beginTransaction();
processor(this);
this.endTransaction();
}
/**
* @public
* @param {Function} observer
* @returns {*}
*/
onTransactionCommitted(observer) {
if (this.disposed) {
return;
}
return this.#transactionCommit.register(observer);
}
/**
* @public
*/
startTracker() {
if (this.#trackerOpen) {
return;
}
this.#trackerOpen = true;
}
/**
* @public
* @returns {object|null}
*/
resetTracker() {
if (!this.#trackerOpen) {
return null;
}
if (this.disposed) {
return null;
}
const returnRef = this.#trackerData;
this.#trackerData = null;
return returnRef;
}
/**
* @public
*/
stopTracking() {
if (!this.#trackerOpen) {
return;
}
if (this.disposed) {
return;
}
this.#trackerOpen = false;
this.#trackerData = null;
}
/**
* @public
* @returns {object}
*/
getSnapshot() {
const snapshot = {};
for (let i = 0; i < this.#propertyNames.length; i++) {
const propertyName = this.#propertyNames[i];
snapshot[propertyName] = this[propertyName];
}
snapshot.sequence = this.#sequence;
return snapshot;
}
/**
* @protected
* @override
*/
_onDispose() {
this.#transactionCommit.dispose();
this.#transactionCommit = null;
}
#createProperty(propertyName, propertyObserver, equalityPredicate) {
let propertyValue = null;
Object.defineProperty(this, propertyName, {
get: () => {
return propertyValue;
},
set: (value) => {
const valueToAssign = is.undef(value) ? null : value;
if (equalityPredicate(propertyValue, valueToAssign)) {
return;
}
propertyValue = valueToAssign;
const implicit = !this.#transactionOpen;
if (implicit) {
this.beginTransaction();
}
this.#transactionData = this.#transactionData || {};
this.#transactionData[propertyName] = propertyValue;
propertyObserver(this);
if (implicit) {
this.endTransaction();
}
}
});
}
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
toString() {
return "[Model]";
}
}
function emptyFunction() {
return;
}
function checkEquals(a, b) {
return a === b;
}
{
const cjsExports = module.exports;
const cjsDefaultExport = cjsExports && cjsExports.__esModule ? cjsExports.default : cjsExports;
if (cjsDefaultExport && (typeof cjsDefaultExport === 'function' || typeof cjsDefaultExport === 'object')) {
Object.keys(cjsExports).forEach((key) => {
if (key !== 'default' && key !== '__esModule') {
cjsDefaultExport[key] = cjsExports[key];
}
});
}
module.exports = cjsDefaultExport;
}