Skip to content

Commit

Permalink
Add spec for Timing (#24889)
Browse files Browse the repository at this point in the history
Summary:
Part of #24875

## Changelog

[General] [Added] - Add TurboModule spec for Timing
Pull Request resolved: #24889

Reviewed By: fkgozali

Differential Revision: D15379559

Pulled By: RSNara

fbshipit-source-id: d254fb4d1cae3533bbd63bf7ec739cebc6ca14b0
  • Loading branch information
jeanregisser authored and facebook-github-bot committed May 22, 2019
1 parent 94029ee commit db0ec66
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
41 changes: 31 additions & 10 deletions Libraries/Core/Timers/JSTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ const Platform = require('../../Utilities/Platform');
const Systrace = require('../../Performance/Systrace');

const invariant = require('invariant');
const {Timing} = require('../../BatchedBridge/NativeModules');
const BatchedBridge = require('../../BatchedBridge/BatchedBridge');

import NativeTiming from './NativeTiming';

import type {ExtendedError} from '../Devtools/parseErrorStack';

let _performanceNow = null;
Expand Down Expand Up @@ -221,7 +222,7 @@ function _freeCallback(timerID: number) {
_clearIndex(index);
const type = types[index];
if (type !== 'setImmediate' && type !== 'requestIdleCallback') {
Timing.deleteTimer(timerID);
deleteTimer(timerID);
}
}
}
Expand Down Expand Up @@ -250,7 +251,7 @@ const JSTimers = {
() => func.apply(undefined, args),
'setTimeout',
);
Timing.createTimer(id, duration || 0, Date.now(), /* recurring */ false);
createTimer(id, duration || 0, Date.now(), /* recurring */ false);
return id;
},

Expand All @@ -276,7 +277,7 @@ const JSTimers = {
() => func.apply(undefined, args),
'setInterval',
);
Timing.createTimer(id, duration || 0, Date.now(), /* recurring */ true);
createTimer(id, duration || 0, Date.now(), /* recurring */ true);
return id;
},

Expand All @@ -298,7 +299,7 @@ const JSTimers = {
*/
requestAnimationFrame: function(func: Function) {
const id = _allocateCallback(func, 'requestAnimationFrame');
Timing.createTimer(id, 1, Date.now(), /* recurring */ false);
createTimer(id, 1, Date.now(), /* recurring */ false);
return id;
},

Expand All @@ -309,7 +310,7 @@ const JSTimers = {
*/
requestIdleCallback: function(func: Function, options: ?Object) {
if (requestIdleCallbacks.length === 0) {
Timing.setSendIdleEvents(true);
setSendIdleEvents(true);
}

const timeout = options && options.timeout;
Expand Down Expand Up @@ -337,7 +338,7 @@ const JSTimers = {
}
delete requestIdleCallbackTimeouts[id];
if (requestIdleCallbacks.length === 0) {
Timing.setSendIdleEvents(false);
setSendIdleEvents(false);
}
}, timeout);
requestIdleCallbackTimeouts[id] = timeoutId;
Expand All @@ -359,7 +360,7 @@ const JSTimers = {
}

if (requestIdleCallbacks.length === 0) {
Timing.setSendIdleEvents(false);
setSendIdleEvents(false);
}
},

Expand Down Expand Up @@ -437,7 +438,7 @@ const JSTimers = {
}

if (requestIdleCallbacks.length === 0) {
Timing.setSendIdleEvents(false);
setSendIdleEvents(false);
}

if (errors) {
Expand Down Expand Up @@ -477,8 +478,28 @@ const JSTimers = {
},
};

function createTimer(
callbackID: number,
duration: number,
jsSchedulingTime: number,
repeats: boolean,
): void {
invariant(NativeTiming, 'NativeTiming is available');
NativeTiming.createTimer(callbackID, duration, jsSchedulingTime, repeats);
}

function deleteTimer(timerID: number): void {
invariant(NativeTiming, 'NativeTiming is available');
NativeTiming.deleteTimer(timerID);
}

function setSendIdleEvents(sendIdleEvents: boolean): void {
invariant(NativeTiming, 'NativeTiming is available');
NativeTiming.setSendIdleEvents(sendIdleEvents);
}

let ExportedJSTimers;
if (!Timing) {
if (!NativeTiming) {
console.warn("Timing native module is not available, can't set timers.");
// $FlowFixMe: we can assume timers are generally available
ExportedJSTimers = ({
Expand Down
27 changes: 27 additions & 0 deletions Libraries/Core/Timers/NativeTiming.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/

'use strict';

import type {TurboModule} from 'RCTExport';
import * as TurboModuleRegistry from 'TurboModuleRegistry';

export interface Spec extends TurboModule {
+createTimer: (
callbackID: number,
duration: number,
jsSchedulingTime: number,
repeats: boolean,
) => void;
+deleteTimer: (timerID: number) => void;
+setSendIdleEvents: (sendIdleEvents: boolean) => void;
}

export default TurboModuleRegistry.get<Spec>('Timing');

0 comments on commit db0ec66

Please sign in to comment.