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

View File

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