UNPKG

@towercg2/react

Version:

React helper code for TowerCG2.

51 lines (50 loc) 2.39 kB
"use strict"; 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")); var client_1 = require("@towercg2/client"); var clientContext = React.createContext(null); exports.ClientConsumer = clientContext.Consumer; var ClientProvider = /** @class */ (function (_super) { __extends(ClientProvider, _super); function ClientProvider(props) { var _this = _super.call(this, props) || this; _this.state = { client: null, connected: false }; return _this; } ClientProvider.prototype.componentWillMount = function () { var _this = this; var _a = this.props, localUri = _a.localUri, clientId = _a.clientId, clientSecret = _a.clientSecret; var client = new client_1.Client(localUri, { clientId: clientId, clientSecret: clientSecret }); client.socket.on("connect", function () { return _this.setState({ connected: true }); }); client.socket.on("reconnect", function () { return _this.setState({ connected: true }); }); client.socket.on("disconnect", function () { return _this.setState({ connected: false }); }); this.setState({ client: client, connected: false }); client.connect(); }; ClientProvider.prototype.render = function () { var _a = this.state, client = _a.client, connected = _a.connected; return (React.createElement(clientContext.Provider, { value: connected && client ? client : null }, this.props.children)); }; return ClientProvider; }(React.Component)); exports.ClientProvider = ClientProvider;