Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clock equals 0 on first evaluation #121

Merged
merged 8 commits into from
Nov 6, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 23 additions & 21 deletions ios/REANodesManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,9 @@ - (void)operationsBatchDidComplete
// been scheduled as it may mean the new view has just been mounted and expects its initial
// props to be calculated.
// Unfortunately if the operation has just scheduled animation callback it won't run until the
// next frame. So if displayLink is set we trigger onAnimationFrame callback to make sure it
// runs in the correct frame.
[REANode runPropUpdates:_updateContext];
if (_operationsInBatch.count != 0) {
NSMutableArray<REANativeAnimationOp> *copiedOperationsQueue = _operationsInBatch;
_operationsInBatch = [NSMutableArray new];
RCTExecuteOnUIManagerQueue(^{
for (int i = 0; i < copiedOperationsQueue.count; i++) {
copiedOperationsQueue[i](self.uiManager);
}
[self.uiManager setNeedsLayout];
});
}
_wantRunUpdates = NO;
// next frame, so it's being triggered manually.
_wantRunUpdates = YES;
[self performOperations];
}
}

Expand All @@ -128,6 +117,13 @@ - (void)postRunUpdatesAfterAnimation
- (void)startUpdatingOnAnimationFrame
{
if (!_displayLink) {
// Setting _currentAnimationTimestamp here is connected with manual triggering of performOperations
// in operationsBatchDidComplete. If new node has been created and clock has not been started,
// _displayLink won't be initialized soon enough and _displayLink.timestamp will be 0.
// However, CADisplayLink is using CACurrentMediaTime so if there's need to perform one more
// evaluation, it could be used it here. In usual case, CACurrentMediaTime is not being used in
// favor of setting it with _displayLink.timestamp in onAnimationFrame method.
_currentAnimationTimestamp = CACurrentMediaTime();
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(onAnimationFrame:)];
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
Expand All @@ -143,9 +139,8 @@ - (void)stopUpdatingOnAnimationFrame

- (void)onAnimationFrame:(CADisplayLink *)displayLink
{
_currentAnimationTimestamp = displayLink.timestamp;
osdnk marked this conversation as resolved.
Show resolved Hide resolved
osdnk marked this conversation as resolved.
Show resolved Hide resolved

// We process all enqueued events first
_currentAnimationTimestamp = _displayLink.timestamp;
for (NSUInteger i = 0; i < _eventQueue.count; i++) {
id<RCTEvent> event = _eventQueue[i];
[self processEvent:event];
Expand All @@ -162,7 +157,18 @@ - (void)onAnimationFrame:(CADisplayLink *)displayLink
block(displayLink);
}

[REANode runPropUpdates:_updateContext];
[self performOperations];

if (_onAnimationCallbacks.count == 0) {
[self stopUpdatingOnAnimationFrame];
}
}

- (void)performOperations
{
if (_wantRunUpdates) {
[REANode runPropUpdates:_updateContext];
}
if (_operationsInBatch.count != 0) {
NSMutableArray<REANativeAnimationOp> *copiedOperationsQueue = _operationsInBatch;
_operationsInBatch = [NSMutableArray new];
Expand All @@ -174,10 +180,6 @@ - (void)onAnimationFrame:(CADisplayLink *)displayLink
});
}
_wantRunUpdates = NO;

if (_onAnimationCallbacks.count == 0) {
[self stopUpdatingOnAnimationFrame];
}
}

- (void)enqueueUpdateViewOnNativeThread:(nonnull NSNumber *)reactTag
Expand Down