dash-renderer
Version:
render dash components in react
89 lines (88 loc) • 4.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _ramda = require("ramda");
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); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var StoreObserver = exports.default = /*#__PURE__*/_createClass(function StoreObserver(_store) {
var _this = this;
_classCallCheck(this, StoreObserver);
_defineProperty(this, "_store", void 0);
_defineProperty(this, "_unsubscribe", void 0);
_defineProperty(this, "_observers", []);
_defineProperty(this, "observe", function (observer, inputs) {
if (typeof observer === 'function') {
if (!Array.isArray(inputs)) {
throw new Error('inputs must be an array');
}
_this.add(observer, inputs);
return function () {
return _this.remove(observer);
};
}
_this.add(observer.observer, observer.inputs);
return function () {
return _this.remove(observer.observer);
};
});
_defineProperty(this, "setStore", function (store) {
_this.__finalize__();
_this.__init__(store);
});
_defineProperty(this, "__finalize__", function () {
var _this$_unsubscribe;
return (_this$_unsubscribe = _this._unsubscribe) === null || _this$_unsubscribe === void 0 ? void 0 : _this$_unsubscribe.call(_this);
});
_defineProperty(this, "__init__", function (store) {
_this._store = store;
if (store) {
_this._unsubscribe = store.subscribe(_this.notify);
}
_this._observers.forEach(function (o) {
o.lastState = null;
});
});
_defineProperty(this, "add", function (observer, inputs) {
return _this._observers.push({
inputPaths: (0, _ramda.map)(function (p) {
return p.split('.');
}, inputs),
lastState: null,
observer,
triggered: false
});
});
_defineProperty(this, "notify", function () {
var store = _this._store;
if (!store) {
return;
}
var state = store.getState();
var triggered = (0, _ramda.filter)(function (o) {
return !o.triggered && (0, _ramda.any)(function (i) {
return (0, _ramda.path)(i, state) !== (0, _ramda.path)(i, o.lastState);
}, o.inputPaths);
}, _this._observers);
triggered.forEach(function (o) {
o.triggered = true;
});
triggered.forEach(function (o) {
o.lastState = store.getState();
o.observer(store);
o.triggered = false;
});
});
_defineProperty(this, "remove", function (observer) {
return _this._observers.splice(_this._observers.findIndex(function (o) {
return observer === o.observer;
}, _this._observers), 1);
});
this.__init__(_store);
});