Bez popisu

preSSRService.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.RenderService = undefined;
  6. exports.default = preSSRService;
  7. var _render = require('./render');
  8. var _render2 = _interopRequireDefault(_render);
  9. var _utils = require('./utils');
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
  12. function getPathsFromRoutes(routes) {
  13. const paths = [];
  14. (0, _utils.searchRoutes)(routes, route => {
  15. paths.push(route.props.path);
  16. });
  17. return paths;
  18. }
  19. class RenderService {
  20. constructor({ url, interval, renderOptions }) {
  21. this.url = url;
  22. this.timeout = interval;
  23. this.renderOptions = renderOptions;
  24. }
  25. render() {
  26. var _this = this;
  27. return _asyncToGenerator(function* () {
  28. yield (0, _render2.default)(_this.renderOptions);
  29. _this.timer = setTimeout(_this.run.bind(_this), _this.timeout);
  30. })();
  31. }
  32. run() {
  33. this.render();
  34. }
  35. stop() {
  36. if (this.timer) {
  37. clearTimeout(this.timer);
  38. }
  39. }
  40. }
  41. exports.RenderService = RenderService;
  42. function preSSRService({
  43. renderFullPage, createApp, initialState, interval = 10000, onRenderSuccess, routes, timeout = 6000, verbose = true
  44. }) {
  45. const paths = getPathsFromRoutes(routes);
  46. paths.forEach(path => {
  47. new RenderService({
  48. url: path,
  49. interval,
  50. renderOptions: {
  51. routes,
  52. url: path,
  53. renderFullPage,
  54. createApp,
  55. initialState,
  56. onRenderSuccess,
  57. timeout,
  58. env: {
  59. platform: 'pc'
  60. },
  61. verbose
  62. }
  63. }).run();
  64. new RenderService({
  65. url: path,
  66. interval,
  67. renderOptions: {
  68. routes,
  69. url: path,
  70. renderFullPage,
  71. createApp,
  72. initialState,
  73. onRenderSuccess,
  74. timeout,
  75. env: {
  76. platform: 'mobile'
  77. },
  78. verbose
  79. }
  80. }).run();
  81. });
  82. }