UNPKG

inferno-window

Version:

Inferno components for efficiently rendering large, scrollable lists and tabular data. NOTE: this is a port from the origin project written in react by @bvaughn

42 lines (31 loc) 1.02 kB
// @flow // Animation frame based implementation of setTimeout. // Inspired by Joe Lambert, https://gist.github.com/joelambert/1002116#file-requesttimeout-js const hasNativePerformanceNow = typeof performance === 'object' && typeof performance.now === 'function'; const now = hasNativePerformanceNow ? () => performance.now() : () => Date.now(); export type TimeoutID = {| id: AnimationFrameID, |}; export function cancelTimeout(timeoutID: TimeoutID, ref: Node) { ownerWindow(ref).cancelAnimationFrame(timeoutID.id); } export function requestTimeout(callback: Function, delay: number, ref: Node): TimeoutID { const start = now(); function tick() { if (now() - start >= delay) { callback.call(null); } else { timeoutID.id = ownerWindow(ref).requestAnimationFrame(tick); } } const timeoutID: TimeoutID = { id: ownerWindow(ref).requestAnimationFrame(tick), }; return timeoutID; } function ownerWindow(ref: Node): Window { return ref.ownerDocument.defaultView; }