This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 846
/
offline.coffee
212 lines (155 loc) · 5.09 KB
/
offline.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# We get a clue that the browser might be offline from suspicious requests or
# the HTML5 offline api. If we suspect it's offline, we make a request for any random path
# which (probably) doesn't exist. If we get a response, we're still online, if not,
# we trigger an event and update our status.
extendNative = (to, from) ->
for key of from::
try
val = from::[key]
if not to[key]? and typeof val isnt 'function'
to[key] = val
catch e
Offline = {}
Offline.options ?= {}
defaultOptions =
checks:
xhr:
url: ->
"/offline-test-request/#{ Math.floor(Math.random() * 1000000000) }"
image:
url: ->
# This can be any image, feel free to use our ultra-small gif:
"http://dqakt69vkj09v.cloudfront.net/are-we-online.gif?_=#{ Math.floor(Math.random() * 1000000000) }"
active: 'image'
checkOnLoad: false
interceptRequests: true
grab = (obj, key) ->
cur = obj
for part, i in key.split('.')
cur = cur[part]
break if typeof cur isnt 'object'
cur
Offline.getOption = (key) ->
val = grab(Offline.options, key) ? grab(defaultOptions, key)
if typeof val is 'function'
val()
else
val
# These events are available in modern browsers, but they mean different things.
# In FF and IE they mean the user has explicitly entered "Offline Mode"
# In Chrome they mean that the internet connection was lost or restored
window.addEventListener? 'online', ->
# The event fires slightly before the browser is ready to make a request
setTimeout Offline.confirmUp, 100
, false
window.addEventListener? 'offline', ->
Offline.confirmDown()
, false
Offline.state = 'up'
Offline.markUp = ->
Offline.trigger 'confirmed-up'
return if Offline.state is 'up'
Offline.state = 'up'
Offline.trigger 'up'
Offline.markDown = ->
Offline.trigger 'confirmed-down'
return if Offline.state is 'down'
Offline.state = 'down'
Offline.trigger 'down'
handlers = {}
Offline.on = (event, handler, ctx) ->
events = event.split(' ')
if events.length > 1
Offline.on(e, handler, ctx) for e in events
else
handlers[event] ?= []
handlers[event].push [ctx, handler]
Offline.off = (event, handler) ->
return unless handlers[event]?
if not handler
handlers[event] = []
else
i = 0
while i < handlers[event].length
[ctx, _handler] = handlers[event][i]
if _handler is handler
handlers[event].splice i--, 1
Offline.trigger = (event) ->
if handlers[event]?
for [ctx, handler] in handlers[event]
handler.call(ctx)
checkXHR = (xhr, onUp, onDown) ->
checkStatus = ->
if xhr.status and xhr.status < 12000
onUp()
else
onDown()
if xhr.onprogress is null
# onprogress would be undefined on older browsers
xhr.addEventListener 'error', onDown, false
xhr.addEventListener 'timeout', onDown, false
xhr.addEventListener 'load', checkStatus, false
else
_onreadystatechange = xhr.onreadystatechange
xhr.onreadystatechange = ->
if xhr.readyState is 4
checkStatus()
else if xhr.readyState is 0
onDown()
_onreadystatechange?(arguments...)
Offline.checks = {}
Offline.checks.xhr = ->
xhr = new XMLHttpRequest
# It doesn't matter what this hits, even a 404 is considered up. It is important however that
# it's on the same domain and port, so CORS issues don't come into play.
xhr.open('GET', Offline.getOption('checks.xhr.url'), true)
checkXHR xhr, Offline.markUp, Offline.markDown
xhr.send()
xhr
Offline.checks.image = ->
img = document.createElement 'img'
img.onerror = Offline.markDown
img.onload = Offline.markUp
img.src = Offline.getOption('checks.image.url')
undefined
Offline.check = ->
Offline.trigger 'checking'
Offline.checks[Offline.getOption('checks.active')]()
Offline.confirmUp = Offline.confirmDown = Offline.check
Offline.onXHR = (cb) ->
monitorXHR = (req, flags) ->
_open = req.open
req.open = (type, url, async, user, password) ->
cb {type, url, async, flags, user, password, xhr: req}
_open.apply req, arguments
_XMLHttpRequest = window.XMLHttpRequest
window.XMLHttpRequest = (flags) ->
req = new _XMLHttpRequest(flags)
monitorXHR req, flags
_setRequestHeader = req.setRequestHeader
req.headers = {}
req.setRequestHeader = (name, value) ->
req.headers[name] = value
_setRequestHeader.call req, name, value
_overrideMimeType = req.overrideMimeType
req.overrideMimeType = (type) ->
req.mimeType = type
_overrideMimeType.call req, type
req
extendNative window.XMLHttpRequest, _XMLHttpRequest
if window.XDomainRequest?
_XDomainRequest = window.XDomainRequest
window.XDomainRequest = ->
req = new _XDomainRequest
monitorXHR req
req
extendNative window.XDomainRequest, _XDomainRequest
init = ->
if Offline.getOption 'interceptRequests'
Offline.onXHR ({xhr}) ->
checkXHR xhr, Offline.confirmUp, Offline.confirmDown
if Offline.getOption 'checkOnLoad'
Offline.check()
# We call init in a setTimeout to give time for options to be set
setTimeout init, 0
window.Offline = Offline