Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core data: Fix some typing issues #39214

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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