Skip to content

Commit

Permalink
Enqueue initializer functions instead of triggering import promises r…
Browse files Browse the repository at this point in the history
…ight away
  • Loading branch information
exogen committed Dec 11, 2018
1 parent 60cfb9c commit 1fbc8bd
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const mockPromises = [];
const mockInitializers = [];

module.exports = function preloadAll() {
if (mockPromises.length) {
const promises = mockPromises.slice();
mockPromises.length = 0;
// While loading the components in `mockPromises`, more components may have
// been dynamically loaded, adding more promises we should wait for.
if (mockInitializers.length) {
const promises = mockInitializers.map(preload => preload());
mockInitializers.length = 0;
// While loading the components in this round of initializers, more
// components may have been dynamically loaded, adding more promises we
// should wait for.
return Promise.all(promises).then(preloadAll);
} else {
return Promise.resolve();
Expand All @@ -17,7 +18,7 @@ jest.doMock("next/dynamic", () => {

const mockDynamic = (...args) => {
const LoadableComponent = dynamic(...args);
mockPromises.push(LoadableComponent.preload());
mockInitializers.push(() => LoadableComponent.preload());
return LoadableComponent;
};

Expand Down

0 comments on commit 1fbc8bd

Please sign in to comment.