RACUnarySequence.m 1.8 KB

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