|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { Observable } from 'rxjs'; |
| 21 | +import { |
| 22 | + ISearchContext, |
| 23 | + TSearchStrategyProvider, |
| 24 | + ISearchStrategy, |
| 25 | +} from '../../../src/plugins/data/public'; |
| 26 | + |
| 27 | +import { ASYNC_DEMO_SEARCH_STRATEGY, IAsyncDemoResponse } from '../common'; |
| 28 | +import { ASYNC_SEARCH_STRATEGY } from '../../../x-pack/plugins/data_enhanced/public'; |
| 29 | + |
| 30 | +/** |
| 31 | + * This demo search strategy provider simply provides a shortcut for calling the DEMO_ASYNC_SEARCH_STRATEGY |
| 32 | + * on the server side, without users having to pass it in explicitly, and it takes advantage of the |
| 33 | + * already registered ASYNC_SEARCH_STRATEGY that exists on the client. |
| 34 | + * |
| 35 | + * so instead of callers having to do: |
| 36 | + * |
| 37 | + * ``` |
| 38 | + * search( |
| 39 | + * { ...request, serverStrategy: DEMO_ASYNC_SEARCH_STRATEGY }, |
| 40 | + * options, |
| 41 | + * ASYNC_SEARCH_STRATEGY |
| 42 | + * ) as Observable<IDemoResponse>, |
| 43 | + *``` |
| 44 | +
|
| 45 | + * They can instead just do |
| 46 | + * |
| 47 | + * ``` |
| 48 | + * search(request, options, DEMO_ASYNC_SEARCH_STRATEGY); |
| 49 | + * ``` |
| 50 | + * |
| 51 | + * and are ensured type safety in regard to the request and response objects. |
| 52 | + * |
| 53 | + * @param context - context supplied by other plugins. |
| 54 | + * @param search - a search function to access other strategies that have already been registered. |
| 55 | + */ |
| 56 | +export const asyncDemoClientSearchStrategyProvider: TSearchStrategyProvider<typeof ASYNC_DEMO_SEARCH_STRATEGY> = ( |
| 57 | + context: ISearchContext |
| 58 | +): ISearchStrategy<typeof ASYNC_DEMO_SEARCH_STRATEGY> => { |
| 59 | + const asyncStrategyProvider = context.getSearchStrategy(ASYNC_SEARCH_STRATEGY); |
| 60 | + const { search } = asyncStrategyProvider(context); |
| 61 | + return { |
| 62 | + search: (request, options) => { |
| 63 | + return search( |
| 64 | + { ...request, serverStrategy: ASYNC_DEMO_SEARCH_STRATEGY }, |
| 65 | + options |
| 66 | + ) as Observable<IAsyncDemoResponse>; |
| 67 | + }, |
| 68 | + }; |
| 69 | +}; |
0 commit comments