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

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

@implementation NSString (YSRACSupport)

+ (YSRACSignal *)ysrac_readContentsOfURL:(NSURL *)URL usedEncoding:(NSStringEncoding *)encoding scheduler:(YSRACScheduler *)scheduler {
	NSCParameterAssert(URL != nil);
	NSCParameterAssert(encoding != nil);
	NSCParameterAssert(scheduler != nil);
	
	YSRACReplaySubject *subject = [YSRACReplaySubject subject];
	[subject setNameWithFormat:@"+ysrac_readContentsOfURL: %@ usedEncoding:scheduler: %@", URL, scheduler];
	
	[scheduler schedule:^{
		NSError *error = nil;
		NSString *string = [NSString stringWithContentsOfURL:URL usedEncoding:encoding error:&error];
		if (string == nil) {
			[subject sendError:error];
		} else {
			[subject sendNext:string];
			[subject sendCompleted];
		}
	}];
	
	return subject;
}

@end
