市场夺宝奇兵
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
912 B

  1. import {onAbortedSignal} from '../utils/abort-signal.js';
  2. // Validate the `cancelSignal` option
  3. export const validateCancelSignal = ({cancelSignal}) => {
  4. if (cancelSignal !== undefined && Object.prototype.toString.call(cancelSignal) !== '[object AbortSignal]') {
  5. throw new Error(`The \`cancelSignal\` option must be an AbortSignal: ${String(cancelSignal)}`);
  6. }
  7. };
  8. // Terminate the subprocess when aborting the `cancelSignal` option and `gracefulSignal` is `false`
  9. export const throwOnCancel = ({subprocess, cancelSignal, gracefulCancel, context, controller}) => cancelSignal === undefined || gracefulCancel
  10. ? []
  11. : [terminateOnCancel(subprocess, cancelSignal, context, controller)];
  12. const terminateOnCancel = async (subprocess, cancelSignal, context, {signal}) => {
  13. await onAbortedSignal(cancelSignal, signal);
  14. context.terminationReason ??= 'cancel';
  15. subprocess.kill();
  16. throw cancelSignal.reason;
  17. };