-
Notifications
You must be signed in to change notification settings - Fork 496
/
NSFileHandle+RACSupport.m
40 lines (32 loc) · 1.07 KB
/
NSFileHandle+RACSupport.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// NSFileHandle+RACSupport.m
// ReactiveObjC
//
// Created by Josh Abernathy on 5/10/12.
// Copyright (c) 2012 GitHub. All rights reserved.
//
#import "NSFileHandle+RACSupport.h"
#import "NSNotificationCenter+RACSupport.h"
#import "NSObject+RACDescription.h"
#import "RACReplaySubject.h"
#import "RACDisposable.h"
@implementation NSFileHandle (RACSupport)
- (RACSignal *)rac_readInBackground {
RACReplaySubject *subject = [RACReplaySubject subject];
[subject setNameWithFormat:@"%@ -rac_readInBackground", RACDescription(self)];
RACSignal *dataNotification = [[[NSNotificationCenter defaultCenter] rac_addObserverForName:NSFileHandleReadCompletionNotification object:self] map:^(NSNotification *note) {
return note.userInfo[NSFileHandleNotificationDataItem];
}];
__block RACDisposable *subscription = [dataNotification subscribeNext:^(NSData *data) {
if (data.length > 0) {
[subject sendNext:data];
[self readInBackgroundAndNotify];
} else {
[subject sendCompleted];
[subscription dispose];
}
}];
[self readInBackgroundAndNotify];
return subject;
}
@end