Skip to content

Commit

Permalink
improved bridge sync mechanism - this time with average time to handl…
Browse files Browse the repository at this point in the history
…e the bridge probe
  • Loading branch information
talkol committed Sep 28, 2016
1 parent 84057cf commit b950133
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions detox/ios/Detox/ReactNativeBridgeIdlingResource.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
#import "Common/GREYDefines.h"
#import "Common/GREYPrivate.h"

static const CGFloat MOVING_AVERAGE_WEIGHT = 0.2;
static const CGFloat MIN_THRESHOLD_FOR_IDLE = 10;

// save this state variable between RN reloads
static NSTimeInterval _timeToIdleMin = 1.0;

typedef enum {
kWaiting,
kBusy,
Expand All @@ -22,7 +28,8 @@ @implementation ReactNativeBridgeIdlingResource
NSString *_name;
id<RN_RCTBridge> _bridge;
IdlingCheckState _state;
int _consecutiveIdles;
NSTimeInterval _lastIdleTime;
NSTimeInterval _timeToIdleAvg;
}

+ (instancetype)idlingResourceForBridge:(id)bridge name:(NSString *)name
Expand Down Expand Up @@ -53,7 +60,8 @@ - (instancetype)initWithBridge:(id<RN_RCTBridge>)bridge name:(NSString *)name
_name = [name copy];
_bridge = bridge;
_state = kBusy;
_consecutiveIdles = 0;
_lastIdleTime = CACurrentMediaTime();
_timeToIdleAvg = 0;
}
return self;
}
Expand All @@ -70,18 +78,22 @@ - (NSString *)idlingResourceDescription {

- (BOOL)isIdleNow
{
NSTimeInterval time = CACurrentMediaTime();
NSTimeInterval timeSoFar = time - _lastIdleTime;

if (_bridge == nil) return NO;
if (![_bridge isValid] || [_bridge isLoading]) return NO;
id<RN_RCTJavaScriptExecutor> executor = [_bridge valueForKey:@"javaScriptExecutor"];
if (executor == nil) return NO;

BOOL wasIdle = NO;
if (_state == kIdle)
{
_consecutiveIdles++;
}
else
{
_consecutiveIdles = 0;
wasIdle = YES;
NSTimeInterval idleTime = CACurrentMediaTime();
_timeToIdleAvg = (1.0 - MOVING_AVERAGE_WEIGHT) * _timeToIdleAvg + MOVING_AVERAGE_WEIGHT * timeSoFar;
if (_timeToIdleAvg < _timeToIdleMin) _timeToIdleMin = _timeToIdleAvg;
_lastIdleTime = idleTime;
}
if (_state != kWaiting)
{
Expand All @@ -94,7 +106,11 @@ - (BOOL)isIdleNow
}];
}
BOOL res = NO;
if (_consecutiveIdles > 2) res = YES;

// consider if (timeSoFar < MIN_THRESHOLD_FOR_IDLE * _timeToIdleMin) only
if (timeSoFar < MIN_THRESHOLD_FOR_IDLE * _timeToIdleMin && _timeToIdleAvg < MIN_THRESHOLD_FOR_IDLE * _timeToIdleMin) res = YES;

// NSLog(@"idle=%d, timeSoFar = %f, avg = %f, min = %f", res, timeSoFar, _timeToIdleAvg, _timeToIdleMin);
// NSLog(@"ReactNativeBridgeIdlingResource: idle=%d (%d)", res, _consecutiveIdles);
return res;
}
Expand Down

0 comments on commit b950133

Please sign in to comment.