mirror of
https://github.com/ThatGuySam/doesitarm.git
synced 2026-05-18 06:44:46 -07:00
Add StorkClient helper class
This commit is contained in:
parent
990bcccb6b
commit
4bef634a9b
1 changed files with 73 additions and 0 deletions
73
helpers/stork/browser.js
Normal file
73
helpers/stork/browser.js
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
|
||||||
|
export class StorkClient {
|
||||||
|
constructor ( options = {} ) {
|
||||||
|
|
||||||
|
this.name = options.name
|
||||||
|
this.url = options.url
|
||||||
|
this.config = options.config
|
||||||
|
|
||||||
|
// Stork instance
|
||||||
|
this.stork = options.stork
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
setupState = 'not-setup'
|
||||||
|
|
||||||
|
get isSetup () {
|
||||||
|
return this.setupState === 'complete'
|
||||||
|
}
|
||||||
|
|
||||||
|
search ( query ) {
|
||||||
|
if ( !this.isSetup ) throw new Error('Not setup')
|
||||||
|
|
||||||
|
return this.stork.search( this.name, query )
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loads the Stork executables into the browser.
|
||||||
|
async lazyQuery ( query ) {
|
||||||
|
if ( !this.isSetup ) await this.setup()
|
||||||
|
|
||||||
|
return this.search( query )
|
||||||
|
}
|
||||||
|
|
||||||
|
waitForSetup () {
|
||||||
|
return new Promise( resolve => {
|
||||||
|
if ( this.isSetup ) resolve()
|
||||||
|
|
||||||
|
// Start timer to check for setup
|
||||||
|
const timer = setInterval( () => {
|
||||||
|
if ( this.isSetup ) {
|
||||||
|
clearInterval( timer )
|
||||||
|
resolve()
|
||||||
|
}
|
||||||
|
}, 50 )
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://github.com/jameslittle230/stork/blob/ff49f163db06734e18ab690c188b45a3c68442ae/js/main.ts#L40
|
||||||
|
async setup () {
|
||||||
|
// Prevent multiple setups
|
||||||
|
if ( this.setupState !== 'not-setup' ) {
|
||||||
|
await waitForSetup()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const {
|
||||||
|
initialize,
|
||||||
|
downloadIndex,
|
||||||
|
} = this.stork
|
||||||
|
|
||||||
|
const initPromise = initialize()
|
||||||
|
const downloadPromise = downloadIndex( this.name, this.url, this.config )
|
||||||
|
|
||||||
|
// This silly `then` call turns a [(void), (void)] into a (void), which is
|
||||||
|
// only necessary to make Typescript happy.
|
||||||
|
// You begin to wonder if you write Typescript code, or if Typescript code writes you.
|
||||||
|
await Promise.all([initPromise, downloadPromise]).then()
|
||||||
|
|
||||||
|
// Mark setup as complete
|
||||||
|
this.setupState = 'complete'
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue