question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature: Event Bubbling

See original GitHub issue
  • Based on behavior outlined here & found in most other HMR implementations.
  • Needed by Prefresh, others.
  • /cc @JoviDeCroock who wanted to implement in Snowpack

Requirements

  • If a changed file has no accept() handler, the client needs a way to “bubble up” the change event to be handled by it’s parent(s)
  • If a file has multiple parents, the event must bubble up each parent’s chain of imports.
  • If an event bubbles up to a dependency tree entrypoint, trigger a full page refresh.
  • No import.meta.hot API changes, if possible
  • The server cannot scan a file that hasn’t been requested by the client (implication: no “full application” scanning step allowed)

The “Let the Server Do All The Work” Proposal

  • No change to the Server->Client interface. Keep existing update message format:

    • {type: 'update': url}.
  • The server stores a representation of your website dependency tree in memory:

    DependencyTree {
      [url: string]: {
        dependents: url[];
        dependencies: url[];
        isHmrEnabled: boolean;
      }
    }
    
  • At server startup, DependencyTree is empty.

  • Every time we serve a file, we scan the JS response and then update the DependencyTree with information about that one file. This never scans more than that one response.

  • Every time a file changes, we call updateOrBubble(file):

    function updateOrBubble(url) {
      node = DependencyTree[url];
      if (node.isHmrEnabled) {
        send to the client: `{type: 'update': url: file}`
      } else if (node.dependents.length > 0) {
        node.dependents.map(updateOrBubble);
      } else {
        // We've reached the top, trigger a full page refresh
        send to the client: `{type: 'reload'}`
      }
    }
    

Open Questions

  • Is it okay to send “update” events as we traverse the DependencyTree, or should we send them all at once in a batch? Webpack seems to send them in batches, but I believe this is more a limitation of bundling and not actually a requirement for good HMR.
  • How do we track our progress through the DependencyTree so that we never call updateOrBubble() twice on the same node (or, would we ever need to?)
  • Make sure that we can handle added/moved/removed imports in the DependencyTree.
  • Make sure that we can handle added/moved/removed files in the DependencyTree.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JoviDeCroockcommented, May 20, 2020

Revisiting the following statement:

I’m trying to consider the impact of non-batching and I think I’m getting there. What if we have a function that is shared across x components. Won’t we potentially render a stale tree if we have a series of updates rather than one big batch?

Since we are doing it in two passes:

  • Pass 1 bubble up and mark modules
  • pass 2 top down imports

It should not be possible to get stale trees due to an outdated parent.

Also we added the rewriting of imports, imagine a scenario where B (no accept) bubbles up to A (accept). When A is triggered again the import of B will be stale, that’s why beforee we send a hot response back we’ll rewrite the imports to match the ?mtime A was requested with

0reactions
FredKSchottcommented, May 21, 2020

Merged here + snowpack published within snowpack v2.0.0-rc.3 + esm-hmr v0.1.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bubbling and capturing - The Modern JavaScript Tutorial
A bubbling event goes from the target element straight up. Normally it goes upwards till <html> , and then to document object, and...
Read more >
Event Bubbling in JavaScript – How Event Propagation Works ...
Event Bubbling is a concept in the DOM (Document Object Model). It happens when an element receives an event, and that event bubbles...
Read more >
Event Bubbling in JavaScript? Event Propagation Explained
Event bubbling is a term you might have come across on your JavaScript travels. It relates to the order in which event handlers...
Read more >
Deep dive into JavaScript event bubbling and capturing
Create event propagations using JavaScript's event bubbling and capturing methods to avoid firing a child and parent element simultaneously.
Read more >
Event.bubbles - Web APIs - MDN Web Docs
A boolean value, which is true if the event bubbles up through the DOM tree. Example. function handleInput(e) { // ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found