UNPKG

3.4 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.fetch$ = fetch$;
7exports.renderToObs$ = renderToObs$;
8exports.render$ = render$;
9exports.renderToString$ = renderToString$;
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
12
13var _rx = require('rx');
14
15var _rx2 = _interopRequireDefault(_rx);
16
17var _reactDom = require('react-dom');
18
19var _reactDomServer = require('react-dom/server');
20
21var _debug = require('debug');
22
23var _debug2 = _interopRequireDefault(_debug);
24
25var _ContextWrapper = require('./ContextWrapper');
26
27var _ContextWrapper2 = _interopRequireDefault(_ContextWrapper);
28
29var debug = (0, _debug2['default'])('thundercats:render');
30
31function fetch$(fetchMap) {
32 if (!fetchMap || fetchMap.size === 0) {
33 debug('cat found empty fetch map');
34 return _rx2['default'].Observable.just(fetchMap);
35 }
36
37 var fetchCtx$ = _rx2['default'].Observable.from(fetchMap.values()).shareReplay();
38 var stores$ = fetchCtx$.map(function (_ref) {
39 var store = _ref.store;
40 return store;
41 }).filter(function (store) {
42 return !!store;
43 }).toArray().flatMap(function (stores) {
44 return _rx2['default'].Observable.combineLatest(stores);
45 });
46
47 var actionDurations$ = fetchCtx$.map(function (_ref2) {
48 var action = _ref2.action;
49 return action.__duration();
50 }).toArray().flatMap(function (actionDurations) {
51 return _rx2['default'].Observable.combineLatest(actionDurations);
52 }).tapOnCompleted(function () {
53 return debug('fetch actions have all completed');
54 });
55
56 var fetch$ = fetchCtx$.tapOnNext(function (_ref3) {
57 var action = _ref3.action;
58 var payload = _ref3.payload;
59
60 action(payload);
61 }).tapOnCompleted(function () {
62 return debug('fetchers activated');
63 }).toArray();
64
65 return _rx2['default'].Observable.combineLatest(stores$, actionDurations$, fetch$.delaySubscription(50)).first().tapOnNext(function () {
66 return debug('fetch completed');
67 }).map(function () {
68 return fetchMap;
69 });
70}
71
72function renderToObs$(Comp, DOMContainer) {
73 return new _rx2['default'].AnonymousObservable(function (observer) {
74 try {
75 (0, _reactDom.render)(Comp, DOMContainer, function () {
76 observer.onNext(this);
77 });
78 } catch (e) {
79 observer.onError(e);
80 observer.onCompleted();
81 }
82 });
83}
84
85function render$(cat, Component, DOMContainer) {
86 var Burrito = undefined;
87 try {
88 Burrito = _ContextWrapper2['default'].wrap(Component, cat);
89 } catch (e) {
90 return _rx.Observable['throw'](e);
91 }
92 return renderToObs$(Burrito, DOMContainer);
93}
94
95function renderToString$(cat, Component) {
96 var fetchMap = undefined;
97 var Burrito = undefined;
98 try {
99 fetchMap = new Map();
100 cat.fetchMap = fetchMap;
101 Burrito = _ContextWrapper2['default'].wrap(Component, cat);
102 debug('initiation fetcher registration');
103 (0, _reactDomServer.renderToStaticMarkup)(Burrito);
104 debug('fetcher registration complete');
105 } catch (e) {
106 return _rx.Observable['throw'](e);
107 }
108
109 return fetch$(fetchMap)
110 // move fetch to next event loop to prevent
111 // synchronous actions from causing infiniti loop
112 .delay(50).map(function (fetchMap) {
113 var markup = (0, _reactDomServer.renderToString)(Burrito);
114 return {
115 markup: markup,
116 fetchMap: fetchMap
117 };
118 }).tapOnNext(function () {
119 return cat.fetchMap = null;
120 });
121}
\No newline at end of file