UNPKG

react-sync-state

Version:

A client side implementation for synchronizing states with server

92 lines (68 loc) 3.86 kB
'use strict'; var _createClass = (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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _Entity = require('./_Entity'); var _Entity2 = _interopRequireDefault(_Entity); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** * A React Component that maintains its state to reflect the changes made * to the Entity (Record and RecordSet) objects. */ var Component = (function (_React$Component) { _inherits(Component, _React$Component); function Component(props) { _classCallCheck(this, Component); // This Component is linked only if it has a state property and // that it is of an Entity type (Record or RecordSet) var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Component).call(this, props)); if (props.state && props.state instanceof _Entity2.default) { _this.linked = true; _this.state = props.state; } else { _this.state = {}; } return _this; } // Listen to the subscription event only when the Component is // made visible, otherwise no need to pollute the State _createClass(Component, [{ key: 'componentDidMount', value: function componentDidMount() { // let's see if the component has a 'state' property and it // is an entity, in which case, we will listen on that entity if (this.linked) { this.props.state.subscribe(this); } } // Gracefully unsubscribe to the state events, to avoid any // memory leak and unwanted pollution of State }, { key: 'componentWillUnmount', value: function componentWillUnmount() { if (this.linked) { this.props.state.unsubscribe(this); } } // Callback event, need to rerender }, { key: 'onStateChanged', value: function onStateChanged(entity) { console.log("State Changed - ", entity); // just make sure that this is the same as the props.state if (entity != this.props.state) { console.error("We got a state mismatch somewhere. " + "The component was initialized with state property", this.props.state, "But now its receiving events from another entity", entity); } else { this.setState(entity); } } }]); return Component; })(_react2.default.Component); exports.default = Component;