2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
|
|
|
* @providesModule JSTimers
|
2016-09-06 10:57:58 +00:00
|
|
|
* @flow
|
2015-01-30 01:10:49 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Note that the module JSTimers is split into two in order to solve a cycle
|
|
|
|
// in dependencies. NativeModules > BatchedBridge > MessageQueue > JSTimersExecution
|
2016-09-06 10:57:58 +00:00
|
|
|
const RCTTiming = require('NativeModules').Timing;
|
|
|
|
const JSTimersExecution = require('JSTimersExecution');
|
2016-10-11 13:51:47 +00:00
|
|
|
|
2016-09-06 10:58:00 +00:00
|
|
|
const parseErrorStack = require('parseErrorStack');
|
2016-09-06 10:57:58 +00:00
|
|
|
|
2016-10-11 13:51:47 +00:00
|
|
|
import type {JSTimerType} from 'JSTimersExecution';
|
|
|
|
|
2016-09-06 10:57:58 +00:00
|
|
|
// Returns a free index if one is available, and the next consecutive index otherwise.
|
|
|
|
function _getFreeIndex(): number {
|
|
|
|
let freeIndex = JSTimersExecution.timerIDs.indexOf(null);
|
|
|
|
if (freeIndex === -1) {
|
|
|
|
freeIndex = JSTimersExecution.timerIDs.length;
|
|
|
|
}
|
|
|
|
return freeIndex;
|
|
|
|
}
|
|
|
|
|
2016-10-11 13:51:47 +00:00
|
|
|
function _allocateCallback(func: Function, type: JSTimerType): number {
|
2016-09-06 10:57:58 +00:00
|
|
|
const id = JSTimersExecution.GUID++;
|
|
|
|
const freeIndex = _getFreeIndex();
|
|
|
|
JSTimersExecution.timerIDs[freeIndex] = id;
|
|
|
|
JSTimersExecution.callbacks[freeIndex] = func;
|
|
|
|
JSTimersExecution.types[freeIndex] = type;
|
2016-09-06 10:58:00 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
const e = (new Error() : any);
|
|
|
|
e.framesToPop = 1;
|
|
|
|
const stack = parseErrorStack(e);
|
|
|
|
if (stack) {
|
2016-09-06 20:36:33 +00:00
|
|
|
/* $FlowFixMe(>=0.32.0) - this seems to be putting something of the wrong
|
|
|
|
* type into identifiers */
|
2016-09-06 10:58:00 +00:00
|
|
|
JSTimersExecution.identifiers[freeIndex] = stack.shift();
|
|
|
|
}
|
|
|
|
}
|
2016-09-06 10:57:58 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _freeCallback(timerID: number) {
|
|
|
|
// JSTimersExecution.timerIDs contains nulls after timers have been removed;
|
|
|
|
// ignore nulls upfront so indexOf doesn't find them
|
|
|
|
if (timerID == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const index = JSTimersExecution.timerIDs.indexOf(timerID);
|
|
|
|
// See corresponding comment in `callTimers` for reasoning behind this
|
|
|
|
if (index !== -1) {
|
|
|
|
JSTimersExecution._clearIndex(index);
|
|
|
|
const type = JSTimersExecution.types[index];
|
2016-10-11 13:51:47 +00:00
|
|
|
if (type !== 'setImmediate' && type !== 'requestIdleCallback') {
|
2016-09-06 10:57:58 +00:00
|
|
|
RCTTiming.deleteTimer(timerID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* JS implementation of timer functions. Must be completely driven by an
|
|
|
|
* external clock signal, all that's stored here is timerID, timer type, and
|
|
|
|
* callback.
|
|
|
|
*/
|
2016-09-06 10:57:58 +00:00
|
|
|
const JSTimers = {
|
2015-01-30 01:10:49 +00:00
|
|
|
/**
|
|
|
|
* @param {function} func Callback to be invoked after `duration` ms.
|
|
|
|
* @param {number} duration Number of milliseconds.
|
|
|
|
*/
|
2016-09-06 10:57:58 +00:00
|
|
|
setTimeout: function(func: Function, duration: number, ...args?: any): number {
|
2016-10-11 13:51:47 +00:00
|
|
|
const id = _allocateCallback(() => func.apply(undefined, args), 'setTimeout');
|
2016-09-06 10:57:58 +00:00
|
|
|
RCTTiming.createTimer(id, duration || 0, Date.now(), /* recurring */ false);
|
|
|
|
return id;
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {function} func Callback to be invoked every `duration` ms.
|
|
|
|
* @param {number} duration Number of milliseconds.
|
|
|
|
*/
|
2016-09-06 10:57:58 +00:00
|
|
|
setInterval: function(func: Function, duration: number, ...args?: any): number {
|
2016-10-11 13:51:47 +00:00
|
|
|
const id = _allocateCallback(() => func.apply(undefined, args), 'setInterval');
|
2016-09-06 10:57:58 +00:00
|
|
|
RCTTiming.createTimer(id, duration || 0, Date.now(), /* recurring */ true);
|
|
|
|
return id;
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {function} func Callback to be invoked before the end of the
|
|
|
|
* current JavaScript execution loop.
|
|
|
|
*/
|
2016-09-06 10:57:58 +00:00
|
|
|
setImmediate: function(func: Function, ...args?: any) {
|
2016-10-11 13:51:47 +00:00
|
|
|
const id = _allocateCallback(() => func.apply(undefined, args), 'setImmediate');
|
2016-09-06 10:57:58 +00:00
|
|
|
JSTimersExecution.immediates.push(id);
|
|
|
|
return id;
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {function} func Callback to be invoked every frame.
|
|
|
|
*/
|
2016-09-06 10:57:58 +00:00
|
|
|
requestAnimationFrame: function(func : Function) {
|
2016-10-11 13:51:47 +00:00
|
|
|
const id = _allocateCallback(func, 'requestAnimationFrame');
|
2016-09-06 10:57:58 +00:00
|
|
|
RCTTiming.createTimer(id, 1, Date.now(), /* recurring */ false);
|
|
|
|
return id;
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
2016-07-14 01:43:27 +00:00
|
|
|
/**
|
|
|
|
* @param {function} func Callback to be invoked every frame and provided
|
|
|
|
* with time remaining in frame.
|
|
|
|
*/
|
2016-09-06 10:57:58 +00:00
|
|
|
requestIdleCallback: function(func : Function) {
|
2016-07-14 01:43:27 +00:00
|
|
|
if (JSTimersExecution.requestIdleCallbacks.length === 0) {
|
|
|
|
RCTTiming.setSendIdleEvents(true);
|
|
|
|
}
|
|
|
|
|
2016-10-11 13:51:47 +00:00
|
|
|
const id = _allocateCallback(func, 'requestIdleCallback');
|
2016-09-06 10:57:58 +00:00
|
|
|
JSTimersExecution.requestIdleCallbacks.push(id);
|
|
|
|
return id;
|
2016-07-14 01:43:27 +00:00
|
|
|
},
|
|
|
|
|
2016-09-06 10:57:58 +00:00
|
|
|
cancelIdleCallback: function(timerID: number) {
|
|
|
|
_freeCallback(timerID);
|
|
|
|
const index = JSTimersExecution.requestIdleCallbacks.indexOf(timerID);
|
2016-07-14 01:43:27 +00:00
|
|
|
if (index !== -1) {
|
|
|
|
JSTimersExecution.requestIdleCallbacks.splice(index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JSTimersExecution.requestIdleCallbacks.length === 0) {
|
|
|
|
RCTTiming.setSendIdleEvents(false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-09-06 10:57:58 +00:00
|
|
|
clearTimeout: function(timerID: number) {
|
|
|
|
_freeCallback(timerID);
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
2016-09-06 10:57:58 +00:00
|
|
|
clearInterval: function(timerID: number) {
|
|
|
|
_freeCallback(timerID);
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
2016-09-06 10:57:58 +00:00
|
|
|
clearImmediate: function(timerID: number) {
|
|
|
|
_freeCallback(timerID);
|
|
|
|
const index = JSTimersExecution.immediates.indexOf(timerID);
|
2016-02-28 19:40:50 +00:00
|
|
|
if (index !== -1) {
|
|
|
|
JSTimersExecution.immediates.splice(index, 1);
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
2016-09-06 10:57:58 +00:00
|
|
|
cancelAnimationFrame: function(timerID: number) {
|
|
|
|
_freeCallback(timerID);
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = JSTimers;
|