Revision: 28986
          http://sourceforge.net/p/bibdesk/svn/28986
Author:   hofman
Date:     2025-02-06 15:42:20 +0000 (Thu, 06 Feb 2025)
Log Message:
-----------
suspend import command when waiting and parsing async

Modified Paths:
--------------
    trunk/bibdesk/BibDocument+Scripting.m

Modified: trunk/bibdesk/BibDocument+Scripting.m
===================================================================
--- trunk/bibdesk/BibDocument+Scripting.m       2025-01-22 10:12:22 UTC (rev 
28985)
+++ trunk/bibdesk/BibDocument+Scripting.m       2025-02-06 15:42:20 UTC (rev 
28986)
@@ -1041,7 +1041,6 @@
     id mergePub = [params objectForKey:@"merge"];
     id overwrite = [params objectForKey:@"overwrite"];
     BOOL synchronous = [wait respondsToSelector:@selector(boolValue)] ? [wait 
boolValue] : (mergePub == nil);
-    NSArray *pubs = nil;
     BDSKServerInfo *serverInfo = nil;
     
     // make sure we get something valid as input
@@ -1112,7 +1111,9 @@
         }
     }
     
-    __block BOOL canceled = NO;
+    __block BOOL finished = NO;
+    __block BOOL suspended = NO;
+    __block NSArray *returnPubs = nil;
     
     // construct the handler to process the pubs when we get them
     void (^handler)(NSArray *) = NULL;
@@ -1132,17 +1133,25 @@
                     [mergeController merge];
                 } else if ([overwrite unsignedIntValue] == 'yes ') {
                     [mergeController mergeOverwriting];
-                } else if (synchronous == NO) {
-                    [mergeController beginSheetModalForWindow:documentWindow 
completionHandler:NULL];
                 } else {
-                    [mergeController beginSheetModalForWindow:documentWindow 
completionHandler:^(NSInteger rv){
-                        [NSApp stopModalWithCode:rv];
+                    [mergeController beginSheetModalForWindow:documentWindow 
completionHandler:^(NSModalResponse rv){
+                        if (rv == NSModalResponseOK)
+                            returnPubs = @[mergePub];
+                        else
+                            returnPubs = @[];
+                        if (suspended)
+                            [command resumeExecutionWithResult:returnPubs];
+                        finished = YES;
                     }];
-                    canceled = NSModalResponseCancel == [NSApp 
runModalForWindow:[mergeController window]];
+                    return;
                 }
+                returnPubs = @[mergePub];
             } else {
-                canceled = YES;
+                returnPubs = @[];
             }
+            if (suspended)
+                [command resumeExecutionWithResult:returnPubs];
+            finished = YES;
         };
     } else {
         handler = ^(NSArray *result){
@@ -1158,6 +1167,10 @@
                 }
                 [self importPublications:result publicationsToAutoFile:([url 
isFileURL] ? result : nil) options:BDSKImportAggregate | BDSKImportNoEdit];
             }
+            returnPubs = result ?: @[];
+            if (suspended)
+                [command resumeExecutionWithResult:returnPubs];
+            finished = YES;
         };
     }
     
@@ -1164,40 +1177,37 @@
     // finally get the pubs and process them
     if ([source isKindOfClass:[NSArray class]]) {
         NSMutableArray *tmpPubs = [NSMutableArray array];
-        for (BibItem *pub in source) {
+        for (BibItem *pub in source)
             // get complex strings with the correct macroResolver
-            BibItem *copiedPub = [pub copyWithMacroResolver:[self 
macroResolver]];
-            [tmpPubs addObject:copiedPub];
-        }
-        pubs = tmpPubs;
+            [tmpPubs addObject:[pub copyWithMacroResolver:[self 
macroResolver]]];
+        handler(tmpPubs);
     } else if ([source isKindOfClass:[NSString class]]) {
-        if (synchronous == NO && [BDSKDOIParser canParseString:source]) {
+        if ([BDSKDOIParser canParseString:source])
             [BDSKDOIParser itemsFromString:source owner:self 
completionHandler:handler];
-            return nil;
-        }
-        pubs = [BDSKStringParser itemsFromString:source 
ofType:BDSKStringTypeUnknown owner:self error:NULL];
+        else
+            handler([BDSKStringParser itemsFromString:source 
ofType:BDSKStringTypeUnknown owner:self error:NULL]);
     } else if ([source isKindOfClass:[NSURL class]]) {
-        pubs = [BDSKWebSearch itemsFromURL:source macroResolver:[self 
macroResolver] completionHandler:synchronous ? nil : handler];
-        if (synchronous == NO)
-            return nil;
+        [BDSKWebSearch itemsFromURL:source macroResolver:[self macroResolver] 
completionHandler:handler];
     } else if (searchTerm) {
-        pubs = [BDSKGroupSearch searchUsingSearchTerm:searchTerm 
serverInfo:serverInfo macroResolver:[self macroResolver] 
completionHandler:synchronous ? nil : handler];
-        if (synchronous == NO)
-            return nil;
+        [BDSKGroupSearch searchUsingSearchTerm:searchTerm 
serverInfo:serverInfo macroResolver:[self macroResolver] 
completionHandler:handler];
+    } else if ([url isFileURL]) {
+        handler(@[[BibItem itemWithFileURL:url owner:self]]);
     } else if (url) {
-        if ([url isFileURL])
-            pubs = @[[BibItem itemWithFileURL:url owner:self]];
-        else
-            pubs = @[[BibItem itemWithURL:url title:nil owner:self]];
+        handler(@[[BibItem itemWithURL:url title:nil owner:self]]);
+    } else {
+        returnPubs = @[];
+        finished = YES;
     }
-    handler(pubs);
     
-    if (synchronous == NO)
+    if (synchronous == NO) {
         return nil;
-    else if (mergePub == nil)
-        return pubs ?: @[];
-    else
-        return [NSArray arrayWithObjects:canceled ? nil : mergePub, nil];
+    } else if (finished) {
+        return returnPubs;
+    } else {
+        [command suspendExecution];
+        suspended = YES;
+        return nil;
+    }
 }
 
 @end

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to