RACUnarySequence.m
1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// RACUnarySequence.m
// ReactiveCocoa
//
// Created by Justin Spahr-Summers on 2013-05-01.
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
//
#import "RACUnarySequence.h"
#import "RACEXTKeyPathCoding.h"
#import "NSObject+RACDescription.h"
@interface RACUnarySequence ()
// The single value stored in this sequence.
@property (nonatomic, strong, readwrite) id head;
@end
@implementation RACUnarySequence
#pragma mark Properties
@synthesize head = _head;
#pragma mark Lifecycle
+ (instancetype)return:(id)value {
RACUnarySequence *sequence = [[self alloc] init];
sequence.head = value;
return [sequence setNameWithFormat:@"+return: %@", [value rac_description]];
}
#pragma mark RACSequence
- (RACSequence *)tail {
return nil;
}
- (instancetype)bind:(RACStreamBindBlock (^)(void))block {
RACStreamBindBlock bindBlock = block();
BOOL stop = NO;
RACSequence *result = (id)[bindBlock(self.head, &stop) setNameWithFormat:@"[%@] -bind:", self.name];
return result ?: self.class.empty;
}
#pragma mark NSCoding
- (Class)classForCoder {
// Unary sequences should be encoded as themselves, not array sequences.
return self.class;
}
- (id)initWithCoder:(NSCoder *)coder {
id value = [coder decodeObjectForKey:@keypath(self.head)];
return [self.class return:value];
}
- (void)encodeWithCoder:(NSCoder *)coder {
if (self.head != nil) [coder encodeObject:self.head forKey:@keypath(self.head)];
}
#pragma mark NSObject
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p>{ name = %@, head = %@ }", self.class, self, self.name, self.head];
}
- (NSUInteger)hash {
return [self.head hash];
}
- (BOOL)isEqual:(RACUnarySequence *)seq {
if (self == seq) return YES;
if (![seq isKindOfClass:RACUnarySequence.class]) return NO;
return self.head == seq.head || [(NSObject *)self.head isEqual:seq.head];
}
@end