-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
224 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import {InfiniteQueryDataObserver} from './observer'; | ||
import type {InfiniteQueryDataSource} from './types'; | ||
|
||
export const makeInfiniteQueryDataSource = <TParams, TRequest, TResponse, TData, TError>( | ||
config: Omit<InfiniteQueryDataSource<TParams, TRequest, TResponse, TData, TError>, 'type'>, | ||
): InfiniteQueryDataSource<TParams, TRequest, TResponse, TData, TError> => ({ | ||
...config, | ||
type: 'infinite', | ||
observe(context, params, options) { | ||
return new InfiniteQueryDataObserver(context, this, params, options); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import type {InfiniteQueryObserverResult} from '@tanstack/react-query'; | ||
import {InfiniteQueryObserver} from '@tanstack/react-query'; | ||
|
||
import type { | ||
DataListener, | ||
DataObserver, | ||
DataSourceContext, | ||
DataSourceData, | ||
DataSourceError, | ||
DataSourceOptions, | ||
DataSourceParams, | ||
DataSourceResponse, | ||
} from '../../../core'; | ||
|
||
import type {AnyInfiniteQueryDataSource} from './types'; | ||
import {composeOptions, transformResult} from './utils'; | ||
|
||
export class InfiniteQueryDataObserver< | ||
TDataSource extends AnyInfiniteQueryDataSource, | ||
TContext extends DataSourceContext<TDataSource> = DataSourceContext<TDataSource>, | ||
TParams extends DataSourceParams<TDataSource> = DataSourceParams<TDataSource>, | ||
TResponse extends DataSourceResponse<TDataSource> = DataSourceResponse<TDataSource>, | ||
TData extends DataSourceData<TDataSource> = DataSourceData<TDataSource>, | ||
TError extends DataSourceError<TDataSource> = DataSourceError<TDataSource>, | ||
TOptions extends DataSourceOptions<TDataSource> = DataSourceOptions<TDataSource>, | ||
> implements DataObserver<TDataSource> | ||
{ | ||
readonly context: TContext; | ||
readonly dataSource: TDataSource; | ||
readonly observer: InfiniteQueryObserver<TResponse, TError, TData, TResponse>; | ||
|
||
constructor(context: TContext, dataSource: TDataSource, params: TParams, options?: TOptions) { | ||
this.context = context; | ||
this.dataSource = dataSource; | ||
this.observer = new InfiniteQueryObserver( | ||
context.queryClient, | ||
this.composeOptions(context, dataSource, params, options), | ||
); | ||
} | ||
|
||
getCurrentState() { | ||
return this.transformResult(this.observer.getCurrentResult()); | ||
} | ||
|
||
updateParams(params: TParams, options?: TOptions) { | ||
this.observer.setOptions( | ||
this.composeOptions(this.context, this.dataSource, params, options), | ||
); | ||
} | ||
|
||
subscribe(listener: DataListener<TDataSource>) { | ||
return this.observer.subscribe((result) => { | ||
listener(this.transformResult(result)); | ||
}); | ||
} | ||
|
||
private composeOptions( | ||
context: TContext, | ||
dataSource: TDataSource, | ||
params: TParams, | ||
options?: TOptions, | ||
) { | ||
return composeOptions(context, dataSource, params, options); | ||
} | ||
|
||
private transformResult(result: InfiniteQueryObserverResult<TData, TError>) { | ||
return transformResult<TDataSource>(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import {PlainQueryDataObserver} from './observer'; | ||
import type {PlainQueryDataSource} from './types'; | ||
|
||
export const makePlainQueryDataSource = <TParams, TRequest, TResponse, TData, TError>( | ||
config: Omit<PlainQueryDataSource<TParams, TRequest, TResponse, TData, TError>, 'type'>, | ||
): PlainQueryDataSource<TParams, TRequest, TResponse, TData, TError> => ({ | ||
...config, | ||
type: 'plain', | ||
observe(context, params, options) { | ||
return new PlainQueryDataObserver(context, this, params, options); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import type {QueryObserverOptions, QueryObserverResult} from '@tanstack/react-query'; | ||
import {QueryObserver} from '@tanstack/react-query'; | ||
|
||
import type { | ||
DataListener, | ||
DataObserver, | ||
DataSourceContext, | ||
DataSourceData, | ||
DataSourceError, | ||
DataSourceOptions, | ||
DataSourceParams, | ||
DataSourceResponse, | ||
} from '../../../core'; | ||
|
||
import type {AnyPlainQueryDataSource} from './types'; | ||
import {composeOptions, transformResult} from './utils'; | ||
|
||
export class PlainQueryDataObserver< | ||
TDataSource extends AnyPlainQueryDataSource, | ||
TContext extends DataSourceContext<TDataSource> = DataSourceContext<TDataSource>, | ||
TParams extends DataSourceParams<TDataSource> = DataSourceParams<TDataSource>, | ||
TResponse extends DataSourceResponse<TDataSource> = DataSourceResponse<TDataSource>, | ||
TData extends DataSourceData<TDataSource> = DataSourceData<TDataSource>, | ||
TError extends DataSourceError<TDataSource> = DataSourceError<TDataSource>, | ||
TOptions extends DataSourceOptions<TDataSource> = DataSourceOptions<TDataSource>, | ||
> implements DataObserver<TDataSource> | ||
{ | ||
readonly context: TContext; | ||
readonly dataSource: TDataSource; | ||
readonly observer: QueryObserver<TResponse, TError, TData, TResponse>; | ||
|
||
constructor(context: TContext, dataSource: TDataSource, params: TParams, options?: TOptions) { | ||
this.context = context; | ||
this.dataSource = dataSource; | ||
this.observer = new QueryObserver( | ||
context.queryClient, | ||
this.composeOptions(context, dataSource, params, options), | ||
); | ||
} | ||
|
||
getCurrentState() { | ||
return this.transformResult(this.observer.getCurrentResult()); | ||
} | ||
|
||
updateParams(params: TParams, options?: TOptions) { | ||
this.observer.setOptions( | ||
this.composeOptions(this.context, this.dataSource, params, options), | ||
); | ||
} | ||
|
||
subscribe(listener: DataListener<TDataSource>) { | ||
return this.observer.subscribe((result) => { | ||
listener(this.transformResult(result)); | ||
}); | ||
} | ||
|
||
private composeOptions( | ||
context: TContext, | ||
dataSource: TDataSource, | ||
params: TParams, | ||
options?: TOptions, | ||
): QueryObserverOptions<TResponse, TError, TData, TResponse> { | ||
return composeOptions(context, dataSource, params, options); | ||
} | ||
|
||
private transformResult(result: QueryObserverResult<TData, TError>) { | ||
return transformResult<TDataSource>(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {useEffect, useState} from 'react'; | ||
|
||
import type { | ||
AnyDataSource, | ||
DataObserver, | ||
DataSourceContext, | ||
DataSourceOptions, | ||
DataSourceParams, | ||
DataSourceState, | ||
} from '../core'; | ||
import {composeKey} from '../core'; | ||
|
||
export const useData = <TDataSource extends AnyDataSource>( | ||
dataSource: TDataSource, | ||
context: DataSourceContext<TDataSource>, | ||
params: DataSourceParams<TDataSource>, | ||
options?: DataSourceOptions<TDataSource>, | ||
): [DataSourceState<TDataSource>, DataObserver<TDataSource>] => { | ||
const [observer] = useState(() => dataSource.observe(context, params, options)); | ||
const [state, setState] = useState<DataSourceState<TDataSource>>(() => | ||
observer.getCurrentState(), | ||
); | ||
|
||
useEffect(() => { | ||
return observer.subscribe(setState); | ||
}, [observer]); | ||
|
||
const key = composeKey(dataSource, params); | ||
|
||
useEffect(() => { | ||
observer.updateParams(params, options); | ||
// Key replaces params and other deps are static | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [options, key]); | ||
|
||
return [state, observer]; | ||
}; |