webterm: improve slogstream management

Don't re-open the slog eventsource every time we navigate to webterm.
This commit is contained in:
fang 2021-04-28 00:11:02 +02:00
parent a13f19d6f9
commit aa244a10ea
No known key found for this signature in database
GPG Key ID: EB035760C1BBA972
2 changed files with 8 additions and 4 deletions

View File

@ -9,11 +9,13 @@ type Sessions = { [id: string]: Session; }
export interface TermState extends BaseState<TermState> {
sessions: Sessions,
selected: string,
slogstream: null | EventSource,
};
const useTermState = createState<TermState>('Term', {
sessions: {},
selected: '', // empty string is default session
}, ['sessions']); //TODO consider persisting
slogstream: null,
}, ['sessions', 'slogstream']); //TODO consider persisting
export default useTermState;

View File

@ -128,7 +128,7 @@ export default function TermApp(props: TermAppProps) {
const container = useRef<HTMLElement>(null);
//TODO allow switching of selected
const { sessions, selected, set } = useTermState();
const { sessions, selected, slogstream, set } = useTermState();
const osDark = useLocalState((state) => state.dark);
const theme = useSettingsState(s => s.display.theme);
@ -246,6 +246,8 @@ export default function TermApp(props: TermAppProps) {
}, 10000);
}
}
set(state => { state.slogstream = slog });
}, [onSlog]);
const onInput = useCallback((ses: string, e: string) => {
@ -341,13 +343,13 @@ export default function TermApp(props: TermAppProps) {
// on-init, open slogstream
//
useEffect(() => {
setupSlog();
if (!slogstream) setupSlog();
window.addEventListener('resize', onResize);
return () => {
//TODO clean up subs?
window.removeEventListener('resize', onResize);
};
}, [onResize]);
}, [onResize, setupSlog]);
// on dark mode change, change terminals' theme
//