-
Notifications
You must be signed in to change notification settings - Fork 17
/
MPWFixedValueSource.m
94 lines (75 loc) · 1.93 KB
/
MPWFixedValueSource.m
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
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// MPWFixedValueSource.m
// MPWFoundation
//
// Created by Marcel Weiher on 02.05.21.
//
#import "MPWFixedValueSource.h"
#import "NSRunLoopAdditions.h"
@interface MPWFixedValueSource()
@property (nonatomic, strong) NSEnumerator *valuesEnumerator;
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation MPWFixedValueSource
-(void)writeObject:(id)anObject
{
id nextValue=self.valuesEnumerator.nextObject;
if (!nextValue) {
self.valuesEnumerator=self.values.objectEnumerator;
nextValue=self.valuesEnumerator.nextObject;
}
[self.target writeObject:nextValue];
}
-(NSTimer*)createTimer
{
return [NSTimer scheduledTimerWithTimeInterval:self.seconds target:self selector:@selector(writeObject:) userInfo:nil repeats:YES];
}
-(void)start
{
if (!self.timer) {
self.timer=[self createTimer];
}
}
-(void)stop
{
[self.timer invalidate];
self.timer = nil;
}
-(void)run
{
[self start];
[[NSRunLoop currentRunLoop] runInterruptibly];
[self stop];
}
-(void)dealloc
{
NSLog(@"deallocating MPWFixedValueSource, will stop timer");
[self stop];
[_values release];
[_valuesEnumerator release];
[super dealloc];
}
@end
#import <MPWFoundation/DebugMacros.h>
@implementation MPWFixedValueSource(testing)
+(void)testWritesValuesRepeatedly
{
MPWFixedValueSource *source=[MPWFixedValueSource stream];
source.values=@[ @1, @7 ];
IDEXPECT(source.target, @[], @"result empty");
[source writeObject:nil];
IDEXPECT(source.target, @[@1], @"one result");
[source writeObject:nil];
IDEXPECT(source.target, (@[@1, @7]), @"two results, at end");
[source writeObject:nil];
IDEXPECT(source.target, (@[@1, @7, @1]), @"three results, start from beginnig");
[source writeObject:nil];
IDEXPECT(source.target, (@[@1, @7, @1,@7]), @"four results, at end");
}
+(NSArray*)testSelectors
{
return @[
@"testWritesValuesRepeatedly",
];
}
@end