Use a hook for the ever-updating now
This commit is contained in:
parent
f8724e91da
commit
4e48d7792b
21 changed files with 34 additions and 78 deletions
27
ts/hooks/useNowThatUpdatesEveryMinute.ts
Normal file
27
ts/hooks/useNowThatUpdatesEveryMinute.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
// 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();
|
||||
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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue