Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dawsmith06 committed Jul 18, 2020
1 parent 52556fd commit af1dc92
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 18 deletions.
127 changes: 111 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,117 @@

const TabsManager = (function(){
return {
setup(){
this.tabs = this.getTabs();
this.tab = btoa(new Date());
this.addTab();
window.addEventListener('beforeunload', () =>{ this.removeTab() });
window.addEventListener('focus', () =>{ this.focusTab() });
window.addEventListener('blur', () =>{ this.unfocusTab() });
},

getTabs(){
return JSON.parse(localStorage.getItem('TabsManager') || JSON.stringify({open : []}));
},

getTab(){
let index = this.getTabs().open.findIndex((t)=> t.id == this.tab);
return this.getTabs().open[index];
},

addTab(){
this.tabs.open.forEach((t) => t.lastFocus = false);
this.tabs.open.push({
id : this.tab,
focus : true,
lastFocus : true,
href : location.href,
});
this.update();
},

removeTab(){
let index = this.tabs.open.findIndex((t)=> t.id == this.tab);
this.tabs.open.splice(index,1);
this.update();
},

focusTab(){
this.tabs = this.getTabs();
this.tabs.open.forEach((t) => t.lastFocus = false);
let index = this.tabs.open.findIndex((t)=> t.id == this.tab);
this.tabs.open[index].focus = true;
this.tabs.open[index].lastFocus = true;
this.update();
},

unfocusTab(){
this.tabs = this.getTabs();
let index = this.tabs.open.findIndex((t)=> t.id == this.tab);
this.tabs.open[index].focus = false;
this.update();
},

update(){
localStorage.setItem('TabsManager',JSON.stringify(this.tabs));
},

clearAll(){
this.tabs = {open : []};
this.update();
}
};
}());
TabsManager.setup();

const idle = (function(){
return {
start(opts) {
this.options = {};
this.options.timeout = opts.timeout || 40;
this.options.inactive = opts.inactive || 5;
this.time = 0;
this.idle = "active";
this.options = {};
this.options.timeout = opts.timeout || 40;
this.options.inactive = opts.inactive || 5;
this.idle = "active";
localStorage.idleTime = 0;
this.listenChannel();
this.startTimer();
this.setEvents();
},

listenChannel(){
let channel = new BroadcastChannel(`dw-idle-handler`);
channel.onmessage = ((e)=>{
if(e.data == "destroyTimers"){
this.destroyTimer();
}
});
},

startTimer(){
this._timer = setInterval(() =>{
this.time++;
this.onTimerChange();
} , 60000);
if(!this._timer){
this._timer = setInterval(() =>{
localStorage.idleTime = this.currentTime() + 1;
this.onTimerChange();
} , 60000);
}
},

destroyTimer(){
this.tab = TabsManager.getTab();
if(this._timer != null && this.tab.lastFocus == true != this.tab.focus == false && TabsManager.getTabs().open.length > 1){
this._timer = null
clearInterval(this._timer);
}
},

currentTime(){
return parseInt(localStorage.idleTime);
},

onTimerChange(){
if(this.time == this.options.timeout){
if(this.currentTime() == this.options.timeout){
this.notify("timeout");
}
else if(this.time >= this.options.inactive && this.idle == "active"){
else if(this.currentTime() >= this.options.inactive && this.idle == "active"){
this.notify("inactive");
}
},
Expand All @@ -30,14 +120,19 @@ const idle = (function(){
window.addEventListener('click', () =>{ this.onWindowInteraction() });
window.addEventListener('keypress', () =>{ this.onWindowInteraction() });
window.addEventListener('mouseover', () =>{ this.onWindowInteraction() });
window.addEventListener('touchstart', () =>{ this.onWindowInteraction() });
window.addEventListener('focus', () =>{ this.startTimer() });

window.addEventListener('blur', () =>{
let channel = new BroadcastChannel(`dw-idle-handler`);
channel.postMessage("destroyTimers");
});
},

onWindowInteraction(){
if(this.idle == "inactive"){
this.notify("active");
}
this.time = 0;
localStorage.idleTime = 0;
},

notify(state){
Expand All @@ -46,9 +141,10 @@ const idle = (function(){
channel.postMessage({state : state});
this.idle = state;
},

on(name,callback){
let channel = new BroadcastChannel(`on${name}`);
let Name = name.charAt(0).toUpperCase() + name.substring(1);
let channel = new BroadcastChannel(`on${Name}`);
channel.onmessage = ((e) =>{
callback(e);
});
Expand All @@ -57,4 +153,3 @@ const idle = (function(){
}());

module.exports = idle;

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dw-idle-handler",
"version": "0.1.0",
"version": "0.1.6",
"description": "Manage window inactivity",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ idleHandler.on("active",()=>{
//Detected user interaction by one of this events : click, keypress, mouseover and touchstart
});
```
### For previous versions <= 0.0.9
## Note
Thanks to the Tabs Manager package and local storage the library can has a persistent timer
even if there are multiple browser tabs the timer will be the same for each window only alowing one lnterval timer for the active tab or the last active tab, anyway when a tab has passed timeout all tabs will be notified with the timeout event

## For previous versions <= 0.0.9
```javascript
const idleHandler = require('dw-idle-handler');
idleHandler.start({
Expand Down

0 comments on commit af1dc92

Please sign in to comment.