//
//  UITextField+YSRACSignalSupport.m
//  ReactiveObjC
//
//  Created by Josh Abernathy on 4/17/12.
//  Copyright (c) 2012 GitHub, Inc. All rights reserved.
//

#import "UITextField+YSRACSignalSupport.h"
#import "YSRACEXTKeyPathCoding.h"
#import "YSRACEXTScope.h"
#import "NSObject+YSRACDeallocating.h"
#import "NSObject+YSRACDescription.h"
#import "YSRACSignal+Operations.h"
#import "UIControl+YSRACSignalSupport.h"
#import "UIControl+YSRACSignalSupportPrivate.h"

@implementation UITextField (YSRACSignalSupport)

- (YSRACSignal *)ysrac_textSignal {
	@weakify(self);
	return [[[[[YSRACSignal
		defer:^{
			@strongify(self);
			return [YSRACSignal return:self];
		}]
		concat:[self ysrac_signalForControlEvents:UIControlEventAllEditingEvents]]
		map:^(UITextField *x) {
			return x.text;
		}]
		takeUntil:self.ysrac_willDeallocSignal]
		setNameWithFormat:@"%@ -ysrac_textSignal", YSRACDescription(self)];
}

- (YSRACChannelTerminal *)ysrac_newTextChannel {
	return [self ysrac_channelForControlEvents:UIControlEventAllEditingEvents key:@keypath(self.text) nilValue:@""];
}

@end
