44 * See License-AGPL.txt in the project root for license information.
55 */
66
7- import { GitpodClient , GitpodServer , GitpodServerPath , GitpodService , GitpodServiceImpl } from '@gitpod/gitpod-protocol' ;
7+ import { GitpodClient , GitpodServer , GitpodServerPath , GitpodServiceImpl } from '@gitpod/gitpod-protocol' ;
88import { WebSocketConnectionProvider } from '@gitpod/gitpod-protocol/lib/messaging/browser/connection' ;
99import { createWindowMessageConnection } from '@gitpod/gitpod-protocol/lib/messaging/browser/window-connection' ;
1010import { JsonRpcProxy , JsonRpcProxyFactory } from '@gitpod/gitpod-protocol/lib/messaging/proxy-factory' ;
@@ -16,6 +16,9 @@ export const gitpodHostUrl = new GitpodHostUrl(window.location.toString());
1616
1717function createGitpodService < C extends GitpodClient , S extends GitpodServer > ( ) {
1818 let proxy : JsonRpcProxy < S > ;
19+ let reconnect = ( ) => {
20+ console . log ( "WebSocket reconnect not possible." ) ;
21+ }
1922 if ( window . top !== window . self ) {
2023 const connection = createWindowMessageConnection ( 'gitpodServer' , window . parent , '*' ) ;
2124 const factory = new JsonRpcProxyFactory < S > ( ) ;
@@ -28,6 +31,12 @@ function createGitpodService<C extends GitpodClient, S extends GitpodServer>() {
2831 . withApi ( ) ;
2932
3033 const connectionProvider = new WebSocketConnectionProvider ( ) ;
34+ let _websocket : any ;
35+ const _createWebSocket = connectionProvider . createWebSocket ;
36+ connectionProvider . createWebSocket = ( url ) => {
37+ return ( _websocket = _createWebSocket ( url ) ) ;
38+ }
39+
3140
3241 let numberOfErrors = 0 ;
3342 proxy = connectionProvider . createProxy < S > ( host . toString ( ) , undefined , {
@@ -38,18 +47,28 @@ function createGitpodService<C extends GitpodClient, S extends GitpodServer>() {
3847 }
3948 }
4049 } ) ;
50+
51+ if ( _websocket && "reconnect" in _websocket ) {
52+ reconnect = ( ) => { ( _websocket as any ) . reconnect ( ) } ;
53+ }
4154 }
4255 const service = new GitpodServiceImpl < C , S > ( proxy ) ;
56+ ( service as any ) . reconnect = reconnect ;
4357 return service ;
4458}
4559
60+ declare global {
61+ interface Window { gitpodService ?: ReturnType < typeof createGitpodService > & { reconnect ?: ( ) => void } ; }
62+ }
4663
47- let gitpodService : GitpodService ;
64+ // reuse existing service object if present
65+ let gitpodService = window . gitpodService || ( window . gitpodService = createGitpodService ( ) ) ;
4866
67+ // allow to reconnect
4968const reconnect = ( ) => {
50- gitpodService = createGitpodService ( ) ;
69+ if ( window . gitpodService ?. reconnect ) {
70+ window . gitpodService ?. reconnect ( ) ;
71+ }
5172}
5273
53- reconnect ( ) ;
54-
5574export { gitpodService , reconnect } ;
0 commit comments