2022-03-08 19:11:11 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
|
|
|
import { MINUTE } from '../util/durations';
|
|
|
|
|
|
|
|
const ev = new EventEmitter();
|
2022-03-09 00:06:44 +00:00
|
|
|
ev.setMaxListeners(Infinity);
|
2022-03-08 19:11:11 +00:00
|
|
|
setInterval(() => ev.emit('tick'), MINUTE);
|
|
|
|
|
|
|
|
export function useNowThatUpdatesEveryMinute(): number {
|
|
|
|
const [now, setNow] = useState(Date.now());
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const updateNow = () => setNow(Date.now());
|
|
|
|
updateNow();
|
|
|
|
|
|
|
|
ev.on('tick', updateNow);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
ev.off('tick', updateNow);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return now;
|
|
|
|
}
|