rac中有個(gè)私有的方法:
+ (instancetype)join:(id<NSFastEnumeration>)streams block:(RACStream * (^)(id, id))block;
雖然是私有的方法,但對(duì)理解racstream比較重要,zipwith:最后都會(huì)走到j(luò)oin:block:
+ (instancetype)zip:(id<NSFastEnumeration>)streams {
return [[self join:streams block:^(RACStream *left, RACStream *right) {
return [left zipWith:right];//兩個(gè)信號(hào)打包
}] setNameWithFormat:@"+zip: %@", streams];
}
+ (instancetype)join:(id<NSFastEnumeration>)streams block:(RACStream * (^)(id, id))block {
RACStream *current = nil;
//遍歷streams
// Creates streams of successively larger tuples by combining the input
// streams one-by-one.
for (RACStream *stream in streams) {
// For the first stream, just wrap its values in a RACTuple. That way,
// if only one stream is given, the result is still a stream of tuples.
if (current == nil) {
//第一個(gè)值露该,直接封裝為ractuple
current = [stream map:^(id x) {
return RACTuplePack(x);
}];
continue;
}
// 除了第一個(gè)stream菇晃,后續(xù)的stream和current打包組成新的current
current = block(current, stream);
}
if (current == nil) return [self empty];
return [current map:^(RACTuple *xs) {
// Right now, each value is contained in its own tuple, sorta like:
//
// (((1), 2), 3)
//
// We need to unwrap all the layers and create a tuple out of the result.
//將(((1), 2), 3)數(shù)據(jù)類型轉(zhuǎn)換為數(shù)組
NSMutableArray *values = [[NSMutableArray alloc] init];
while (xs != nil) {
[values insertObject:xs.last ?: RACTupleNil.tupleNil atIndex:0];
xs = (xs.count > 1 ? xs.first : nil);
}
return [RACTuple tupleWithObjectsFromArray:values];
}];
}