Skip to content

Commit

Permalink
fix: error overlay should not be injected into the first instance, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Sep 10, 2019
1 parent 941b41c commit c019663
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/global/generation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ let generation = 1;
// these counters are aimed to mitigate the "first render"
let hotComparisonCounter = 0;
let hotComparisonRuns = 0;
let hotReplacementGeneration = 0;

const nullFunction = () => ({});

// these callbacks would be called on component update
Expand All @@ -24,7 +26,8 @@ export const setComparisonHooks = (open, element, close) => {
export const getElementComparisonHook = component => onHotComparisonElement(component);
export const getElementCloseHook = component => onHotComparisonClose(component);

export const hotComparisonOpen = () => hotComparisonCounter > 0 && hotComparisonRuns > 0;
export const hotComparisonOpen = () =>
hotComparisonCounter > 0 && hotComparisonRuns > 0 && hotReplacementGeneration > 0;

const openGeneration = () => forEachKnownClass(onHotComparisonElement);

Expand All @@ -48,6 +51,7 @@ const decrementHot = () => {
export const configureGeneration = (counter, runs) => {
hotComparisonCounter = counter;
hotComparisonRuns = runs;
hotReplacementGeneration = runs;
};

// TODO: shall it be called from incrementHotGeneration?
Expand All @@ -63,6 +67,5 @@ export const increment = () => {
export const get = () => generation;

// These counters tracks HMR generations, and probably should be used instead of the old one
let hotReplacementGeneration = 0;
export const incrementHotGeneration = () => hotReplacementGeneration++;
export const getHotGeneration = () => hotReplacementGeneration;
21 changes: 21 additions & 0 deletions src/utils/runQueue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const createQueue = (runner = a => a()) => {
let promise;
let queue = [];

const runAll = () => {
const oldQueue = queue;
oldQueue.forEach(cb => cb());
queue = [];
};

const add = cb => {
if (queue.length === 0) {
promise = Promise.resolve().then(() => runner(runAll()));
}
queue.push(cb);

return promise;
};

return add;
};

0 comments on commit c019663

Please sign in to comment.