|
1 | 1 | /* @flow strict-local */
|
2 | 2 | import Immutable from 'immutable';
|
3 |
| -import { createSelector } from 'reselect'; |
4 | 3 |
|
5 | 4 | import type { Action } from '../actionTypes';
|
6 | 5 | import type {
|
7 | 6 | UnreadState,
|
| 7 | + UnreadStreamsState, |
8 | 8 | UnreadPmsState,
|
9 | 9 | UnreadHuddlesState,
|
10 | 10 | UnreadMentionsState,
|
11 | 11 | } from './unreadModelTypes';
|
12 | 12 | import type { GlobalState } from '../reduxTypes';
|
13 |
| -import type { Selector } from '../types'; |
14 |
| -import unreadStreamsReducer from './unreadStreamsReducer'; |
15 | 13 | import unreadPmsReducer from './unreadPmsReducer';
|
16 | 14 | import unreadHuddlesReducer from './unreadHuddlesReducer';
|
17 | 15 | import unreadMentionsReducer from './unreadMentionsReducer';
|
| 16 | +import { |
| 17 | + ACCOUNT_SWITCH, |
| 18 | + EVENT_MESSAGE_DELETE, |
| 19 | + EVENT_NEW_MESSAGE, |
| 20 | + EVENT_UPDATE_MESSAGE_FLAGS, |
| 21 | + LOGOUT, |
| 22 | + MESSAGE_FETCH_COMPLETE, |
| 23 | + REALM_INIT, |
| 24 | +} from '../actionConstants'; |
| 25 | +import { getOwnUserId } from '../users/userSelectors'; |
18 | 26 |
|
19 |
| -export const getUnreadStreams: Selector< |
20 |
| - Immutable.Map<number, Immutable.Map<string, Immutable.List<number>>>, |
21 |
| -> = createSelector( |
22 |
| - state => state.unread.streams, |
23 |
| - data => { |
24 |
| - // prettier-ignore |
25 |
| - // This is an example of the style-guide rule "Always provide a type |
26 |
| - // when writing an empty `Immutable` value". Without the explicit type, |
27 |
| - // `result` gets inferred as `Immutable.Map<number, empty>`, and then |
28 |
| - // e.g. the `setIn` call could be completely wrong and the type-checker |
29 |
| - // wouldn't notice. |
30 |
| - const result: Immutable.Map<number, Immutable.Map<string, Immutable.List<number>>> = |
31 |
| - Immutable.Map().asMutable(); |
32 |
| - for (const { stream_id, topic, unread_message_ids } of data) { |
33 |
| - result.setIn([stream_id, topic], Immutable.List(unread_message_ids)); |
34 |
| - } |
35 |
| - return result.asImmutable(); |
36 |
| - }, |
37 |
| -); |
| 27 | +// |
| 28 | +// |
| 29 | +// Selectors. |
| 30 | +// |
| 31 | + |
| 32 | +export const getUnreadStreams = (state: GlobalState): UnreadStreamsState => state.unread.streams; |
38 | 33 |
|
39 | 34 | export const getUnreadPms = (state: GlobalState): UnreadPmsState => state.unread.pms;
|
40 | 35 |
|
41 | 36 | export const getUnreadHuddles = (state: GlobalState): UnreadHuddlesState => state.unread.huddles;
|
42 | 37 |
|
43 | 38 | export const getUnreadMentions = (state: GlobalState): UnreadMentionsState => state.unread.mentions;
|
44 | 39 |
|
| 40 | +// |
| 41 | +// |
| 42 | +// Reducer. |
| 43 | +// |
| 44 | + |
| 45 | +const initialStreamsState: UnreadStreamsState = Immutable.Map(); |
| 46 | + |
| 47 | +// Like `Immutable.Map#map`, but with the update-only-if-different semantics |
| 48 | +// of `Immutable.Map#update`. Kept for comparison to `updateAllAndPrune`. |
| 49 | +/* eslint-disable-next-line no-unused-vars */ |
| 50 | +function updateAll<K, V>(map: Immutable.Map<K, V>, updater: V => V): Immutable.Map<K, V> { |
| 51 | + return map.withMutations(mapMut => { |
| 52 | + map.forEach((value, key) => { |
| 53 | + const newValue = updater(value); |
| 54 | + if (newValue !== value) { |
| 55 | + mapMut.set(key, newValue); |
| 56 | + } |
| 57 | + }); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +// Like `updateAll`, but prune values equal to `zero` given by `updater`. |
| 62 | +function updateAllAndPrune<K, V>( |
| 63 | + map: Immutable.Map<K, V>, |
| 64 | + zero: V, |
| 65 | + updater: V => V, |
| 66 | +): Immutable.Map<K, V> { |
| 67 | + return map.withMutations(mapMut => { |
| 68 | + map.forEach((value, key) => { |
| 69 | + const newValue = updater(value); |
| 70 | + if (newValue === value) { |
| 71 | + return; // i.e., continue |
| 72 | + } |
| 73 | + if (newValue === zero) { |
| 74 | + mapMut.delete(key); |
| 75 | + } else { |
| 76 | + mapMut.set(key, newValue); |
| 77 | + } |
| 78 | + }); |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +function deleteMessages( |
| 83 | + state: UnreadStreamsState, |
| 84 | + ids: $ReadOnlyArray<number>, |
| 85 | +): UnreadStreamsState { |
| 86 | + const idSet = new Set(ids); |
| 87 | + const toDelete = id => idSet.has(id); |
| 88 | + const emptyList = Immutable.List(); |
| 89 | + return updateAllAndPrune(state, Immutable.Map(), perStream => |
| 90 | + updateAllAndPrune(perStream, emptyList, perTopic => |
| 91 | + perTopic.find(toDelete) ? perTopic.filterNot(toDelete) : perTopic, |
| 92 | + ), |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +function streamsReducer( |
| 97 | + state: UnreadStreamsState = initialStreamsState, |
| 98 | + action: Action, |
| 99 | + globalState: GlobalState, |
| 100 | +): UnreadStreamsState { |
| 101 | + switch (action.type) { |
| 102 | + case LOGOUT: |
| 103 | + case ACCOUNT_SWITCH: |
| 104 | + // TODO(#4446) also LOGIN_SUCCESS, presumably |
| 105 | + return initialStreamsState; |
| 106 | + |
| 107 | + case REALM_INIT: { |
| 108 | + // This may indeed be unnecessary, but it's legacy; have not investigated |
| 109 | + // if it's this bit of our API types that is too optimistic. |
| 110 | + // flowlint-next-line unnecessary-optional-chain:off |
| 111 | + const data = action.data.unread_msgs?.streams ?? []; |
| 112 | + |
| 113 | + const st = initialStreamsState.asMutable(); |
| 114 | + for (const { stream_id, topic, unread_message_ids } of data) { |
| 115 | + // unread_message_ids is already sorted; see comment at its |
| 116 | + // definition in src/api/initialDataTypes.js. |
| 117 | + st.setIn([stream_id, topic], Immutable.List(unread_message_ids)); |
| 118 | + } |
| 119 | + return st.asImmutable(); |
| 120 | + } |
| 121 | + |
| 122 | + case MESSAGE_FETCH_COMPLETE: |
| 123 | + // TODO handle MESSAGE_FETCH_COMPLETE here. This rarely matters, but |
| 124 | + // could in principle: we could be fetching some messages from |
| 125 | + // before the (long) window included in the initial unreads data. |
| 126 | + // For comparison, the webapp does handle this case; see the call to |
| 127 | + // message_util.do_unread_count_updates in message_fetch.js. |
| 128 | + return state; |
| 129 | + |
| 130 | + case EVENT_NEW_MESSAGE: { |
| 131 | + const { message } = action; |
| 132 | + if (message.type !== 'stream') { |
| 133 | + return state; |
| 134 | + } |
| 135 | + |
| 136 | + if (message.sender_id === getOwnUserId(globalState)) { |
| 137 | + return state; |
| 138 | + } |
| 139 | + |
| 140 | + // prettier-ignore |
| 141 | + return state.updateIn([message.stream_id, message.subject], |
| 142 | + (perTopic = Immutable.List()) => perTopic.push(message.id)); |
| 143 | + } |
| 144 | + |
| 145 | + case EVENT_MESSAGE_DELETE: |
| 146 | + // TODO optimize by using `state.messages` to look up directly |
| 147 | + return deleteMessages(state, action.messageIds); |
| 148 | + |
| 149 | + case EVENT_UPDATE_MESSAGE_FLAGS: { |
| 150 | + if (action.flag !== 'read') { |
| 151 | + return state; |
| 152 | + } |
| 153 | + |
| 154 | + if (action.all) { |
| 155 | + return initialStreamsState; |
| 156 | + } |
| 157 | + |
| 158 | + if (action.operation === 'remove') { |
| 159 | + // Zulip doesn't support un-reading a message. Ignore it. |
| 160 | + return state; |
| 161 | + } |
| 162 | + |
| 163 | + // TODO optimize by using `state.messages` to look up directly. |
| 164 | + // Then when do, also optimize so deleting the oldest items is fast, |
| 165 | + // as that should be the common case here. |
| 166 | + return deleteMessages(state, action.messages); |
| 167 | + } |
| 168 | + |
| 169 | + default: |
| 170 | + return state; |
| 171 | + } |
| 172 | +} |
| 173 | + |
45 | 174 | export const reducer = (
|
46 | 175 | state: void | UnreadState,
|
47 | 176 | action: Action,
|
48 | 177 | globalState: GlobalState,
|
49 | 178 | ): UnreadState => {
|
50 | 179 | const nextState = {
|
51 |
| - streams: unreadStreamsReducer(state?.streams, action, globalState), |
| 180 | + streams: streamsReducer(state?.streams, action, globalState), |
| 181 | + |
| 182 | + // Note for converting these other sub-reducers to the new design: |
| 183 | + // Probably first push this four-part data structure down through the |
| 184 | + // `switch` statement, and the other logic that's duplicated between them. |
52 | 185 | pms: unreadPmsReducer(state?.pms, action),
|
53 | 186 | huddles: unreadHuddlesReducer(state?.huddles, action),
|
54 | 187 | mentions: unreadMentionsReducer(state?.mentions, action),
|
|
0 commit comments