mirror of
https://github.com/tiddly-gittly/TidGi-Desktop.git
synced 2026-01-10 11:12:15 -08:00
fix: observable subscription causes lots of rerender
beware not pipe in the react hock, as it will re-pipe every time React reRenders, and every time regarded as new Observable, so re-subscribe
This commit is contained in:
parent
3ca09cf366
commit
77335fbbf0
1 changed files with 8 additions and 6 deletions
|
|
@ -2,15 +2,17 @@ import { useState } from 'react';
|
|||
import { map } from 'rxjs/operators';
|
||||
import { useObservable } from 'beautiful-react-hooks';
|
||||
import { IWorkspace } from './interface';
|
||||
import { Observable } from 'rxjs';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
|
||||
// beware not pipe in the react hock, as it will re-pipe every time React reRenders, and every time regarded as new Observable, so re-subscribe
|
||||
const workspacesList$ = window.service.workspace.workspaces$.pipe(
|
||||
map<Record<string, IWorkspace>, IWorkspace[]>((workspaces) => Object.values(workspaces)),
|
||||
);
|
||||
|
||||
export function useWorkspacesListObservable(): IWorkspace[] | undefined {
|
||||
const [workspaces, workspacesSetter] = useState<IWorkspace[] | undefined>();
|
||||
useObservable<IWorkspace[] | undefined>(
|
||||
window.service.workspace.workspaces$.pipe(
|
||||
map<Record<string, IWorkspace>, IWorkspace[]>((workspaces) => Object.values(workspaces)),
|
||||
),
|
||||
workspacesSetter,
|
||||
);
|
||||
useObservable<IWorkspace[] | undefined>(workspacesList$, workspacesSetter);
|
||||
return workspaces;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue