//
//  YSRACEmptySignal.m
//  ReactiveObjC
//
//  Created by Justin Spahr-Summers on 2013-10-10.
//  Copyright (c) 2013 GitHub, Inc. All rights reserved.
//

#import "YSRACEmptySignal.h"
#import "YSRACScheduler+Private.h"
#import "YSRACSubscriber.h"

@implementation YSRACEmptySignal

#pragma mark Properties

// Only allow this signal's name to be customized in DEBUG, since it's
// a singleton in release builds (see +empty).
- (void)setName:(NSString *)name {
#ifdef DEBUG
	[super setName:name];
#endif
}

- (NSString *)name {
#ifdef DEBUG
	return super.name;
#else
	return @"+empty";
#endif
}

#pragma mark Lifecycle

+ (YSRACSignal *)empty {
#ifdef DEBUG
	// Create multiple instances of this class in DEBUG so users can set custom
	// names on each.
	return [[[self alloc] init] setNameWithFormat:@"+empty"];
#else
	static id singleton;
	static dispatch_once_t pred;

	dispatch_once(&pred, ^{
		singleton = [[self alloc] init];
	});

	return singleton;
#endif
}

#pragma mark Subscription

- (YSRACDisposable *)subscribe:(id<YSRACSubscriber>)subscriber {
	NSCParameterAssert(subscriber != nil);

	return [YSRACScheduler.subscriptionScheduler schedule:^{
		[subscriber sendCompleted];
	}];
}

@end
