| 1 |
import { useExternalListener } from "@odoo/owl"; |
| 2 |
|
| 3 |
const UserPresenceEvents = ["mousemove", "mousedown", "touchmove", "click", "scroll", "keypress"]; |
| 4 |
|
| 5 |
export function useIdleTimer(steps, onAlive) { |
| 6 |
const state = { |
| 7 |
timeout: new Set(steps.map((s) => s.timeout)), |
| 8 |
idle: false, |
| 9 |
time: 0, |
| 10 |
}; |
| 11 |
|
| 12 |
const checkSteps = () => { |
| 13 |
for (const step of steps) { |
| 14 |
if (step.timeout === state.time * 1000 && !state.idle) { |
| 15 |
state.idle = step.action(); |
| 16 |
} |
| 17 |
} |
| 18 |
}; |
| 19 |
|
| 20 |
const onMove = (ev) => { |
| 21 |
if (state.idle) { |
| 22 |
state.idle = onAlive(ev); |
| 23 |
} |
| 24 |
state.time = 0; |
| 25 |
}; |
| 26 |
|
| 27 |
for (const event of UserPresenceEvents) { |
| 28 |
useExternalListener(window, event, onMove); |
| 29 |
} |
| 30 |
|
| 31 |
setInterval(() => { |
| 32 |
state.time++; |
| 33 |
if (state.timeout.has(state.time * 1000)) { |
| 34 |
checkSteps(); |
| 35 |
} |
| 36 |
}, 1000); |
| 37 |
|
| 38 |
return state; |
| 39 |
} |