From d9eafd28b3da2e0030c65a2b99eba90a04a74147 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E8=90=A7=E7=8E=89?= Date: Mon, 15 Jul 2019 11:39:37 +0800 Subject: [PATCH] =?UTF-8?q?alwaysInvokeBlock=20=E4=BC=A0=E5=85=A5=E7=9A=84?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=8F=82=E6=95=B0=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=20MTInvocation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MTDemo/MTDemo/ViewController.m | 2 +- MTDemo/MTDemoTests/MTDemoTests.m | 4 ++-- MessageThrottle.podspec | 2 +- MessageThrottle/MessageThrottle.h | 9 ++++++++- MessageThrottle/MessageThrottle.m | 18 +++++++++++++++++- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/MTDemo/MTDemo/ViewController.m b/MTDemo/MTDemo/ViewController.m index fe5a6c7..b64637f 100644 --- a/MTDemo/MTDemo/ViewController.m +++ b/MTDemo/MTDemo/ViewController.m @@ -39,7 +39,7 @@ - (void)viewDidLoad { // [MTEngine.defaultEngine applyRule:rule]; // 跟上面的用法等价 - __unused MTRule *rule = [self.stub mt_limitSelector:@selector(foo:) oncePerDuration:0.5 usingMode:MTPerformModeDebounce onMessageQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) alwaysInvokeBlock:^(MTRule *rule, NSDate *date) { + __unused MTRule *rule = [self.stub mt_limitSelector:@selector(foo:) oncePerDuration:0.5 usingMode:MTPerformModeDebounce onMessageQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) alwaysInvokeBlock:^(MTInvocation *invocation, NSDate *date) { if ([date isEqualToDate:[NSDate dateWithTimeIntervalSince1970:0]]) { return YES; } diff --git a/MTDemo/MTDemoTests/MTDemoTests.m b/MTDemo/MTDemoTests/MTDemoTests.m index c982167..aad8f0e 100644 --- a/MTDemo/MTDemoTests/MTDemoTests.m +++ b/MTDemo/MTDemoTests/MTDemoTests.m @@ -69,7 +69,7 @@ - (void)testSampleDurationZero - (void)testSampleAlwaysInvoke { - [self.stub mt_limitSelector:@selector(foo:) oncePerDuration:0.01 usingMode:MTPerformModeFirstly onMessageQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) alwaysInvokeBlock:^(MTRule *rule, NSDate *date) { + [self.stub mt_limitSelector:@selector(foo:) oncePerDuration:0.01 usingMode:MTPerformModeFirstly onMessageQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) alwaysInvokeBlock:^(MTInvocation *invocation, NSDate *date) { return YES; }]; [self.stub foo:[NSDate date]]; @@ -289,7 +289,7 @@ - (void)testApplyRuleTwice { - (void)testPerformanceExample { // This is an example of a performance test case. - [self.stub mt_limitSelector:@selector(foo:) oncePerDuration:0.01 usingMode:MTPerformModeDebounce onMessageQueue:nil alwaysInvokeBlock:^(MTRule *rule, NSDate *date) { + [self.stub mt_limitSelector:@selector(foo:) oncePerDuration:0.01 usingMode:MTPerformModeDebounce onMessageQueue:nil alwaysInvokeBlock:^(MTInvocation *invocation, NSDate *date) { return YES; }]; NSDate *date = [NSDate date]; diff --git a/MessageThrottle.podspec b/MessageThrottle.podspec index d0c358f..feebf3f 100644 --- a/MessageThrottle.podspec +++ b/MessageThrottle.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "MessageThrottle" -s.version = "1.3.7" +s.version = "1.4.0" s.summary = "A lightweight Objective-C message throttle and debounce library." s.description = <<-DESC MessageThrottle is a lightweight, simple library for controlling frequency of forwarding Objective-C messages. You can choose to control existing methods per instance or per class. It's an implementation of function throttle/debounce developed with Objective-C runtime. diff --git a/MessageThrottle/MessageThrottle.h b/MessageThrottle/MessageThrottle.h index 74e144c..1f649c8 100644 --- a/MessageThrottle/MessageThrottle.h +++ b/MessageThrottle/MessageThrottle.h @@ -58,7 +58,7 @@ Class mt_metaClass(Class cls); /** 是否必须执行消息。block 的参数列表可选,返回值为 BOOL 类型。 - block 传入的第一个参数为 `self`,其余参数列表与消息调用的参数列表相同。 + block 传入的第一个参数为 `MTInvocation`,其余参数列表与消息调用的参数列表相同。 block 如果返回 YES,则消息立即执行,但不会影响当前节流模式。 */ @property (nonatomic, readonly) id alwaysInvokeBlock; @@ -193,4 +193,11 @@ Class mt_metaClass(Class cls); @end +@interface MTInvocation : NSObject + +@property (nonatomic, weak, readonly) NSInvocation *invocation; +@property (nonatomic, weak, readonly) MTRule *rule; + +@end + NS_ASSUME_NONNULL_END diff --git a/MessageThrottle/MessageThrottle.m b/MessageThrottle/MessageThrottle.m index f31e189..9a6eabb 100644 --- a/MessageThrottle/MessageThrottle.m +++ b/MessageThrottle/MessageThrottle.m @@ -250,6 +250,17 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder @end +@interface MTInvocation () + +@property (nonatomic, weak, readwrite) NSInvocation *invocation; +@property (nonatomic, weak, readwrite) MTRule *rule; + +@end + +@implementation MTInvocation + +@end + @interface MTEngine () @property (nonatomic) NSMapTable *> *targetSELs; @@ -549,8 +560,13 @@ static BOOL mt_invokeFilterBlock(MTRule *rule, NSInvocation *originalInvocation) return NO; } + MTInvocation *invocation = nil; + if (numberOfArguments > 1) { - [blockInvocation setArgument:&rule atIndex:1]; + invocation = [MTInvocation new]; + invocation.invocation = originalInvocation; + invocation.rule = rule; + [blockInvocation setArgument:&invocation atIndex:1]; } void *argBuf = NULL;