]> git.gir.st - VimFx.git/blob - extension/includes/unload.coffee
Closes #120. Cleaned up locales folder. Fixed logic of locale discovery.
[VimFx.git] / extension / includes / unload.coffee
1 unload = do ->
2 # Initialize the array of unloaders on the first usage
3 unloaders = []
4
5 return (callback, container) ->
6
7 # Calling with no arguments runs all the unloader callbacks
8 if !callback
9 unloader() for unloader in unloaders
10 unloaders.length = 0
11
12 # The callback is bound to the lifetime of the container if we have one
13 else if container
14 # Remove the unloader when the container unloads
15 container.addEventListener('unload', removeUnloader, false)
16
17 # Wrap the callback to additionally remove the unload listener
18 origCallback = callback
19 callback = ->
20 container.removeEventListener('unload', removeUnloader, false)
21 origCallback()
22
23 # Wrap the callback in a function that ignores failures
24 unloader = -> try callback()
25 unloaders.push(unloader)
26
27 # Provide a way to remove the unloader
28 removeUnloader = ->
29 index = unloaders.indexOf(unloader)
30 if index > -1
31 unloaders.splice(index, 1)
32
33 return removeUnloader
Imprint / Impressum