how to handle such scenarios:
yairEO opened this issue · 4 comments
written in pseudo code:
// do the bellow on "scroll" event
if( readDom < 0 )
return;
...do stuff...
writeSomethingToDOM
This creates a synced situation, that the rest of the code might or might not run, depending if readDom is greater than zero or not. How could such case be solved since fastdom seperates reads and writes in an async way, how could I even use fastdom here?
I'm not sure I fully understand your problem. FastDOM is supposed to abstract these problems away from you so you just read when you want, and write when you want.
do you mean:
fastdom.read(function(){
if(!something)
return;
fastdom.write(function(){
// write something
});
});
that "something" is a readDom, as I said. at the end, I just this this:
// do the bellow on "scroll" event
var foo;
fastdom.read(function(){
foo = readSomeDOM;
});
fastdom.write(function(){
if( foo < 0 )
return;
...do stuff...
writeToDOM
});
So the if
statement must move inside the fastdom.write()
, there is no other way around it, since it's async. using fastdom isn't so easy sometimes.
I find it quite difficult to use fastdom to "fix" existing other slow codes, which does heavy read-write. such plugin would be this for example: https://github.com/ed-lea/jquery-collagePlus
where reads and writes are mixed all over the place, it's quite a job to wrap things in the right place.
also, please check my open issue on your jquery-fastdom, I have a javascript error from your code... thanks!
Andrew's example looks like the best solution. You should use nesting if a write depends on a read.
Jquery-fastdom is a very slow work in progress. I'm looking for people to help finish it as I've only wrapped a few of the APIs.