@difizen/mana-observable
Version:
146 lines (145 loc) • 6.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getOrigin = exports.equals = exports.ObservableProperties = exports.Observability = exports.InstanceValue = void 0;
exports.isReadonly = isReadonly;
require("reflect-metadata");
var _manaCommon = require("@difizen/mana-common");
var _config = require("./config");
var _core = require("./core");
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var Observability;
(function (_Observability) {
function isTraceable(data) {
return !!data && data[_core.ObservableSymbol.Self];
}
_Observability.isTraceable = isTraceable;
function isObject(obj) {
return !!obj && _typeof(obj) === 'object';
}
_Observability.isObject = isObject;
function canBeObservable(obj) {
if (!isObject(obj)) {
return false;
}
if (_config.ObservableConfig.shouldExclude(obj)) {
return false;
}
return true;
}
_Observability.canBeObservable = canBeObservable;
function marked(obj, property) {
if (!isObject(obj)) {
return false;
}
var origin = getOrigin(obj);
if (property) {
return Reflect.hasOwnMetadata(_core.ObservableSymbol.Observable, origin, property);
}
return Reflect.hasOwnMetadata(_core.ObservableSymbol.Observable, origin);
}
_Observability.marked = marked;
function mark(obj, property) {
Reflect.defineMetadata(_core.ObservableSymbol.Observable, true, obj);
if (property) {
Reflect.defineMetadata(_core.ObservableSymbol.Observable, true, obj, property);
}
}
_Observability.mark = mark;
function defineOrigin(obj, property) {
Reflect.defineMetadata(_core.ObservableSymbol.KeepOrigin, true, obj, property);
}
_Observability.defineOrigin = defineOrigin;
function shouldKeepOrigin(obj, property) {
if (!isObject(obj)) {
return false;
}
var origin = getOrigin(obj);
if ((0, _manaCommon.isPlainObject)(origin)) {
return false;
}
return Reflect.hasMetadata(_core.ObservableSymbol.KeepOrigin, origin.constructor, property);
}
_Observability.shouldKeepOrigin = shouldKeepOrigin;
function getOrigin(obj) {
if (!isTraceable(obj)) {
return obj;
}
return obj[_core.ObservableSymbol.Self];
}
_Observability.getOrigin = getOrigin;
function equals(a, b) {
return getOrigin(a) === getOrigin(b);
}
_Observability.equals = equals;
function getDisposable(metaKey, obj, property) {
if (property) {
return Reflect.getOwnMetadata(metaKey, obj, property);
}
return Reflect.getOwnMetadata(metaKey, obj);
}
_Observability.getDisposable = getDisposable;
function setDisposable(metaKey, disposable, obj, property) {
if (property) {
Reflect.defineMetadata(metaKey, disposable, obj, property);
}
Reflect.defineMetadata(metaKey, disposable, obj);
}
_Observability.setDisposable = setDisposable;
})(Observability || (exports.Observability = Observability = {}));
var ObservableProperties;
(function (_ObservableProperties) {
function getOwn(obj) {
return Reflect.getOwnMetadata(_core.ObservableSymbol.ObservableProperties, obj);
}
_ObservableProperties.getOwn = getOwn;
function get(obj) {
return Reflect.getMetadata(_core.ObservableSymbol.ObservableProperties, obj);
}
_ObservableProperties.get = get;
function find(obj) {
if (obj && obj.constructor) {
return get(obj.constructor);
}
return undefined;
}
_ObservableProperties.find = find;
function add(obj, property) {
var existingProperties = getOwn(obj);
if (existingProperties) {
existingProperties.push(property);
} else {
var protoProperties = get(obj) || [];
Reflect.defineMetadata(_core.ObservableSymbol.ObservableProperties, [].concat(_toConsumableArray(protoProperties), [property]), obj);
}
}
_ObservableProperties.add = add;
})(ObservableProperties || (exports.ObservableProperties = ObservableProperties = {}));
var InstanceValue;
(function (_InstanceValue) {
function set(target, property, value) {
Reflect.defineMetadata(property, value, target);
}
_InstanceValue.set = set;
function get(target, property) {
return Reflect.getMetadata(property, target);
}
_InstanceValue.get = get;
})(InstanceValue || (exports.InstanceValue = InstanceValue = {}));
var getOrigin = exports.getOrigin = Observability.getOrigin;
var equals = exports.equals = Observability.equals;
function isReadonly(target, property) {
var descriptor = (0, _manaCommon.getPropertyDescriptor)(target, property);
if ((descriptor === null || descriptor === void 0 ? void 0 : descriptor.configurable) === false && (descriptor === null || descriptor === void 0 ? void 0 : descriptor.writable) === false) {
// non-configurable and non-writable property should return the actual value
return true;
}
return false;
}