File tree Expand file tree Collapse file tree 1 file changed +29
-7
lines changed Expand file tree Collapse file tree 1 file changed +29
-7
lines changed Original file line number Diff line number Diff line change 1
1
( ( ) => {
2
+ const TURBO_LOAD_TIMEOUT = 4000
3
+
2
4
// Bridge between Turbo JS and native code. Built for Turbo 7
3
5
// with backwards compatibility for Turbolinks 5
4
6
class TurboNative {
5
- constructor ( ) {
6
- this . registerAdapter ( )
7
- }
8
-
9
7
registerAdapter ( ) {
10
8
if ( window . Turbo ) {
11
9
Turbo . registerAdapter ( this )
14
12
Turbolinks . controller . adapter = this
15
13
TurboSession . turboIsReady ( true )
16
14
} else {
17
- TurboSession . turboIsReady ( false )
18
- this . pageLoadFailed ( )
15
+ throw new Error ( "Failed to register the TurboNative adapter" )
19
16
}
20
17
}
21
18
170
167
}
171
168
172
169
window . turboNative = new TurboNative ( )
173
- window . turboNative . pageLoaded ( )
170
+
171
+ const setup = function ( ) {
172
+ window . turboNative . registerAdapter ( )
173
+ window . turboNative . pageLoaded ( )
174
+
175
+ document . removeEventListener ( "turbo:load" , setup )
176
+ document . removeEventListener ( "turbolinks:load" , setup )
177
+ }
178
+
179
+ const setupOnLoad = ( ) => {
180
+ document . addEventListener ( "turbo:load" , setup )
181
+ document . addEventListener ( "turbolinks:load" , setup )
182
+
183
+ setTimeout ( ( ) => {
184
+ if ( ! window . Turbo && ! window . Turbolinks ) {
185
+ TurboSession . turboIsReady ( false )
186
+ window . turboNative . pageLoadFailed ( )
187
+ }
188
+ } , TURBO_LOAD_TIMEOUT )
189
+ }
190
+
191
+ if ( window . Turbo || window . Turbolinks ) {
192
+ setup ( )
193
+ } else {
194
+ setupOnLoad ( )
195
+ }
174
196
} ) ( )
You can’t perform that action at this time.
0 commit comments