UNPKG

hele-hbus

Version:
156 lines (131 loc) 4.98 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('hele'), require('hbus')) : typeof define === 'function' && define.amd ? define(['exports', 'hele', 'hbus'], factory) : (factory((global.HEle = global.HEle || {}),global.HEle,global.HBus)); }(this, (function (exports,hele,HBus) { 'use strict'; var classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; 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; }; }(); var defineProperty = function (obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }; var inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }; var possibleConstructorReturn = function (self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }; var defaultBusName = 'hele-hbus-bus'; var Bus = function (_Component) { inherits(Bus, _Component); function Bus(props, context) { classCallCheck(this, Bus); var _this = possibleConstructorReturn(this, (Bus.__proto__ || Object.getPrototypeOf(Bus)).call(this, props, context)); _this.bus = new HBus.Bus(props.processor, props.defaultState); _this.name = props.name; return _this; } createClass(Bus, [{ key: "render", value: function render() { return hele.createElement(hele.Context, { value: defineProperty({}, this.name, this.bus) }, this.props.children); } }]); return Bus; }(hele.Component); Bus.defaultProps = { name: defaultBusName }; var Subscription = function (_Component) { inherits(Subscription, _Component); function Subscription(props, context) { classCallCheck(this, Subscription); var _this = possibleConstructorReturn(this, (Subscription.__proto__ || Object.getPrototypeOf(Subscription)).call(this, props, context)); var bus = _this.bus = context[props.bus], state = bus.getState(); _this.state = 'prop' in props ? state[props.prop] : state; _this.subscriber = _this.update.bind(_this); return _this; } createClass(Subscription, [{ key: "onWillMount", value: function onWillMount() { var props = this.props, bus = this.bus, subscriber = this.subscriber; if ('prop' in props) { bus.subscribeProp(props.prop, subscriber); } else { bus.subscribe(subscriber); } } }, { key: "render", value: function render() { return this.props.children[0](this.state); } }, { key: "onWillUnmount", value: function onWillUnmount() { var props = this.props, bus = this.bus, subscriber = this.subscriber; if ('prop' in props) { bus.unsubscribeProp(props.prop, subscriber); } else { bus.unsubscribe(subscriber); } } }]); return Subscription; }(hele.Component); Subscription.defaultProps = { bus: defaultBusName }; exports.defaultBusName = defaultBusName; exports.Bus = Bus; exports.Subscription = Subscription; Object.defineProperty(exports, '__esModule', { value: true }); })));