-
Notifications
You must be signed in to change notification settings - Fork 9
/
actions.js
112 lines (90 loc) · 4.34 KB
/
actions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import invariant from 'invariant'
import { startBlueprint, stopBlueprint, resetIdleStatusBlueprint, activityBlueprint, activityDetectionBlueprint } from './blueprints'
import { IS_DEV, IDLESTATUS_ACTIVE, USER_ACTIVE, NEXT_IDLE_STATUS, RESET_IDLE_STATUS } from './constants'
import localsync from 'localsync'
const STOP_TYPES = [ 'pointermove', 'MSPointerMove' ]
const FILTER_TYPES = [ 'mousemove' ]
const isBrowser = () => typeof window === 'object'
/** Detects whether the activity should trigger a redux update */
const createShouldActivityUpdate = ({ log, thresholds }) => store => ({ type, pageX, pageY }) => {
if(STOP_TYPES.indexOf(type) !== -1)
return false
if(!FILTER_TYPES.indexOf(type) !== -1)
return true
/** If last event was not the same event type, trigger an update. */
const { lastActive, lastEvent } = selectIdleState(store.getState())
if(lastEvent.type !== type)
return true
/** If last mouse events coordinates were not within mouse threshold, trigger an update. */
const { x, y } = lastEvent
if((pageX && pageY && x && y) && Math.abs(pageX - x) < thresholds.mouse && Math.abs(pageY - y) < thresholds.mouse)
return false
return true
}
const selectIdleState = state => {
const { idle } = state
if(IS_DEV) {
invariant(idle, 'idle monitor state should have idle value')
invariant(typeof idle === 'object', 'idle monitor state should have type object')
}
return idle
}
const isRunning = (dispatch, getState) => {
const { isDetectionRunning } = selectIdleState(getState())
if(IS_DEV) {
invariant(isDetectionRunning, 'idle monitor state should have isDetectionRunning defined')
invariant(typeof isDetectionRunning === 'boolean', 'isDetectionRunning should be type boolean')
}
return isDetectionRunning
}
const createLocalSync = ({ log, activity, getIsTransition }) => store => {
const action = isActive => {
if(isActive)
return { isActive, lastActive: Date.now() }
else
return { isActive }
}
const handler = (value, old, url) => {
log.info({ value, old, url }, 'local sync')
if(value.isActive)
store.dispatch(activity({ type: 'local', isTransition: getIsTransition() }))
}
return localsync('idlemonitor', action, handler)
}
const createActivityDetection = ({ log, thresholds, activeEvents, activity, activityDetection, getIsTransition }) => store => {
const { dispatch } = store
const shouldActivityUpdate = createShouldActivityUpdate({ log, thresholds })(store)
/** One of the event listeners triggered an activity occurrence event. This gets spammed */
const onActivity = e => {
if (!shouldActivityUpdate(e))
return
dispatch(activity({ x: e.pageX, y: e.pageY, type: e.type, isTransition: getIsTransition() }))
}
const startActivityDetection = () => {
if(isBrowser()) activeEvents.forEach(x => document.addEventListener(x, onActivity))
dispatch(activityDetection(true))
}
const stopActivityDetection = () => {
if(isBrowser()) activeEvents.forEach(x => document.removeEventListener(x, onActivity))
dispatch(activityDetection(false))
}
return { startActivityDetection, stopActivityDetection }
}
export const createDetection = ({ log, activeEvents, thresholds, translateBlueprints }) => store => {
const { activity
, activityDetection
} = translateBlueprints({ activity: activityBlueprint
, activityDetection: activityDetectionBlueprint
})
const getIsTransition = () => selectIdleState(store.getState()).idleStatus !== IDLESTATUS_ACTIVE
const { startActivityDetection, stopActivityDetection } = createActivityDetection({ log, thresholds, activeEvents, activity, activityDetection, getIsTransition })(store)
const localSync = createLocalSync({ log, activity, getIsTransition })(store)
invariant(startActivityDetection, 'startActivityDetection should be a return property of createActivityDetection')
invariant(stopActivityDetection, 'stopActivityDetection should be a return property of createActivityDetection')
invariant(localSync, 'localSync should exist')
invariant(localSync.start, 'localSync.start should exist')
invariant(localSync.stop, 'localSync.stop should exist')
invariant(localSync.trigger, 'localSync.trigger should exist')
log.info('activity detection starting')
return { startActivityDetection, stopActivityDetection, localSync }
}