RACUnarySequence.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // RACUnarySequence.m
  3. // ReactiveCocoa
  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 <ReactiveCocoa/EXTKeyPathCoding.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. + (instancetype)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. - (instancetype)bind:(RACStreamBindBlock (^)(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. - (id)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