This repository has been archived by the owner on Jun 1, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standardize event system, build a user activity monitor
- Loading branch information
Showing
4 changed files
with
146 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/resources/javascript/modules/logic/activity-monitor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
var HexEvent = require("./events.js"), | ||
activityEventName = "user.activity", | ||
inactivityEventName = "user.inactivity"; | ||
|
||
function ActivityMonitor(hexEvent, limit) { | ||
this.hexEvent = hexEvent; | ||
this.limitMilliseconds = limit || 60 * 1000; | ||
|
||
this.checkingIntervalMilliseconds = Math.floor(this.limitMilliseconds / 2); | ||
this.latestActivityTimestamp = null; | ||
this.activityInterval = null; | ||
this.isMonitorStarted = false; | ||
this.isUserIsActive = false; | ||
} | ||
|
||
ActivityMonitor.prototype.getTimestamp = function() { | ||
return new Date().valueOf(); | ||
}; | ||
|
||
ActivityMonitor.prototype.resetActivityInterval = function() { | ||
this.latestActivityTimestamp = this.getTimestamp(); | ||
}; | ||
|
||
ActivityMonitor.prototype.startActivityInterval = function() { | ||
this.activityInterval = setInterval(this.checkActivityInterval.bind(this), this.checkingIntervalMilliseconds); | ||
}; | ||
|
||
ActivityMonitor.prototype.stopActivityInterval = function() { | ||
clearInterval(this.activityInterval); | ||
|
||
this.activityInterval = null; | ||
}; | ||
|
||
ActivityMonitor.prototype.checkActivityInterval = function() { | ||
if (Math.abs(this.getTimestamp() - this.latestActivityTimestamp) > this.limitMilliseconds) { | ||
this.inactivityDetected(); | ||
} | ||
}; | ||
|
||
ActivityMonitor.prototype.activityDetected = function() { | ||
this.isUserIsActive = true; | ||
this.resetActivityInterval(); | ||
|
||
if (this.activityInterval === null) { | ||
this.startActivityInterval(); | ||
} | ||
|
||
this.hexEvent.fire(activityEventName); | ||
}; | ||
|
||
ActivityMonitor.prototype.inactivityDetected = function() { | ||
this.stopActivityInterval(); | ||
this.isUserIsActive = false; | ||
|
||
this.hexEvent.fire(inactivityEventName); | ||
}; | ||
|
||
ActivityMonitor.prototype.isStarted = function() { | ||
return this.isMonitorStarted === true; | ||
}; | ||
|
||
ActivityMonitor.prototype.isUserIsActive = function() { | ||
return this.isUserIsActive === true; | ||
}; | ||
|
||
ActivityMonitor.prototype.start = function() { | ||
if (this.isStarted()) { | ||
throw new Error("Was already started."); | ||
} | ||
|
||
this.resetActivityInterval(); | ||
this.startActivityInterval(); | ||
|
||
// TODO: use hexagonif triggered events? | ||
document.addEventListener("mousemove", this.activityDetected.bind(this)); | ||
}; | ||
|
||
ActivityMonitor.prototype.stop = function() { | ||
if (this.isStarted()) { | ||
throw new Error("Was not started."); | ||
} | ||
|
||
document.removeEventListener("mousemove", this.activityDetected.bind(this)); | ||
|
||
this.stopActivityInterval(); | ||
}; | ||
|
||
module.exports = ActivityMonitor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
function HexEvents(canvasElement, namespacePrefix) { | ||
this.canvasElement = canvasElement; | ||
this.namespacePrefix = namespacePrefix || 'hexagonif.'; | ||
} | ||
|
||
HexEvents.prototype.getEventName = function(name) { | ||
return this.namespacePrefix + name; | ||
} | ||
|
||
HexEvents.prototype.fire = function(name, graphic, object) { | ||
var event = document.createEvent('HTMLEvents'), | ||
namespacedName = this.getEventName(name); | ||
|
||
event.initEvent(namespacedName, true, true); | ||
event.graphic = graphic; | ||
event.object = object; | ||
return this.canvasElement.dispatchEvent(event); | ||
} | ||
|
||
HexEvents.prototype.listen = function(name, fn) { | ||
var namespacedName = this.getEventName(name); | ||
|
||
this.canvasElement.addEventListener(namespacedName, fn); | ||
} | ||
|
||
HexEvents.prototype.cancel = function(name, fn) { | ||
var namespacedName = this.getEventName(name); | ||
|
||
this.canvasElement.removeEventListener(namespacedName, fn); | ||
} | ||
|
||
module.exports = HexEvents; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters