diff --git a/Libraries/BatchedBridge/MessageQueue.js b/Libraries/BatchedBridge/MessageQueue.js index 7cdc5fe18..c9b750df9 100644 --- a/Libraries/BatchedBridge/MessageQueue.js +++ b/Libraries/BatchedBridge/MessageQueue.js @@ -37,9 +37,6 @@ const TRACE_TAG_REACT_APPS = 1 << 17; const DEBUG_INFO_LIMIT = 32; -// Work around an initialization order issue -let JSTimers = null; - class MessageQueue { _lazyCallableModules: {[key: string]: (void) => Object}; _queue: [number[], number[], any[], number]; @@ -48,6 +45,7 @@ class MessageQueue { _callID: number; _lastFlush: number; _eventLoopStartTime: number; + _immediatesCallback: ?() => void; _debugInfo: {[number]: [number, number]}; _remoteModuleTable: {[number]: string}; @@ -63,6 +61,7 @@ class MessageQueue { this._callID = 0; this._lastFlush = 0; this._eventLoopStartTime = new Date().getTime(); + this._immediatesCallback = null; if (__DEV__) { this._debugInfo = {}; @@ -279,6 +278,13 @@ class MessageQueue { } } + // For JSTimers to register its callback. Otherwise a circular dependency + // between modules is introduced. Note that only one callback may be + // registered at a time. + setImmediatesCallback(fn: () => void) { + this._immediatesCallback = fn; + } + /** * Private methods */ @@ -310,10 +316,9 @@ class MessageQueue { __callImmediates() { Systrace.beginEvent('JSTimers.callImmediates()'); - if (!JSTimers) { - JSTimers = require('JSTimers'); + if (this._immediatesCallback != null) { + this._immediatesCallback(); } - JSTimers.callImmediates(); Systrace.endEvent(); } diff --git a/Libraries/Core/Timers/JSTimers.js b/Libraries/Core/Timers/JSTimers.js index dbd23f807..7cffb4e3d 100644 --- a/Libraries/Core/Timers/JSTimers.js +++ b/Libraries/Core/Timers/JSTimers.js @@ -14,6 +14,7 @@ const Systrace = require('Systrace'); const invariant = require('fbjs/lib/invariant'); const {Timing} = require('NativeModules'); +const BatchedBridge = require('BatchedBridge'); import type {ExtendedError} from 'parseErrorStack'; @@ -494,4 +495,8 @@ if (!Timing) { ExportedJSTimers = JSTimers; } +BatchedBridge.setImmediatesCallback( + ExportedJSTimers.callImmediates.bind(ExportedJSTimers), +); + module.exports = ExportedJSTimers;