-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathqueryCancellation.js
More file actions
67 lines (61 loc) · 1.96 KB
/
Copy pathqueryCancellation.js
File metadata and controls
67 lines (61 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file "COPYING".
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
/**
* Custom error class for query cancellation
*/
class AbortError extends Error {
/**
* Create an AbortError
* @param {string} message - error message
*/
constructor(message = 'Query cancelled by client') {
super(message);
this.name = 'AbortError';
this.code = 'QUERY_CANCELLED';
}
}
/**
* Throw an error if the given signal is already aborted.
* @param {AbortSignal|null} signal - optional abort signal
* @throws {AbortError} if signal is aborted
*/
const throwIfQueryAborted = (signal) => {
if (signal?.aborted) {
throw new AbortError();
}
};
/**
* Attach a one-time abort handler to the signal that destroys the connection.
* Return a cleanup function to remove the listener
* @param {AbortSignal|null} signal - optional abort signal
* @param {object} connection - mariadb connection-like object
* @param {function(): void} beforeDestroy - callback called just before destroying connection
* @returns {function(): void} cleanup callback to remove the listener
*/
const attachAbortDestroyHandler = (signal, connection, beforeDestroy) => {
if (!signal) {
return () => {};
}
const abortHandler = () => {
beforeDestroy();
connection.destroy();
};
signal.addEventListener('abort', abortHandler, { once: true });
return () => signal.removeEventListener('abort', abortHandler);
};
module.exports = {
AbortError,
throwIfQueryAborted,
attachAbortDestroyHandler,
};