forked from npm/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.worker.js
More file actions
32 lines (29 loc) · 1 KB
/
search.worker.js
File metadata and controls
32 lines (29 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import Fuse from 'fuse.js'
import debounce from 'lodash.debounce'
(function searchWorker () {
let fuse = null
// [MKT]: I landed on the debouce wait value of 50 based mostly on
// experimentation. With both `leading` and `trailing` set to `true`, this
// feels pretty snappy.
//
// From https://lodash.com/docs/#debounce:
//
// > Note: If `leading` and `trailing` options are `true`, `func` is invoked
// > on the trailing edge of the timeout only if the debounced function is
// > invoked more than once during the wait timeout.
const performSearch = debounce(function performSearch (query) {
const results = fuse.search(query).slice(0, 20)
postMessage({ results: results, query: query })
}, 50, { leading: true, trailing: true })
onmessage = function ({ data }) {
if (data.list) {
fuse = new Fuse(data.list, {
threshold: 0.2,
keys: ['title', 'rawBody'],
tokenize: true,
})
} else if (data.query) {
performSearch(data.query)
}
}
})()