UNPKG

immreact

Version:

Use react classes with an immstruct structure

115 lines (85 loc) 5.08 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _state2 = require('./state'); var _state3 = _interopRequireDefault(_state2); var _constants = require('./constants'); var _constants2 = _interopRequireDefault(_constants); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 _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; } var _state = Symbol('state'); var _id = Symbol('id'); var Component = function (_React$Component) { _inherits(Component, _React$Component); function Component(props) { _classCallCheck(this, Component); return _possibleConstructorReturn(this, Object.getPrototypeOf(Component).call(this, props)); // Be lazy, dont create symbols on the object unless required } _createClass(Component, [{ key: 'update', value: function update(state) { // console.log( 'setting state' ) // console.log( state ) if (state.id) { delete state.id; } this.cursor.update(function (cursor) { return cursor.merge(state); }); } }, { key: 'cursor', set: function set(state) { var _this2 = this; if (!state) { // @TODO should throw return console.warn('[Immreact.Component] state unspecified'); } if (!state.id) { return console.warn('[Immreact.Component] can not create cursor without id'); } // Only generate stateful container if cursor is defined // Id also serves as an indicator of being linked to centralised state this[_id] = state.id; // Add new stateful container to the components map unless already specified or is forced if (state.force || !_state3.default.state.cursor(_constants2.default.COMPONENTS).get(state.id)) { _state3.default.state.cursor(_constants2.default.COMPONENTS).update(function (cursor) { // @TODO many components being created at the same time will repeatedly // trigger immstruct swap events, can this be batched? The call to this.update // triggers another swap return cursor.merge(_defineProperty({}, _this2[_id], {})); }); // console.log( 'fresh state using cursor = {...}' ) this.update(state); } }, get: function get() { if (!this[_id]) { // @TODO should throw? return console.warn('[Immreact.Component] component has no cursor set, can not access'); } // Lazily reference the state and cache, update when the reference changes // if ( !this[ _state ] ) { // this[ _state ] = appState.state.reference([ CONSTANTS.COMPONENTS, this[ _id ] ] ) // } // Grab a fresh cursor to the referenced data return _state3.default.state.cursor([_constants2.default.COMPONENTS, this[_id]]); } }]); return Component; }(_react2.default.Component); Component.propTypes = { cursor: _react2.default.PropTypes.object }; Component.defaultProps = { cursor: null }; exports.default = Component;