chore: fix generic data provider isUpdate (#1560)

This commit is contained in:
Bartłomiej Głownia 2022-10-03 10:28:52 +02:00 committed by GitHub
parent 96811b45e3
commit 1d87eab47c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 18 deletions

View File

@ -5,8 +5,15 @@ import type {
Subscribe,
Load,
UpdateCallback,
UpdateDelta,
} from '../lib/generic-data-provider';
function hasDelta<T>(
updateData: UpdateDelta<T>
): updateData is Required<UpdateDelta<T>> {
return !!updateData.isUpdate;
}
/**
*
* @param dataProvider subscribe function created by makeDataProvider
@ -63,16 +70,16 @@ export function useDataProvider<Data, Delta>({
return Promise.reject();
}, []);
const callback = useCallback<UpdateCallback<Data, Delta>>(
({
(arg) => {
const {
data,
error,
loading,
delta,
insertionData,
totalCount,
isInsert,
isUpdate,
}) => {
} = arg;
setError(error);
setLoading(loading);
if (!error && !loading) {
@ -83,7 +90,8 @@ export function useDataProvider<Data, Delta>({
isUpdate &&
!noUpdate &&
update &&
(!delta || update({ delta, data }))
hasDelta<Delta>(arg) &&
update({ delta: arg.delta, data })
) {
return;
}

View File

@ -9,10 +9,13 @@ import type { Subscription } from 'zen-observable-ts';
import isEqual from 'lodash/isEqual';
import type { Pagination as PaginationWithoutSkip } from '@vegaprotocol/types';
interface UpdateData<Data, Delta> {
export interface UpdateDelta<Delta> {
delta?: Delta;
insertionData?: Data | null;
isUpdate?: boolean;
}
interface UpdateData<Data, Delta> extends UpdateDelta<Delta> {
insertionData?: Data | null;
isInsert?: boolean;
}
export interface UpdateCallback<Data, Delta> {
@ -557,13 +560,13 @@ function makeDerivedDataProviderInternal<Data, Delta>(
: data;
if (newLoaded) {
const updatedPart = parts[updatedPartIndex];
if (updatedPart.isUpdate && updatedPart.delta && combineDelta) {
if (updatedPart.isUpdate) {
isUpdate = true;
if (updatedPart.delta && combineDelta) {
delta = combineDelta(parts, variables);
}
delete updatedPart.isUpdate;
delete updatedPart.delta;
if (delta) {
isUpdate = true;
}
}
}
if (