@@ -93,16 +93,12 @@ export async function open_file(
93
93
return ;
94
94
}
95
95
96
- let store = actions . get_store ( ) ;
97
- if ( store == undefined ) {
98
- return ;
99
- }
100
-
101
96
// ensure the project is opened -- otherwise the modal to start the project won't appear.
102
97
redux . getActions ( "projects" ) . open_project ( { project_id : actions . project_id } ) ;
103
98
104
- let open_files = store . get ( "open_files" ) ;
105
- const alreadyOpened = open_files . has ( opts . path ) ;
99
+ const tabIsOpened = ( ) =>
100
+ ! ! actions . get_store ( ) ?. get ( "open_files" ) ?. has ( opts . path ) ;
101
+ const alreadyOpened = tabIsOpened ( ) ;
106
102
107
103
if ( ! alreadyOpened ) {
108
104
// Make the visible tab itself appear ASAP (just the tab at the top,
@@ -140,6 +136,9 @@ export async function open_file(
140
136
}
141
137
142
138
const intl = await getIntl ( ) ;
139
+ if ( ! tabIsOpened ( ) ) {
140
+ return ;
141
+ }
143
142
const what = intl . formatMessage ( dialogs . project_open_file_what , {
144
143
path : opts . path ,
145
144
} ) ;
@@ -149,6 +148,9 @@ export async function open_file(
149
148
actions . open_files . delete ( opts . path ) ;
150
149
return ;
151
150
}
151
+ if ( ! tabIsOpened ( ) ) {
152
+ return ;
153
+ }
152
154
153
155
try {
154
156
// Unfortunately (it adds a roundtrip to the server), we **have** to do this
@@ -160,6 +162,9 @@ export async function open_file(
160
162
project_id : actions . project_id ,
161
163
path : opts . path ,
162
164
} ) ;
165
+ if ( ! tabIsOpened ( ) ) {
166
+ return ;
167
+ }
163
168
if ( opts . path != realpath ) {
164
169
if ( ! actions . open_files ) return ; // closed
165
170
alert_message ( {
@@ -176,35 +181,36 @@ export async function open_file(
176
181
}
177
182
let ext = opts . ext ?? filename_extension_notilde ( opts . path ) . toLowerCase ( ) ;
178
183
179
- // Returns true if the project is closed or the file tab is now closed.
180
- function is_closed ( ) : boolean {
181
- const store = actions . get_store ( ) ;
182
- // if store isn't defined (so project closed) *or*
183
- // open_files doesn't have path in since tab got closed
184
- // (see https://github.com/sagemathinc/cocalc/issues/4692):
185
- return store ?. getIn ( [ "open_files" , opts . path ] ) == null ;
186
- }
187
-
188
184
// Next get the group.
189
185
let group : string ;
190
186
try {
191
187
group = await get_my_group ( actions . project_id ) ;
192
- if ( is_closed ( ) ) return ;
188
+ if ( ! tabIsOpened ( ) ) {
189
+ return ;
190
+ }
193
191
} catch ( err ) {
194
192
actions . set_activity ( {
195
193
id : uuid ( ) ,
196
194
error : `opening file '${ opts . path } ' (error getting group) -- ${ err } ` ,
197
195
} ) ;
198
196
return ;
199
197
}
198
+
199
+ let store = actions . get_store ( ) ;
200
+ if ( store == null ) {
201
+ return ;
202
+ }
203
+
200
204
const is_public = group === "public" ;
201
205
202
206
if ( ! is_public ) {
203
207
// Check if have capability to open this file. Important
204
208
// to only do this if not public, since again, if public we
205
209
// are not even using the project (it is all client side).
206
210
const can_open_file = await store . can_open_file_ext ( ext , actions ) ;
207
- if ( is_closed ( ) ) return ;
211
+ if ( ! tabIsOpened ( ) ) {
212
+ return ;
213
+ }
208
214
if ( ! can_open_file ) {
209
215
const site_name =
210
216
redux . getStore ( "customize" ) . get ( "site_name" ) || SITE_NAME ;
@@ -223,14 +229,19 @@ export async function open_file(
223
229
// know anything about the state of the project).
224
230
try {
225
231
await callback ( actions . _ensure_project_is_open . bind ( actions ) ) ;
232
+ if ( ! tabIsOpened ( ) ) {
233
+ return ;
234
+ }
226
235
} catch ( err ) {
227
236
actions . set_activity ( {
228
237
id : uuid ( ) ,
229
238
error : `Error opening file '${ opts . path } ' (error ensuring project is open) -- ${ err } ` ,
230
239
} ) ;
231
240
return ;
232
241
}
233
- if ( is_closed ( ) ) return ;
242
+ if ( ! tabIsOpened ( ) ) {
243
+ return ;
244
+ }
234
245
}
235
246
236
247
if ( ! is_public && ( ext === "sws" || ext . slice ( 0 , 4 ) === "sws~" ) ) {
@@ -244,12 +255,14 @@ export async function open_file(
244
255
}
245
256
246
257
store = actions . get_store ( ) ; // because async stuff happened above.
247
- if ( store == undefined ) return ;
258
+ if ( store == undefined ) {
259
+ return ;
260
+ }
248
261
249
262
// Only generate the editor component if we don't have it already
250
263
// Also regenerate if view type (public/not-public) changes.
251
264
// (TODO: get rid of that change code since public is deprecated)
252
- open_files = store . get ( "open_files" ) ;
265
+ const open_files = store . get ( "open_files" ) ;
253
266
if ( open_files == null || actions . open_files == null ) {
254
267
// project is closing
255
268
return ;
@@ -277,6 +290,7 @@ export async function open_file(
277
290
}
278
291
279
292
actions . open_files . set ( opts . path , "fragmentId" , opts . fragmentId ?? "" ) ;
293
+
280
294
if ( ( opts . compute_server_id != null || opts . explicit ) && ! alreadyOpened ) {
281
295
let path = opts . path ;
282
296
if ( path . endsWith ( ".term" ) ) {
@@ -298,6 +312,9 @@ export async function open_file(
298
312
return ;
299
313
}
300
314
}
315
+ if ( ! tabIsOpened ( ) ) {
316
+ return ;
317
+ }
301
318
302
319
if ( opts . foreground ) {
303
320
actions . foreground_project ( opts . change_history ) ;
0 commit comments