@@ -4,8 +4,20 @@ import { describe, it } from 'mocha';
44import { expectPromise } from '../../../__testUtils__/expectPromise.js' ;
55import { resolveOnNextTick } from '../../../__testUtils__/resolveOnNextTick.js' ;
66
7+ import { isPromise } from '../../../jsutils/isPromise.js' ;
8+
79import { Computation } from '../Computation.js' ;
810
11+ function abortIgnoringCleanup (
12+ computation : Computation < unknown > ,
13+ reason ?: unknown ,
14+ ) : void {
15+ const aborted = computation . abort ( reason ) ;
16+ if ( isPromise ( aborted ) ) {
17+ aborted . catch ( ( ) => undefined ) ;
18+ }
19+ }
20+
921describe ( 'Computation' , ( ) => {
1022 it ( 'can return a result' , ( ) => {
1123 const computation = new Computation ( ( ) => ( { value : 123 } ) ) ;
@@ -114,7 +126,7 @@ describe('Computation', () => {
114126 onAbortRan = true ;
115127 } ,
116128 ) ;
117- computation . abort ( ) ;
129+ abortIgnoringCleanup ( computation ) ;
118130 expect ( ( ) => computation . result ( ) ) . to . throw ( 'Cancelled!' ) ;
119131 expect ( onAbortRan ) . to . equal ( false ) ;
120132 } ) ;
@@ -129,7 +141,7 @@ describe('Computation', () => {
129141 ) ;
130142
131143 computation . prime ( ) ;
132- computation . abort ( ) ;
144+ abortIgnoringCleanup ( computation ) ;
133145 expect ( computation . result ( ) ) . to . deep . equal ( { value : 123 } ) ;
134146 expect ( onAbortRan ) . to . equal ( false ) ;
135147 } ) ;
@@ -146,7 +158,7 @@ describe('Computation', () => {
146158 ) ;
147159
148160 computation . prime ( ) ;
149- computation . abort ( ) ;
161+ abortIgnoringCleanup ( computation ) ;
150162 expect ( ( ) => computation . result ( ) ) . to . throw ( 'failure' ) ;
151163 expect ( onAbortRan ) . to . equal ( false ) ;
152164 } ) ;
@@ -164,16 +176,52 @@ describe('Computation', () => {
164176 ) ;
165177
166178 computation . prime ( ) ;
167- computation . abort ( ) ;
179+ abortIgnoringCleanup ( computation ) ;
168180 expect ( onAbortRan ) . to . equal ( true ) ;
169181 expect ( ( ) => computation . result ( ) ) . to . throw ( 'Cancelled!' ) ;
170182 } ) ;
171183
184+ it ( 'returns async abort cleanup while running' , async ( ) => {
185+ let resolveCleanup ! : ( ) => void ;
186+ const cleanupPromise = new Promise < void > ( ( resolve ) => {
187+ resolveCleanup = resolve ;
188+ } ) ;
189+ const computation = new Computation (
190+ ( ) =>
191+ new Promise ( ( ) => {
192+ // Never resolves.
193+ } ) ,
194+ ( ) => cleanupPromise ,
195+ ) ;
196+
197+ computation . prime ( ) ;
198+ const abortResult = computation . abort ( ) ;
199+ expect ( abortResult ) . to . equal ( cleanupPromise ) ;
200+ expect ( isPromise ( abortResult ) ) . to . equal ( true ) ;
201+ if ( ! isPromise ( abortResult ) ) {
202+ throw new Error ( 'Expected async abort cleanup promise.' ) ;
203+ }
204+
205+ let abortSettled = false ;
206+ abortResult . then (
207+ ( ) => {
208+ abortSettled = true ;
209+ } ,
210+ ( ) => {
211+ abortSettled = true ;
212+ } ,
213+ ) ;
214+ expect ( abortSettled ) . to . equal ( false ) ;
215+ resolveCleanup ( ) ;
216+ await abortResult ;
217+ expect ( abortSettled ) . to . equal ( true ) ;
218+ } ) ;
219+
172220 it ( 'can be aborted with a provided reason before running' , ( ) => {
173221 const abortReason = new Error ( 'aborted' ) ;
174222 const computation = new Computation ( ( ) => ( { value : 123 } ) ) ;
175223
176- computation . abort ( abortReason ) ;
224+ abortIgnoringCleanup ( computation , abortReason ) ;
177225 expect ( ( ) => computation . result ( ) ) . to . throw ( 'aborted' ) ;
178226 } ) ;
179227
@@ -191,7 +239,7 @@ describe('Computation', () => {
191239 ) ;
192240
193241 computation . prime ( ) ;
194- computation . abort ( abortReason ) ;
242+ abortIgnoringCleanup ( computation , abortReason ) ;
195243 expect ( onAbortReason ) . to . equal ( abortReason ) ;
196244 expect ( ( ) => computation . result ( ) ) . to . throw ( 'aborted' ) ;
197245 } ) ;
0 commit comments