From c0d535f1b0c7c8a9dbbb55f3e989c8c3117ce7ea Mon Sep 17 00:00:00 2001 From: Michael Peters Date: Sun, 23 Oct 2022 14:11:26 -0700 Subject: [PATCH] trying out initializeState --- archive/external-stores.ts | 32 +++++++++++++++++++ src/client/webapp/elements/app.tsx | 18 +++++++++++ src/client/webapp/elements/require/atoms.ts | 18 +++++------ src/client/webapp/elements/root.tsx | 29 ++++------------- src/client/webapp/elements/sections/guild.tsx | 12 +++---- src/client/webapp/preload.tsx | 12 +++++-- 6 files changed, 80 insertions(+), 41 deletions(-) create mode 100644 archive/external-stores.ts create mode 100644 src/client/webapp/elements/app.tsx diff --git a/archive/external-stores.ts b/archive/external-stores.ts new file mode 100644 index 0000000..b1f31f8 --- /dev/null +++ b/archive/external-stores.ts @@ -0,0 +1,32 @@ +import * as electronRemote from '@electron/remote'; +const electronConsole = electronRemote.getGlobal('console') as Console; +import Logger from '../../../../logger/logger'; +const LOG = Logger.create(__filename, electronConsole); + +import { useEffect, useSyncExternalStore } from 'react'; +import { atom, useSetRecoilState } from 'recoil'; + +export const exampleState = atom({ + key: 'exampleState', + default: 4, +}); + +export function useExampleStateSubscription() { + LOG.debug('using'); + const setExample = useSetRecoilState(exampleState); + const value = useSyncExternalStore( + () => { + LOG.debug('subscribing'); + return () => { + LOG.debug('unsubscribing') + }; + }, + () => { + LOG.debug('returning'); + return 6; + }, + ); + useEffect(() => { + setExample(value); + }, [setExample, value]) +} diff --git a/src/client/webapp/elements/app.tsx b/src/client/webapp/elements/app.tsx new file mode 100644 index 0000000..2453a42 --- /dev/null +++ b/src/client/webapp/elements/app.tsx @@ -0,0 +1,18 @@ +import React, { FC, ReactNode } from "react"; +import { useRecoilValue } from "recoil"; +import { overlayState } from "./require/atoms"; +import GuildsManagerElement from "./sections/guilds-manager"; +import TitleBar from "./sections/title-bar"; + +const App: FC = () => { + const overlay = useRecoilValue(overlayState); + + return ( +
+ + +
{overlay}
+
+ ); +} +export default App; diff --git a/src/client/webapp/elements/require/atoms.ts b/src/client/webapp/elements/require/atoms.ts index 39a6010..d08327a 100644 --- a/src/client/webapp/elements/require/atoms.ts +++ b/src/client/webapp/elements/require/atoms.ts @@ -14,6 +14,7 @@ import { selector, selectorFamily, useRecoilState, + useRecoilValue, useSetRecoilState, } from 'recoil'; import { @@ -57,15 +58,16 @@ import { useRecoilValueLoadableOrElse, } from './atoms-funcs'; import { randomUUID } from 'crypto'; +import assert from 'assert'; export const overlayState = atom({ key: 'overlayState', default: null, }); -export const guildsManagerState = atom({ +export const guildsManagerState = atom({ key: 'guildsManager', - default: null, + default: null as unknown as GuildsManager, // this will be set by initializeState // allow mutability so that we can have changes to internal guild stuff // we will still listen to new/update/delete/conflict events to maintain the UI's state @@ -88,7 +90,7 @@ export const guildMetaState = atomFamily, number>({ guildDataSubscriptionLoadableSingleEffect( guildId, async (guild: CombinedGuild) => { - LOG.debug('fetching guild metadata', { guild }); + LOG.debug('fetching guild metadata'); return await guild.fetchMetadata(); }, { @@ -548,15 +550,13 @@ export function useRecoilValueSoftImgSrc(recoilValue: RecoilValue): stri } // initialize with a guildsManager -export function useInitRecoil(guildsManager: GuildsManager) { +export function useInitRecoil() { + const guildsManager = useRecoilValue(guildsManagerState); + assert(guildsManager !== null); + LOG.debug(`recoil init, gm has ${guildsManager.guilds.length} guilds`); - const setGuildsManager = useSetRecoilState(guildsManagerState); const setGuilds = useSetRecoilState(allGuildsState); const [currGuildId, setCurrGuildId] = useRecoilState(currGuildIdState); - useEffect(() => { - LOG.debug(`setting guildsManager to ${guildsManager?.guilds.length}`); - setGuildsManager(guildsManager); - }, [guildsManager, setGuildsManager]); useEffect(() => { const updateGuilds = () => { setGuilds(guildsManager.guilds.slice()); diff --git a/src/client/webapp/elements/root.tsx b/src/client/webapp/elements/root.tsx index 6bf8919..f617cfb 100644 --- a/src/client/webapp/elements/root.tsx +++ b/src/client/webapp/elements/root.tsx @@ -1,28 +1,11 @@ -import React, { FC, ReactNode } from 'react'; -import { useRecoilValue } from 'recoil'; -import GuildsManager from '../guilds-manager'; -import { useInitRecoil, overlayState } from './require/atoms'; -import GuildsManagerElement from './sections/guilds-manager'; -import TitleBar from './sections/title-bar'; +import React, { FC } from 'react'; +import { useInitRecoil } from './require/atoms'; +import App from './app'; -export interface RootElementProps { - guildsManager: GuildsManager; -} +const RootElement: FC = () => { + useInitRecoil(); -const RootElement: FC = (props: RootElementProps) => { - const { guildsManager } = props; - - useInitRecoil(guildsManager); - - const overlay = useRecoilValue(overlayState); - - return ( -
- - -
{overlay}
-
- ); + return }; export default RootElement; diff --git a/src/client/webapp/elements/sections/guild.tsx b/src/client/webapp/elements/sections/guild.tsx index 4e0f265..9248852 100644 --- a/src/client/webapp/elements/sections/guild.tsx +++ b/src/client/webapp/elements/sections/guild.tsx @@ -28,12 +28,12 @@ const GuildElement: FC = () => { const activeChannel = useRecoilValue(currGuildActiveChannelState); const setActiveChannelId = useSetRecoilState(guildActiveChannelIdState(guild?.id ?? -1)); - useEffect(() => { - LOG.debug('guild changed', { guildId: guild?.id ?? '' }); - }, [ guild ]); - useEffect(() => { - LOG.debug('self member changed', { selfMember }); - }, [ selfMember ]); + //useEffect(() => { + // lOG.debug('guild changed', { guildId: guild?.id ?? '' }); + //}, [ guild ]); + //useEffect(() => { + // lOG.debug('self member changed', { selfMember }); + //}, [ selfMember ]); // useEffect(() => { // lOG.debug('active channel changed', { activeChannel }); // }, [ activeChannel ]) diff --git a/src/client/webapp/preload.tsx b/src/client/webapp/preload.tsx index 99ba7b3..2a1556b 100644 --- a/src/client/webapp/preload.tsx +++ b/src/client/webapp/preload.tsx @@ -18,7 +18,8 @@ import ResourceRAMCache from './resource-ram-cache'; import { createRoot } from 'react-dom/client'; import React from 'react'; import RootElement from './elements/root'; -import { RecoilRoot } from 'recoil'; +import { MutableSnapshot, RecoilRoot } from 'recoil'; +import { guildsManagerState } from './elements/require/atoms'; LOG.silly('modules loaded'); @@ -59,10 +60,15 @@ window.addEventListener('DOMContentLoaded', () => { LOG.silly('guildsManager initialized'); + // TODO: Try using initializeState from recoilRoot + // https://recoiljs.org/docs/api-reference/core/RecoilRoot/ const root = createRoot(document.getElementById('react-root')!); + function initializeState({ set }: MutableSnapshot): void { + set(guildsManagerState, guildsManager); + } root.render( - - + + , ); })();