Skip to content

Commit

Permalink
Merge pull request #1 from FormidableLabs/feature/enqueue-initializer…
Browse files Browse the repository at this point in the history
…-function

Enqueue initializer functions instead of triggering import promises immediately
  • Loading branch information
exogen authored Dec 11, 2018
2 parents 60cfb9c + 1fbc8bd commit 20070a8
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 20070a8

Please sign in to comment.