Remove keyMirror usage in JSTimers

Reviewed By: davidaurelio

Differential Revision: D3981015

fbshipit-source-id: 9ce7601038b7cdf8a7feb580380557ed84008397
This commit is contained in:
Pieter De Baets 2016-10-11 06:51:47 -07:00 committed by Facebook Github Bot
parent 3137ba993d
commit d99623fcdc
2 changed files with 26 additions and 31 deletions

View File

@ -15,8 +15,11 @@
// in dependencies. NativeModules > BatchedBridge > MessageQueue > JSTimersExecution // in dependencies. NativeModules > BatchedBridge > MessageQueue > JSTimersExecution
const RCTTiming = require('NativeModules').Timing; const RCTTiming = require('NativeModules').Timing;
const JSTimersExecution = require('JSTimersExecution'); const JSTimersExecution = require('JSTimersExecution');
const parseErrorStack = require('parseErrorStack'); const parseErrorStack = require('parseErrorStack');
import type {JSTimerType} from 'JSTimersExecution';
// Returns a free index if one is available, and the next consecutive index otherwise. // Returns a free index if one is available, and the next consecutive index otherwise.
function _getFreeIndex(): number { function _getFreeIndex(): number {
let freeIndex = JSTimersExecution.timerIDs.indexOf(null); let freeIndex = JSTimersExecution.timerIDs.indexOf(null);
@ -26,7 +29,7 @@ function _getFreeIndex(): number {
return freeIndex; return freeIndex;
} }
function _allocateCallback(func: Function, type: $Keys<typeof JSTimersExecution.Type>): number { function _allocateCallback(func: Function, type: JSTimerType): number {
const id = JSTimersExecution.GUID++; const id = JSTimersExecution.GUID++;
const freeIndex = _getFreeIndex(); const freeIndex = _getFreeIndex();
JSTimersExecution.timerIDs[freeIndex] = id; JSTimersExecution.timerIDs[freeIndex] = id;
@ -57,8 +60,7 @@ function _freeCallback(timerID: number) {
if (index !== -1) { if (index !== -1) {
JSTimersExecution._clearIndex(index); JSTimersExecution._clearIndex(index);
const type = JSTimersExecution.types[index]; const type = JSTimersExecution.types[index];
if (type !== JSTimersExecution.Type.setImmediate && if (type !== 'setImmediate' && type !== 'requestIdleCallback') {
type !== JSTimersExecution.Type.requestIdleCallback) {
RCTTiming.deleteTimer(timerID); RCTTiming.deleteTimer(timerID);
} }
} }
@ -75,8 +77,7 @@ const JSTimers = {
* @param {number} duration Number of milliseconds. * @param {number} duration Number of milliseconds.
*/ */
setTimeout: function(func: Function, duration: number, ...args?: any): number { setTimeout: function(func: Function, duration: number, ...args?: any): number {
const id = _allocateCallback(() => func.apply(undefined, args), const id = _allocateCallback(() => func.apply(undefined, args), 'setTimeout');
JSTimersExecution.Type.setTimeout);
RCTTiming.createTimer(id, duration || 0, Date.now(), /* recurring */ false); RCTTiming.createTimer(id, duration || 0, Date.now(), /* recurring */ false);
return id; return id;
}, },
@ -86,8 +87,7 @@ const JSTimers = {
* @param {number} duration Number of milliseconds. * @param {number} duration Number of milliseconds.
*/ */
setInterval: function(func: Function, duration: number, ...args?: any): number { setInterval: function(func: Function, duration: number, ...args?: any): number {
const id = _allocateCallback(() => func.apply(undefined, args), const id = _allocateCallback(() => func.apply(undefined, args), 'setInterval');
JSTimersExecution.Type.setInterval);
RCTTiming.createTimer(id, duration || 0, Date.now(), /* recurring */ true); RCTTiming.createTimer(id, duration || 0, Date.now(), /* recurring */ true);
return id; return id;
}, },
@ -97,8 +97,7 @@ const JSTimers = {
* current JavaScript execution loop. * current JavaScript execution loop.
*/ */
setImmediate: function(func: Function, ...args?: any) { setImmediate: function(func: Function, ...args?: any) {
const id = _allocateCallback(() => func.apply(undefined, args), const id = _allocateCallback(() => func.apply(undefined, args), 'setImmediate');
JSTimersExecution.Type.setImmediate);
JSTimersExecution.immediates.push(id); JSTimersExecution.immediates.push(id);
return id; return id;
}, },
@ -107,7 +106,7 @@ const JSTimers = {
* @param {function} func Callback to be invoked every frame. * @param {function} func Callback to be invoked every frame.
*/ */
requestAnimationFrame: function(func : Function) { requestAnimationFrame: function(func : Function) {
const id = _allocateCallback(func, JSTimersExecution.Type.requestAnimationFrame); const id = _allocateCallback(func, 'requestAnimationFrame');
RCTTiming.createTimer(id, 1, Date.now(), /* recurring */ false); RCTTiming.createTimer(id, 1, Date.now(), /* recurring */ false);
return id; return id;
}, },
@ -121,7 +120,7 @@ const JSTimers = {
RCTTiming.setSendIdleEvents(true); RCTTiming.setSendIdleEvents(true);
} }
const id = _allocateCallback(func, JSTimersExecution.Type.requestIdleCallback); const id = _allocateCallback(func, 'requestIdleCallback');
JSTimersExecution.requestIdleCallbacks.push(id); JSTimersExecution.requestIdleCallbacks.push(id);
return id; return id;
}, },

View File

@ -14,7 +14,6 @@
const Systrace = require('Systrace'); const Systrace = require('Systrace');
const invariant = require('fbjs/lib/invariant'); const invariant = require('fbjs/lib/invariant');
const keyMirror = require('fbjs/lib/keyMirror');
const performanceNow = require('fbjs/lib/performanceNow'); const performanceNow = require('fbjs/lib/performanceNow');
const warning = require('fbjs/lib/warning'); const warning = require('fbjs/lib/warning');
@ -25,6 +24,13 @@ const IDLE_CALLBACK_FRAME_DEADLINE = 1;
let hasEmittedTimeDriftWarning = false; let hasEmittedTimeDriftWarning = false;
export type JSTimerType =
'setTimeout' |
'setInterval' |
'requestAnimationFrame' |
'setImmediate' |
'requestIdleCallback';
/** /**
* JS implementation of timer functions. Must be completely driven by an * JS implementation of timer functions. Must be completely driven by an
* external clock signal, all that's stored here is timerID, timer type, and * external clock signal, all that's stored here is timerID, timer type, and
@ -32,18 +38,11 @@ let hasEmittedTimeDriftWarning = false;
*/ */
const JSTimersExecution = { const JSTimersExecution = {
GUID: 1, GUID: 1,
Type: keyMirror({
setTimeout: null,
setInterval: null,
requestAnimationFrame: null,
setImmediate: null,
requestIdleCallback: null,
}),
// Parallel arrays // Parallel arrays
callbacks: [], callbacks: ([] : Array<?Function>),
types: [], types: ([] : Array<?JSTimerType>),
timerIDs: [], timerIDs: ([] : Array<?number>),
immediates: [], immediates: [],
requestIdleCallbacks: [], requestIdleCallbacks: [],
identifiers: ([] : Array<null | {methodName: string}>), identifiers: ([] : Array<null | {methodName: string}>),
@ -85,21 +84,18 @@ const JSTimersExecution = {
} }
// Clear the metadata // Clear the metadata
if (type === JSTimersExecution.Type.setTimeout || if (type === 'setTimeout' || type === 'setImmediate' ||
type === JSTimersExecution.Type.setImmediate || type === 'requestAnimationFrame' || type === 'requestIdleCallback') {
type === JSTimersExecution.Type.requestAnimationFrame ||
type === JSTimersExecution.Type.requestIdleCallback) {
JSTimersExecution._clearIndex(timerIndex); JSTimersExecution._clearIndex(timerIndex);
} }
try { try {
if (type === JSTimersExecution.Type.setTimeout || if (type === 'setTimeout' || type === 'setInterval' ||
type === JSTimersExecution.Type.setInterval || type === 'setImmediate') {
type === JSTimersExecution.Type.setImmediate) {
callback(); callback();
} else if (type === JSTimersExecution.Type.requestAnimationFrame) { } else if (type === 'requestAnimationFrame') {
callback(performanceNow()); callback(performanceNow());
} else if (type === JSTimersExecution.Type.requestIdleCallback) { } else if (type === 'requestIdleCallback') {
callback({ callback({
timeRemaining: function() { timeRemaining: function() {
// TODO: Optimisation: allow running for longer than one frame if // TODO: Optimisation: allow running for longer than one frame if