2 # Initialize the array of unloaders on the first usage
5 return (callback, container) ->
7 # Calling with no arguments runs all the unloader callbacks
9 unloader() for unloader in unloaders
12 # The callback is bound to the lifetime of the container if we have one
14 # Remove the unloader when the container unloads
15 container.addEventListener "unload", removeUnloader, false
17 # Wrap the callback to additionally remove the unload listener
18 origCallback = callback
20 container.removeEventListener "unload", removeUnloader, false
23 # Wrap the callback in a function that ignores failures
24 unloader = -> try callback()
25 unloaders.push(unloader)
27 # Provide a way to remove the unloader
29 index = unloaders.indexOf unloader
31 unloaders.splice(index, 1)