Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add resetLimitForName: to SSRateLimit

  • Loading branch information...
commit bb374bcc71d81c20c4de99e03eae1b30d0cfe231 1 parent c83db82
@soffes soffes authored
Showing with 31 additions and 6 deletions.
  1. +9 −0 SSToolkit/SSRateLimit.h
  2. +22 −6 SSToolkit/SSRateLimit.m
View
9 SSToolkit/SSRateLimit.h
@@ -26,4 +26,13 @@
*/
+ (BOOL)executeBlock:(void(^)(void))block name:(NSString *)name limit:(NSTimeInterval)limit;
+/**
+ Resets the time for a given name.
+
+ @param name A string identifying a block's stored time.
+
+ The next time a block with the same name is passed to `executeBlock:name:limit:` it will be executed.
+ */
++ (void)resetLimitForName:(NSString *)name;
+
@end
View
28 SSToolkit/SSRateLimit.m
@@ -8,21 +8,20 @@
#import "SSRateLimit.h"
+@interface SSRateLimit ()
++ (NSMutableDictionary *)_dictionary;
+@end
+
@implementation SSRateLimit
+ (BOOL)executeBlock:(void(^)(void))block name:(NSString *)name limit:(NSTimeInterval)limit {
- static NSMutableDictionary *dictionary = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- dictionary = [[NSMutableDictionary alloc] init];
- });
-
// Prevent a nil block
if (!block) {
return NO;
}
// Lookup last executed
+ NSMutableDictionary *dictionary = [self _dictionary];
NSDate *last = [dictionary objectForKey:name];
NSTimeInterval timeInterval = [last timeIntervalSinceNow];
@@ -37,4 +36,21 @@ + (BOOL)executeBlock:(void(^)(void))block name:(NSString *)name limit:(NSTimeInt
return YES;
}
+
++ (void)resetLimitForName:(NSString *)name {
+ [[self _dictionary] removeObjectForKey:name];
+}
+
+
+#pragma mark - Private
+
++ (NSMutableDictionary *)_dictionary {
+ static NSMutableDictionary *dictionary = nil;
+ static dispatch_once_t onceToken;
+ dispatch_once(&onceToken, ^{
+ dictionary = [[NSMutableDictionary alloc] init];
+ });
+ return dictionary;
+}
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.