|  | 
|  | 1 | +import { expect } from 'chai'; | 
|  | 2 | +import { describe, it } from 'mocha'; | 
|  | 3 | + | 
|  | 4 | +import { expectPromise } from '../../__testUtils__/expectPromise.js'; | 
|  | 5 | + | 
|  | 6 | +import { | 
|  | 7 | +  AbortSignalListener, | 
|  | 8 | +  cancellableIterable, | 
|  | 9 | +  cancellablePromise, | 
|  | 10 | +} from '../AbortSignalListener.js'; | 
|  | 11 | + | 
|  | 12 | +describe('AbortSignalListener', () => { | 
|  | 13 | +  it('works to add a listener', () => { | 
|  | 14 | +    const abortController = new AbortController(); | 
|  | 15 | + | 
|  | 16 | +    const abortSignalListener = new AbortSignalListener(abortController.signal); | 
|  | 17 | + | 
|  | 18 | +    let called = false; | 
|  | 19 | +    const onAbort = () => { | 
|  | 20 | +      called = true; | 
|  | 21 | +    }; | 
|  | 22 | +    abortSignalListener.add(onAbort); | 
|  | 23 | + | 
|  | 24 | +    abortController.abort(); | 
|  | 25 | + | 
|  | 26 | +    expect(called).to.equal(true); | 
|  | 27 | +  }); | 
|  | 28 | + | 
|  | 29 | +  it('works to delete a listener', () => { | 
|  | 30 | +    const abortController = new AbortController(); | 
|  | 31 | + | 
|  | 32 | +    const abortSignalListener = new AbortSignalListener(abortController.signal); | 
|  | 33 | + | 
|  | 34 | +    let called = false; | 
|  | 35 | +    /* c8 ignore next 3 */ | 
|  | 36 | +    const onAbort = () => { | 
|  | 37 | +      called = true; | 
|  | 38 | +    }; | 
|  | 39 | +    abortSignalListener.add(onAbort); | 
|  | 40 | +    abortSignalListener.delete(onAbort); | 
|  | 41 | + | 
|  | 42 | +    abortController.abort(); | 
|  | 43 | + | 
|  | 44 | +    expect(called).to.equal(false); | 
|  | 45 | +  }); | 
|  | 46 | + | 
|  | 47 | +  it('works to disconnect a listener from the abortSignal', () => { | 
|  | 48 | +    const abortController = new AbortController(); | 
|  | 49 | + | 
|  | 50 | +    const abortSignalListener = new AbortSignalListener(abortController.signal); | 
|  | 51 | + | 
|  | 52 | +    let called = false; | 
|  | 53 | +    /* c8 ignore next 3 */ | 
|  | 54 | +    const onAbort = () => { | 
|  | 55 | +      called = true; | 
|  | 56 | +    }; | 
|  | 57 | +    abortSignalListener.add(onAbort); | 
|  | 58 | + | 
|  | 59 | +    abortSignalListener.disconnect(); | 
|  | 60 | + | 
|  | 61 | +    abortController.abort(); | 
|  | 62 | + | 
|  | 63 | +    expect(called).to.equal(false); | 
|  | 64 | +  }); | 
|  | 65 | +}); | 
|  | 66 | + | 
|  | 67 | +describe('cancellablePromise', () => { | 
|  | 68 | +  it('works to cancel an already resolved promise', async () => { | 
|  | 69 | +    const abortController = new AbortController(); | 
|  | 70 | + | 
|  | 71 | +    const abortSignalListener = new AbortSignalListener(abortController.signal); | 
|  | 72 | + | 
|  | 73 | +    const promise = Promise.resolve(1); | 
|  | 74 | + | 
|  | 75 | +    const withCancellation = cancellablePromise(promise, abortSignalListener); | 
|  | 76 | + | 
|  | 77 | +    abortController.abort(new Error('Cancelled!')); | 
|  | 78 | + | 
|  | 79 | +    await expectPromise(withCancellation).toRejectWith('Cancelled!'); | 
|  | 80 | +  }); | 
|  | 81 | + | 
|  | 82 | +  it('works to cancel an already resolved promise after abort signal triggered', async () => { | 
|  | 83 | +    const abortController = new AbortController(); | 
|  | 84 | +    const abortSignalListener = new AbortSignalListener(abortController.signal); | 
|  | 85 | + | 
|  | 86 | +    abortController.abort(new Error('Cancelled!')); | 
|  | 87 | + | 
|  | 88 | +    const promise = Promise.resolve(1); | 
|  | 89 | + | 
|  | 90 | +    const withCancellation = cancellablePromise(promise, abortSignalListener); | 
|  | 91 | + | 
|  | 92 | +    await expectPromise(withCancellation).toRejectWith('Cancelled!'); | 
|  | 93 | +  }); | 
|  | 94 | + | 
|  | 95 | +  it('works to cancel a hanging promise', async () => { | 
|  | 96 | +    const abortController = new AbortController(); | 
|  | 97 | +    const abortSignalListener = new AbortSignalListener(abortController.signal); | 
|  | 98 | + | 
|  | 99 | +    const promise = new Promise(() => { | 
|  | 100 | +      /* never resolves */ | 
|  | 101 | +    }); | 
|  | 102 | + | 
|  | 103 | +    const withCancellation = cancellablePromise(promise, abortSignalListener); | 
|  | 104 | + | 
|  | 105 | +    abortController.abort(new Error('Cancelled!')); | 
|  | 106 | + | 
|  | 107 | +    await expectPromise(withCancellation).toRejectWith('Cancelled!'); | 
|  | 108 | +  }); | 
|  | 109 | + | 
|  | 110 | +  it('works to cancel a hanging promise created after abort signal triggered', async () => { | 
|  | 111 | +    const abortController = new AbortController(); | 
|  | 112 | +    const abortSignalListener = new AbortSignalListener(abortController.signal); | 
|  | 113 | + | 
|  | 114 | +    abortController.abort(new Error('Cancelled!')); | 
|  | 115 | + | 
|  | 116 | +    const promise = new Promise(() => { | 
|  | 117 | +      /* never resolves */ | 
|  | 118 | +    }); | 
|  | 119 | + | 
|  | 120 | +    const withCancellation = cancellablePromise(promise, abortSignalListener); | 
|  | 121 | + | 
|  | 122 | +    await expectPromise(withCancellation).toRejectWith('Cancelled!'); | 
|  | 123 | +  }); | 
|  | 124 | +}); | 
|  | 125 | + | 
|  | 126 | +describe('cancellableAsyncIterable', () => { | 
|  | 127 | +  it('works to abort a next call', async () => { | 
|  | 128 | +    const abortController = new AbortController(); | 
|  | 129 | +    const abortSignalListener = new AbortSignalListener(abortController.signal); | 
|  | 130 | + | 
|  | 131 | +    const asyncIterable = { | 
|  | 132 | +      [Symbol.asyncIterator]: () => ({ | 
|  | 133 | +        next: () => Promise.resolve({ value: 1, done: false }), | 
|  | 134 | +      }), | 
|  | 135 | +    }; | 
|  | 136 | + | 
|  | 137 | +    const withCancellation = cancellableIterable( | 
|  | 138 | +      asyncIterable, | 
|  | 139 | +      abortSignalListener, | 
|  | 140 | +    ); | 
|  | 141 | + | 
|  | 142 | +    const nextPromise = withCancellation[Symbol.asyncIterator]().next(); | 
|  | 143 | + | 
|  | 144 | +    abortController.abort(new Error('Cancelled!')); | 
|  | 145 | + | 
|  | 146 | +    await expectPromise(nextPromise).toRejectWith('Cancelled!'); | 
|  | 147 | +  }); | 
|  | 148 | + | 
|  | 149 | +  it('works to abort a next call when already aborted', async () => { | 
|  | 150 | +    const abortController = new AbortController(); | 
|  | 151 | +    const abortSignalListener = new AbortSignalListener(abortController.signal); | 
|  | 152 | + | 
|  | 153 | +    abortController.abort(new Error('Cancelled!')); | 
|  | 154 | + | 
|  | 155 | +    const asyncIterable = { | 
|  | 156 | +      [Symbol.asyncIterator]: () => ({ | 
|  | 157 | +        next: () => Promise.resolve({ value: 1, done: false }), | 
|  | 158 | +      }), | 
|  | 159 | +    }; | 
|  | 160 | + | 
|  | 161 | +    const withCancellation = cancellableIterable( | 
|  | 162 | +      asyncIterable, | 
|  | 163 | +      abortSignalListener, | 
|  | 164 | +    ); | 
|  | 165 | + | 
|  | 166 | +    const nextPromise = withCancellation[Symbol.asyncIterator]().next(); | 
|  | 167 | + | 
|  | 168 | +    await expectPromise(nextPromise).toRejectWith('Cancelled!'); | 
|  | 169 | +  }); | 
|  | 170 | +}); | 
0 commit comments