forked from erkyrath/Inform7-IDE-Mac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIFMaintenanceTask.m
116 lines (87 loc) · 2.59 KB
/
IFMaintenanceTask.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// IFMaintenanceTask.m
// Inform-xc2
//
// Created by Andrew Hunter on 25/04/2006.
// Copyright 2006 Andrew Hunter. All rights reserved.
//
#import "IFMaintenanceTask.h"
NSString* IFMaintenanceTasksStarted = @"IFMaintenanceTasksStarted";
NSString* IFMaintenanceTasksFinished = @"IFMaintenanceTasksFinished";
@implementation IFMaintenanceTask
// = Initialisation =
+ (IFMaintenanceTask*) sharedMaintenanceTask {
static IFMaintenanceTask* maintenanceTask = nil;
if (!maintenanceTask) {
maintenanceTask = [[IFMaintenanceTask alloc] init];
}
return maintenanceTask;
}
- (id) init {
self = [super init];
if (self) {
activeTask = nil;
pendingTasks = [[NSMutableArray alloc] init];
haveFinished = YES;
}
return self;
}
- (void) dealloc {
[activeTask release];
[pendingTasks release];
[super dealloc];
}
// = Starting tasks =
- (BOOL) startNextTask {
if (activeTask != nil) return YES;
if ([pendingTasks count] <= 0) return NO;
// Retrieve the next task to run
NSArray* newTask = [[[pendingTasks objectAtIndex: 0] retain] autorelease];
[pendingTasks removeObjectAtIndex: 0];
// Set up a new task
activeTask = [[NSTask alloc] init];
[activeTask setLaunchPath: [newTask objectAtIndex: 0]];
[activeTask setArguments: [newTask objectAtIndex: 1]];
// Register for notifications
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(taskFinished:)
name: NSTaskDidTerminateNotification
object: activeTask];
// Notify anyone who's interested that we're started
if (haveFinished) {
[[NSNotificationCenter defaultCenter] postNotificationName: IFMaintenanceTasksStarted
object: self];
haveFinished = NO;
}
// Start the task
[activeTask launch];
return YES;
}
- (void) taskFinished: (NSNotification*) not {
// Stop monitoring the old task
[[NSNotificationCenter defaultCenter] removeObserver: self
name: NSTaskDidTerminateNotification
object: activeTask];
// Clear up the old task
[activeTask release];
activeTask = nil;
// Start the next task in the queue
if (![self startNextTask]) {
// We've finished!
haveFinished = YES;
[[NSNotificationCenter defaultCenter] postNotificationName: IFMaintenanceTasksFinished
object: self];
}
}
// = Queuing tasks =
- (void) queueTask: (NSString*) command {
[self queueTask: command
withArguments: [NSArray array]];
}
- (void) queueTask: (NSString*) command
withArguments: (NSArray*) arguments {
[pendingTasks addObject:
[NSArray arrayWithObjects: command, arguments, nil]];
[self startNextTask];
}
@end