wilsonpage / fastdom

Eliminates layout thrashing by batching DOM measurement and mutation tasks
6.85k stars 240 forks source link

How to handle calls to Defer function #79

Closed abhijeetsane closed 8 years ago

abhijeetsane commented 8 years ago

Hi , I am working on a code set that is using the 0.8.6 version of fastdom. On migrating to newer 1.0.1 version of fastdom , I am seeing that there are calls to "defer()" function which need to be replaced as defer() is now deprecated. Should i just remove the references to defer or replace defer by "mutate() or "measure"

Please guide .

-Abhijeet Sane

wilsonpage commented 8 years ago

Hi @abhijeetsane :) What kind of logic is inside the .defer() task? .defer() would have postpone the given task until the given number of frames, or if no frames argument given, then next frame.

hhamilton-turner commented 8 years ago

+1

I have a similar issue. I'm upgrading to the 1.0.1 version and noticed the fastdom.defer function is no longer available. I am currently using the defer function in the 0.8.6 version to perform a task at a specific frame, for example frame 5. How can I do this in version 1.0.1 since the defer function is no longer available?

wilsonpage commented 8 years ago

A quick hack would be:

requestionAnimationFrame(() => {
  requestionAnimationFrame(() => {
    requestionAnimationFrame(() => {
      requestionAnimationFrame(() => {
        fastdom.mutate(() => {
          // do stuff ...
        })
      });
    });
  });
});

Make sure to use the correct API (.measure/.mutate) based on the logic in your containing task.

wilsonpage commented 8 years ago

As long as DOM mutation/measurement code remains inside a fastdom. task, you should be safe :)

hhamilton-turner commented 8 years ago

Ok thanks for the hack, but is it fair to say that you won't be adding the defer function back into your API? If so, can your extend API safely be used to add the defer functionality back manually (https://github.com/wilsonpage/fastdom/blob/v0.8.6/index.js#L122)? I see that defer depends on schedule and a boolean, so I would have to manually add those back in too.

wilsonpage commented 8 years ago

@hhamilton-turner we have no intension of re-introducing the .defer() API, it's use-cases are few and far between, and we wanted to give the library more focus around it's core proposition.

The internals of fastdom changes quite a bit with the v1 release, so I wouldn't look to the old code as an example. You could use extend to add something on the same principles as the hack I posted, but I would ask yourself what your use-case is and try to work around it a different way.