Skip to content

Instantly share code, notes, and snippets.

@dv
Last active March 2, 2021 04:05
Show Gist options
  • Save dv/fc51f798d825bff5a371 to your computer and use it in GitHub Desktop.
Save dv/fc51f798d825bff5a371 to your computer and use it in GitHub Desktop.
Fix setTimout, setInterval, and global ajax requests when using Turbolinks
# This library fixes common problems with turbolinks
# - Overwrite setTimeout and setInterval to intercept generated ID's
# - Keep track of Ajax requests
#
# When turbolinks' unload event is called, we:
# - Cancel all setTimeouts and setIntervals
# - Abort all still running Ajax requests
$.turboTurbo =
xhrPool: [],
setTimeoutPool: [],
setIntervalPool: [],
setup: ->
window.turboTurboSetTimeout = window.setTimeout
window.turboTurboSetInterval = window.setInterval
window.setTimeout = (func, delay) ->
timeoutId = window.turboTurboSetTimeout(func, delay)
$.turboTurbo.setTimeoutPool.push(timeoutId)
timeoutId
window.setInterval = (func, interval) ->
intervalId = window.turboTurboSetInterval(func, interval)
$.turboTurbo.setIntervalPool.push(intervalId)
intervalId
$.ajaxSetup
beforeSend: (jqXHR) -> $.turboTurbo.xhrPool.push(jqXHR),
complete: (jqXHR) -> $.turboTurbo.xhrPool.splice($.inArray(jqXHR, $.turboTurbo.xhrPool), 1),
$(document).on "page:before-unload", ->
$.turboTurbo.unload()
unload: ->
while xhr = $.turboTurbo.xhrPool.pop()
xhr.abort()
while timeoutId = $.turboTurbo.setTimeoutPool.pop()
clearTimeout(timeoutId)
while intervalId = $.turboTurbo.setIntervalPool.pop()
clearInterval(intervalId)
$.turboTurbo.setup()
#
# =NMN?
# 8M?DMO?=
# $MDIIINM+
# NN$III?OD+
# ==MDIIIII?NM8
# =INI???III??$MZ
# =OO?????I?I??ZNM
# +N7?????IIIII??ZM?
# +N7????I?IIIIII?NM=
# ?N7IIIIII??III?I?ON
# IN$IIIII???IIODNNMMM?
# ?D$IZDNMMNZI= IZ8MMI
# 8MM8+ ?7?.... DD+ =OMM
# O+..NMNI8M8 ?MD... . .$8 ==
# Z?.$Z.... ZMM8. $.. ..M7 MDNM?
# $7 M...+D .IM+ IM8 . ..MO 8 ..OM
# I$+M. .NM. M=.. . . .=M? M...=M
# =8?M..... . MMMMMO=+NMM +M... M
# MZZD7= ?MMM.. 8M?..Z7 IM...=M
# $DMMZ DM 88DDD7MO. . .?M8$NMMO =M...7M
# M+. $8 IM7.IDMMMMD . . NMI ZM? +N88MMMN7
# M=...MZ NDMD=.. 7M OMD.....DM+ Z8+ = $M=
# MI...M8 .7MO... ..7MMMMMD+ ++ ..OMI N ......+M+
# NZ. .MI =OMMD . . .. .=MM$8M ZMMMM7 +MMNO$ ..MO
# ?MO . MZ =D... . D MMMMMZIM+.?O.. ..OM M ......MZ
# N ....NN .ZI.... .O .=O..M$...M8D?.....OM MZ ... MO
# IM...?7M+ .Z+... ..87 D ..ND MM8.....8M8 DMMN$. 88MM=
# IM..=NZMMI .$7.......8MMNOONMMMM?.....+MMMN M ... OM+OM
# IM......$M M8= .......=?I?+ ............OD MD? 8MDIOM
# =M......=M $M7....... ................. M7=++MNNMMD$IIZM
# ?N .$NMM$ . 8?........ ............ $MMNI?IIII7IIIIII8MD+=
# 7MN ...7M 8M8 ... Z........... NMMMIIIIIIII77IIIII7MZ+
# =M+DM$?INM =MMINM8? =NM8 ... NM?+ZMD=$MZ?IIIII7IIIII$NM? =
# N+??8DOIIMMMMD7III78MMNZ?NMMDDMMO8DO+??+ONI??IIIII?I8NM8=
# =N?IIII?+=++I??IIII?$O?II?II777??????III?8MMMMMMNNMMMO?
# OI?IIIIIIIIIIIIII$NMO?IIIIII???+???III?$M+ ===++=
# =MZIIIIIIIIIIIII$MD?M7?IIII???III??IIIZMM
# 7NMMND88O8NMMZ IMZ?IIII??IIIIII7DMM$
# =?7$Z$ZI= MMN7I??????7ONMM?DM
# M8ZNNMMMMMMMDDZ??7M
# M8??$M +?+= IDI?+OO
# MO??OM INI???M?
# MZ?IOM ?NI??+MO
# 7N$$$8M IMMMDO7$DMN
# ZMOOO8MNMM N?.......=M
# +M?....... M7 OM........?M
# 7$ ...... =M$ $M.....=IDMD
# NMMMMMMMMM? 8MMMMMMMO$
# =
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment