//
//  NSData+YSRACSupport.m
//  ReactiveObjC
//
//  Created by Josh Abernathy on 5/11/12.
//  Copyright (c) 2012 GitHub, Inc. All rights reserved.
//

#import "NSData+YSRACSupport.h"
#import "YSRACReplaySubject.h"
#import "YSRACScheduler.h"

@implementation NSData (YSRACSupport)

+ (YSRACSignal *)ysrac_readContentsOfURL:(NSURL *)URL options:(NSDataReadingOptions)options scheduler:(YSRACScheduler *)scheduler {
	NSCParameterAssert(scheduler != nil);
	
	YSRACReplaySubject *subject = [YSRACReplaySubject subject];
	[subject setNameWithFormat:@"+ysrac_readContentsOfURL: %@ options: %lu scheduler: %@", URL, (unsigned long)options, scheduler];
	
	[scheduler schedule:^{
		NSError *error = nil;
		NSData *data = [[NSData alloc] initWithContentsOfURL:URL options:options error:&error];
		if (data == nil) {
			[subject sendError:error];
		} else {
			[subject sendNext:data];
			[subject sendCompleted];
		}
	}];
	
	return subject;
}

@end
