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.

Lifecycle of elements

See original GitHub issue

I’ve been trying to work out a sensible way to handle the lifecycle of elements. It would be useful to know when an element is added to the document, when it’s morphed, and when it’s discarded. Though this functionality exists (aside from creation), it’s not very well set up to allow you to respond to these changes on a per-element basis.

var button = document.createElement('button')
button.on('update', function () {
  button.focus()
})

Some other inspiration… https://github.com/substack/virtual-hyperscript-mount https://github.com/substack/virtual-hyperscript-hook virtual-dom: https://github.com/Matt-Esch/virtual-dom/blob/master/docs/hooks.md React: http://facebook.github.io/react/docs/component-specs.html

What do you think? I would like to put a library together that wraps morphdom to provide this functionality, but I’m not sure if I’ve got the wrong idea.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
patrick-steele-idemcommented, Jul 18, 2016

@callum

I like what you did with morphdom-hooks, but what are your thoughts on changing the API a bit:

constant morphdomHooks = require('morphdom-hooks');
const b = document.createElement('button');
morphdomHooks.on(b, 'add', console.log.bind(console, 'button added to page!'))
morphdomHooks.on(b, 'update', console.log.bind(console, 'button updated in page!'))
morphdomHooks.on(b, 'discard', console.log.bind(console, 'button discarded from page!'))

I realize that makes the user code dependent on morphdom-hooks wherever listeners are to be added, but maybe that is okay?

@AutoSponge

I would prefer an eventEmitter implementation here.

I think an EventEmitter API is better exposed as a wrapper to keep this library as small and as fast as possible.

@yoshuawuyts

The one thing it doesn’t allow doing, however, is setting the equivalent of React’s .shouldComponentUpdate(), which means that morphdom will always diff the complete trees. It’d be pretty cool if we could apply some smart heuristics to determining if a particular branch of a DOM tree should be diffed (perhaps by going full Merkle-tree? - idk)

As @AutoSponge mentioned, you can tell morphdom to skip parts of a DOM subtree by returning false in the onBeforeElUpdated callback.

Good discussion, but closing issue to do avoid keeping issues open for too long. If you have thoughts or comments please feel free to add here or open a new issue. Thanks all.

0reactions
AutoSpongecommented, Jul 2, 2016

@yoshuawuyts Agreed. shouldComponentUpdate would be helpful for large interfaces to either not render out-of-viewport or delegate it to a background/idle task. While I haven’t tried that yet, I suspect everything we need is already in morphdom via the #onBefore... hooks.

By returning false, I can prevent a node/tree from rendering. If I then cache that node in a map with the target that I would have rendered, I can create an idle worker to update cached nodes by calling morphdom on each cache pair.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lifecycle - Lit.dev
Lit components are standard custom elements and inherit the custom element lifecycle methods. For information about the custom element lifecycle, see Using the ......
Read more >
The life cycle of the elements: rocks, soils, organisms ...
This workshop will identify forefront research questions probing the life cycle of elements, especially metals, moving through the environment and living ...
Read more >
Universal element formation - Science Learning Hub
Click on each stage of the star life cycle and discover how the elements that we are all made up of were created....
Read more >
Global cycles of life-giving elements | AMNH
Elements that sustain life stay in the Earth's reservoirs for different lengths of time. Carbon in the form of coal or carbonate rocks...
Read more >
8 Steps in the Data Life Cycle - HBS Online
Data Life Cycle Stages · 1. Generation · 2. Collection · 3. Processing · 4. Storage · 5. Management · 6. Analysis ·...
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