Remove keyMirror usage in JSTimers
Reviewed By: davidaurelio Differential Revision: D3981015 fbshipit-source-id: 9ce7601038b7cdf8a7feb580380557ed84008397
This commit is contained in:
parent
3137ba993d
commit
d99623fcdc
|
@ -15,8 +15,11 @@
|
|||
// in dependencies. NativeModules > BatchedBridge > MessageQueue > JSTimersExecution
|
||||
const RCTTiming = require('NativeModules').Timing;
|
||||
const JSTimersExecution = require('JSTimersExecution');
|
||||
|
||||
const parseErrorStack = require('parseErrorStack');
|
||||
|
||||
import type {JSTimerType} from 'JSTimersExecution';
|
||||
|
||||
// Returns a free index if one is available, and the next consecutive index otherwise.
|
||||
function _getFreeIndex(): number {
|
||||
let freeIndex = JSTimersExecution.timerIDs.indexOf(null);
|
||||
|
@ -26,7 +29,7 @@ function _getFreeIndex(): number {
|
|||
return freeIndex;
|
||||
}
|
||||
|
||||
function _allocateCallback(func: Function, type: $Keys<typeof JSTimersExecution.Type>): number {
|
||||
function _allocateCallback(func: Function, type: JSTimerType): number {
|
||||
const id = JSTimersExecution.GUID++;
|
||||
const freeIndex = _getFreeIndex();
|
||||
JSTimersExecution.timerIDs[freeIndex] = id;
|
||||
|
@ -57,8 +60,7 @@ function _freeCallback(timerID: number) {
|
|||
if (index !== -1) {
|
||||
JSTimersExecution._clearIndex(index);
|
||||
const type = JSTimersExecution.types[index];
|
||||
if (type !== JSTimersExecution.Type.setImmediate &&
|
||||
type !== JSTimersExecution.Type.requestIdleCallback) {
|
||||
if (type !== 'setImmediate' && type !== 'requestIdleCallback') {
|
||||
RCTTiming.deleteTimer(timerID);
|
||||
}
|
||||
}
|
||||
|
@ -75,8 +77,7 @@ const JSTimers = {
|
|||
* @param {number} duration Number of milliseconds.
|
||||
*/
|
||||
setTimeout: function(func: Function, duration: number, ...args?: any): number {
|
||||
const id = _allocateCallback(() => func.apply(undefined, args),
|
||||
JSTimersExecution.Type.setTimeout);
|
||||
const id = _allocateCallback(() => func.apply(undefined, args), 'setTimeout');
|
||||
RCTTiming.createTimer(id, duration || 0, Date.now(), /* recurring */ false);
|
||||
return id;
|
||||
},
|
||||
|
@ -86,8 +87,7 @@ const JSTimers = {
|
|||
* @param {number} duration Number of milliseconds.
|
||||
*/
|
||||
setInterval: function(func: Function, duration: number, ...args?: any): number {
|
||||
const id = _allocateCallback(() => func.apply(undefined, args),
|
||||
JSTimersExecution.Type.setInterval);
|
||||
const id = _allocateCallback(() => func.apply(undefined, args), 'setInterval');
|
||||
RCTTiming.createTimer(id, duration || 0, Date.now(), /* recurring */ true);
|
||||
return id;
|
||||
},
|
||||
|
@ -97,8 +97,7 @@ const JSTimers = {
|
|||
* current JavaScript execution loop.
|
||||
*/
|
||||
setImmediate: function(func: Function, ...args?: any) {
|
||||
const id = _allocateCallback(() => func.apply(undefined, args),
|
||||
JSTimersExecution.Type.setImmediate);
|
||||
const id = _allocateCallback(() => func.apply(undefined, args), 'setImmediate');
|
||||
JSTimersExecution.immediates.push(id);
|
||||
return id;
|
||||
},
|
||||
|
@ -107,7 +106,7 @@ const JSTimers = {
|
|||
* @param {function} func Callback to be invoked every frame.
|
||||
*/
|
||||
requestAnimationFrame: function(func : Function) {
|
||||
const id = _allocateCallback(func, JSTimersExecution.Type.requestAnimationFrame);
|
||||
const id = _allocateCallback(func, 'requestAnimationFrame');
|
||||
RCTTiming.createTimer(id, 1, Date.now(), /* recurring */ false);
|
||||
return id;
|
||||
},
|
||||
|
@ -121,7 +120,7 @@ const JSTimers = {
|
|||
RCTTiming.setSendIdleEvents(true);
|
||||
}
|
||||
|
||||
const id = _allocateCallback(func, JSTimersExecution.Type.requestIdleCallback);
|
||||
const id = _allocateCallback(func, 'requestIdleCallback');
|
||||
JSTimersExecution.requestIdleCallbacks.push(id);
|
||||
return id;
|
||||
},
|
||||
|
|
|
@ -14,7 +14,6 @@
|
|||
const Systrace = require('Systrace');
|
||||
|
||||
const invariant = require('fbjs/lib/invariant');
|
||||
const keyMirror = require('fbjs/lib/keyMirror');
|
||||
const performanceNow = require('fbjs/lib/performanceNow');
|
||||
const warning = require('fbjs/lib/warning');
|
||||
|
||||
|
@ -25,6 +24,13 @@ const IDLE_CALLBACK_FRAME_DEADLINE = 1;
|
|||
|
||||
let hasEmittedTimeDriftWarning = false;
|
||||
|
||||
export type JSTimerType =
|
||||
'setTimeout' |
|
||||
'setInterval' |
|
||||
'requestAnimationFrame' |
|
||||
'setImmediate' |
|
||||
'requestIdleCallback';
|
||||
|
||||
/**
|
||||
* JS implementation of timer functions. Must be completely driven by an
|
||||
* external clock signal, all that's stored here is timerID, timer type, and
|
||||
|
@ -32,18 +38,11 @@ let hasEmittedTimeDriftWarning = false;
|
|||
*/
|
||||
const JSTimersExecution = {
|
||||
GUID: 1,
|
||||
Type: keyMirror({
|
||||
setTimeout: null,
|
||||
setInterval: null,
|
||||
requestAnimationFrame: null,
|
||||
setImmediate: null,
|
||||
requestIdleCallback: null,
|
||||
}),
|
||||
|
||||
// Parallel arrays
|
||||
callbacks: [],
|
||||
types: [],
|
||||
timerIDs: [],
|
||||
callbacks: ([] : Array<?Function>),
|
||||
types: ([] : Array<?JSTimerType>),
|
||||
timerIDs: ([] : Array<?number>),
|
||||
immediates: [],
|
||||
requestIdleCallbacks: [],
|
||||
identifiers: ([] : Array<null | {methodName: string}>),
|
||||
|
@ -85,21 +84,18 @@ const JSTimersExecution = {
|
|||
}
|
||||
|
||||
// Clear the metadata
|
||||
if (type === JSTimersExecution.Type.setTimeout ||
|
||||
type === JSTimersExecution.Type.setImmediate ||
|
||||
type === JSTimersExecution.Type.requestAnimationFrame ||
|
||||
type === JSTimersExecution.Type.requestIdleCallback) {
|
||||
if (type === 'setTimeout' || type === 'setImmediate' ||
|
||||
type === 'requestAnimationFrame' || type === 'requestIdleCallback') {
|
||||
JSTimersExecution._clearIndex(timerIndex);
|
||||
}
|
||||
|
||||
try {
|
||||
if (type === JSTimersExecution.Type.setTimeout ||
|
||||
type === JSTimersExecution.Type.setInterval ||
|
||||
type === JSTimersExecution.Type.setImmediate) {
|
||||
if (type === 'setTimeout' || type === 'setInterval' ||
|
||||
type === 'setImmediate') {
|
||||
callback();
|
||||
} else if (type === JSTimersExecution.Type.requestAnimationFrame) {
|
||||
} else if (type === 'requestAnimationFrame') {
|
||||
callback(performanceNow());
|
||||
} else if (type === JSTimersExecution.Type.requestIdleCallback) {
|
||||
} else if (type === 'requestIdleCallback') {
|
||||
callback({
|
||||
timeRemaining: function() {
|
||||
// TODO: Optimisation: allow running for longer than one frame if
|
||||
|
|
Loading…
Reference in New Issue