1
1
// ignore-tidy-filelength
2
- /* global addClass, getNakedUrl, getSettingValue */
2
+ /* global addClass, getNakedUrl, getSettingValue, getVar */
3
3
/* global onEachLazy, removeClass, searchState, browserSupportsHistoryApi, exports */
4
4
5
5
"use strict" ;
@@ -1298,7 +1298,7 @@ class NameTrie {
1298
1298
}
1299
1299
1300
1300
class DocSearch {
1301
- constructor ( rawSearchIndex , rootPath , searchState ) {
1301
+ constructor ( rootPath , searchState ) {
1302
1302
/**
1303
1303
* @type {Map<String, RoaringBitmap> }
1304
1304
*/
@@ -1417,7 +1417,7 @@ class DocSearch {
1417
1417
/**
1418
1418
* @type {Array<Row> }
1419
1419
*/
1420
- this . searchIndex = this . buildIndex ( rawSearchIndex ) ;
1420
+ this . searchIndex = [ ] ;
1421
1421
}
1422
1422
1423
1423
/**
@@ -1695,7 +1695,7 @@ class DocSearch {
1695
1695
*
1696
1696
* @param {[string, RawSearchIndexCrate][] } rawSearchIndex
1697
1697
*/
1698
- buildIndex ( rawSearchIndex ) {
1698
+ async buildIndex ( rawSearchIndex ) {
1699
1699
/**
1700
1700
* Convert from RawFunctionSearchType to FunctionSearchType.
1701
1701
*
@@ -1903,6 +1903,20 @@ class DocSearch {
1903
1903
paths [ i ] = { ty, name, path, exactPath, unboxFlag } ;
1904
1904
}
1905
1905
1906
+ // Throttlers are used to yield to the JavaScript event loop
1907
+ // while this is being built.
1908
+ // They're generated up-front to avoid the "nesting level"
1909
+ // limit that limits our speed to 4ms per tick.
1910
+ // https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html
1911
+ const throttlers = [ ] ;
1912
+ len = itemTypes . length ;
1913
+ for ( let i = 0 ; i < len ; ++ i ) {
1914
+ if ( ( i & 0xFF ) === 0 ) { // 256 - 1
1915
+ throttlers . push ( new Promise ( resolve => {
1916
+ setTimeout ( resolve , 0 ) ;
1917
+ } ) ) ;
1918
+ }
1919
+ }
1906
1920
// convert `item*` into an object form, and construct word indices.
1907
1921
//
1908
1922
// before any analysis is performed lets gather the search terms to
@@ -1915,6 +1929,9 @@ class DocSearch {
1915
1929
let lastName = "" ;
1916
1930
let lastWord = "" ;
1917
1931
for ( let i = 0 ; i < len ; ++ i ) {
1932
+ if ( ( i & 0xFF ) === 0 ) { // 256 - 1
1933
+ await throttlers [ i >> 8 ] ;
1934
+ }
1918
1935
const bitIndex = i + 1 ;
1919
1936
if ( descIndex >= descShard . len &&
1920
1937
! this . searchIndexEmptyDesc . get ( crate ) . contains ( bitIndex ) ) {
@@ -4864,17 +4881,19 @@ function updateCrate(ev) {
4864
4881
search ( true ) ;
4865
4882
}
4866
4883
4867
- function initSearch ( searchIndx ) {
4884
+ async function initSearch ( searchIndx ) {
4868
4885
rawSearchIndex = searchIndx ;
4869
4886
if ( typeof window !== "undefined" ) {
4870
- docSearch = new DocSearch ( rawSearchIndex , ROOT_PATH , searchState ) ;
4887
+ docSearch = new DocSearch ( ROOT_PATH , searchState ) ;
4888
+ docSearch . searchIndex = await docSearch . buildIndex ( rawSearchIndex ) ;
4871
4889
registerSearchEvents ( ) ;
4872
4890
// If there's a search term in the URL, execute the search now.
4873
4891
if ( window . searchState . getQueryStringParams ( ) . search !== undefined ) {
4874
4892
search ( ) ;
4875
4893
}
4876
4894
} else if ( typeof exports !== "undefined" ) {
4877
- docSearch = new DocSearch ( rawSearchIndex , ROOT_PATH , searchState ) ;
4895
+ docSearch = new DocSearch ( ROOT_PATH , searchState ) ;
4896
+ docSearch . searchIndex = await docSearch . buildIndex ( rawSearchIndex ) ;
4878
4897
exports . docSearch = docSearch ;
4879
4898
exports . parseQuery = DocSearch . parseQuery ;
4880
4899
}
0 commit comments