mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-08-25 13:07:02 +00:00
Previously, we could only give a selector and a target for perform in the runLoop. Now we can give a block. This feature is used in the CPTextField class. Previously, when we wanted to call a function at the end of the stack we used window.setTimeout, however due to HTML5 specifications this wasn't called just at the end of the stack but at least 4ms (more information here https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout#Minimum_delay_and_timeout_nesting). Now we give a block to perform, and this block will be performed in the next runloop. Unittest has been added in Tests/Foundation/CPRunLoopTest.j
65 lines
1.9 KiB
Plaintext
65 lines
1.9 KiB
Plaintext
@import <Foundation/CPObject.j>
|
|
@import <Foundation/CPRunLoop.j>
|
|
@import <OJUnit/OJTestCase.j>
|
|
|
|
@implementation CPRunLoopTest : OJTestCase
|
|
{
|
|
}
|
|
|
|
- (void)testDelayedPerform
|
|
{
|
|
var aPerformer = [Performer new];
|
|
|
|
[aPerformer performSelector:@selector(performWithObject:) withObject:5 afterDelay:0];
|
|
[aPerformer performSelector:@selector(performWithObject:) withObject:1 afterDelay:0 inModes:[CPDefaultRunLoopMode]];
|
|
[aPerformer performSelector:@selector(performWithObject:) withObject:10 afterDelay:9999 inModes:[CPDefaultRunLoopMode]];
|
|
|
|
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
|
|
|
|
[self assertTrue:[aPerformer.callArgs containsObject:5]];
|
|
[self assertTrue:[aPerformer.callArgs containsObject:1]];
|
|
[CPObject cancelPreviousPerformRequestsWithTarget:aPerformer];
|
|
|
|
var performer1 = [Performer new],
|
|
performer2 = [Performer new];
|
|
|
|
[performer1 performSelector:@selector(performWithObject:) withObject:5 afterDelay:0 inModes:[CPDefaultRunLoopMode]];
|
|
[performer2 performSelector:@selector(performWithObject:) withObject:10 afterDelay:0 inModes:[CPDefaultRunLoopMode]];
|
|
[performer1 performSelector:@selector(performWithObject:) withObject:1 afterDelay:0 inModes:[CPDefaultRunLoopMode]];
|
|
[CPObject cancelPreviousPerformRequestsWithTarget:performer1];
|
|
|
|
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
|
|
|
|
[self assert:nil equals:performer1.callArgs];
|
|
[self assert:[10] equals:performer2.callArgs];
|
|
}
|
|
|
|
- (void)testPerformBlock
|
|
{
|
|
var i = 0;
|
|
|
|
[[CPRunLoop mainRunLoop] performBlock:function()
|
|
{i++} argument:nil order:0 modes:[CPDefaultRunLoopMode]];
|
|
|
|
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
|
|
|
|
[self assert:i equals:1];
|
|
}
|
|
|
|
@end
|
|
|
|
@implementation Performer : CPObject
|
|
{
|
|
CPArray callArgs;
|
|
}
|
|
|
|
- (void)performWithObject:(id)anObject
|
|
{
|
|
if (!callArgs)
|
|
callArgs = [];
|
|
|
|
[callArgs addObject:anObject];
|
|
}
|
|
|
|
@end
|