Skip to content

Commit

Permalink
fix yield demo and docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
NianJi committed Mar 5, 2019
1 parent c2473cd commit 257e60c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@
#import <coobjc/coobjc.h>
#import "coobjcCommon.h"

static COPromise<NSData *> *co_downloadWithURL(NSString *url) {

return [COPromise promise:^(COPromiseFullfill _Nonnull fullfill, COPromiseReject _Nonnull reject) {

[NSURLSession sharedSession].configuration.requestCachePolicy = NSURLRequestReloadIgnoringCacheData;
NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithURL:[NSURL URLWithString:url] completionHandler:
^(NSURL *location, NSURLResponse *response, NSError *error) {
if (error) {
reject(error);
return;
}
else{
NSData *data = [[NSData alloc] initWithContentsOfURL:location];

fullfill(data);
return;
}
}];

[task resume];

}];
}

SpecBegin(coSequence)

describe(@"test sequence on same queue", ^{
Expand Down Expand Up @@ -90,7 +114,7 @@
COCoroutine *co1 = co_sequence(^{
int index = 0;
while(co_isActive()){
yield([NSData co_downloadWithURL:@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa"]);
yield(co_downloadWithURL(@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa"));
index++;
}
});
Expand All @@ -117,7 +141,7 @@
COCoroutine *co2 = co_sequence(^{
int index = 0;
while(co_isActive()){
yield([NSData co_downloadWithURL:@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa"]);
yield(co_downloadWithURL(@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa"));
index++;
}
});
Expand Down Expand Up @@ -217,7 +241,7 @@
COCoroutine *co1 = co_sequence_onqueue(q1, ^{
int index = 0;
while(co_isActive()){
yield([NSData co_downloadWithURL:@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa"]);
yield(co_downloadWithURL(@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa"));
index++;
}
});
Expand Down Expand Up @@ -246,7 +270,7 @@
COCoroutine *co2 = co_sequence_onqueue(q1, ^{
int index = 0;
while(co_isActive()){
yield([NSData co_downloadWithURL:@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa"]);
yield(co_downloadWithURL(@"http://pytstore.oss-cn-shanghai.aliyuncs.com/GalileoShellApp.ipa"));
index++;
}
});
Expand Down
14 changes: 10 additions & 4 deletions coobjc/api/coobjc.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,18 @@ NS_INLINE NSArray<id> *_Nullable batch_await(NSArray<id> * _Nonnull _promiseOrCh
}

/**
yield with a COPromise or COChan
yield with a COPromise
@param _promiseOrChan the COPromise object or COChan object.
@discussion `yield` means pause the expression execution,
until Generator(coroutine) call `next`.
So, you should passing a `COPromise` object with constructor,
To make the expression lazy loading.
@param _promise the COPromise object.
*/
NS_INLINE void yield(id _Nonnull _promiseOrChan) {
co_generator_yield(_promiseOrChan);
NS_INLINE void yield(id _Nonnull _promise) {
co_generator_yield(_promise);
}

/**
Expand Down

0 comments on commit 257e60c

Please sign in to comment.