MVVM+RAC

RACSignal操作的核心bind实现分析

2018-01-13  本文已影响67人  月影随风_

先来说说bind函数的作用:

// 创建源信号
// 创建了一个 RACDynamicSignal 类型的信号,并将传入的代码块保存起来,留待以后调用。
1. RACSignal *signal = [RACSignal createSignal:
                         ^RACDisposable *(id<RACSubscriber> subscriber)
    {
        //block1
8.      [subscriber sendNext:@1];
        [subscriber sendNext:@2];
        [subscriber sendNext:@3];
        [subscriber sendCompleted];
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"signal dispose");
        }];
    }];
    
    //绑定源信号,生成绑定信号
    //bind操作实际上是直接生成绑定信号并返回,并且在生成绑定信号传入的didSubscriber block代码块中,保存了bind传入的block,初始化了信号数组,并且订阅了源信号,针对源信号发送信号的流程做了一些处理。(此时未执行,订阅才执行)
2.  RACSignal *bindSignal = [signal bind:^RACStreamBindBlock{
        // block 2
6.      return ^RACSignal *(NSNumber *value, BOOL *stop){
            // block 3
10.         value = @(value.integerValue * 2);
            return [RACSignal return:value];
        };
    }];
    //订阅绑定信号
    //订阅绑定信号就是保存了nextBlock,并且创建订阅者,实现信号的didSubscriber block代码块。
4.  [bindSignal subscribeNext:^(id x) {
        // block 4
14.     NSLog(@"subscribe value = %@", x);
    }];
- (RACSignal *)bind:(RACStreamBindBlock (^)(void))block {
    // block 5
3.    return [[RACSignal createSignal:^(id<RACSubscriber> subscriber) {
        // block 6
5.      RACStreamBindBlock bindingBlock = block();
        NSMutableArray *signals = [NSMutableArray arrayWithObject:self];
        
        void (^completeSignal)(RACSignal *, RACDisposable *) = ^(RACSignal *signal, RACDisposable *finishedDisposable) {
15.     // block 7
        };
        
        void (^addSignal)(RACSignal *) = ^(RACSignal *signal) {
            // block 8
12.         RACDisposable *disposable = [signal subscribeNext:^(id x) {
            // block 9
13.             [subscriber sendNext:x];
            } error:^(NSError *error) {
                [compoundDisposable dispose];
                [subscriber sendError:error];
            } completed:^{
                @autoreleasepool {
14.                 completeSignal(signal, selfDisposable);
                }
            }];
        };
        
        @autoreleasepool {
7.          RACDisposable *bindingDisposable = [self subscribeNext:^(id x) {
                // block 10
9.           id signal = bindingBlock(x, &stop);
                
                @autoreleasepool {
11.                 if (signal != nil) addSignal(signal);
                    if (signal == nil || stop) {
                        [selfDisposable dispose];
                        completeSignal(self, selfDisposable);
                    }
                }
            } error:^(NSError *error) {
                [compoundDisposable dispose];
                [subscriber sendError:error];
            } completed:^{
                @autoreleasepool {
                    completeSignal(self, selfDisposable);
                }
            }];
        }
        return compoundDisposable;
    }] ;
}

bind底层实现分析:

一句话总结:bind操作方法实质上就是生成新的绑定信号,利用returnSignal作为中间信号来改变源数据生成新的数据并执行新绑定信号的nextBlock代码块!

上一篇 下一篇

猜你喜欢

热点阅读