Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change behavior of onCreate/onRender #17

Open
Gusted opened this issue Jun 1, 2021 · 2 comments
Open

Change behavior of onCreate/onRender #17

Gusted opened this issue Jun 1, 2021 · 2 comments

Comments

@Gusted
Copy link
Contributor

Gusted commented Jun 1, 2021

At this moment at the very start malevic will render every child/node and thus calling onCreate once and on every node changes, every thing that specified onRender will also be executed.

as darkreader/darkreader#5926 indicates, it doesn't need to be this way. Especially when certain functions like rendering a canvas can be quite on the heavy side for the rendering.

Could the same technique be used to just execute(maybe even create these nodes) until they are visible?

@alexanderby
Copy link
Owner

But you will not find out if it is visible or not until you create a node. To perform some kind of lazy rendering, you have to subscribe on window resize and scroll events (not applicable to Dark Reader's UI), and do some custom logic inside a component, to check if it is visible and to render its content separately, for example:

import {m} from 'malevic';
import {sync, render, getContext} from 'malevic/dom';

export default function LazyContainer(props, ...content) {
    const context = getContext();
    context.onRender((node) => {
        if (node.offsetHeight === 0) { // or maybe some more stable check
            render(node, content);
        }
    };
    if (!context.node) {
        const element = context.node || document.createElement('div');
        sync(element, <div class="lazy-element"></div>);
        return element;
    }
    return context.node;
}

@Gusted
Copy link
Contributor Author

Gusted commented Jun 1, 2021

Sounds about right and good for now. Considering that it the execute function is in most cased under a millisecond and that these big render functions can be mitigated to check the offsetXXX property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants