Skip to content

Commit

Permalink
改用channel实现progress
Browse files Browse the repository at this point in the history
  • Loading branch information
NianJi committed Apr 22, 2019
1 parent 7531846 commit 2bf1ffd
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions coobjc/promise/COPromise.m
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ @interface COProgressPromise (){
}

@property (nonatomic, strong) NSProgress *progress;
@property (nonatomic, strong) COPromise *internalPromise;
@property (nonatomic, strong) COChan *progressChannel;
@property (nonatomic, strong) id lastValue;

@end
Expand All @@ -348,33 +348,26 @@ @implementation COProgressPromise
- (instancetype)init{
self = [super init];
if (self) {
_progressChannel = [COChan chanWithBuffCount:1];
}
return self;
}

- (void)fulfill:(id)value{
[self.internalPromise fulfill:nil];
[self.progressChannel cancel];
[super fulfill:value];
}

- (void)reject:(NSError *)error{
[self.internalPromise fulfill:nil];
[self.progressChannel cancel];
[super reject:error];
}

- (COProgressValue*)_nextProgressValue{
if (![self isPending]) {
return nil;
}
do {
COOBJC_SCOPELOCK(_lock);
self.internalPromise = [COPromise promise];
} while (0);
COProgressValue* result = co_await(self.internalPromise);
do {
COOBJC_SCOPELOCK(_lock);
self.internalPromise = [COPromise promise];
} while (0);
COProgressValue *result = [self.progressChannel receive];
return result;
}

Expand Down Expand Up @@ -409,12 +402,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
NSProgress *progress = object;
COProgressValue *value = [[COProgressValue alloc] init];
value.progress = progress.fractionCompleted;
COPromise *promise;
do {
COOBJC_SCOPELOCK(_lock);
promise = self.internalPromise;
} while (0);
[promise fulfill:value];
[self.progressChannel send_nonblock:value];
}
else
{
Expand Down

0 comments on commit 2bf1ffd

Please sign in to comment.