mirror of
https://github.com/ThatGuySam/doesitarm.git
synced 2026-05-18 06:44:46 -07:00
Enable cancelling lazy query
This commit is contained in:
parent
ef2c9222d1
commit
559a5c4e3d
1 changed files with 36 additions and 2 deletions
|
|
@ -77,6 +77,8 @@ export class StorkClient {
|
||||||
|
|
||||||
// Stork instance
|
// Stork instance
|
||||||
this.stork = options.stork || null
|
this.stork = options.stork || null
|
||||||
|
|
||||||
|
this.cancelCurrentQuery = null
|
||||||
}
|
}
|
||||||
|
|
||||||
setupState = 'not-setup'
|
setupState = 'not-setup'
|
||||||
|
|
@ -95,9 +97,41 @@ export class StorkClient {
|
||||||
// Loads the Stork WASM and Index into the browser on first query
|
// Loads the Stork WASM and Index into the browser on first query
|
||||||
// so that we don't have to load them initially.
|
// so that we don't have to load them initially.
|
||||||
async lazyQuery ( query ) {
|
async lazyQuery ( query ) {
|
||||||
if ( !this.isSetup ) await this.setup()
|
|
||||||
|
|
||||||
return this.search( query )
|
// Sleep
|
||||||
|
// await new Promise( resolve => setTimeout( resolve, 50000000 ) )
|
||||||
|
|
||||||
|
const result = await new Promise( async ( resolve, reject ) => {
|
||||||
|
|
||||||
|
// If there an existing query to cancel
|
||||||
|
// then cancel it
|
||||||
|
// so that we don't race bad conditions
|
||||||
|
// such as earrly queries beating the final one
|
||||||
|
if ( this.cancelCurrentQuery !== null ) {
|
||||||
|
this.cancelCurrentQuery()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Plugin this promise to our cancel method
|
||||||
|
this.cancelCurrentQuery = () => { reject({ message: `Cancelled previous query for ${ query }`, canceled: true }) }
|
||||||
|
|
||||||
|
if ( !this.isSetup ) await this.setup()
|
||||||
|
|
||||||
|
// console.log('debounce', this.query)
|
||||||
|
|
||||||
|
const result = this.search( query )
|
||||||
|
|
||||||
|
// this.cancelCurrentQuery = null
|
||||||
|
|
||||||
|
resolve( result )
|
||||||
|
|
||||||
|
}).catch( err => {
|
||||||
|
console.log('Query rejected', err)
|
||||||
|
return null
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log( 'result', result )
|
||||||
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
waitForSetup () {
|
waitForSetup () {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue