-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'lukasolson/searchInterceptor' into newp…
…latform/ui-search-cancel
- Loading branch information
Showing
23 changed files
with
486 additions
and
79 deletions.
There are no files selected for viewing
11 changes: 0 additions & 11 deletions
11
docs/development/plugins/data/server/kibana-plugin-plugins-data-server.icancel.md
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
...elopment/plugins/data/server/kibana-plugin-plugins-data-server.isearchcancel.md
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,11 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
[Home](./index.md) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [ISearchCancel](./kibana-plugin-plugins-data-server.isearchcancel.md) | ||
|
||
## ISearchCancel type | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export declare type ISearchCancel<T extends TStrategyTypes> = (id: string) => Promise<void>; | ||
``` |
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,114 @@ | ||
/* | ||
* 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 { AbortError, toPromise, getCombinedSignal } from './abort_utils'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
const flushPromises = () => new Promise(resolve => setImmediate(resolve)); | ||
|
||
describe('AbortUtils', () => { | ||
describe('AbortError', () => { | ||
test('should preserve `message`', () => { | ||
const message = 'my error message'; | ||
const error = new AbortError(message); | ||
expect(error.message).toBe(message); | ||
}); | ||
|
||
test('should have a name of "AbortError"', () => { | ||
const error = new AbortError(); | ||
expect(error.name).toBe('AbortError'); | ||
}); | ||
}); | ||
|
||
describe('toPromise', () => { | ||
describe('resolves', () => { | ||
test('should not resolve if the signal does not abort', async () => { | ||
const controller = new AbortController(); | ||
const promise = toPromise(controller.signal); | ||
const whenResolved = jest.fn(); | ||
promise.then(whenResolved); | ||
await flushPromises(); | ||
expect(whenResolved).not.toBeCalled(); | ||
}); | ||
|
||
test('should resolve if the signal does abort', async () => { | ||
const controller = new AbortController(); | ||
const promise = toPromise(controller.signal); | ||
const whenResolved = jest.fn(); | ||
promise.then(whenResolved); | ||
controller.abort(); | ||
await flushPromises(); | ||
expect(whenResolved).toBeCalled(); | ||
}); | ||
}); | ||
|
||
describe('rejects', () => { | ||
test('should not reject if the signal does not abort', async () => { | ||
const controller = new AbortController(); | ||
const promise = toPromise(controller.signal, true); | ||
const whenRejected = jest.fn(); | ||
promise.catch(whenRejected); | ||
await flushPromises(); | ||
expect(whenRejected).not.toBeCalled(); | ||
}); | ||
|
||
test('should reject if the signal does abort', async () => { | ||
const controller = new AbortController(); | ||
const promise = toPromise(controller.signal, true); | ||
const whenRejected = jest.fn(); | ||
promise.catch(whenRejected); | ||
controller.abort(); | ||
await flushPromises(); | ||
expect(whenRejected).toBeCalled(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getCombinedSignal', () => { | ||
test('should return an AbortSignal', () => { | ||
const signal = getCombinedSignal([]); | ||
expect(signal instanceof AbortSignal).toBe(true); | ||
}); | ||
|
||
test('should not abort if none of the signals abort', async () => { | ||
const controller1 = new AbortController(); | ||
const controller2 = new AbortController(); | ||
setTimeout(() => controller1.abort(), 2000); | ||
setTimeout(() => controller2.abort(), 1000); | ||
const signal = getCombinedSignal([controller1.signal, controller2.signal]); | ||
expect(signal.aborted).toBe(false); | ||
jest.advanceTimersByTime(500); | ||
await flushPromises(); | ||
expect(signal.aborted).toBe(false); | ||
}); | ||
|
||
test('should abort when the first signal aborts', async () => { | ||
const controller1 = new AbortController(); | ||
const controller2 = new AbortController(); | ||
setTimeout(() => controller1.abort(), 2000); | ||
setTimeout(() => controller2.abort(), 1000); | ||
const signal = getCombinedSignal([controller1.signal, controller2.signal]); | ||
expect(signal.aborted).toBe(false); | ||
jest.advanceTimersByTime(1000); | ||
await flushPromises(); | ||
expect(signal.aborted).toBe(true); | ||
}); | ||
}); | ||
}); |
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,56 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* Class used to signify that something was aborted. Useful for applications to conditionally handle | ||
* this type of error differently than other errors. | ||
*/ | ||
export class AbortError extends Error { | ||
constructor(message = 'Aborted') { | ||
super(message); | ||
this.message = message; | ||
this.name = 'AbortError'; | ||
} | ||
} | ||
|
||
/** | ||
* Returns a `Promise` corresponding with when the given `AbortSignal` is aborted. Useful for | ||
* situations when you might need to `Promise.race` multiple `AbortSignal`s, or an `AbortSignal` | ||
* with any other expected errors (or completions). | ||
* @param signal The `AbortSignal` to generate the `Promise` from | ||
* @param shouldReject If `false`, the promise will be resolved, otherwise it will be rejected | ||
*/ | ||
export function toPromise(signal: AbortSignal, shouldReject = false) { | ||
return new Promise((resolve, reject) => { | ||
const action = shouldReject ? reject : resolve; | ||
if (signal.aborted) action(); | ||
signal.addEventListener('abort', action); | ||
}); | ||
} | ||
|
||
/** | ||
* Returns an `AbortSignal` that will be aborted when the first of the given signals aborts. | ||
* @param signals | ||
*/ | ||
export function getCombinedSignal(signals: AbortSignal[]) { | ||
const promises = signals.map(signal => toPromise(signal)); | ||
const controller = new AbortController(); | ||
Promise.race(promises).then(() => controller.abort()); | ||
return controller.signal; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* Class used to signify that a request timed out. Useful for applications to conditionally handle | ||
* this type of error differently than other errors. | ||
*/ | ||
export class RequestTimeoutError extends Error { | ||
constructor(message = 'Request timed out') { | ||
super(message); | ||
this.message = message; | ||
this.name = 'RequestTimeoutError'; | ||
} | ||
} |
Oops, something went wrong.