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