@@ -4,7 +4,13 @@ import {enableCopyButtons} from "./pre.js";
4
4
5
5
export * from "./index.js" ;
6
6
7
+ let minReopenDelay = 1000 ;
8
+ let maxReopenDelay = 30000 ;
9
+ let reopenDelay = minReopenDelay ;
10
+ let reopenDecay = 1.1 ; // exponential backoff factor
11
+
7
12
export function open ( { hash, eval : compile } = { } ) {
13
+ let opened = false ;
8
14
const socket = new WebSocket (
9
15
Object . assign ( new URL ( "/_observablehq" , location . href ) , {
10
16
protocol : location . protocol === "https:" ? "wss" : "ws"
@@ -13,6 +19,8 @@ export function open({hash, eval: compile} = {}) {
13
19
14
20
socket . onopen = ( ) => {
15
21
console . info ( "socket open" ) ;
22
+ opened = true ;
23
+ reopenDelay = minReopenDelay ;
16
24
send ( { type : "hello" , path : location . pathname , hash} ) ;
17
25
} ;
18
26
@@ -123,12 +131,10 @@ export function open({hash, eval: compile} = {}) {
123
131
}
124
132
} ;
125
133
126
- socket . onerror = ( error ) => {
127
- console . error ( error ) ;
128
- } ;
129
-
130
134
socket . onclose = ( ) => {
131
- console . info ( "socket close" ) ;
135
+ if ( opened ) console . info ( "socket close" ) ;
136
+ reopenDelay = Math . min ( maxReopenDelay , reopenDelay * reopenDecay ) ; // exponential backoff
137
+ setTimeout ( ( ) => open ( { hash, eval : compile } ) , reopenDelay ) ;
132
138
} ;
133
139
134
140
function indexCells ( map , node ) {
0 commit comments