@rxap/reflect-metadata
Version:
Provides utilities for working with Reflect Metadata. It includes functions for setting, getting, and manipulating metadata on objects and properties. This package also offers functionalities for change detection using proxies and metadata.
42 lines • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.handler = exports.RXAP_DETECT_CHANGES = exports.hasOnChangeMethod = void 0;
exports.HasOnChangeMethod = HasOnChangeMethod;
exports.ProxyChangeDetection = ProxyChangeDetection;
exports.RxapDetectChanges = RxapDetectChanges;
const meta_data_1 = require("./meta-data");
const utilities_1 = require("@rxap/utilities");
function HasOnChangeMethod(obj) {
return obj['rxapOnPropertyChange'] && typeof obj['rxapOnPropertyChange'] === 'function';
}
/**
* @deprecated use HasOnChangeMethod instead
*/
exports.hasOnChangeMethod = HasOnChangeMethod;
exports.RXAP_DETECT_CHANGES = 'rxap-detect-changes';
exports.handler = {
set: function (instance, propertyKey, value, receiver) {
// eslint-disable-next-line no-prototype-builtins
if (!this.hasOwnProperty('initialized') || instance.initialized === true) {
if (((0, meta_data_1.getMetadata)(exports.RXAP_DETECT_CHANGES, instance) || []).includes(propertyKey)) {
if (!(0, utilities_1.equals)(instance[propertyKey], value)) {
if ((0, exports.hasOnChangeMethod)(instance)) {
instance.rxapOnPropertyChange({
propertyKey,
currentValue: value,
previousValue: instance[propertyKey],
});
}
}
}
}
return Reflect.set(instance, propertyKey, value, receiver);
},
};
function ProxyChangeDetection(instance) {
return new Proxy(instance, exports.handler);
}
function RxapDetectChanges(target, propertyKey) {
(0, meta_data_1.addToMetadata)(exports.RXAP_DETECT_CHANGES, propertyKey, target);
}
//# sourceMappingURL=change.js.map