NSFileHandle+RACSupport.m 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //
  2. // NSFileHandle+RACSupport.m
  3. // ReactiveCocoa
  4. //
  5. // Created by Josh Abernathy on 5/10/12.
  6. // Copyright (c) 2012 GitHub. All rights reserved.
  7. //
  8. #import "NSFileHandle+RACSupport.h"
  9. #import "NSNotificationCenter+RACSupport.h"
  10. #import "NSObject+RACDescription.h"
  11. #import "RACReplaySubject.h"
  12. #import "RACDisposable.h"
  13. @implementation NSFileHandle (RACSupport)
  14. - (RACSignal *)rac_readInBackground {
  15. RACReplaySubject *subject = [RACReplaySubject subject];
  16. [subject setNameWithFormat:@"%@ -rac_readInBackground", RACDescription(self)];
  17. RACSignal *dataNotification = [[[NSNotificationCenter defaultCenter] rac_addObserverForName:NSFileHandleReadCompletionNotification object:self] map:^(NSNotification *note) {
  18. return note.userInfo[NSFileHandleNotificationDataItem];
  19. }];
  20. __block RACDisposable *subscription = [dataNotification subscribeNext:^(NSData *data) {
  21. if (data.length > 0) {
  22. [subject sendNext:data];
  23. [self readInBackgroundAndNotify];
  24. } else {
  25. [subject sendCompleted];
  26. [subscription dispose];
  27. }
  28. }];
  29. [self readInBackgroundAndNotify];
  30. return subject;
  31. }
  32. @end