From abf871da65fe51e1055d1a9d438cbee85abb111e Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Thu, 3 Mar 2022 23:47:46 -0700 Subject: [PATCH] Core data: Fix some typing issues in JSDoc comments --- packages/core-data/src/batch/create-batch.js | 28 +++++++++++--------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/core-data/src/batch/create-batch.js b/packages/core-data/src/batch/create-batch.js index dbd9073dac36a..380637545528f 100644 --- a/packages/core-data/src/batch/create-batch.js +++ b/packages/core-data/src/batch/create-batch.js @@ -43,6 +43,7 @@ import defaultProcessor from './default-processor'; */ export default function createBatch( processor = defaultProcessor ) { let lastId = 0; + /** @type {Array<{ input: any; resolve: ( value: any ) => void; reject: ( error: any ) => void }>} */ let queue = []; const pending = new ObservableSet(); @@ -100,7 +101,7 @@ export default function createBatch( processor = defaultProcessor ) { * Runs the batch. This calls `batchProcessor` and resolves or rejects * all promises returned by `add()`. * - * @return {Promise} A promise that resolves to a boolean that is true + * @return {Promise} A promise that resolves to a boolean that is true * if the processor returned no errors. */ async run() { @@ -109,7 +110,7 @@ export default function createBatch( processor = defaultProcessor ) { const unsubscribe = pending.subscribe( () => { if ( ! pending.size ) { unsubscribe(); - resolve(); + resolve( undefined ); } } ); } ); @@ -137,15 +138,18 @@ export default function createBatch( processor = defaultProcessor ) { let isSuccess = true; - for ( const [ result, { resolve, reject } ] of zip( - results, - queue - ) ) { + for ( const pair of zip( results, queue ) ) { + /** @type {{error?: unknown, output?: unknown}} */ + const result = pair[ 0 ]; + + /** @type {{resolve: (value: any) => void; reject: (error: any) => void} | undefined} */ + const queueItem = pair[ 1 ]; + if ( result?.error ) { - reject( result.error ); + queueItem?.reject( result.error ); isSuccess = false; } else { - resolve( result?.output ?? result ); + queueItem?.resolve( result?.output ?? result ); } } @@ -166,14 +170,14 @@ class ObservableSet { return this.set.size; } - add( ...args ) { - this.set.add( ...args ); + add( value ) { + this.set.add( value ); this.subscribers.forEach( ( subscriber ) => subscriber() ); return this; } - delete( ...args ) { - const isSuccess = this.set.delete( ...args ); + delete( value ) { + const isSuccess = this.set.delete( value ); this.subscribers.forEach( ( subscriber ) => subscriber() ); return isSuccess; }