From c8fd9f7588ad02d2293cac7224715f4af7b0f352 Mon Sep 17 00:00:00 2001 From: Milen Dzhumerov Date: Thu, 19 Nov 2015 03:30:45 -0800 Subject: [PATCH] Split immediate into multiple passes Reviewed By: tadeuzagallo Differential Revision: D2663957 fb-gh-sync-id: d7f0041fc98edb46e518f684527effe2f5201240 --- .../System/JSTimers/JSTimersExecution.js | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/Libraries/JavaScriptAppEngine/System/JSTimers/JSTimersExecution.js b/Libraries/JavaScriptAppEngine/System/JSTimers/JSTimersExecution.js index 6f56d8357..bc2b74d22 100644 --- a/Libraries/JavaScriptAppEngine/System/JSTimers/JSTimersExecution.js +++ b/Libraries/JavaScriptAppEngine/System/JSTimers/JSTimersExecution.js @@ -14,6 +14,7 @@ var invariant = require('invariant'); var keyMirror = require('keyMirror'); var performanceNow = require('performanceNow'); var warning = require('warning'); +var BridgeProfiling = require('BridgeProfiling'); /** * JS implementation of timer functions. Must be completely driven by an @@ -107,15 +108,36 @@ var JSTimersExecution = { } }, + /** + * Performs a single pass over the enqueued immediates. Returns whether + * more immediates are queued up (can be used as a condition a while loop). + */ + callImmediatesPass: function() { + BridgeProfiling.profile('JSTimersExecution.callImmediatesPass()'); + + // The main reason to extract a single pass is so that we can track + // in the system trace + if (JSTimersExecution.immediates.length > 0) { + var passImmediates = JSTimersExecution.immediates.slice(); + JSTimersExecution.immediates = []; + + passImmediates.forEach((timerID) => { + JSTimersExecution.callTimer(timerID); + }); + } + + BridgeProfiling.profileEnd(); + + return JSTimersExecution.immediates.length > 0; + }, + /** * This is called after we execute any command we receive from native but * before we hand control back to native. */ callImmediates: function() { JSTimersExecution.errors = null; - while (JSTimersExecution.immediates.length !== 0) { - JSTimersExecution.callTimer(JSTimersExecution.immediates.shift()); - } + while (JSTimersExecution.callImmediatesPass()) {} if (JSTimersExecution.errors) { JSTimersExecution.errors.forEach((error) => require('JSTimers').setTimeout(() => { throw error; }, 0)