Skip to content

matt-holden/MFHMessageDebouncer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MFHMessageDebouncer

This is an Objective-C implementation of a method debouncer. You can read more about deboucing here.

MFHMessageDebouncer is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "MFHMessageDebouncer"

#Usage

You have the option of:

  1. Debouncing all messages to an object, or
  2. Debounce all messages to any object originating at a specific call site (not yet available in Swift):

1 (most common usage):

NSTimeInterval delayLength = 1.0;
NSMutableArray *myArray = [NSMutableArray array];

for (int i = 0; i < 3; i++) {
    [[myArray debounceWithDelay:delayLength] addObject:@(i)];
}

// One second (delayLength) later, myArray will hold a single element, the number 3.
// The `addObject:` message was only sent to myArray a single time

2: (pulled straight from the unit tests)

// Make three mutable arrays
NSArray *arrayInstances = @[[NSMutableArray new], [NSMutableArray new], [NSMutableArray new]];

NSTimeInterval delay = 1.0;
for (int i = 0; i < 3; i++) {
  // Debounce any message sent from this specific call site, regardless
  // of the receiver or message name
  [MFHDebouncedCallSite(arrayInstances[i], delay) addObject:@(i)];
}

// .... one second later, 
// Only the last object that was messaged will have received 'addObject:'
[arrayInstances[0] count] == 0;  //true
[arrayInstances[1] count] == 0;  //true
[arrayInstances[2] count] == 1;  //true
[arrayInstances[2] objectAtIndex:0] == @3;  //true

Author

Matthew Holden @MFHolden

License

MFHMessageDebouncer is available under the MIT license. See the LICENSE file for more info.

About

Objective-C message wrapped in syntactic sugar

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages