-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split utility hooks into several files
- Loading branch information
1 parent
8f62d8c
commit acf0ceb
Showing
8 changed files
with
279 additions
and
242 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export * from './types'; | ||
export * from './use_data_search_request'; | ||
export * from './use_latest_partial_data_search_response'; |
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,36 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { Observable } from 'rxjs'; | ||
import { | ||
IKibanaSearchRequest, | ||
IKibanaSearchResponse, | ||
ISearchOptions, | ||
} from '../../../../../../src/plugins/data/public'; | ||
import { SearchStrategyError } from '../../../common/search_strategies/common/errors'; | ||
|
||
export interface DataSearchRequestDescriptor<Request extends IKibanaSearchRequest, RawResponse> { | ||
request: Request; | ||
options: ISearchOptions; | ||
response$: Observable<IKibanaSearchResponse<RawResponse>>; | ||
abortController: AbortController; | ||
} | ||
|
||
export interface NormalizedKibanaSearchResponse<ResponseData> { | ||
total?: number; | ||
loaded?: number; | ||
isRunning: boolean; | ||
isPartial: boolean; | ||
data: ResponseData; | ||
errors: SearchStrategyError[]; | ||
} | ||
|
||
export interface DataSearchResponseDescriptor<Request extends IKibanaSearchRequest, Response> { | ||
request: Request; | ||
options: ISearchOptions; | ||
response: NormalizedKibanaSearchResponse<Response>; | ||
abortController: AbortController; | ||
} |
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
99 changes: 99 additions & 0 deletions
99
x-pack/plugins/infra/public/utils/data_search/use_data_search_request.ts
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,99 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { useCallback } from 'react'; | ||
import { Observable, of, Subject } from 'rxjs'; | ||
import { catchError, map, share, startWith, switchMap, tap } from 'rxjs/operators'; | ||
import { | ||
IKibanaSearchRequest, | ||
IKibanaSearchResponse, | ||
ISearchOptions, | ||
} from '../../../../../../src/plugins/data/public'; | ||
import { AbortError } from '../../../../../../src/plugins/kibana_utils/public'; | ||
import { SearchStrategyError } from '../../../common/search_strategies/common/errors'; | ||
import { useKibanaContextForPlugin } from '../../hooks/use_kibana'; | ||
import { tapUnsubscribe, useLatest, useObservable, useObservableState } from '../use_observable'; | ||
|
||
export type DataSearchRequestFactory<Args extends any[], Request extends IKibanaSearchRequest> = ( | ||
...args: Args | ||
) => | ||
| { | ||
request: Request; | ||
options: ISearchOptions; | ||
} | ||
| null | ||
| undefined; | ||
|
||
export const useDataSearch = < | ||
RequestFactoryArgs extends any[], | ||
Request extends IKibanaSearchRequest, | ||
RawResponse | ||
>({ | ||
getRequest, | ||
}: { | ||
getRequest: DataSearchRequestFactory<RequestFactoryArgs, Request>; | ||
}) => { | ||
const { services } = useKibanaContextForPlugin(); | ||
const request$ = useObservable( | ||
() => new Subject<{ request: Request; options: ISearchOptions }>(), | ||
[] | ||
); | ||
const requests$ = useObservable( | ||
(inputs$) => | ||
inputs$.pipe( | ||
switchMap(([currentRequest$]) => currentRequest$), | ||
map(({ request, options }) => { | ||
const abortController = new AbortController(); | ||
let isAbortable = true; | ||
|
||
return { | ||
abortController, | ||
request, | ||
options, | ||
response$: services.data.search | ||
.search<Request, IKibanaSearchResponse<RawResponse>>(request, { | ||
abortSignal: abortController.signal, | ||
...options, | ||
}) | ||
.pipe( | ||
// avoid aborting failed or completed requests | ||
tap({ | ||
error: () => { | ||
isAbortable = false; | ||
}, | ||
complete: () => { | ||
isAbortable = false; | ||
}, | ||
}), | ||
tapUnsubscribe(() => { | ||
if (isAbortable) { | ||
abortController.abort(); | ||
} | ||
}), | ||
share() | ||
), | ||
}; | ||
}) | ||
), | ||
[request$] | ||
); | ||
|
||
const search = useCallback( | ||
(...args: RequestFactoryArgs) => { | ||
const request = getRequest(...args); | ||
|
||
if (request) { | ||
request$.next(request); | ||
} | ||
}, | ||
[getRequest, request$] | ||
); | ||
|
||
return { | ||
requests$, | ||
search, | ||
}; | ||
}; |
Oops, something went wrong.