Skip to content

Commit

Permalink
Improve synchronization for pluginUpdateOrInstall()
Browse files Browse the repository at this point in the history
This way we ensure that all GUI element functionality happens *after* the completion of this method.
  • Loading branch information
npyl committed Dec 25, 2019
1 parent 35c3d72 commit 98656ac
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
5 changes: 2 additions & 3 deletions MacForge/MacForge/Views/Bundle/MF_bundleView.m
Original file line number Diff line number Diff line change
Expand Up @@ -668,14 +668,13 @@ - (void)donateDev {
}

- (void)pluginInstall {
[PluginManager.sharedInstance pluginUpdateOrInstall:item :repoPackages];
dispatch_async(dispatch_get_main_queue(), ^{
[PluginManager.sharedInstance pluginUpdateOrInstall:item :repoPackages withCompletionHandler:^(BOOL res) {
[PluginManager.sharedInstance readPlugins:nil];
[self.bundleInstall setTitle:@"Open"];
[self.bundleInstall setAction:@selector(pluginFinder)];
[self.bundleDelete setEnabled:true];
[self viewWillDraw];
});
}];
}

- (void)pluginFinder {
Expand Down
21 changes: 18 additions & 3 deletions MacForge/MacForge/Views/Tables/updatesTable.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,19 @@ - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn
}

- (IBAction)updateAll:(id)sender {
__block NSUInteger count = [needsUpdate count];

for (NSString* key in [needsUpdate allKeys]) {
NSDictionary *installDict = [needsUpdate objectForKey:key];
[self->sharedMethods pluginUpdateOrInstall:installDict :[installDict objectForKey:@"sourceURL"]];
[self->sharedMethods pluginUpdateOrInstall:installDict :[installDict objectForKey:@"sourceURL"] withCompletionHandler:^(BOOL res) {
count--;
}];
}

/* wait until all installs have finished */
while (count != 0)
;

dispatch_queue_t backgroundQueue = dispatch_queue_create("com.w0lf.MacForge", 0);
dispatch_async(backgroundQueue, ^{
[needsUpdate removeAllObjects];
Expand All @@ -77,13 +85,20 @@ - (IBAction)updatePlugin:(id)sender {
NSTableView *t = (NSTableView*)[[[sender superview] superview] superview];
long selected = [t rowForView:sender];
@try {
__block NSUInteger count = [needsUpdate count];

NSString *key = [[needsUpdate allKeys] objectAtIndex:selected];
NSDictionary *installDict = [needsUpdate objectForKey:key];
[self->sharedMethods pluginUpdateOrInstall:installDict :[installDict objectForKey:@"sourceURL"]];
[self->sharedMethods pluginUpdateOrInstall:installDict :[installDict objectForKey:@"sourceURL"] withCompletionHandler:^(BOOL res) {
count--;
}];

/* wait until all installs have finished */
while (count != 0)
;

dispatch_queue_t backgroundQueue = dispatch_queue_create("com.w0lf.MacForge", 0);
dispatch_async(backgroundQueue, ^{
// [needsUpdate removeObjectForKey:key];
[self->sharedMethods checkforPluginUpdates:self->_tblView :myDelegate.viewUpdateCounter];
});
} @catch (NSException *exception) {
Expand Down
5 changes: 2 additions & 3 deletions MacForge/Shared Classes/MF_Purchase.m
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,10 @@ + (void)pluginInstallWithProgress:(MSPlugin*)plugin :(NSString*)repo :(NSButton*

+ (void)pluginInstall:(MSPlugin*)plugin :(NSButton*)theButton :(NSString*)repo {
NSDictionary* item = plugin.webPlist;
[PluginManager.sharedInstance pluginUpdateOrInstall:item :repo];
dispatch_async(dispatch_get_main_queue(), ^{
[PluginManager.sharedInstance pluginUpdateOrInstall:item :repo withCompletionHandler:^(BOOL res) {
[PluginManager.sharedInstance readPlugins:nil];
[theButton setTitle:@"OPEN"];
});
}];
}

@end
2 changes: 1 addition & 1 deletion MacForge/Shared Classes/PluginManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

- (void)installBundles:(NSArray*)pathArray;
- (void)replaceFile:(NSString*)start :(NSString*)end;
- (Boolean)pluginUpdateOrInstall:(NSDictionary*)item :(NSString*)repo;
- (Boolean)pluginUpdateOrInstall:(NSDictionary*)item :(NSString*)repo withCompletionHandler:(void (^)(BOOL))completionBlock;
- (Boolean)pluginDelete:(NSDictionary*)item;
- (Boolean)pluginRevealFinder:(NSDictionary*)item;

Expand Down
23 changes: 12 additions & 11 deletions MacForge/Shared Classes/PluginManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ - (Boolean)pluginDownloaded:(NSData*)data {
}

// Try to update or install a plugin given a bundle plist and a repo
- (Boolean)pluginUpdateOrInstall:(NSDictionary *)item :(NSString *)repo {
Boolean success = false;
- (Boolean)pluginUpdateOrInstall:(NSDictionary *)item :(NSString *)repo withCompletionHandler:(void (^)(BOOL result))completionBlock {
__block Boolean success = false;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Get installation URL
Expand Down Expand Up @@ -376,9 +376,7 @@ - (Boolean)pluginUpdateOrInstall:(NSDictionary *)item :(NSString *)repo {
NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/unzip" arguments:@[@"-o", temp, @"-d", unzipDir]];
[task waitUntilExit];
if ([task terminationStatus] == 0) {
// presumably the only case where we've successfully installed
// ???
// success = true;
success = true;
}

// Try to install the contents
Expand All @@ -388,10 +386,11 @@ - (Boolean)pluginUpdateOrInstall:(NSDictionary *)item :(NSString *)repo {
[self readPlugins:nil];
}

//This is your completion handler
dispatch_sync(dispatch_get_main_queue(), ^{

});
// This is your completion handler
dispatch_sync(dispatch_get_main_queue(), ^{
if (completionBlock)
completionBlock(success);
});
});

return success;
Expand Down Expand Up @@ -594,8 +593,10 @@ - (void)checkforPluginUpdatesAndInstall:(NSTableView*)table {
if (needsUpdate.count > 0) {
for (NSString *key in needsUpdate.allKeys) {
NSDictionary *itemDict = [needsUpdate objectForKey:key];
if ([self pluginUpdateOrInstall:itemDict :[itemDict objectForKey:@"sourceURL"]])
[needsUpdate removeObjectForKey:key];
[self pluginUpdateOrInstall:itemDict :[itemDict objectForKey:@"sourceURL"] withCompletionHandler:^(BOOL res) {
if (res)
[needsUpdate removeObjectForKey:key];
}];
}
[self updateApplicationIcon];
}
Expand Down

0 comments on commit 98656ac

Please sign in to comment.