| 1 |
/** |
| 2 |
* WordPress dependencies. |
| 3 |
*/ |
| 4 |
import apiFetch from '@wordpress/api-fetch'; |
| 5 |
import { useCallback, useRef } from '@wordpress/element'; |
| 6 |
|
| 7 |
/** |
| 8 |
* Internal dependencies. |
| 9 |
*/ |
| 10 |
import { ajaxUrl, nonce } from './config'; |
| 11 |
|
| 12 |
/** |
| 13 |
* Indexing hook. |
| 14 |
* |
| 15 |
* Provides methods for indexing, getting indexing status, and cancelling |
| 16 |
* indexing. Methods share an abort controller so that requests can |
| 17 |
* interrupt eachother to avoid multiple sync requests causing race conditions |
| 18 |
* or duplicate output, such as by rapidly pausing and unpausing indexing. |
| 19 |
* |
| 20 |
* @returns {object} Sync, sync status, and cancel functions. |
| 21 |
*/ |
| 22 |
export const useIndex = () => { |
| 23 |
const abort = useRef(new AbortController()); |
| 24 |
const request = useRef(null); |
| 25 |
|
| 26 |
const sendRequest = useCallback( |
| 27 |
/** |
| 28 |
* Send AJAX request. |
| 29 |
* |
| 30 |
* Silently catches abort errors and clears the current request on |
| 31 |
* completion. |
| 32 |
* |
| 33 |
* @param {object} options Request options. |
| 34 |
* @throws {Error} Any non-abort errors. |
| 35 |
* @returns {Promise} Current request promise. |
| 36 |
*/ |
| 37 |
(options) => { |
| 38 |
request.current = apiFetch(options).finally(() => { |
| 39 |
request.current = null; |
| 40 |
}); |
| 41 |
|
| 42 |
return request.current; |
| 43 |
}, |
| 44 |
[], |
| 45 |
); |
| 46 |
|
| 47 |
const cancelIndex = useCallback( |
| 48 |
/** |
| 49 |
* Send a request to cancel sync. |
| 50 |
* |
| 51 |
* @returns {Promise} Fetch request promise. |
| 52 |
*/ |
| 53 |
async () => { |
| 54 |
abort.current.abort(); |
| 55 |
abort.current = new AbortController(); |
| 56 |
|
| 57 |
const body = new FormData(); |
| 58 |
|
| 59 |
body.append('action', 'ep_cancel_index'); |
| 60 |
body.append('nonce', nonce); |
| 61 |
|
| 62 |
const options = { |
| 63 |
url: ajaxUrl, |
| 64 |
method: 'POST', |
| 65 |
body, |
| 66 |
signal: abort.current.signal, |
| 67 |
}; |
| 68 |
|
| 69 |
return sendRequest(options); |
| 70 |
}, |
| 71 |
[sendRequest], |
| 72 |
); |
| 73 |
|
| 74 |
const index = useCallback( |
| 75 |
/** |
| 76 |
* Send a request to sync. |
| 77 |
* |
| 78 |
* @param {boolean} putMapping Whether to put mapping. |
| 79 |
* @returns {Promise} Fetch request promise. |
| 80 |
*/ |
| 81 |
async (putMapping) => { |
| 82 |
abort.current.abort(); |
| 83 |
abort.current = new AbortController(); |
| 84 |
|
| 85 |
const body = new FormData(); |
| 86 |
|
| 87 |
body.append('action', 'ep_index'); |
| 88 |
body.append('put_mapping', putMapping ? 1 : 0); |
| 89 |
body.append('nonce', nonce); |
| 90 |
|
| 91 |
const options = { |
| 92 |
url: ajaxUrl, |
| 93 |
method: 'POST', |
| 94 |
body, |
| 95 |
signal: abort.current.signal, |
| 96 |
}; |
| 97 |
|
| 98 |
return sendRequest(options); |
| 99 |
}, |
| 100 |
[sendRequest], |
| 101 |
); |
| 102 |
|
| 103 |
const indexStatus = useCallback( |
| 104 |
/** |
| 105 |
* Send a request for CLI sync status. |
| 106 |
* |
| 107 |
* @returns {Promise} Fetch request promise. |
| 108 |
*/ |
| 109 |
async () => { |
| 110 |
abort.current.abort(); |
| 111 |
abort.current = new AbortController(); |
| 112 |
|
| 113 |
const body = new FormData(); |
| 114 |
|
| 115 |
body.append('action', 'ep_index_status'); |
| 116 |
body.append('nonce', nonce); |
| 117 |
|
| 118 |
const options = { |
| 119 |
url: ajaxUrl, |
| 120 |
method: 'POST', |
| 121 |
body, |
| 122 |
signal: abort.current.signal, |
| 123 |
}; |
| 124 |
|
| 125 |
return sendRequest(options); |
| 126 |
}, |
| 127 |
[sendRequest], |
| 128 |
); |
| 129 |
|
| 130 |
return { cancelIndex, index, indexStatus }; |
| 131 |
}; |
| 132 |
|