aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/delayApp/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/features/delayApp/index.js')
-rw-r--r--src/features/delayApp/index.js67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/features/delayApp/index.js b/src/features/delayApp/index.js
new file mode 100644
index 000000000..a3cce03ee
--- /dev/null
+++ b/src/features/delayApp/index.js
@@ -0,0 +1,67 @@
1import { autorun, observable, reaction } from 'mobx';
2import moment from 'moment';
3import DelayAppComponent from './Component';
4
5const debug = require('debug')('Franz:feature:delayApp');
6
7const DEFAULT_DELAY_DURATION = 10000; // 10 seconds
8const DEFAULT_DELAY_OFFSET = 3600000; // 1 hour
9const DEFAULT_VISIBILITY = false;
10
11export const config = {
12 delayOffset: DEFAULT_DELAY_OFFSET,
13 delayDuration: DEFAULT_DELAY_DURATION,
14};
15
16export const state = observable({
17 isDelayAppScreenVisible: DEFAULT_VISIBILITY,
18});
19
20function setVisibility(value) {
21 Object.assign(state, {
22 isDelayAppScreenVisible: value,
23 });
24}
25
26export default function init(stores) {
27 reaction(
28 () => stores.features.features.needToWaitToProceed,
29 (enabled, r) => {
30 if (enabled) {
31 debug('Initializing `delayApp` feature');
32
33 // Dispose the reaction to run this only once
34 r.dispose();
35
36 const { needToWaitToProceedConfig: globalConfig } = stores.features.features;
37
38 let shownAfterLaunch = false;
39 let timeLastDelay = moment();
40
41 config.delayOffset = globalConfig.delayOffset || DEFAULT_DELAY_OFFSET;
42 config.delayDuration = globalConfig.wait || DEFAULT_DELAY_DURATION;
43
44 autorun(() => {
45 const diff = moment().diff(timeLastDelay);
46 if ((stores.app.isFocused && diff >= config.delayOffset) || !shownAfterLaunch) {
47 debug(`App will be delayed for ${config.delayDuration / 1000}s`);
48
49 setVisibility(true);
50
51 timeLastDelay = moment();
52 shownAfterLaunch = true;
53
54 setTimeout(() => {
55 debug('Resetting app delay');
56
57 setVisibility(false);
58 }, DEFAULT_DELAY_DURATION + 1000); // timer needs to be able to hit 0
59 }
60 });
61 }
62 },
63 );
64}
65
66export const component = DelayAppComponent;
67