Sorcerer's Apprentice Bug and Solution
-
Ever heard of Sorcerer’s Apprentice? It is a poem by Goethe, set to music by Paul Dukas, and visualized by Walt Disney in the Movie ‘Fantasia’. (See: http://www.youtube.com/watch?v=XChxLGnIwCU)
In the movie, Mickey Mouse, the apprentice, dons the Sorcerer’s cap to animate a broom so that it will do Mickey’s chores for him. The broom soon gets out of control, and Mickey is forced to hack up the broom with an axe. But then each of the splinters of the broom become a broom, and Mickey’s troubles intensify.What does this have to do with bbPress? An interesting bug. The subscribe (and Favorites) toggle don’t work properly in Topics. Click once, good, state and text go to ‘unsubscribe’. Click again, good, state and text go back to ‘subscribe’. Click again, and nothing *appears* to happen. I’m new to bbPress, but I thought I’d take a shot at troubleshooting. The implementation uses javascript, jquery, ajax, and a wordpress jquery plugin called wpList. In the bbpress file topic.js, the function subsLinkSetup() is passed as an event handler for a click on the ‘Subscribe’/’Unsubscribe’ link. But every time this event handler is called, it ‘delegates’ a new instance of itself, and each new instance delegates a new instance, and so on…
For each click, the number of event handlers grows as follows:
Click # of Event Handlers
1 1
2 2
3 4
4 8
5 16
6 32
n 2^(n-1)
So after the second click, there are two event handlers.
On the 3rd click, the two event handlers each delegate a new event handler(now there are 4), each toggle the subscription state, (essentially canceling each other out) and the state appears unchanged. Since the number of event handlers is always even, their collective execution will always return the subscription state to the same state as that before they were executed.Example: 3rd click, 4 event handlers, state: subscribed, Event handlers result in the following state chain: unsubscribed->subscribed->unsubscribed->subscribed, and were are right back where we started from.
This is a dangerous bug, because when nothing happens, users tend to click again and again, and 10 clicks (for example) = 2^9=512 event handlers.A fix that works, but might not be the correct one, because it breaks the encapsulation of wpList (we must peer inside wpList to know that the event handler is bound to the subscription link via .delegate()) is to do the following:
In topic.js, function ubsLinkSetup(), replace the line
subscriptionToggle.get(0).wpList.process( subscriptionToggle );
with
subscriptionToggle.get(0).wpList.process( subscriptionToggle.undelegate() );
This removes the event handler before adding it back.
Parallel fix for favorites.Hoping to see some fix in the next release
Mumbling
- You must be logged in to reply to this topic.