00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import "CPObject.j"
00024 @import "CPInvocation.j"
00025 @import "CPDate.j"
00026 @import "CPRunLoop.j"
00027
00034 @implementation CPTimer : CPObject
00035 {
00036 CPTimeInterval _timeInterval;
00037 CPInvocation _invocation;
00038 Function _callback;
00039
00040 BOOL _repeats;
00041 BOOL _isValid;
00042 CPDate _fireDate;
00043 id _userInfo;
00044 }
00045
00049 + (CPTimer)scheduledTimerWithTimeInterval:(CPTimeInterval)seconds invocation:(CPInvocation)anInvocation repeats:(BOOL)shouldRepeat
00050 {
00051 var timer = [[self alloc] initWithFireDate:[CPDate dateWithTimeIntervalSinceNow:seconds] interval:seconds invocation:anInvocation repeats:shouldRepeat];
00052
00053
00054 [[CPRunLoop currentRunLoop] addTimer:timer forMode:CPDefaultRunLoopMode];
00055
00056 return timer;
00057 }
00058
00062 + (CPTimer)scheduledTimerWithTimeInterval:(CPTimeInterval)seconds target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)shouldRepeat
00063 {
00064 var timer = [[self alloc] initWithFireDate:[CPDate dateWithTimeIntervalSinceNow:seconds] interval:seconds target:aTarget selector:aSelector userInfo:userInfo repeats:shouldRepeat]
00065
00066
00067 [[CPRunLoop currentRunLoop] addTimer:timer forMode:CPDefaultRunLoopMode];
00068
00069 return timer;
00070 }
00071
00075 + (CPTimer)scheduledTimerWithTimeInterval:(CPTimeInterval)seconds callback:(Function)aFunction repeats:(BOOL)shouldRepeat
00076 {
00077 var timer = [[self alloc] initWithFireDate:[CPDate dateWithTimeIntervalSinceNow:seconds] interval:seconds callback:aFunction repeats:shouldRepeat];
00078
00079
00080 [[CPRunLoop currentRunLoop] addTimer:timer forMode:CPDefaultRunLoopMode];
00081
00082 return timer;
00083 }
00084
00088 + (CPTimer)timerWithTimeInterval:(CPTimeInterval)seconds invocation:(CPInvocation)anInvocation repeats:(BOOL)shouldRepeat
00089 {
00090 return [[self alloc] initWithFireDate:[CPDate dateWithTimeIntervalSinceNow:seconds] interval:seconds invocation:anInvocation repeats:shouldRepeat];
00091 }
00092
00096 + (CPTimer)timerWithTimeInterval:(CPTimeInterval)seconds target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)shouldRepeat
00097 {
00098 return [[self alloc] initWithFireDate:[CPDate dateWithTimeIntervalSinceNow:seconds] interval:seconds target:aTarget selector:aSelector userInfo:userInfo repeats:shouldRepeat];
00099 }
00100
00104 + (CPTimer)timerWithTimeInterval:(CPTimeInterval)seconds callback:(Function)aFunction repeats:(BOOL)shouldRepeat
00105 {
00106 return [[self alloc] initWithFireDate:[CPDate dateWithTimeIntervalSinceNow:seconds] interval:seconds callback:aFunction repeats:shouldRepeat];
00107 }
00108
00112 - (id)initWithFireDate:(CPDate)aDate interval:(CPTimeInterval)seconds invocation:(CPInvocation)anInvocation repeats:(BOOL)shouldRepeat
00113 {
00114 self = [super init];
00115
00116 if (self)
00117 {
00118 _timeInterval = seconds;
00119 _invocation = anInvocation;
00120 _repeats = shouldRepeat;
00121 _isValid = YES;
00122 _fireDate = aDate;
00123 }
00124
00125 return self;
00126 }
00127
00131 - (id)initWithFireDate:(CPDate)aDate interval:(CPTimeInterval)seconds target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)shouldRepeat
00132 {
00133 var invocation = [CPInvocation invocationWithMethodSignature:1];
00134
00135 [invocation setTarget:aTarget];
00136 [invocation setSelector:aSelector];
00137 [invocation setArgument:self atIndex:2];
00138
00139 self = [self initWithFireDate:aDate interval:seconds invocation:invocation repeats:shouldRepeat];
00140
00141 if (self)
00142 _userInfo = userInfo;
00143
00144 return self;
00145 }
00146
00150 - (id)initWithFireDate:(CPDate)aDate interval:(CPTimeInterval)seconds callback:(Function)aFunction repeats:(BOOL)shouldRepeat
00151 {
00152 self = [super init];
00153
00154 if (self)
00155 {
00156 _timeInterval = seconds;
00157 _callback = aFunction;
00158 _repeats = shouldRepeat;
00159 _isValid = YES;
00160 _fireDate = aDate;
00161 }
00162
00163 return self;
00164 }
00165
00169 - (CPTimeInterval)timeInterval
00170 {
00171 return _timeInterval;
00172 }
00173
00177 - (CPDate)fireDate
00178 {
00179 return _fireDate;
00180 }
00181
00185 - (void)setFireDate:(CPDate)aDate
00186 {
00187 _fireDate = aDate;
00188 }
00189
00193 - (void)fire
00194 {
00195 if (!_isValid)
00196 return;
00197
00198 if (_callback)
00199 _callback();
00200 else
00201 [_invocation invoke];
00202
00203 if (!_isValid)
00204 return;
00205
00206 if (_repeats)
00207 _fireDate = [CPDate dateWithTimeIntervalSinceNow:_timeInterval];
00208
00209 else
00210 [self invalidate];
00211 }
00212
00216 - (BOOL)isValid
00217 {
00218 return _isValid;
00219 }
00220
00224 - (void)invalidate
00225 {
00226 _isValid = NO;
00227 _userInfo = nil;
00228 _invocation = nil;
00229 _callback = nil;
00230 }
00231
00235 - (id)userInfo
00236 {
00237 return _userInfo;
00238 }
00239
00240 @end
00241
00242 var CPTimersTimeoutID = 1000,
00243 CPTimersForTimeoutIDs = {};
00244
00245 var _CPTimerBridgeTimer = function(codeOrFunction, aDelay, shouldRepeat, functionArgs)
00246 {
00247 var timeoutID = CPTimersTimeoutID++,
00248 theFunction = nil;
00249
00250 if (typeof codeOrFunction === "string")
00251 theFunction = function() { new Function(codeOrFunction)(); if (!shouldRepeat) CPTimersForTimeoutIDs[timeoutID] = nil; }
00252 else
00253 {
00254 if (!functionArgs)
00255 functionArgs = [];
00256
00257 theFunction = function() { codeOrFunction.apply(window, functionArgs); if (!shouldRepeat) CPTimersForTimeoutIDs[timeoutID] = nil; }
00258 }
00259
00260 CPTimersForTimeoutIDs[timeoutID] = [CPTimer scheduledTimerWithTimeInterval:aDelay / 1000 callback:theFunction repeats:shouldRepeat];
00261
00262 return timeoutID;
00263 }
00264
00265 window.setTimeout = function(codeOrFunction, aDelay)
00266 {
00267 return _CPTimerBridgeTimer(codeOrFunction, aDelay, NO, Array.prototype.slice.apply(arguments, [2]));
00268 }
00269
00270 window.clearTimeout = function(aTimeoutID)
00271 {
00272 var timer = CPTimersForTimeoutIDs[aTimeoutID];
00273
00274 if (timer)
00275 [timer invalidate];
00276
00277 CPTimersForTimeoutIDs[aTimeoutID] = nil;
00278 }
00279
00280 window.setInterval = function(codeOrFunction, aDelay, functionArgs)
00281 {
00282 return _CPTimerBridgeTimer(codeOrFunction, aDelay, YES, Array.prototype.slice.apply(arguments, [2]));
00283 }
00284
00285 window.clearInterval = function(aTimeoutID)
00286 {
00287 window.clearTimeout(aTimeoutID);
00288 }