-
Notifications
You must be signed in to change notification settings - Fork 19
/
onResize.coffee
43 lines (41 loc) · 1.1 KB
/
onResize.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# out: ../onResize.js
if window
resizeRunning = false
allResizeCbs = []
resizeHandler = ->
unless resizeRunning
resizeRunning = true
if window.requestAnimationFrame
window.requestAnimationFrame callResizeCbs
else
setTimeout callResizeCbs, 66
callResizeCbs = (e) ->
for cb in allResizeCbs
cb(e)
resizeRunning = false
window.addEventListener "resize", resizeHandler
observer = new MutationObserver resizeHandler
observer.observe document.body,
attributes: true
childList: true
characterData: true
subtree: true
module.exports =
data: ->
resizeCbDisposables: []
methods:
addResizeCb: (cb) ->
allResizeCbs.push cb
dispose = ->
index = allResizeCbs.indexOf cb
if index > -1
allResizeCbs.splice index,1
@resizeCbDisposables.push dispose
return =>
dispose()
index = @resizeCbDisposables.indexOf dispose
if index > -1
@resizeCbDisposables.splice index,1
beforeDestroy: ->
for resizeCbDisposable in @resizeCbDisposables
resizeCbDisposable()