-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve blurring mechanism by delaying another run
- Loading branch information
Showing
8 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// DebugLog.h | ||
// Bedim | ||
// | ||
// Created by Victor Gama on 16/10/2017. | ||
// Copyright © 2017 Victor Gama. All rights reserved. | ||
// | ||
|
||
#ifndef DebugLog_h | ||
#define DebugLog_h | ||
|
||
#ifdef DEBUG | ||
#define Debug(...) NSLog(__VA_ARGS__); | ||
#else | ||
#define Debug(...) while(false){}; | ||
#endif | ||
#endif /* DebugLog_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// NSObject+BDDebouncer.h | ||
// Bedim | ||
// | ||
// Created by Victor Gama on 16/10/2017. | ||
// Copyright © 2017 Victor Gama. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface NSObject (BDDebouncer) | ||
|
||
- (void)debounce:(nonnull SEL)action withObject:(nullable id)obj delaying:(NSTimeInterval)interval; | ||
- (void)repeat:(nonnull SEL)action after:(NSTimeInterval)interval; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// NSObject+BDDebouncer.m | ||
// Bedim | ||
// | ||
// Created by Victor Gama on 16/10/2017. | ||
// Copyright © 2017 Victor Gama. All rights reserved. | ||
// | ||
|
||
#import "NSObject+BDDebouncer.h" | ||
#import "DebugLog.h" | ||
|
||
@implementation NSObject (BDDebouncer) | ||
|
||
- (void)debounce:(SEL)action withObject:(id)obj delaying:(NSTimeInterval)interval { | ||
__weak typeof(self) weakSelf = self; | ||
[NSObject cancelPreviousPerformRequestsWithTarget:weakSelf selector:action object:obj]; | ||
if(![self respondsToSelector:action]) { | ||
Debug(@"[BDDebouncer] Cancelling debounce: call since %@ does not respond to %@", self, NSStringFromSelector(action)); | ||
return; | ||
} | ||
[weakSelf performSelector:action withObject:obj afterDelay:interval]; | ||
} | ||
|
||
- (void)repeat:(SEL)action after:(NSTimeInterval)interval { | ||
[self debounce:@selector(redo:) withObject:NSStringFromSelector(action) delaying:interval]; | ||
} | ||
|
||
- (void)redo:(NSString *)rawAction { | ||
SEL action = NSSelectorFromString(rawAction); | ||
Debug(@"[BDDebouncer] Performing redo:(%@) on %@", NSStringFromSelector(action), self); | ||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ | ||
if(![self respondsToSelector:action]) { | ||
NSLog(@"[BDDebouncer] Cancelling redo: call since %@ does not respond to %@", self, NSStringFromSelector(action)); | ||
return; | ||
} | ||
IMP imp = [self methodForSelector:action]; | ||
void (*func)(id, SEL) = (void *)imp; | ||
func(self, action); | ||
}); | ||
} | ||
|
||
@end |