UNPKG

@b-side/parser

Version:

HTML Parser used to parse HTML string and bind data to it.

152 lines 6.86 kB
"use strict"; 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 _AutoUpdate_instances, _AutoUpdate_functionWrapper, _AutoUpdate_descriptorHandler, _AutoUpdate_proxyHandler, _AutoUpdate_chooseAction, _AutoUpdate_performAction; Object.defineProperty(exports, "__esModule", { value: true }); exports.AutoUpdate = exports.PROXY_IDENTITY = void 0; const utils_1 = require("./utils"); exports.PROXY_IDENTITY = Symbol('proxy_target_identity'); const reservedProperties = ['constructor']; const AsyncFunction = (async () => { }).constructor; var Action; (function (Action) { Action["WRAP"] = "Wrap function"; Action["PROXY"] = "Replace object with Proxy"; Action["HANDLER"] = "Replace value with Description Handler"; Action["SKIP"] = "Do nothing"; })(Action || (Action = {})); class AutoUpdate { constructor(data, element, callback) { _AutoUpdate_instances.add(this); this.data = data; this.element = element; this.callback = callback; this.searchProperties(); } searchProperties() { let target = this.data; do { Object.getOwnPropertyNames(target).forEach((property) => __classPrivateFieldGet(this, _AutoUpdate_instances, "m", _AutoUpdate_performAction).call(this, target, property)); } while (Object.getPrototypeOf(target).constructor !== (0, utils_1.getDocument)().defaultView?.HTMLElement && Object.getPrototypeOf(target) !== Object.prototype && (target = Object.getPrototypeOf(target))); } } exports.AutoUpdate = AutoUpdate; _AutoUpdate_instances = new WeakSet(), _AutoUpdate_functionWrapper = function _AutoUpdate_functionWrapper(property, descriptor) { const backupValue = descriptor.value; const context = this.data; const element = this.element; const callback = async (properties) => { properties.forEach((property) => __classPrivateFieldGet(this, _AutoUpdate_instances, "m", _AutoUpdate_performAction).call(this, context, property)); return await this.callback(properties); }; if (context[property] instanceof AsyncFunction) { context[property] = async function () { const result = await backupValue.apply(context, arguments); const newKeys = element.updateDependancies(context); if (newKeys.length) { await callback(newKeys); } return result; }.bind(context); } else { context[property] = function () { const result = backupValue.apply(context, arguments); const newKeys = element.updateDependancies(context); if (newKeys.length) { callback(newKeys); } return result; }.bind(context); } }, _AutoUpdate_descriptorHandler = function _AutoUpdate_descriptorHandler(property, descriptor) { const backupGet = descriptor.get; const backupSet = descriptor.set; const callback = async (property) => await this.callback([property]); const context = this.data; if (backupGet && !backupSet) { console.warn(`Expression [${property}] might change after initialisation without knowing, template won't be updated automatically. Setter should be configured or prevent use in template.`); } let savedvalue = descriptor.value; descriptor.get = () => { return backupGet ? backupGet.call(context) : savedvalue; }; descriptor.set = async (value) => { savedvalue = backupSet ? backupSet.call(context, value) : value; await callback(property); }; delete descriptor.value; delete descriptor.writable; Object.defineProperty(context, property, descriptor); }, _AutoUpdate_proxyHandler = function _AutoUpdate_proxyHandler(property) { const callback = (property) => this.callback([property]); const target = this.data; const handler = { deleteProperty(target, prop) { delete target[prop]; return true; }, get(target, prop, receiver) { if (prop === exports.PROXY_IDENTITY) { return target; } const value = Reflect.get(target, prop, receiver); return typeof value === 'function' ? value.bind(target) : value; }, set(target, prop, value) { callback(property); return Reflect.set(target, prop, value); }, }; let proxy = new Proxy(target[property], handler); Object.defineProperty(target, property, { get: () => proxy, set: (value) => { proxy = new Proxy(value, handler); callback(property); }, }); }, _AutoUpdate_chooseAction = function _AutoUpdate_chooseAction(property, descriptor) { const effects = this.element.dependancies; const hasValue = Object.hasOwnProperty.call(descriptor, 'value') || !!descriptor?.get; if (!descriptor || descriptor.writable === false || descriptor.configurable === false || hasValue === false) { return Action.SKIP; } if (hasValue && descriptor?.value instanceof Function) { return Action.WRAP; } if (reservedProperties.indexOf(property) !== -1 || (!effects.methods.has(property) && !effects.properties.has(property) && !effects.vars.has(property))) { return Action.SKIP; } if (hasValue && descriptor?.value instanceof Object) { return Action.PROXY; } return Action.HANDLER; }, _AutoUpdate_performAction = function _AutoUpdate_performAction(target, property) { const descriptor = Object.getOwnPropertyDescriptor(target, property); if (!descriptor) { return; } const action = __classPrivateFieldGet(this, _AutoUpdate_instances, "m", _AutoUpdate_chooseAction).call(this, property, descriptor); if (action === Action.WRAP) { return __classPrivateFieldGet(this, _AutoUpdate_instances, "m", _AutoUpdate_functionWrapper).call(this, property, descriptor); } else if (action === Action.PROXY) { return __classPrivateFieldGet(this, _AutoUpdate_instances, "m", _AutoUpdate_proxyHandler).call(this, property); } else if (action === Action.HANDLER) { return __classPrivateFieldGet(this, _AutoUpdate_instances, "m", _AutoUpdate_descriptorHandler).call(this, property, descriptor); } }; //# sourceMappingURL=auto-update.js.map