publisherproxy
Version:
Association between a Javascript Proxy and a kind of pubsub pattern
386 lines (314 loc) • 14.6 kB
JavaScript
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ // The require scope
/******/ var __webpack_require__ = {};
/******/
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
/*!*******************!*\
!*** ./index.mjs ***!
\*******************/
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "PublisherManager": () => (/* binding */ PublisherManager),
/* harmony export */ "default": () => (/* binding */ Publisher)
/* harmony export */ });
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function isComplex(value) {
return _typeof(value) === "object" && value != null;
}
var CustomProxy = /*#__PURE__*/function () {
function CustomProxy(target) {
var parentProxPub = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
_classCallCheck(this, CustomProxy);
this._proxies_ = new Map();
this._value_ = target;
this._assignListeners_ = new Set();
this._mutationListeners_ = new Set();
this._fillListeners_ = new Set();
this._templateFillListeners_ = new Set();
this._lockInternalMutationPublishing_ = false;
this.parent = parentProxPub;
this.root = this;
while (this.root.parent) {
this.root = this.root.parent;
}
}
_createClass(CustomProxy, [{
key: "hasListener",
value: function hasListener() {
return this._templateFillListeners_.size > 0 || this._assignListeners_.size > 0 || this._mutationListeners_.size > 0 || this._fillListeners_.size > 0;
}
}, {
key: "_publishInternalMutation_",
value: function _publishInternalMutation_() {
var lockInternalMutationsTransmission = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
this._mutationListeners_.forEach(function (handler) {
return handler();
});
if (lockInternalMutationsTransmission) return;
if (this.parent) {
this.parent._publishInternalMutation_();
}
}
}, {
key: "_publishAssignement_",
value: function _publishAssignement_() {
var _this = this;
var lockInternalMutationsTransmission = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
this._assignListeners_.forEach(function (handler) {
return handler(_this._value_.__value ? _this._value_.__value : _this._value_);
});
this._publishInternalMutation_(lockInternalMutationsTransmission);
}
}, {
key: "_publishDynamicFilling_",
value: function _publishDynamicFilling_(key, value) {
this._fillListeners_.forEach(function (handler) {
return handler[key] = value;
});
this._publishTemplateFilling_(key, value);
}
}, {
key: "_publishTemplateFilling_",
value: function _publishTemplateFilling_(key, value) {
this._templateFillListeners_.forEach(function (handler) {
if (typeof handler[key] != "undefined") {
handler[key] = value;
}
});
}
}, {
key: "onAssign",
value: function onAssign(handler) {
if (typeof handler != "function") return;
this._assignListeners_.add(handler);
this._publishAssignement_();
}
}, {
key: "offAssign",
value: function offAssign(handler) {
this._assignListeners_["delete"](handler);
}
}, {
key: "onInternalMutation",
value: function onInternalMutation(handler) {
if (typeof handler != "function") return;
this._mutationListeners_.add(handler);
this._publishInternalMutation_();
}
}, {
key: "offInternalMutation",
value: function offInternalMutation(handler) {
this._mutationListeners_["delete"](handler);
}
}, {
key: "startTemplateFilling",
value: function startTemplateFilling(handler) {
this._templateFillListeners_.add(handler);
for (var z in this._value_) {
this._publishTemplateFilling_(z, this._value_[z]);
}
}
}, {
key: "stopTemplateFilling",
value: function stopTemplateFilling(handler) {
this._templateFillListeners_["delete"](handler);
}
}, {
key: "startDynamicFilling",
value: function startDynamicFilling(handler) {
this._fillListeners_.add(handler);
for (var z in this._value_) {
this._publishDynamicFilling_(z, this._value_[z]);
}
}
}, {
key: "stopDynamicFilling",
value: function stopDynamicFilling(handler) {
this._fillListeners_["delete"](handler);
}
}, {
key: "set",
value: function set(newValue) {
var _this2 = this;
var lockInternalMutationsTransmission = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
if (this._value_.hasOwnProperty("__value") && newValue.hasOwnProperty("__value") && this._value_.__value == newValue.__value) {
return true;
}
this._value_ = newValue;
if (this._value_.hasOwnProperty("__value")) {
this._publishAssignement_(lockInternalMutationsTransmission);
return true;
}
Array.from(this._proxies_.keys()).forEach(function (key) {
// delete this._value_[key];
if (!_this2._value_[key] && _this2._proxies_.has(key) && !_this2._proxies_.get(key).hasListener()) {
_this2._proxies_["delete"](key);
}
});
for (var key in this._value_) {
var v = newValue[key];
var isVComplex = isComplex(v);
var valueV = isVComplex ? v : {
__value: v
};
if (!this._proxies_.has(key)) {
this._proxies_.set(key, new Publisher(valueV, this), true);
}
this._proxies_.get(key).set(valueV, true);
this._publishDynamicFilling_(key, v);
}
this._publishAssignement_();
return true;
}
}, {
key: "get",
value: function get() {
if (this._value_.__value) return this._value_.__value;
return this._value_;
}
}]);
return CustomProxy;
}();
var PublisherManager = /*#__PURE__*/function () {
function PublisherManager() {
_classCallCheck(this, PublisherManager);
if (PublisherManager.instance != null) throw "Singleton / use getInstance";
PublisherManager.instance = this;
this.publishers = new Map();
}
_createClass(PublisherManager, [{
key: "get",
value: function get(id) {
if (!this.publishers.has(id)) this.publishers.set(id, new Publisher({}));
return this.publishers.get(id);
}
}], [{
key: "getInstance",
value: function getInstance(channel) {
if (PublisherManager.instance == null) return new PublisherManager();
return PublisherManager.instance;
}
}]);
return PublisherManager;
}();
_defineProperty(PublisherManager, "instance", null);
var Publisher = /*#__PURE__*/function (_CustomProxy) {
_inherits(Publisher, _CustomProxy);
var _super = _createSuper(Publisher);
function Publisher(target) {
var _this3;
var parentProxPub = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
_classCallCheck(this, Publisher);
_this3 = _super.call(this, target, parentProxPub);
var that = _assertThisInitialized(_this3);
return _possibleConstructorReturn(_this3, new Proxy(_assertThisInitialized(_this3), {
get: function get(oTarget, sKey) {
if (["onAssign", "offAssign", "startDynamicFilling", "stopDynamicFilling", "startTemplateFilling", "stopTemplateFilling", "onInternalMutation", "set", "get", "_templateFillListeners_", "_fillListeners_", "_assignListeners_", "_publishInternalMutation_", "hasListener", "_mutationListeners_", "_publishDynamicFilling_", "_publishTemplateFilling_", "_publishAssignement_", "_proxies_", "parent", "_value_", "_lockInternalMutationPublishing_"].includes(sKey)) return that[sKey];
if (!that._proxies_.has(sKey)) {
var vValue = that._value_[sKey];
that._proxies_.set(sKey, new Publisher(isComplex(vValue) ? vValue : {
__value: vValue
}, that));
}
return that._proxies_.get(sKey);
},
set: function set(oTarget, sKey, vValue) {
if (sKey == "_value_") {
oTarget._value_ = vValue;
return oTarget._value_;
}
var isValueComplex = isComplex(vValue);
if (!that._proxies_.has(sKey)) {
that._proxies_.set(sKey, new Publisher(isValueComplex ? vValue : {
__value: vValue
}, that));
}
if (that._value_[sKey] == vValue && isValueComplex) return vValue;
that._value_[sKey] = vValue;
that._publishDynamicFilling_(sKey, vValue);
that._proxies_.get(sKey).set(isComplex(vValue) ? vValue : {
__value: vValue
});
return that._proxies_.get(sKey);
},
deleteProperty: function deleteProperty(oTarget, sKey) {
that._proxies_.get(sKey).set(null);
that._publishDynamicFilling_(sKey, null);
that._proxies_["delete"](sKey);
return delete that._value_[sKey];
},
enumerate: function enumerate(oTarget, sKey) {
return that._value_.keys();
},
has: function has(oTarget, sKey) {
return sKey in that._value_ && sKey != "_lockInternalMutationPublishing_";
},
defineProperty: function defineProperty(oTarget, sKey, oDesc) {
if (oDesc && "value" in oDesc) {
that._value_[sKey] = oDesc.value;
}
return that._value_;
},
getOwnPropertyDescriptor: function getOwnPropertyDescriptor(oTarget, sKey) {
var vValue = that._value_[sKey];
return {
enumerable: true,
configurable: true
};
},
ownKeys: function ownKeys(target) {
if (that._value_.__value) return Object.keys(this._value_.__value);
return Object.keys(that._value_);
}
}));
}
return Publisher;
}(CustomProxy);
if (typeof module != "undefined") module.exports = {
Publisher: Publisher,
PublisherManager: PublisherManager
};
/******/ })()
;