Skip to content

Commit

Permalink
Core data: Fix some typing issues in JSDoc comments (#39214)
Browse files Browse the repository at this point in the history
Part of #39211

Fixes a few small issues with the JSDoc types in @wordpress/core-data.

 - queue of actions in `batch` was untyped, added type holding vague structure of type
 - `await`'ed `Promise` called `resolve()` but wasn't typed as `Promise<void>`. this may not be possible in JSDoc whereas it is in `.ts` with the type parameter. I've made it `resolve(undefined)` to get around this.
 - inner loop of batched actions had ambiguous types (list of things or tuple?) and I've extracted the inline assignments to variables in order to eliminate the ambiguity
 - `ObservedSet` used spread arguments for `Set`'s 1-arity functions. I've replaced `...args` with `value` to match the API of `Set`
  • Loading branch information
dmsnell authored Mar 4, 2022
1 parent baeb329 commit 24ece16
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions packages/core-data/src/batch/create-batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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<boolean>} A promise that resolves to a boolean that is true
* if the processor returned no errors.
*/
async run() {
Expand All @@ -109,7 +110,7 @@ export default function createBatch( processor = defaultProcessor ) {
const unsubscribe = pending.subscribe( () => {
if ( ! pending.size ) {
unsubscribe();
resolve();
resolve( undefined );
}
} );
} );
Expand Down Expand Up @@ -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 );
}
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 24ece16

Please sign in to comment.