Optional debounce for events.

This commit is contained in:
Julien Fontanet
2016-04-14 17:55:41 +02:00
parent eac29993d3
commit 03eaa652ce
2 changed files with 15 additions and 4 deletions

View File

@@ -41,6 +41,7 @@ const main = coroutine(function * (args) {
boolean: ['help', 'read-only', 'verbose'],
alias: {
debounce: 'd',
help: 'h',
'read-only': 'ro',
verbose: 'v'
@@ -74,6 +75,7 @@ const main = coroutine(function * (args) {
const xapi = createClient({
url,
auth: { user, password },
debounce: opts.debounce != null ? +opts.debounce : null,
readOnly: opts.ro
})
yield xapi.connect()

View File

@@ -207,6 +207,9 @@ export class Xapi extends EventEmitter {
const objects = this._objects = new Collection()
objects.getKey = getKey
this._debounce = opts.debounce == null
? 200
: opts.debounce
this._fromToken = ''
this.on('connected', this._watchEvents)
this.on('disconnected', () => {
@@ -556,6 +559,8 @@ export class Xapi extends EventEmitter {
}
_watchEvents () {
const debounce = this._debounce
const loop = ((onSucess, onFailure) => {
return () => this._sessionCall('event.from', [
['*'],
@@ -567,7 +572,9 @@ export class Xapi extends EventEmitter {
this._fromToken = token
this._processEvents(events)
return loop()
return debounce != null
? Bluebird.delay(debounce).then(loop)
: loop()
},
error => {
if (areEventsLost(error)) {
@@ -636,10 +643,12 @@ export class Xapi extends EventEmitter {
const loop = ((onSuccess, onFailure) => {
return () => this._sessionCall('event.next', []).then(onSuccess, onFailure)
})(
events => {
(debounce => events => {
this._processEvents(events)
return loop()
},
return debounce == null
? loop()
: Bluebird.delay(debounce).then(loop)
})(this._debounce),
error => {
if (areEventsLost(error)) {
return this._sessionCall('event.unregister', [ ['*'] ]).then(watchEvents)