Skip to content

Commit

Permalink
fix(prerender): hAsync only returns promise if it has to
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Aug 28, 2020
1 parent dea56be commit 25a547a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 32 deletions.
27 changes: 23 additions & 4 deletions src/hydrate/platform/h-async.ts
@@ -1,9 +1,28 @@
import * as d from '../../declarations';
import { h } from '@runtime';
import { isPromise } from '@utils';
import { consoleDevError } from '@platform';

export const hAsync = async (nodeName: any, vnodeData: any, ...children: d.ChildType[]) => {
if (children) {
children = await Promise.all(children);
export const hAsync = (nodeName: any, vnodeData: any, ...children: d.ChildType[]) => {
if (Array.isArray(children) && children.length > 0) {
// only return a promise if we have to
if (children.some(isPromise)) {
// has children and at least one of them is async
// wait on all of them to be resolved
return Promise.all(children)
.then(resolvedChildren => {
return h(nodeName, vnodeData, ...resolvedChildren);
})
.catch(err => {
consoleDevError(err);
return h(nodeName, vnodeData);
});
}

// no async children, return sync
return h(nodeName, vnodeData, ...children);
}
return h(nodeName, vnodeData, ...children);

// no children, return sync
return h(nodeName, vnodeData);
};
68 changes: 40 additions & 28 deletions src/mock-doc/window.ts
Expand Up @@ -423,39 +423,47 @@ export class MockWindow {

if (this.__allowInterval) {
const intervalId = this.__setInterval(() => {
this.__timeouts.delete(intervalId);

try {
callback(...args);
} catch (e) {
if (this.console) {
this.console.error(e);
} else {
console.error(e);
if (this.__timeouts) {
this.__timeouts.delete(intervalId);

try {
callback(...args);
} catch (e) {
if (this.console) {
this.console.error(e);
} else {
console.error(e);
}
}
}
}, ms) as any;

this.__timeouts.add(intervalId);
if (this.__timeouts) {
this.__timeouts.add(intervalId);
}

return intervalId;
}

const timeoutId = this.__setTimeout(() => {
this.__timeouts.delete(timeoutId);
if (this.__timeouts) {
this.__timeouts.delete(timeoutId);

try {
callback(...args);
} catch (e) {
if (this.console) {
this.console.error(e);
} else {
console.error(e);
try {
callback(...args);
} catch (e) {
if (this.console) {
this.console.error(e);
} else {
console.error(e);
}
}
}
}, ms) as any;

this.__timeouts.add(timeoutId);
if (this.__timeouts) {
this.__timeouts.add(timeoutId);
}

return timeoutId;
}
Expand All @@ -468,20 +476,24 @@ export class MockWindow {
ms = Math.min(ms, this.__maxTimeout);

const timeoutId = (this.__setTimeout(() => {
this.__timeouts.delete(timeoutId);
if (this.__timeouts) {
this.__timeouts.delete(timeoutId);

try {
callback(...args);
} catch (e) {
if (this.console) {
this.console.error(e);
} else {
console.error(e);
try {
callback(...args);
} catch (e) {
if (this.console) {
this.console.error(e);
} else {
console.error(e);
}
}
}
}, ms) as any) as number;

this.__timeouts.add(timeoutId);
if (this.__timeouts) {
this.__timeouts.add(timeoutId);
}

return timeoutId;
}
Expand Down

0 comments on commit 25a547a

Please sign in to comment.