Skip to content

Commit

Permalink
alwaysInvokeBlock 传入的第一个参数改为 MTInvocation
Browse files Browse the repository at this point in the history
  • Loading branch information
yulingtianxia committed Jul 15, 2019
1 parent 7f129e0 commit d9eafd2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion MTDemo/MTDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions MTDemo/MTDemoTests/MTDemoTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
Expand Down Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion MessageThrottle.podspec
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
9 changes: 8 additions & 1 deletion MessageThrottle/MessageThrottle.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Class mt_metaClass(Class cls);

/**
是否必须执行消息。block 的参数列表可选,返回值为 BOOL 类型。
block 传入的第一个参数为 `self`,其余参数列表与消息调用的参数列表相同。
block 传入的第一个参数为 `MTInvocation`,其余参数列表与消息调用的参数列表相同。
block 如果返回 YES,则消息立即执行,但不会影响当前节流模式。
*/
@property (nonatomic, readonly) id alwaysInvokeBlock;
Expand Down Expand Up @@ -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
18 changes: 17 additions & 1 deletion MessageThrottle/MessageThrottle.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<id, NSMutableSet<NSString *> *> *targetSELs;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d9eafd2

Please sign in to comment.