autobahnreact
Version:
Do realtime without headaches with Autobahn and React.
106 lines (83 loc) • 5.18 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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 _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
exports.requireSubscriptions = requireSubscriptions;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function _inherits(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) subClass.__proto__ = superClass; }
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _ConnectionJs = require('./Connection.js');
function requireSubscriptions(Component) {
var staticMethods = arguments[1] === undefined ? {} : arguments[1];
var highOrderComponent = (function (_React$Component) {
var _class = function highOrderComponent(props) {
_classCallCheck(this, _class);
_get(Object.getPrototypeOf(_class.prototype), 'constructor', this).call(this, props);
this.state = {
data: {}
};
this.subscriptions = [];
this.subscriptionsMeta = {};
};
_inherits(_class, _React$Component);
_createClass(_class, [{
key: 'onPublished',
value: function onPublished(variable, args, kwargs, details) {
var _this = this;
this.setState(function (previousState, curProps) {
if (_this.subscriptionsMeta[variable].store) {
previousState.data[variable].push({ args: args, kwargs: kwargs, details: details });
} else {
previousState.data[variable] = { args: args, kwargs: kwargs, details: details };
}
return previousState;
});
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
var _this2 = this;
var routes = Component.observeSubscriptions();
for (var variable in routes) {
_ConnectionJs.Connection.currentConnection.session.subscribe(routes[variable].route, this.onPublished.bind(this, variable)).then(function (subscription) {
_this2.subscriptions.push(subscription);
var isStore = routes[variable].store || false;
_this2.subscriptionsMeta[variable] = { store: isStore };
_this2.setState(function (previousState, curProps) {
if (isStore) {
previousState.data[variable] = [];
} else {
previousState.data[variable] = {};
}
});
})['catch'](function (error) {
console.error('Failed to auto-subscribe to a topic: ' + routes[variable] + ' !', error);
});
}
}
}, {
key: 'componentDidUnmount',
value: function componentDidUnmount() {
var routes = Component.observeSubscriptions();
this.subscriptions.foreach(function (subscription) {
_ConnectionJs.Connection.currentConnection.session.subscribe(subscription);
});
}
}, {
key: 'render',
value: function render() {
return _react2['default'].createElement(Component, _extends({ data: this.state.data }, this.props));
}
}]);
return _class;
})(_react2['default'].Component);
for (var functionName in staticMethods) {
highOrderComponent[functionName] = staticMethods[functionName];
}
return highOrderComponent;
}