@b-side/parser
Version:
HTML Parser used to parse HTML string and bind data to it.
150 lines • 6.03 kB
JavaScript
;
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Effects_instances, _Effects_findDependancies;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Effects = void 0;
const utils_1 = require("./utils");
class Effects {
constructor(expression = '') {
_Effects_instances.add(this);
this.properties = new Set();
this.methods = new Set();
this.vars = new Set();
this.expression = '';
this.previousValues = new Map();
this.firstExecution = true;
this.isLocked = false;
this.expression = expression;
}
updateDependancies(context) {
const keys = Object.keys((0, utils_1.getConstructorMethods)(context));
const newContextSnapshot = keys.join('|');
const oldKeys = this.contextSnapshot?.split('|');
const diff = keys.filter((key) => !oldKeys?.includes(key));
this.eval(context, newContextSnapshot);
return diff;
}
eval(context, contextSnapshot) {
const shouldFindDependancies = contextSnapshot === undefined || contextSnapshot !== this.contextSnapshot;
let foundNewDependancies = false;
if (shouldFindDependancies) {
this.contextSnapshot = contextSnapshot || Object.keys(context).join('|');
foundNewDependancies = __classPrivateFieldGet(this, _Effects_instances, "m", _Effects_findDependancies).call(this, this.contextSnapshot);
}
return foundNewDependancies;
}
add(name) {
this.vars.add(name);
}
merge(effets) {
this.contextSnapshot = effets.contextSnapshot;
this.properties = new Set([...this.properties, ...effets.properties]);
this.methods = new Set([...this.methods, ...effets.methods]);
this.vars = new Set([...this.vars, ...effets.vars]);
}
clone() {
const effects = new Effects(this.expression);
effects.properties = new Set([...this.properties]);
effects.methods = new Set([...this.methods]);
effects.vars = new Set([...this.vars]);
return effects;
}
reset() {
this.previousValues.clear();
}
setPreviousValue(dataset, data, dataModifier) {
for (const name of dataset) {
const source = Object.prototype.hasOwnProperty.call(dataModifier, name)
? dataModifier[name]
: data[name];
try {
if (source instanceof Map) {
this.previousValues.set(name, JSON.stringify(Object.fromEntries(source)));
}
else {
this.previousValues.set(name, JSON.stringify(source));
}
}
catch (error) {
if (source && typeof source === 'object') {
this.previousValues.set(name, source.toString());
}
}
}
}
comparePreviousValue(compareFrom, data, dataModifier) {
if (compareFrom.size === 0) {
return false;
}
return Array.from(compareFrom).some((name) => {
const compareTo = Object.prototype.hasOwnProperty.call(dataModifier, name)
? dataModifier[name]
: data[name];
let value = compareTo;
if (compareTo instanceof Map) {
value = Object.fromEntries(compareTo);
}
return this.previousValues.get(name) !== JSON.stringify(value);
});
}
activateProcessingChanges() {
this.isLocked = true;
}
doneProcessingChanges() {
this.isLocked = false;
}
shouldUpdate(data, dataModifier, propertychanged) {
let willUpdate = false;
if (this.isLocked) {
return false;
}
else if (!this.firstExecution &&
!this.methods.size &&
!this.properties.size &&
!this.vars.size) {
return false;
}
else if (this.methods.size || !this.previousValues.size) {
willUpdate = true;
}
else if ((!propertychanged.length ||
propertychanged.some((name) => this.properties.has(name) || this.vars.has(name))) &&
(this.comparePreviousValue(this.properties, data, dataModifier) ||
this.comparePreviousValue(this.vars, data, dataModifier))) {
willUpdate = true;
}
else {
}
this.firstExecution = false;
if (willUpdate) {
this.setPreviousValue(this.properties, data, dataModifier);
this.setPreviousValue(this.vars, data, dataModifier);
}
return willUpdate;
}
}
exports.Effects = Effects;
_Effects_instances = new WeakSet(), _Effects_findDependancies = function _Effects_findDependancies(contextSnapshot) {
const regex = new RegExp(`['"\`](${contextSnapshot})\\2?|(${contextSnapshot})(?:\\b)(\\()?`, 'g');
const matches = (this.expression || '').matchAll(regex);
let foundNewDependancies = false;
for (const match of matches) {
const [, , variableName, isMethodCall] = match;
if (variableName) {
if (isMethodCall && !this.methods.has(variableName)) {
this.methods.add(variableName);
foundNewDependancies = true;
}
else if (!this.properties.has(variableName)) {
this.properties.add(variableName);
foundNewDependancies = true;
}
}
}
return foundNewDependancies;
};
//# sourceMappingURL=side-effects.js.map