Browse Source

[RN] Legacy support of calendar-sync's knownDomains

Knowledge is power, man!

We moved "knownDomains" from calendar-sync to base/known-domains.
However, we do have an official release in the app stores and I'd like
us to not throw away the knowledge it has acquired.
j8
Lyubo Marinov 7 years ago
parent
commit
631f51d627
2 changed files with 68 additions and 19 deletions
  1. 47
    15
      react/features/calendar-sync/middleware.js
  2. 21
    4
      react/features/calendar-sync/reducer.js

+ 47
- 15
react/features/calendar-sync/middleware.js View File

@@ -3,7 +3,7 @@
3 3
 import RNCalendarEvents from 'react-native-calendar-events';
4 4
 
5 5
 import { APP_WILL_MOUNT } from '../app';
6
-import { ADD_KNOWN_DOMAINS } from '../base/known-domains';
6
+import { ADD_KNOWN_DOMAINS, addKnownDomains } from '../base/known-domains';
7 7
 import { MiddlewareRegistry } from '../base/redux';
8 8
 import { APP_LINK_SCHEME, parseURIString } from '../base/util';
9 9
 import { APP_STATE_CHANGED } from '../mobile/background';
@@ -31,24 +31,58 @@ const MAX_LIST_LENGTH = 10;
31 31
 
32 32
 CALENDAR_ENABLED
33 33
     && MiddlewareRegistry.register(store => next => action => {
34
-        const result = next(action);
35
-
36 34
         switch (action.type) {
37
-        case ADD_KNOWN_DOMAINS:
38
-        case APP_WILL_MOUNT:
39
-            _fetchCalendarEntries(store, false, false);
40
-            break;
35
+        case ADD_KNOWN_DOMAINS: {
36
+            // XXX Fetch new calendar entries only when an actual domain has
37
+            // become known.
38
+            const { getState } = store;
39
+            const oldValue = getState()['features/base/known-domains'];
40
+            const result = next(action);
41
+            const newValue = getState()['features/base/known-domains'];
42
+
43
+            oldValue === newValue || _fetchCalendarEntries(store, false, false);
44
+
45
+            return result;
46
+        }
47
+
48
+        case APP_STATE_CHANGED: {
49
+            const result = next(action);
41 50
 
42
-        case APP_STATE_CHANGED:
43 51
             _maybeClearAccessStatus(store, action);
44
-            break;
45 52
 
46
-        case REFRESH_CALENDAR:
53
+            return result;
54
+        }
55
+
56
+        case APP_WILL_MOUNT: {
57
+            // For legacy purposes, we've allowed the deserialization of
58
+            // knownDomains and now we're to translate it to base/known-domains.
59
+            const state = store.getState()['features/calendar-sync'];
60
+
61
+            if (state) {
62
+                const { knownDomains } = state;
63
+
64
+                Array.isArray(knownDomains)
65
+                    && knownDomains.length
66
+                    && store.dispatch(addKnownDomains(knownDomains));
67
+            }
68
+
69
+            const result = next(action);
70
+
71
+            _fetchCalendarEntries(store, false, false);
72
+
73
+            return result;
74
+        }
75
+
76
+        case REFRESH_CALENDAR: {
77
+            const result = next(action);
78
+
47 79
             _fetchCalendarEntries(store, true, action.forcePermission);
48
-            break;
80
+
81
+            return result;
82
+        }
49 83
         }
50 84
 
51
-        return result;
85
+        return next(action);
52 86
     });
53 87
 
54 88
 /**
@@ -122,9 +156,7 @@ function _fetchCalendarEntries(
122 156
                 logger.warn('Calendar access not granted.');
123 157
             }
124 158
         })
125
-        .catch(reason => {
126
-            logger.error('Error accessing calendar.', reason);
127
-        });
159
+        .catch(reason => logger.error('Error accessing calendar.', reason));
128 160
 }
129 161
 
130 162
 /**

+ 21
- 4
react/features/calendar-sync/reducer.js View File

@@ -1,6 +1,8 @@
1 1
 // @flow
2 2
 
3
-import { ReducerRegistry } from '../base/redux';
3
+import { APP_WILL_MOUNT } from '../app';
4
+import { ReducerRegistry, set } from '../base/redux';
5
+import { PersistenceRegistry } from '../base/storage';
4 6
 
5 7
 import {
6 8
     SET_CALENDAR_AUTHORIZATION,
@@ -20,9 +22,25 @@ const DEFAULT_STATE = {
20 22
 
21 23
 const STORE_NAME = 'features/calendar-sync';
22 24
 
25
+// XXX For legacy purposes, read any {@code knownDomains} persisted by the
26
+// feature calendar-sync.
27
+CALENDAR_ENABLED
28
+    && PersistenceRegistry.register(STORE_NAME, {
29
+        knownDomains: true
30
+    });
31
+
23 32
 CALENDAR_ENABLED
24 33
     && ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => {
25 34
         switch (action.type) {
35
+        case APP_WILL_MOUNT:
36
+            // For legacy purposes, we've allowed the deserialization of
37
+            // knownDomains. At this point, it should have already been
38
+            // translated into the new state format (namely, base/known-domains)
39
+            // and the app no longer needs it.
40
+            if (typeof state.knownDomains !== 'undefined') {
41
+                return set(state, 'knownDomains', undefined);
42
+            }
43
+            break;
26 44
 
27 45
         case SET_CALENDAR_AUTHORIZATION:
28 46
             return {
@@ -35,8 +53,7 @@ CALENDAR_ENABLED
35 53
                 ...state,
36 54
                 events: action.events
37 55
             };
38
-
39
-        default:
40
-            return state;
41 56
         }
57
+
58
+        return state;
42 59
     });

Loading…
Cancel
Save