@@ -41,6 +41,7 @@ export class GephiLiteDriver extends TypedEventEmitter<GephiLiteEvents> {
41
41
private timeout : number = 2000 ;
42
42
private channel : BroadcastChannel ;
43
43
private pendingReplies : Map < string , ( payload : unknown ) => void > = new Map ( ) ;
44
+ private window : Window | null = null ;
44
45
45
46
constructor ( name : string = uuidV4 ( ) ) {
46
47
super ( ) ;
@@ -141,17 +142,48 @@ export class GephiLiteDriver extends TypedEventEmitter<GephiLiteEvents> {
141
142
setFilters ( filters : FiltersState ) {
142
143
return this . callMethod < SetFiltersMethod > ( "setFilters" , filters ) ;
143
144
}
145
+ getWindow ( ) {
146
+ return this . window ;
147
+ }
144
148
145
149
/**
146
150
* Helper/lifecycle methods:
147
151
* *************************
148
152
*/
149
- openGephiLite ( {
153
+ async openGephiLite ( {
150
154
baseUrl = "/gephi-lite" ,
151
155
target = "_blank" ,
152
156
features = "noopener" ,
153
- } : { baseUrl ?: string ; target ?: string ; features ?: string } = { } ) {
154
- return open ( `${ baseUrl } ?broadcast=${ this . name } ` , target , features ) ;
157
+ timeout = 10000 ,
158
+ } : { baseUrl ?: string ; target ?: string ; features ?: string ; timeout ?: number } = { } ) {
159
+ let url : URL | undefined = undefined ;
160
+ try {
161
+ url = new URL ( baseUrl ) ;
162
+ } catch {
163
+ // not an url, assuming a path, nothing to check
164
+ }
165
+
166
+ if ( url ) {
167
+ if ( url . host !== window . location . host ) {
168
+ throw new Error ( "Communicating with Gephi Lite is only possible on the same domain." ) ;
169
+ }
170
+ }
171
+
172
+ const gephiLiteWindow = await new Promise < Window | null > ( ( resolve , reject ) => {
173
+ const openingGephiLiteTimeout = timeout
174
+ ? setTimeout ( ( ) => {
175
+ reject ( new Error ( `Couldn't set up communication channel with Gephi Lite before ${ timeout } ` ) ) ;
176
+ } , timeout )
177
+ : null ;
178
+
179
+ // Wait for new instance to be fully working:
180
+ this . on ( "newInstance" , ( ) => {
181
+ if ( openingGephiLiteTimeout ) clearTimeout ( openingGephiLiteTimeout ) ;
182
+ resolve ( this . getWindow ( ) ) ;
183
+ } ) ;
184
+ this . window = open ( `${ baseUrl } ?broadcast=${ this . name } ` , target , features ) ;
185
+ } ) ;
186
+ return gephiLiteWindow ;
155
187
}
156
188
destroy ( ) : void {
157
189
this . channel . onmessage = null ;
0 commit comments