1 | function makeDefaultQueryInfo() {
|
2 | return {
|
3 | seen: false,
|
4 | observable: null
|
5 | };
|
6 | }
|
7 | var RenderPromises = (function () {
|
8 | function RenderPromises() {
|
9 | this.queryPromises = new Map();
|
10 | this.queryInfoTrie = new Map();
|
11 | this.stopped = false;
|
12 | }
|
13 | RenderPromises.prototype.stop = function () {
|
14 | if (!this.stopped) {
|
15 | this.queryPromises.clear();
|
16 | this.queryInfoTrie.clear();
|
17 | this.stopped = true;
|
18 | }
|
19 | };
|
20 | RenderPromises.prototype.registerSSRObservable = function (observable) {
|
21 | if (this.stopped)
|
22 | return;
|
23 | this.lookupQueryInfo(observable.options).observable = observable;
|
24 | };
|
25 | RenderPromises.prototype.getSSRObservable = function (props) {
|
26 | return this.lookupQueryInfo(props).observable;
|
27 | };
|
28 | RenderPromises.prototype.addQueryPromise = function (queryInstance, finish) {
|
29 | if (!this.stopped) {
|
30 | var info = this.lookupQueryInfo(queryInstance.getOptions());
|
31 | if (!info.seen) {
|
32 | this.queryPromises.set(queryInstance.getOptions(), new Promise(function (resolve) {
|
33 | resolve(queryInstance.fetchData());
|
34 | }));
|
35 | return null;
|
36 | }
|
37 | }
|
38 | return finish ? finish() : null;
|
39 | };
|
40 | RenderPromises.prototype.addObservableQueryPromise = function (obsQuery) {
|
41 | return this.addQueryPromise({
|
42 | getOptions: function () { return obsQuery.options; },
|
43 | fetchData: function () { return new Promise(function (resolve) {
|
44 | var sub = obsQuery.subscribe({
|
45 | next: function (result) {
|
46 | if (!result.loading) {
|
47 | resolve();
|
48 | sub.unsubscribe();
|
49 | }
|
50 | },
|
51 | error: function () {
|
52 | resolve();
|
53 | sub.unsubscribe();
|
54 | },
|
55 | complete: function () {
|
56 | resolve();
|
57 | },
|
58 | });
|
59 | }); },
|
60 | });
|
61 | };
|
62 | RenderPromises.prototype.hasPromises = function () {
|
63 | return this.queryPromises.size > 0;
|
64 | };
|
65 | RenderPromises.prototype.consumeAndAwaitPromises = function () {
|
66 | var _this = this;
|
67 | var promises = [];
|
68 | this.queryPromises.forEach(function (promise, queryInstance) {
|
69 | _this.lookupQueryInfo(queryInstance).seen = true;
|
70 | promises.push(promise);
|
71 | });
|
72 | this.queryPromises.clear();
|
73 | return Promise.all(promises);
|
74 | };
|
75 | RenderPromises.prototype.lookupQueryInfo = function (props) {
|
76 | var queryInfoTrie = this.queryInfoTrie;
|
77 | var query = props.query, variables = props.variables;
|
78 | var varMap = queryInfoTrie.get(query) || new Map();
|
79 | if (!queryInfoTrie.has(query))
|
80 | queryInfoTrie.set(query, varMap);
|
81 | var variablesString = JSON.stringify(variables);
|
82 | var info = varMap.get(variablesString) || makeDefaultQueryInfo();
|
83 | if (!varMap.has(variablesString))
|
84 | varMap.set(variablesString, info);
|
85 | return info;
|
86 | };
|
87 | return RenderPromises;
|
88 | }());
|
89 | export { RenderPromises };
|
90 |
|
\ | No newline at end of file |