美文网首页
RAC中的join: block:

RAC中的join: block:

作者: 一只二进制编码的狗 | 来源:发表于2016-01-20 18:04 被阅读81次

    rac中有个私有的方法:

    + (instancetype)join:(id<NSFastEnumeration>)streams block:(RACStream * (^)(id, id))block;
    

    虽然是私有的方法,但对理解racstream比较重要,zipwith:最后都会走到join:block:

    + (instancetype)zip:(id<NSFastEnumeration>)streams {
        return [[self join:streams block:^(RACStream *left, RACStream *right) {
            return [left zipWith:right];//两个信号打包
        }] 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) {
    //第一个值,直接封装为ractuple
                current = [stream map:^(id x) {
                    return RACTuplePack(x);
                }];
    
                continue;
            }
    // 除了第一个stream,后续的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)数据类型转换为数组
            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];
        }];
    }
    

    相关文章

      网友评论

          本文标题:RAC中的join: block:

          本文链接:https://www.haomeiwen.com/subject/aorlkttx.html