UNPKG

react-sync-state

Version:

A client side implementation for synchronizing states with server

233 lines (187 loc) 7.7 kB
'use strict'; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 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 _Types = require('./_Types'); var _Entity2 = require('./_Entity'); var _Entity3 = _interopRequireDefault(_Entity2); var _react = require('react'); var _react2 = _interopRequireDefault(_react); 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 RecordSet keeps list of records as either a seed list in State * or as a reference to many children on another record. * * The RecordSet provides two methods */ var RecordSet = (function (_Entity) { _inherits(RecordSet, _Entity); function RecordSet(state) { _classCallCheck(this, RecordSet); var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(RecordSet).call(this)); _this._state = state; _this.list = []; return _this; } /** * Add a record on this record set * * @param {Record} record The record that needs to be appended * @return {Integer} Returns the size of the list after the operation */ _createClass(RecordSet, [{ key: 'add', value: function add(record) { this.list.push(record); this._state.dispatch(this); return this.list.length; } /** * Remove the given record from this list * * @param {Record} record The record that needs to be removed from this list * @return {Integer} The position of the record in the list from where it was removed * or -1 if the record was not there in the list */ }, { key: 'remove', value: function remove(record) { var idx = this.list.indexOf(record); if (idx != -1) { this.list.splice(idx, 1); this._state.dispatch(this); } return idx; } /** * An alias to the {#add} method. * * @param {Record} record The record that needs to be appended * @return {[type]} [description] */ }, { key: 'push', value: function push(record) { return this.add(record); } /** * Remove the record from the end of the list * * @return {Record} The record that was removed or null if the list was empty */ }, { key: 'pop', value: function pop() { if (this.list.length) { var res = this.list.pop(); this._state.dispatch(this); return res; } else { return null; } } /** * Get the size of this list * @return {Integer} The size of the list */ }, { key: 'size', value: function size() { return this.list.length; } /** * Replace the existing record at the given index with the given record * * @param {Integer} index The index position which needs to be updated * @param {Record} record The record that needs to be put at the given index * @return {Record} The existing Record that was there at the given index * before being replaced. */ }, { key: 'update', value: function update(index, record) { var old = this.list[index]; this.list[index] = record; this._state.dispatch(this); return old; } /** * Return a React Element encompassing this list by the given component * and the properties and the children elements. The children element * could be generated using the {#renderList} method of this list or * the children could be generated with some other way through this list * * @param {React.Component} Component The React Class to render this list * @param {Object} props The Component properties to be passed on * @param {React.Component} childComponent A child component to render each item * @param {Object} childProps The Child component properties to be passed on * @return {React Element} A react element ready to be rendered */ }, { key: 'render', value: function render(Component, props, childComponent, childProps) { if (arguments.length > 2) { return _react2.default.createElement( Component, _extends({ state: this }, props), this.renderList(childComponent, childProps) ); } else { return _react2.default.createElement( Component, _extends({ state: this }, props), this.props.children ); } } /** * Return a list of Component rendered for each record in the list * * @param {React.Component} Component The React Component Class that needs * to be rendered * @param {Object} props [Optional] properties that need to be set on the * component. * @return {List} A list of React Elements */ }, { key: 'renderList', value: function renderList(Component, props) { return this.list.map(function (record, index) { return _react2.default.createElement(Component, _extends({ key: index, state: record }, props)); }); } }]); return RecordSet; })(_Entity3.default); var RecordSetType = (function (_DataType) { _inherits(RecordSetType, _DataType); function RecordSetType(recordType) { _classCallCheck(this, RecordSetType); var _this2 = _possibleConstructorReturn(this, Object.getPrototypeOf(RecordSetType).call(this)); _this2._type = recordType; return _this2; } _createClass(RecordSetType, [{ key: 'getRecordType', value: function getRecordType() { return this._type; } // adapt(value) { // // The value is supposed to be an array of number // if (!value instanceof Array) { // console.error("The value - " + value + " should be an array"); // } else { // for(var i in value) { // } // } // } }]); return RecordSetType; })(_Types.DataType); RecordSet.Type = RecordSetType; exports.default = RecordSet;