-
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.
Move our workaround to a helper function
This was repeated in five places, time to consolidate.
- Loading branch information
Showing
8 changed files
with
124 additions
and
27 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
55 changes: 55 additions & 0 deletions
55
src/plugins/data/server/search/es_search/shim_abort_signal.test.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,55 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { elasticsearchServiceMock } from '../../../../../core/server/mocks'; | ||
import { shimAbortSignal } from '.'; | ||
|
||
describe('shimAbortSignal', () => { | ||
it('aborts the promise if the signal is aborted', () => { | ||
const promise = elasticsearchServiceMock.createSuccessTransportRequestPromise({ | ||
success: true, | ||
}); | ||
const controller = new AbortController(); | ||
shimAbortSignal(promise, controller.signal); | ||
controller.abort(); | ||
|
||
expect(promise.abort).toHaveBeenCalled(); | ||
}); | ||
|
||
it('returns the original promise', async () => { | ||
const promise = elasticsearchServiceMock.createSuccessTransportRequestPromise({ | ||
success: true, | ||
}); | ||
const controller = new AbortController(); | ||
const response = await shimAbortSignal(promise, controller.signal); | ||
|
||
expect(response).toEqual(expect.objectContaining({ body: { success: true } })); | ||
}); | ||
|
||
it('allows the promise to be aborted manually', () => { | ||
const promise = elasticsearchServiceMock.createSuccessTransportRequestPromise({ | ||
success: true, | ||
}); | ||
const controller = new AbortController(); | ||
const enhancedPromise = shimAbortSignal(promise, controller.signal); | ||
|
||
enhancedPromise.abort(); | ||
expect(promise.abort).toHaveBeenCalled(); | ||
}); | ||
}); |
40 changes: 40 additions & 0 deletions
40
src/plugins/data/server/search/es_search/shim_abort_signal.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,40 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { TransportRequestPromise } from '@elastic/elasticsearch/lib/Transport'; | ||
|
||
/** | ||
* | ||
* NOTE: Temporary workaround until https://github.com/elastic/elasticsearch-js/issues/1297 | ||
* is resolved | ||
* | ||
* @param promise a TransportRequestPromise | ||
* @param signal optional AbortSignal | ||
* | ||
* @returns a TransportRequestPromise that will be aborted if the signal is aborted | ||
*/ | ||
export const shimAbortSignal = <T extends TransportRequestPromise<unknown>>( | ||
promise: T, | ||
signal: AbortSignal | undefined | ||
): T => { | ||
if (signal) { | ||
signal.addEventListener('abort', () => promise.abort()); | ||
} | ||
return promise; | ||
}; |
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