Asynchronous switch functions don't work #72
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The documents make it seem like asynchronous functions can be used as switching functions, since there's a callback
this.onSwitchwe're asked to call.I presumed this meant I could do animations and such, and actually switch the content once I am ready, and then call
this.onSwitchonce finished. The node would then be parsed for new links etc at that point.But that's not how it works -- all
this.onSwitchdoes is trigger theresizeandscrollevents. So the DOM is parsed for new links etc before the markup has been switched, in my case, and so the handlers aren't attached to any links in the new markup.Would it be a simple matter to add support for async switching functions? If you're expecting the user to call
this.onSwitchanyway, you could just treat all switching functions as async.My current workaround is to call
this.parseDOM(document)once all my switches are finished.Can't remember how I did this since it's a long time ago, but pretty sure you can do async stuff. I was using this
259dc4b098/src/assets/_scripts/vanilla.js (L42-L95)That works around the issue by immediately inserting the new content side by side with the old. It's then successfully parsed immediately and click handlers are added. It therefore doesn't suffer from the issue. Then the async part is removing the old content later.
The execution of the pjax code isn't halted at all.
If the new content isn't added until later, the
parseDOMmethod isn't run over it.@tremby What type of of async operations were you doing? Pjax is basically synchronous, so even if you have an animation that takes some time, execution won't continue until it's done, and
parseDOM()won't be called until then.@BehindTheMath, I don't remember since it was a year and a half ago, but my original post here mentions animations, so that's a good bet. What you said would only be true if the animation were entirely synchronous. I don't know how one would animate something on a web page with entirely synchronous code.
@tremby Pardon my ignorance; I'm primarily a backend developer, so I don't have experience with animations.
I think the code as it stands today isn't structured sensibly. #79 fixes this issue, but that just worsens #46, since
parseDOM(document)is being called after every switch finishes. That's besidesparseDOM(el)being called for each switch after the switches are triggered, even if they're async.Instead, I think it would be better to call
parseDOM(el)only fromonSwitch(), and require the user to pass the current element when callingonSwitch()This might change depending on how we fix #46, but at least for now, I think this will simplify things.
Seems reasonable that you might detect whether the user wants to do this asynchronously or synchronously, then call
parseDOMautomatically if synchronous, or as part of the callback if asynchronous.I'm not sure how we would be able to detect that.
What we might be able to do, is assume it's synchronous unless the user returns a truthy value from the switch. The WebExtensions Messaging API has something like that.
Yes, I meant by the user telling advising pjax that async is wanted. That wasn't at all clear; sorry.
But if I'm reading the docs properly, the default switching function calls the callback anyway. That means you can rely on it being called at the right moment in all cases, right? So what you suggested before, only running the post-switch logic in onSwitch, seems like the thing to do. No need to know or care if the switching fiction is sync or async.