I have a requirement where I have to move all the user's worklist items to a workbasket when their session ends. This should happen whether they log off, the session times out, or any other termination event such as connection lost or the browser window closed.
I know we can specialize Code-Security.LogOff to cover when they log off but what about all the other events that end the session?
I am not aware of a good extension point for doing what you'd want to do. I think you'd have to look at various cases and plan accordingly. You could definitely do things like onBeforeUnload() to catch the closing of the browser window, but there are other legitimate reasons to unload so you need to find and code around them (i.e. I really wouldn't suggest it). Likewise, you want to make sure that your code doesn't fire when a requestor passivates. You might be better off creating an agent that looks at the assign-worklist table for users who aren't currently online and then reassigns their work. It won't be real time, but it will be a single point to maintain and not a patchwork of approaches each with their own unknowingly large list of exceptions and caveats.
I've been told that there's no way to detect that the browser has been closed, other than to send a periodic keep-alive question and if you don't get an answer, assume the browser went away.
You're probably better off keeping items in the work basket until the user is actually working on them, and then once the user starts working on them, leave them in the user's work list until they're done or some SLA times out. That way, if user goes to lunch, and will resume work on the item when they return, you don't try to take the item and put it back in the work basket.