···3344// get the Intl.RelativeTimeFormat formatter for the given locale
55export function getFormatter(locale: string) {
66- if (formatters.has(locale)) {
77- return formatters.get(locale)!;
88- }
99- const formatter = new Intl.RelativeTimeFormat(locale, { numeric: 'always', style: 'narrow' });
1010- formatters.set(locale, formatter);
1111- return formatter;
66+ if (formatters.has(locale)) {
77+ return formatters.get(locale)!;
88+ }
99+ const formatter = new Intl.RelativeTimeFormat(locale, { numeric: 'always', style: 'narrow' });
1010+ formatters.set(locale, formatter);
1111+ return formatter;
1212}
+36-36
src/lib/Components/relative-time/state.ts
···33import type { Callback, RenderState } from './render';
4455interface UpdateState extends RenderState {
66- update: number;
66+ update: number;
77}
8899// keep track of each instance
···14141515// register or update instance
1616export function register(
1717- instance: Object,
1818- date: Date | number,
1919- locale: string,
2020- live: boolean,
2121- callback: Callback
1717+ instance: Object,
1818+ date: Date | number,
1919+ locale: string,
2020+ live: boolean,
2121+ callback: Callback
2222) {
2323- // get the formatter for the given locale, we do this here so we don't keep having to look it up on each tick
2424- const formatter = getFormatter(locale);
2323+ // get the formatter for the given locale, we do this here so we don't keep having to look it up on each tick
2424+ const formatter = getFormatter(locale);
25252626- // create state to render
2727- const state = { date, callback, formatter };
2626+ // create state to render
2727+ const state = { date, callback, formatter };
28282929- // initial render is immediate, so works for SSR
3030- const update = render(state, Date.now());
2929+ // initial render is immediate, so works for SSR
3030+ const update = render(state, Date.now());
31313232- // if it's to update live, we keep a track and schedule the next update
3333- if (live) {
3434- instances.set(instance, { ...state, update });
3535- } else {
3636- instances.delete(instance);
3737- }
3232+ // if it's to update live, we keep a track and schedule the next update
3333+ if (live) {
3434+ instances.set(instance, { ...state, update });
3535+ } else {
3636+ instances.delete(instance);
3737+ }
38383939- // start the clock ticking if there are any live instances
4040- if (instances.size) {
4141- updateInterval =
4242- updateInterval ||
4343- setInterval(() => {
4444- const now = Date.now();
4545- for (const state of instances.values()) {
4646- if (state.update <= now) {
4747- state.update = render(state, now);
4848- }
4949- }
5050- }, 1000);
5151- }
3939+ // start the clock ticking if there are any live instances
4040+ if (instances.size) {
4141+ updateInterval =
4242+ updateInterval ||
4343+ setInterval(() => {
4444+ const now = Date.now();
4545+ for (const state of instances.values()) {
4646+ if (state.update <= now) {
4747+ state.update = render(state, now);
4848+ }
4949+ }
5050+ }, 1000);
5151+ }
5252}
53535454export function unregister(instance: Object) {
5555- instances.delete(instance);
5656- if (instances.size === 0) {
5757- clearInterval(updateInterval);
5858- updateInterval = 0;
5959- }
5555+ instances.delete(instance);
5656+ if (instances.size === 0) {
5757+ clearInterval(updateInterval);
5858+ updateInterval = 0;
5959+ }
6060}