cordis/archive/guilds-manager-subscriptions.ts

27 lines
754 B
TypeScript
Raw Permalink Normal View History

2021-12-30 00:50:03 +00:00
import { useCallback, useEffect, useMemo, useState } from 'react';
import * as uuid from 'uuid';
import CombinedGuild from '../../guild-combined';
import GuildsManager from "../../guilds-manager";
// Subscribes to changes in the list of guilds in the manager
2022-02-07 01:24:25 +00:00
function useGuildListSubscription(guildsManager: GuildsManager): [ guilds: CombinedGuild[] ] {
2021-12-30 00:50:03 +00:00
const [ refreshId, setRefreshId ] = useState<string>(uuid.v4());
const refresh = useCallback(() => {
setRefreshId(uuid.v4());
}, []);
useEffect(() => {
guildsManager.on('update-guilds', refresh);
return () => {
guildsManager.off('update-guilds', refresh);
}
}, []);
const guilds = useMemo(() => {
return guildsManager.guilds.slice();
}, [ refreshId ]);
return [ guilds ];
}