From d99623fcdc662cd329477bf42ca8740ee4433fe7 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Tue, 11 Oct 2016 06:51:47 -0700 Subject: [PATCH] Remove keyMirror usage in JSTimers Reviewed By: davidaurelio Differential Revision: D3981015 fbshipit-source-id: 9ce7601038b7cdf8a7feb580380557ed84008397 --- .../System/JSTimers/JSTimers.js | 21 ++++++----- .../System/JSTimers/JSTimersExecution.js | 36 +++++++++---------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/Libraries/JavaScriptAppEngine/System/JSTimers/JSTimers.js b/Libraries/JavaScriptAppEngine/System/JSTimers/JSTimers.js index fe802bc9b..c89a6695b 100644 --- a/Libraries/JavaScriptAppEngine/System/JSTimers/JSTimers.js +++ b/Libraries/JavaScriptAppEngine/System/JSTimers/JSTimers.js @@ -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): 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; }, diff --git a/Libraries/JavaScriptAppEngine/System/JSTimers/JSTimersExecution.js b/Libraries/JavaScriptAppEngine/System/JSTimers/JSTimersExecution.js index 23512da1b..3dd753c45 100644 --- a/Libraries/JavaScriptAppEngine/System/JSTimers/JSTimersExecution.js +++ b/Libraries/JavaScriptAppEngine/System/JSTimers/JSTimersExecution.js @@ -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), + types: ([] : Array), + timerIDs: ([] : Array), immediates: [], requestIdleCallbacks: [], identifiers: ([] : Array), @@ -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