Skip to content

Commit ca57260

Browse files
committed
Add methods to manually enable and disable tracking. Fixes CodeByZach#35. Fixes CodeByZach#24.
1 parent ea8bcf3 commit ca57260

5 files changed

Lines changed: 180 additions & 66 deletions

File tree

docs/intro.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,48 @@ whenever `pushState` or `replaceState` is called by default.
162162

163163
- `Pace.stop`: Hide the progress bar and stop updating it.
164164

165+
- `Pace.track': Explicitly track one or more requests, see Tracking below
166+
167+
- `Pace.ignore`: Expliticly ignore one or more requests, see Tracking below
168+
169+
Tracking
170+
--------
171+
172+
By default, Pace will show any ajax requests which begin as a part of a normal or ajax-y page load, or which last longer than
173+
500ms.
174+
175+
You can disable all ajax tracking by setting `ajax` to false:
176+
177+
```javascript
178+
Pace.options = {
179+
ajax: false
180+
}
181+
```
182+
183+
You can disable ajax tracking except on page navigation by setting `restartOnRequestAfter` to false:
184+
185+
```javascript
186+
Pace.options = {
187+
restartOnRequestAfter: false
188+
}
189+
```
190+
191+
You can manually disable tracking for a specific request or requests by triggering them within a `Pace.ignore` callback:
192+
193+
```javascript
194+
Pace.ignore(function(){
195+
$.ajax(...)
196+
});
197+
```
198+
199+
You can force the progress bar to be shown for a specific request by triggering them within a `Pace.track` callback:
200+
201+
```javascript
202+
Pace.track(function(){
203+
$.ajax(...)
204+
});
205+
```
206+
165207
Dependencies
166208
------------
167209

pace.coffee

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,32 @@ extendNative = (to, from) ->
240240
to[key] = val
241241
catch e
242242

243+
ignoreStack = []
244+
245+
Pace.ignore = (fn, args...) ->
246+
ignoreStack.unshift 'ignore'
247+
ret = fn(args...)
248+
ignoreStack.shift()
249+
ret
250+
251+
Pace.track = (fn, args...) ->
252+
ignoreStack.unshift 'track'
253+
ret = fn(args...)
254+
ignoreStack.shift()
255+
ret
256+
257+
shouldTrack = (method='GET') ->
258+
if ignoreStack[0] is 'track'
259+
return 'force'
260+
261+
if not ignoreStack.length and options.ajax
262+
if method is 'socket' and options.ajax.trackWebSockets
263+
return true
264+
else if method.toUpperCase() in options.ajax.trackMethods
265+
return true
266+
267+
return false
268+
243269
# We should only ever instantiate one of these
244270
class RequestIntercept extends Events
245271
constructor: ->
@@ -248,7 +274,7 @@ class RequestIntercept extends Events
248274
monitorXHR = (req) =>
249275
_open = req.open
250276
req.open = (type, url, async) =>
251-
if (type ? 'GET').toUpperCase() in options.ajax.trackMethods
277+
if shouldTrack(type)
252278
@trigger 'request', {type, url, request: req}
253279

254280
_open.apply req, arguments
@@ -276,7 +302,8 @@ class RequestIntercept extends Events
276302
window.WebSocket = (url, protocols) =>
277303
req = new _WebSocket(url, protocols)
278304

279-
@trigger 'request', {type: 'socket', url, protocols, request: req}
305+
if shouldTrack('socket')
306+
@trigger 'request', {type: 'socket', url, protocols, request: req}
280307

281308
req
282309

@@ -288,30 +315,33 @@ getIntercept = ->
288315
_intercept = new RequestIntercept
289316
_intercept
290317

291-
if options.restartOnRequestAfter isnt false
292-
# If we want to start the progress bar
293-
# on every request, we need to hear the request
294-
# and then inject it into the new ajax monitor
295-
# start will have created.
318+
# If we want to start the progress bar
319+
# on every request, we need to hear the request
320+
# and then inject it into the new ajax monitor
321+
# start will have created.
296322

297-
getIntercept().on 'request', ({type, request}) ->
298-
if not Pace.running
299-
args = arguments
323+
getIntercept().on 'request', ({type, request}) ->
324+
if not Pace.running and (options.restartOnRequestAfter isnt false or shouldTrack(type) is 'force')
325+
args = arguments
300326

301-
setTimeout ->
302-
if type is 'socket'
303-
stillActive = request.readyState < 2
304-
else
305-
stillActive = 0 < request.readyState < 4
327+
after = options.restartOnRequestAfter or 0
328+
if typeof after is 'boolean'
329+
after = 0
330+
331+
setTimeout ->
332+
if type is 'socket'
333+
stillActive = request.readyState < 2
334+
else
335+
stillActive = 0 < request.readyState < 4
306336

307-
if stillActive
308-
Pace.restart()
337+
if stillActive
338+
Pace.restart()
309339

310-
for source in Pace.sources
311-
if source instanceof AjaxMonitor
312-
source.watch args...
313-
break
314-
, options.restartOnRequestAfter
340+
for source in Pace.sources
341+
if source instanceof AjaxMonitor
342+
source.watch args...
343+
break
344+
, after
315345

316346
class AjaxMonitor
317347
constructor: ->

pace.js

Lines changed: 80 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)