API  1.0.0
CPDelayedPerform.j
Go to the documentation of this file.
1 /*
2  * CPDelayedPerform.j
3  * Foundation
4  *
5  * Portions based on NSDelayedPerform.m (2013-03-03) in Cocotron (http://www.cocotron.org/)
6  * Copyright (c) 2006-2007 Christopher J. W. Lloyd
7  *
8  * Created by Alexander Ljungberg.
9  * Copyright 2013, SlevenBits Ltd.
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 
27 @implementation CPDelayedPerform : CPObject
28 {
29  id _object;
30  SEL _selector;
31  id _argument;
32 }
33 
34 + (CPDelayedPerform)delayedPerformWithObject:anObject selector:(SEL)aSelector argument:anArgument
35 {
36  return [[self alloc] initWithObject:anObject selector:aSelector argument:anArgument];
37 }
38 
39 - (id)initWithObject:(id)anObject selector:(SEL)aSelector argument:(id)anArgument
40 {
41  if (self = [super init])
42  {
43  _object = anObject;
44  _selector = aSelector;
45  _argument = anArgument;
46  }
47 
48  return self;
49 }
50 
51 - (BOOL)isEqualToPerform:(CPDelayedPerform)anOther
52 {
53  if (!anOther || !anOther.isa)
54  return NO;
55 
56  if (_object !== anOther._object)
57  return NO;
58 
59  if (!_selector || !anOther._selector)
60  return YES;
61 
62  if (_selector !== anOther._selector)
63  return NO;
64 
65  if (_argument !== anOther._argument)
66  return NO;
67 
68  return YES;
69 }
70 
71 - (void)perform
72 {
73  try
74  {
75  [_object performSelector:_selector withObject:_argument];
76  }
77  catch(ex)
78  {
79  CPLog(@"exception %@ raised during delayed perform", ex);
80  }
81 }
82 
83 @end
84 
86 
87 - (void)invalidateTimerWithDelayedPerform:(CPDelayedPerform)aDelayedPerform
88 {
89  for (var aKey in _timersForModes)
90  {
91  if (!_timersForModes.hasOwnProperty(aKey))
92  continue;
93 
94  var timersForMode = _timersForModes[aKey];
95  for (var i = 0, count = [timersForMode count]; i < count; i++)
96  {
97  var aTimer = [timersForMode objectAtIndex:i],
98  userInfo = [aTimer userInfo];
99 
100  if ([userInfo isKindOfClass:CPDelayedPerform] && [userInfo isEqualToPerform:aDelayedPerform])
101  [aTimer invalidate];
102  }
103  }
104 }
105 
106 @end
107 
109 
110 + (void)cancelPreviousPerformRequestsWithTarget:target selector:(SEL)selector object:argument
111 {
112  var aDelayedPerform = [CPDelayedPerform delayedPerformWithObject:target selector:selector argument:argument];
113 
115 }
116 
117 + (void)cancelPreviousPerformRequestsWithTarget:target
118 {
119  var aDelayedPerform = [CPDelayedPerform delayedPerformWithObject:target selector:NULL argument:nil];
120 
122 }
123 
124 + (void)_delayedPerform:(CPTimer)aTimer
125 {
126  var aDelayedPerform = [aTimer userInfo];
127 
128  [aDelayedPerform perform];
129 }
130 
131 + (void)object:object performSelector:(SEL)selector withObject:argument afterDelay:(CPTimeInterval)delay inModes:(CPArray)modes
132 {
133  var aDelayedPerform = [CPDelayedPerform delayedPerformWithObject:object selector:selector argument:argument],
134  aTimer = [CPTimer timerWithTimeInterval:delay target:[CPObject class] selector:@selector(_delayedPerform:) userInfo:aDelayedPerform repeats:NO];
135 
136  for (var i = 0, count = [modes count]; i < count; i++)
137  [[CPRunLoop currentRunLoop] addTimer:aTimer forMode:[modes objectAtIndex:i]];
138 }
139 
140 - (void)performSelector:(SEL)selector withObject:object afterDelay:(CPTimeInterval)delay
141 {
142  [[self class] object:self performSelector:selector withObject:object afterDelay:delay inModes:[CPArray arrayWithObject:CPDefaultRunLoopMode]];
143 }
144 
145 - (void)performSelector:(SEL)selector withObject:object afterDelay:(CPTimeInterval)delay inModes:(CPArray)modes
146 {
147  [[self class] object:self performSelector:selector withObject:object afterDelay:delay inModes:modes];
148 }
149 
150 @end
id init()
Definition: CALayer.j:126
CPDelayedPerform delayedPerformWithObject:selector:argument:(id anObject, [selector] SEL aSelector, [argument] id anArgument)
The main run loop for the application.
Definition: CPRunLoop.h:2
CPRunLoop currentRunLoop()
Definition: CPRunLoop.j:232
void invalidateTimerWithDelayedPerform:(CPDelayedPerform aDelayedPerform)
CPTimer timerWithTimeInterval:target:selector:userInfo:repeats:(CPTimeInterval seconds, [target] id aTarget, [selector] SEL aSelector, [userInfo] id userInfo, [repeats] BOOL shouldRepeat)
Definition: CPTimer.j:90
id userInfo()
Definition: CPTimer.j:229
A timer object that can send a message after the given time interval.
Definition: CPTimer.h:2
id initWithObject:selector:argument:(id anObject, [selector] SEL aSelector, [argument] id anArgument)
Class class()
Definition: CPObject.j:179
id alloc()
Definition: CPObject.j:130