@stated-library/react
Version:
React bindings for [`Stated Libraries`](https://github.com/bradfordlemley/stated-library)
277 lines (231 loc) • 7.29 kB
JavaScript
import { createElement, Component, useRef, useState, useEffect } from 'react';
import { getValue } from '@stated-library/core';
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 _typeof2(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof2 = function _typeof2(obj) {
return typeof obj;
};
} else {
_typeof2 = function _typeof2(obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof2(obj);
}
function _typeof(obj) {
if (typeof Symbol === "function" && _typeof2(Symbol.iterator) === "symbol") {
_typeof = function _typeof(obj) {
return _typeof2(obj);
};
} else {
_typeof = function _typeof(obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : _typeof2(obj);
};
}
return _typeof(obj);
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
return call;
}
return _assertThisInitialized(self);
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
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 _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 _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === 'function') {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function (key) {
_defineProperty(target, key, source[key]);
});
}
return target;
}
function link(component, state$) {
var comp = component;
var subscription;
var lastState = getValue(state$);
comp.state = _objectSpread({}, comp.state || {}, lastState);
return {
connect: function connect() {
subscription = state$.subscribe(function (state) {
if (!Object.is(state, lastState)) {
lastState = state;
comp.setState(_objectSpread({}, state));
}
});
},
disconnect: function disconnect() {
subscription.unsubscribe();
comp = null;
}
};
}
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
}
function connect(value$) {
return function (WrappedComp) {
var _class, _temp;
return _temp = _class =
/*#__PURE__*/
function (_React$Component) {
_inherits(WrapperComp, _React$Component);
function WrapperComp(props) {
var _this;
_classCallCheck(this, WrapperComp);
_this = _possibleConstructorReturn(this, _getPrototypeOf(WrapperComp).call(this, props));
_this.link = void 0;
_this.state = void 0;
_this.link = link(_assertThisInitialized(_this), value$);
return _this;
}
_createClass(WrapperComp, [{
key: "componentDidMount",
value: function componentDidMount() {
this.link.connect();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.link.disconnect();
}
}, {
key: "render",
value: function render() {
// @ts-ignore
return createElement(WrappedComp, Object.assign({}, this.props, this.state));
}
}]);
return WrapperComp;
}(Component), _class.displayName = "connect-sl(".concat(getDisplayName(WrappedComp), ")"), _temp;
};
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
function use(value$OrFunc) {
var value$Ref = useRef(undefined);
if (value$Ref.current === undefined) {
value$Ref.current = typeof value$OrFunc === 'function' ? value$OrFunc() : value$OrFunc;
if (!value$Ref.current) {
throw new Error("Invalid value$: ".concat(value$Ref.current));
}
}
var value$ = value$Ref.current;
/*
Initial value will be set properly for observables that carry a value or
observables that emit the latest value immediately upon subscription.
*/
var _React$useState = useState(function () {
return getValue(value$);
}),
_React$useState2 = _slicedToArray(_React$useState, 2),
value = _React$useState2[0],
setValue = _React$useState2[1];
useEffect(function () {
var subscription = value$.subscribe(setValue);
return function () {
return subscription.unsubscribe();
};
}, [value$]);
return value;
}
export { connect, link, use };
//# sourceMappingURL=index.es.js.map