Skip to content
Merged
Show file tree
Hide file tree
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: 22 additions & 6 deletions onboarding/src/Components/Steps/SiteList.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import { get, track } from '../../utils/rest';

const { onboarding } = tiobDash;

// Debounce the search so it stays off the network while typing: only fire after the
// field is idle for SEARCH_DEBOUNCE_MS and the query is at least SEARCH_MIN_CHARS.
const SEARCH_DEBOUNCE_MS = 700;
const SEARCH_MIN_CHARS = 3;

// Guards tracking-session init against firing twice while the first request is in flight.
let trackingInitStarted = false;

Expand Down Expand Up @@ -89,7 +94,6 @@ const SiteList = ( {
}

let active = true;
let safety;
const reveal = setTimeout( () => {
if ( active ) {
setPersonalizing( true );
Expand All @@ -103,7 +107,7 @@ const SiteList = ( {
}
};

safety = setTimeout( done, 9000 );
const safety = setTimeout( done, 9000 );
get(
onboarding.root +
'/starter_order?builder=' +
Expand Down Expand Up @@ -134,13 +138,15 @@ const SiteList = ( {
setSearchFailed( false );

const q = ( searchQuery || '' ).trim();
if ( q.length < 3 ) {
if ( q.length < SEARCH_MIN_CHARS ) {
setSearching( false );
return undefined;
}

let active = true;
let safety;
// Lets cleanup abort a superseded in-flight request, not just ignore its result.
const controller = new AbortController();
const done = () => {
if ( active ) {
clearTimeout( safety );
Expand All @@ -166,7 +172,10 @@ const SiteList = ( {
'/starter_search?builder=' +
encodeURIComponent( editor ) +
'&q=' +
encodeURIComponent( q )
encodeURIComponent( q ),
false,
true,
controller.signal
)
.then( ( res ) => {
if ( ! active ) {
Expand All @@ -181,11 +190,18 @@ const SiteList = ( {

fail();
} )
.catch( fail );
}, 600 );
.catch( ( err ) => {
// Aborted = superseded, not a real failure → skip the fallback.
if ( err && err.name === 'AbortError' ) {
return;
}
fail();
} );
}, SEARCH_DEBOUNCE_MS );

return () => {
active = false;
controller.abort();
clearTimeout( timer );
clearTimeout( safety );
};
Expand Down
12 changes: 9 additions & 3 deletions onboarding/src/utils/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ export const send = ( route, data, simple = false ) => {
return requestData( route, simple, data );
};

export const get = ( route, simple = false, useNonce = true ) => {
return requestData( route, simple, {}, 'GET', useNonce );
export const get = ( route, simple = false, useNonce = true, signal = null ) => {
return requestData( route, simple, {}, 'GET', useNonce, signal );
};

const requestData = async (
route,
simple = false,
data = {},
method = 'POST',
useNonce = true
useNonce = true,
signal = null
) => {
const options = {
method,
Expand All @@ -37,6 +38,11 @@ const requestData = async (
options.headers[ 'x-wp-nonce' ] = tiobDash.nonce;
}

// Optional AbortSignal so callers can cancel an in-flight request.
if ( signal ) {
options.signal = signal;
}

if ( 'POST' === method ) {
options.body = JSON.stringify( data );
}
Expand Down
Loading