next-redux-saga
Version:
redux-saga HOC for Next.js
205 lines (167 loc) • 5.46 kB
JavaScript
import _regeneratorRuntime from '@babel/runtime/regenerator';
import React, { Component } from 'react';
import { END } from 'redux-saga';
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
function _asyncToGenerator(fn) {
return function () {
var self = this,
args = arguments;
return new Promise(function (resolve, reject) {
var gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a 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);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
function _defineProperty(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;
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf(subClass, superClass);
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _possibleConstructorReturn(self, call) {
if (call && (typeof call === "object" || typeof call === "function")) {
return call;
}
return _assertThisInitialized(self);
}
function withReduxSaga(BaseComponent) {
var WrappedComponent =
/*#__PURE__*/
function (_Component) {
_inherits(WrappedComponent, _Component);
function WrappedComponent() {
_classCallCheck(this, WrappedComponent);
return _possibleConstructorReturn(this, _getPrototypeOf(WrappedComponent).apply(this, arguments));
}
_createClass(WrappedComponent, [{
key: "render",
value: function render() {
return React.createElement(BaseComponent, this.props);
}
}], [{
key: "getInitialProps",
value: function () {
var _getInitialProps = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee(props) {
var _props$ctx, isServer, store, pageProps;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_props$ctx = props.ctx, isServer = _props$ctx.isServer, store = _props$ctx.store;
pageProps = {};
if (!BaseComponent.getInitialProps) {
_context.next = 6;
break;
}
_context.next = 5;
return BaseComponent.getInitialProps(props);
case 5:
pageProps = _context.sent;
case 6:
if (!isServer) {
_context.next = 10;
break;
}
store.dispatch(END);
_context.next = 10;
return store.sagaTask.toPromise();
case 10:
return _context.abrupt("return", pageProps);
case 11:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
return function getInitialProps(_x) {
return _getInitialProps.apply(this, arguments);
};
}()
}]);
return WrappedComponent;
}(Component);
_defineProperty(WrappedComponent, "displayName", "withReduxSaga(".concat(BaseComponent.displayName || BaseComponent.name || 'BaseComponent', ")"));
return WrappedComponent;
}
export default withReduxSaga;
//# sourceMappingURL=next-redux-saga.es.js.map