UNPKG

@felangel/react-bloc

Version:

React Components that make it easy to implement the BLoC (Business Logic Component) design pattern. Built to be used with the bloc.js state management package.

66 lines 2.51 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { 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 extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var React = require("react"); var rxjs_1 = require("rxjs"); var BlocBuilder = /** @class */ (function (_super) { __extends(BlocBuilder, _super); function BlocBuilder(props) { var _this = _super.call(this, props) || this; _this.bloc = props.bloc; _this.builder = props.builder; _this.condition = props.condition || null; _this.previousState = _this.bloc.state; _this.subscription = rxjs_1.Subscription.EMPTY; _this.state = { blocState: _this.bloc.state }; return _this; } BlocBuilder.prototype.subscribe = function () { var _this = this; this.subscription = this.bloc.listen(function (state) { var rebuild = _this.condition !== null ? _this.condition.call(_this, _this.previousState, state) : true; if (rebuild) { _this.setState({ blocState: state }); } _this.previousState = state; }); }; BlocBuilder.prototype.unsubscribe = function () { this.subscription.unsubscribe(); }; BlocBuilder.prototype.componentDidUpdate = function (prevProps) { if (prevProps.bloc !== this.props.bloc) { this.unsubscribe(); this.bloc = this.props.bloc; this.previousState = this.bloc.state; this.setState({ blocState: this.bloc.state }); this.subscribe(); } }; BlocBuilder.prototype.componentDidMount = function () { this.subscribe(); }; BlocBuilder.prototype.componentWillUnmount = function () { this.unsubscribe(); }; BlocBuilder.prototype.render = function () { return this.builder(this.state.blocState); }; return BlocBuilder; }(React.Component)); exports.BlocBuilder = BlocBuilder; //# sourceMappingURL=bloc-builder.js.map