Skip to content

Commit d9eafd2

Browse files
committed
alwaysInvokeBlock 传入的第一个参数改为 MTInvocation
1 parent 7f129e0 commit d9eafd2

5 files changed

Lines changed: 29 additions & 6 deletions

File tree

MTDemo/MTDemo/ViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ - (void)viewDidLoad {
3939
// [MTEngine.defaultEngine applyRule:rule];
4040

4141
// 跟上面的用法等价
42-
__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) {
42+
__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) {
4343
if ([date isEqualToDate:[NSDate dateWithTimeIntervalSince1970:0]]) {
4444
return YES;
4545
}

MTDemo/MTDemoTests/MTDemoTests.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ - (void)testSampleDurationZero
6969

7070
- (void)testSampleAlwaysInvoke
7171
{
72-
[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) {
72+
[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) {
7373
return YES;
7474
}];
7575
[self.stub foo:[NSDate date]];
@@ -289,7 +289,7 @@ - (void)testApplyRuleTwice {
289289

290290
- (void)testPerformanceExample {
291291
// This is an example of a performance test case.
292-
[self.stub mt_limitSelector:@selector(foo:) oncePerDuration:0.01 usingMode:MTPerformModeDebounce onMessageQueue:nil alwaysInvokeBlock:^(MTRule *rule, NSDate *date) {
292+
[self.stub mt_limitSelector:@selector(foo:) oncePerDuration:0.01 usingMode:MTPerformModeDebounce onMessageQueue:nil alwaysInvokeBlock:^(MTInvocation *invocation, NSDate *date) {
293293
return YES;
294294
}];
295295
NSDate *date = [NSDate date];

MessageThrottle.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "MessageThrottle"
3-
s.version = "1.3.7"
3+
s.version = "1.4.0"
44
s.summary = "A lightweight Objective-C message throttle and debounce library."
55
s.description = <<-DESC
66
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.

MessageThrottle/MessageThrottle.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Class mt_metaClass(Class cls);
5858

5959
/**
6060
是否必须执行消息。block 的参数列表可选,返回值为 BOOL 类型。
61-
block 传入的第一个参数为 `self`,其余参数列表与消息调用的参数列表相同。
61+
block 传入的第一个参数为 `MTInvocation`,其余参数列表与消息调用的参数列表相同。
6262
block 如果返回 YES,则消息立即执行,但不会影响当前节流模式。
6363
*/
6464
@property (nonatomic, readonly) id alwaysInvokeBlock;
@@ -193,4 +193,11 @@ Class mt_metaClass(Class cls);
193193

194194
@end
195195

196+
@interface MTInvocation : NSObject
197+
198+
@property (nonatomic, weak, readonly) NSInvocation *invocation;
199+
@property (nonatomic, weak, readonly) MTRule *rule;
200+
201+
@end
202+
196203
NS_ASSUME_NONNULL_END

MessageThrottle/MessageThrottle.m

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,17 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder
250250

251251
@end
252252

253+
@interface MTInvocation ()
254+
255+
@property (nonatomic, weak, readwrite) NSInvocation *invocation;
256+
@property (nonatomic, weak, readwrite) MTRule *rule;
257+
258+
@end
259+
260+
@implementation MTInvocation
261+
262+
@end
263+
253264
@interface MTEngine ()
254265

255266
@property (nonatomic) NSMapTable<id, NSMutableSet<NSString *> *> *targetSELs;
@@ -549,8 +560,13 @@ static BOOL mt_invokeFilterBlock(MTRule *rule, NSInvocation *originalInvocation)
549560
return NO;
550561
}
551562

563+
MTInvocation *invocation = nil;
564+
552565
if (numberOfArguments > 1) {
553-
[blockInvocation setArgument:&rule atIndex:1];
566+
invocation = [MTInvocation new];
567+
invocation.invocation = originalInvocation;
568+
invocation.rule = rule;
569+
[blockInvocation setArgument:&invocation atIndex:1];
554570
}
555571

556572
void *argBuf = NULL;

0 commit comments

Comments
 (0)