-
-
Notifications
You must be signed in to change notification settings - Fork 844
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #852 from binhodev/dev
Dev - IdleState
- Loading branch information
Showing
7 changed files
with
115 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const { EventEmitter } = require('node:events') | ||
|
||
class IdleState extends EventEmitter { | ||
timer = null | ||
startTime = 0 | ||
endTime = 0 | ||
|
||
setIdleTime = (timeInSeconds) => { | ||
this.startTime = timeInSeconds | ||
return this.reset() | ||
} | ||
|
||
startTimer = () => { | ||
this.timer = setInterval(() => { | ||
const currentTime = new Date().getTime() | ||
|
||
if (currentTime > this.endTime) { | ||
this.stop() | ||
this.emit('idle') | ||
} else { | ||
this.debugTime() | ||
} | ||
}, 1000) | ||
} | ||
|
||
start = () => { | ||
if (!this.timer) { | ||
this.reset() | ||
this.startTimer() | ||
} | ||
} | ||
|
||
reset = () => { | ||
const currentTime = new Date().getTime() | ||
this.endTime = currentTime + this.startTime * 1000 | ||
} | ||
|
||
stop = () => { | ||
if (this.timer) { | ||
clearInterval(this.timer) | ||
this.timer = null | ||
} | ||
} | ||
|
||
debugTime = () => { | ||
const currentTime = new Date().getTime() | ||
return `Tiempo restante: ${((this.endTime - currentTime) / 1000).toFixed(0)} segundos` | ||
} | ||
} | ||
|
||
module.exports = IdleState |
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
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
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
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
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
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