UNPKG

react-sync-state

Version:

A client side implementation for synchronizing states with server

122 lines (105 loc) 4.07 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 _Types = require("./_Types"); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * A State keeps track of all the objects that are kept in constant synchronization * with the server. An application normally has only one instance of a State which * is created from a initial data by some State Loader. One such loader is available * in loaders/JavaSyncStateLoader.jsx which converts the data provided by react-server * Java library. * * The state is also responsible for keeping track of changes made to the Entitys which * when changed should be able to dispatch events */ var State = (function () { function State() { _classCallCheck(this, State); // initial member variables this._locked = false; this._deferred = []; this._registeredTypes = {}; this._seeds = {}; // Register all the primitive types when during init this.registerDataType("Int", new _Types.DataType()); this.registerDataType("Short", new _Types.DataType()); this.registerDataType("Long", new _Types.DataType()); this.registerDataType("Boolean", new _Types.DataType()); this.registerDataType("String", new _Types.DataType()); this.registerDataType("Float", new _Types.DataType()); this.registerDataType("Double", new _Types.DataType()); this.registerDataType("Date", new _Types.DateType()); // This is Date } /** * Register a Data Type allowed to be used on this state * @param {String} name A name to recoginize the data type. The data * is synchornized with this state using these * type names * @param {DataType} type A DataType object to be registered */ _createClass(State, [{ key: "registerDataType", value: function registerDataType(name, type) { this._registeredTypes[name] = type; } /** * Retrieve the DataType by name * @param {String} name The name of the datatype to be retrieved * @return {DataType} The already registered DataType or null if not * registered yet. */ }, { key: "getDataType", value: function getDataType(name) { return this._registeredTypes[name]; } /** * Retrieve a list with the given name. The list is created by Loader * during initialization. * * @param {String} name The name of the list to be retrieved * @return {RecordSet} A RecordSet object hat holds the list of * Records as primary data set of this state */ }, { key: "getList", value: function getList(name) { return this._seeds[name]; } }, { key: "lock", value: function lock() { this._locked = true; } }, { key: "release", value: function release() { this._locked = false; while (this._deferred.length) { this._deferred.pop().fire(); } } }, { key: "reset", value: function reset() { this._locked = false; this._deferred = []; } }, { key: "dispatch", value: function dispatch(entity) { if (this._locked) { if (this._deferred.indexOf(entity) == -1) { this._deferred.push(entity); } } else { entity.fire(); } } }]); return State; })(); exports.default = State;