chainInfo from server, routes, wallet rework
This commit is contained in:
35
src/misc.js
Normal file
35
src/misc.js
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
export class SingletonCoroutine {
|
||||
constructor(f, delay=10, retry=true) {
|
||||
this.f = f
|
||||
this.delay = delay
|
||||
this.timeout = null
|
||||
this.args = null
|
||||
}
|
||||
|
||||
pending() {
|
||||
return this.timeout !== null
|
||||
}
|
||||
|
||||
invoke(/*arguments*/) {
|
||||
this.args = arguments
|
||||
if( this.timeout !== null )
|
||||
clearTimeout(this.timeout)
|
||||
this.timeout = setTimeout(this.onTimeout, this.delay, this)
|
||||
}
|
||||
|
||||
async onTimeout(self) {
|
||||
self.timeout = null
|
||||
console.log('invoking', self.f, self.args)
|
||||
try {
|
||||
await self.f(...self.args)
|
||||
}
|
||||
catch (e) {
|
||||
if( self.retry )
|
||||
self.invoke(self.args)
|
||||
else
|
||||
console.error(e)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user