@towercg2/react
Version:
React helper code for TowerCG2.
63 lines (62 loc) • 2.51 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
function buildPluginStateProvider(pluginName) {
var context = React.createContext(null);
var Consumer = context.Consumer;
var Provider = /** @class */ (function (_super) {
__extends(class_1, _super);
function class_1(props) {
var _this = _super.call(this, props) || this;
_this.state = { pluginState: null };
return _this;
}
class_1.prototype.componentWillMount = function () {
var client = this.props.client;
if (client) {
this.attachEventToClient(client);
}
};
class_1.prototype.componentWillReceiveProps = function (nextProps) {
var client = nextProps.client;
if (client) {
this.attachEventToClient(client);
}
};
class_1.prototype.render = function () {
return (React.createElement(context.Provider, { value: this.state.pluginState }, this.props.children));
};
class_1.prototype.attachEventToClient = function (client) {
var _this = this;
client.handleEvent(pluginName + ":stateUpdated", function (pluginState) {
_this.setState({ pluginState: pluginState });
_this.forceUpdate();
});
};
return class_1;
}(React.Component));
return {
Provider: Provider,
Consumer: Consumer
};
}
exports.buildPluginStateProvider = buildPluginStateProvider;
exports.TickContext = buildPluginStateProvider("tick");
exports.RosterContext = buildPluginStateProvider("roster");
;