react-sync-state
Version:
A client side implementation for synchronizing states with server
56 lines (45 loc) • 1.8 kB
JavaScript
;
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
});
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Entity = (function () {
function Entity() {
_classCallCheck(this, Entity);
this._listeners = [];
}
/**
* Register a listener on this entity to be informed about changes
* made to this entity
*/
_createClass(Entity, [{
key: "subscribe",
value: function subscribe(listener) {
if (this._listeners.indexOf(listener) == -1) {
this._listeners.push(listener);
}
}
/**
* Unregister a listener on this entity to avoid being informed
* about changes made to this entity
*/
}, {
key: "unsubscribe",
value: function unsubscribe(listener) {
var idx = this._listeners.indexOf(listener);
if (idx >= 0) {
this._listeners.splice(idx, 1);
}
}
}, {
key: "fire",
value: function fire() {
for (var x in this._listeners) {
this._listeners[x].onStateChanged && this._listeners[x].onStateChanged(this);
}
}
}]);
return Entity;
})();
exports.default = Entity;