EntityStore: parse entities after fetch, not during render (performance)
This commit is contained in:
parent
e9ae8d2c45
commit
a19b1e83a9
2 changed files with 10 additions and 11 deletions
|
@ -3,6 +3,7 @@ import z from 'zod';
|
|||
|
||||
import { getNextLink, getPrevLink } from 'soapbox/api';
|
||||
import { useApi, useAppDispatch, useAppSelector } from 'soapbox/hooks';
|
||||
import { filteredArray } from 'soapbox/schemas/utils';
|
||||
|
||||
import { entitiesFetchFail, entitiesFetchRequest, entitiesFetchSuccess } from '../actions';
|
||||
|
||||
|
@ -52,10 +53,9 @@ function useEntities<TEntity extends Entity>(
|
|||
|
||||
const entities: readonly TEntity[] = entityIds ? (
|
||||
Array.from(entityIds).reduce<TEntity[]>((result, id) => {
|
||||
// TODO: parse after fetch, not during render.
|
||||
const entity = schema.safeParse(cache?.store[id]);
|
||||
if (entity.success) {
|
||||
result.push(entity.data);
|
||||
const entity = cache?.store[id];
|
||||
if (entity) {
|
||||
result.push(entity as TEntity);
|
||||
}
|
||||
return result;
|
||||
}, [])
|
||||
|
@ -74,7 +74,9 @@ function useEntities<TEntity extends Entity>(
|
|||
dispatch(entitiesFetchRequest(entityType, listKey));
|
||||
try {
|
||||
const response = await api.get(url);
|
||||
dispatch(entitiesFetchSuccess(response.data, entityType, listKey, {
|
||||
const entities = filteredArray(schema).parse(response.data);
|
||||
|
||||
dispatch(entitiesFetchSuccess(entities, entityType, listKey, {
|
||||
next: getNextLink(response),
|
||||
prev: getPrevLink(response),
|
||||
fetching: false,
|
||||
|
|
|
@ -30,11 +30,7 @@ function useEntity<TEntity extends Entity>(
|
|||
const defaultSchema = z.custom<TEntity>();
|
||||
const schema = opts.schema || defaultSchema;
|
||||
|
||||
const entity = useAppSelector(state => {
|
||||
// TODO: parse after fetch, not during render.
|
||||
const result = schema.safeParse(state.entities[entityType]?.store[entityId]);
|
||||
return result.success ? result.data : undefined;
|
||||
});
|
||||
const entity = useAppSelector(state => state.entities[entityType]?.store[entityId] as TEntity | undefined);
|
||||
|
||||
const [isFetching, setIsFetching] = useState(false);
|
||||
const isLoading = isFetching && !entity;
|
||||
|
@ -42,7 +38,8 @@ function useEntity<TEntity extends Entity>(
|
|||
const fetchEntity = () => {
|
||||
setIsFetching(true);
|
||||
api.get(endpoint).then(({ data }) => {
|
||||
dispatch(importEntities([data], entityType));
|
||||
const entity = schema.parse(data);
|
||||
dispatch(importEntities([entity], entityType));
|
||||
setIsFetching(false);
|
||||
}).catch(() => {
|
||||
setIsFetching(false);
|
||||
|
|
Loading…
Reference in a new issue