happo/happo.io

Wait for all async to settle before capturing HTML when prerendering

lencioni opened this issue · 0 comments

I'm not even totally sure this kind of thing would be useful, so this is more of just an idea.

I wonder if diffs could be made more stable by waiting for any in-flight async things (e.g. setTimeout) to settle before capturing the HTML when prerendering. Code that looks something like this could be used to handle setTimeout, for instance:

    const timers = new Set();
    originalSetTimeout = window.setTimeout;
    originalClearTimeout = window.clearTimeout;
    
    function deleteTimer(id) {
      timers.delete(id);
      if (timers.size === 0) {
        // Something here, like a callback maybe
      }
    }
    
    window.setTimeout = function(cb, delay) {
      const id = originalSetTimeout(() => {
        cb();
        
        deleteTimer(id);
      }, delay);
      
      timers.add(id);
      return id;
    }
    
    window.clearTimeout = function(id) {
      deleteTimer(id);      
      return originalClearTimeout(id);
    };