/**
Keeps track of how many tabs a user has open at one time
@memberof module:Viki
**/
class TabCounter {
/**
@constructor
@param {Object} options
@param {Function} options.getCount - callback to retrieve current tab count
@param {Function} options.saveCount(n) - callback to update tab count. Should take in a number.
**/
constructor(options) {
this.options = options;
this.beforePageUnload = this.beforePageUnload.bind(this);
// load previous tab count
this.count = this.getCount();
// increment count and save
this.saveCount(this.count + 1);
// reduce tabs open before page unload
window.addEventListener('beforeunload', this.beforePageUnload);
}
/**
@returns {Number} number of tabs open
**/
getCount() {
return this.options.getCount();
}
/**
@param {Number} count - number of tabs to save count
**/
saveCount(count) {
this.count = count;
this.options.saveCount(count);
}
beforePageUnload() {
this.saveCount(this.count - 1);
}
}
module.exports = TabCounter;