import { isBrowser } from '@typed/common' ; import { Clock } from './types' ; /** Create a clock relative to the current time */ export const createRelativeClock = ( clock ) : Clock => new RelativeClock ( clock , clock . currentTime ( ) ) ; export const createClock = ( ) : Clock => { const clock = isBrowser ? { currentTime : ( ) => performance . now ( ) } : new HRTimeClock ( process . hrtime , process . hrtime ( ) ) ; const relative = createRelativeClock ( clock ) ; return relative ; } ; class RelativeClock implements Clock { constructor ( private clock , private origin ) { } public currentTime = ( ) => { return this . clock . currentTime ( ) - this . origin ; } ; } class HRTimeClock implements Clock { constructor ( private hrtime , private origin ) { } public currentTime ( ) { const hrt = this . hrtime ( this . origin ) ; return ( hrt [ 0 ] * 1e9 + hrt [ 1 ] ) / 1e6 ; } }