react-sync-state
Version:
A client side implementation for synchronizing states with server
225 lines (183 loc) • 7.94 kB
JavaScript
;
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 _Entity2 = require('./_Entity');
var _Entity3 = _interopRequireDefault(_Entity2);
var _Types = require('./_Types');
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; }
// Even though this object is not used directly in the code. The ES6 React
// code when converted uses the 'React' symbol, that is why React needs
// to be imported here
/**
* A Record entity holds the data from a state for a single entity
*/
var Record = (function (_Entity) {
_inherits(Record, _Entity);
function Record(state, type) {
_classCallCheck(this, Record);
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Record).call(this));
_this._type = type;
_this._state = state;
return _this;
}
/**
* Retreive the type of this record, which might be helpful in performing
* lot of Type specific operation on the record. For example, take a look
* at toString method
*
* @return {Record.Type} The type of this string
*/
_createClass(Record, [{
key: 'getType',
value: function getType() {
return this._type;
}
/**
* Convert this record into its string representation
*
* @return {String} A string representation for this record type
*/
}, {
key: 'toString',
value: function toString() {
return this._type.getString(this);
}
/**
* The method available for the State builders for updating the
* record values
* @param {String} field The field name
* @param {Primitive} value The new value for the field
*/
}, {
key: 'update',
value: function update(field, value) {
// Only do the update if the values are different
if (this[field] !== value) {
this[field] = value;
// We will let the State know that this record has changed
this._state.dispatch(this);
}
}
/**
* A helper method for rendering a React Component using thiss
* particular record
*
* @param {React.Component} Component A React component class to render
* using data from this record
* @param {[type]} props Additional and optional properties needed for
* rendering the React Component
* @return {React Element} React element ready for rendering
*/
}, {
key: 'render',
value: function render(Component, props) {
return _react2.default.createElement(Component, _extends({ state: this }, props));
}
}]);
return Record;
})(_Entity3.default);
var RecordType = (function (_DataType) {
_inherits(RecordType, _DataType);
function RecordType(name) {
_classCallCheck(this, RecordType);
// this variable has to be set by the loader
var _this2 = _possibleConstructorReturn(this, Object.getPrototypeOf(RecordType).call(this));
_this2._structure = null;
_this2._name = name;
// A null record reference
_this2._NULL = new Record();
_this2._set = {};
// Hook to convert this specific type of record into string
_this2._toString = null;
return _this2;
}
_createClass(RecordType, [{
key: 'getString',
value: function getString(record) {
if (this._toString) {
return this._toString(record);
} // by default we will return a value for the field - name, title or description;
else if (record.name) {
// the default text representation is by a 'name' field if available
return record.name;
} else if (record.title) {
// The second choice is the 'title' field
return record.title;
} else if (record.description) {
// the third choice is the 'description' field
return record.description;
} else if (record.id) {
// if we are here means, we probably forgot to override the toString method for
// this specific type
if (DEBUG) {
console.warn("Trying to convert a record object into string without a proper text " + "field (name, title or description). You can override this method for using " + "state.getDataType('" + this._name + "')._toString = function(record) { return ... }");
}
return this._name + "#" + record.id;
} else {
return "?" + this._name + "?";
}
}
}, {
key: 'get',
value: function get(id) {
return this._set[id];
}
}, {
key: 'has',
value: function has(id) {
return id in this._set;
}
}, {
key: '_own',
value: function _own(id, record) {
this._set[id] = record;
}
}, {
key: 'getFieldType',
value: function getFieldType(name) {
return this._structure[name];
}
// adapt(value) {
// // The possible values here are
// // 1. null => for null reference
// // 2. number => a numeric id the references an existing record
// // 3. object => a object that needs to be converted
// if (value == null) {
// return this._NULL;
// } else if (typeof value == "object") {
// var rec = new Record();
// for(var prop in value) {
// if (prop in this.structure) {
// var type = this.structure[prop];
// var value = type.adapt(value[prop]);
// rec[prop] = value;
// // We may get a shadow reference here, keep track of the shadow references
// if (value instanceof Shadow) {
// value.append(rec, prop);
// }
// }
// }
// return rec;
// } else if (value in this._set) {
// return this._set[value];
// } else if (!isNaN(parseFloat(value))) {
// var shadow = new Shadow(this.type, value);
// this._set[value] = shadow;
// return shadow;
// } else {
// console.error("The value - " + value + " is not known for " + this.type);
// }
// }
}]);
return RecordType;
})(_Types.DataType);
Record.Type = RecordType;
exports.default = Record;