src/frontend/screens/central/private-tab/model.tsView |
---|
3 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
4 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | 5 | * file, You can obtain one at http: |
6 | 6 | |
7 | | -import xs, {Stream} from 'xstream'; |
| 7 | +import xs from 'xstream'; |
8 | 8 | import {FeedId, MsgId} from 'ssb-typescript'; |
9 | 9 | import {GetReadable, SSBSource} from '../../../drivers/ssb'; |
10 | 10 | import {PrivateThreadAndExtras} from '../../../ssb/types'; |
11 | 11 | import {NavSource} from 'cycle-native-navigation'; |
12 | 12 | import {Screens} from '../../enums'; |
13 | | -import {Props as ConversationProps} from '../../conversation/props'; |
| 13 | +import { |
| 14 | + Props as ConversationProps, |
| 15 | + isExistingConversationProps, |
| 16 | + isNewConversationProps, |
| 17 | +} from '../../conversation/props'; |
14 | 18 | |
15 | 19 | export type State = { |
16 | 20 | selfFeedId: FeedId; |
17 | 21 | selfAvatarUrl?: string; |
32 | 36 | |
33 | 37 | const incUpdatesReducer$ = ssbSource.privateLiveUpdates$.map( |
34 | 38 | rootId => |
35 | 39 | function incUpdatesReducer(prev: State): State { |
36 | | - |
37 | | - |
38 | | - if (prev.conversationsOpen.has('new')) return prev; |
39 | | - |
40 | 40 | |
41 | 41 | if (prev.conversationsOpen.has(rootId)) return prev; |
42 | 42 | |
43 | 43 | return { |
47 | 47 | }; |
48 | 48 | }, |
49 | 49 | ); |
50 | 50 | |
51 | | - const conversationOpenedReducer$ = navSource |
| 51 | + const newConversationOpenedReducer$ = navSource |
52 | 52 | .globalDidAppear(Screens.Conversation) |
| 53 | + .filter(ev => isNewConversationProps(ev.passProps as any)) |
| 54 | + .map(appear => { |
| 55 | + const conversationDisappears$ = navSource |
| 56 | + .globalDidDisappear(Screens.Conversation) |
| 57 | + .filter(disappear => disappear.componentId === appear.componentId); |
| 58 | + return ssbSource.selfPrivateRoots$ |
| 59 | + .map(msg => [msg.key, appear.componentId]) |
| 60 | + .endWhen(conversationDisappears$) |
| 61 | + .take(1); |
| 62 | + }) |
| 63 | + .flatten() |
53 | 64 | .map( |
54 | | - ev => |
55 | | - function conversationOpenedReducer(prev: State): State { |
56 | | - const conversationProps = ev.passProps as ConversationProps; |
57 | | - const {selfFeedId, recps, rootMsgId} = conversationProps; |
58 | | - const {conversationsOpen} = prev; |
59 | | - if (!selfFeedId) return prev; |
60 | | - if (rootMsgId) { |
61 | | - prev.updates.delete(rootMsgId); |
62 | | - if (conversationsOpen.has(rootMsgId)) { |
63 | | - return { |
64 | | - ...prev, |
65 | | - updatesFlag: !prev.updatesFlag, |
66 | | - }; |
67 | | - } |
68 | | - |
69 | | - conversationsOpen.set(rootMsgId, ev.componentId); |
70 | | - conversationsOpen.set(ev.componentId, rootMsgId); |
| 65 | + ([rootMsgId, componentId]) => |
| 66 | + function newRootReducer(prev: State): State { |
| 67 | + prev.updates.delete(rootMsgId); |
| 68 | + if (prev.conversationsOpen.has(rootMsgId)) { |
71 | 69 | return { |
72 | 70 | ...prev, |
73 | | - conversationsOpen, |
74 | 71 | updatesFlag: !prev.updatesFlag, |
75 | 72 | }; |
76 | | - } else if (Array.isArray(recps)) { |
77 | | - if (conversationsOpen.has('new')) return prev; |
78 | | - |
79 | | - conversationsOpen.set('new', ev.componentId); |
80 | | - conversationsOpen.set(ev.componentId, 'new'); |
| 73 | + } |
| 74 | + |
| 75 | + prev.conversationsOpen.set(rootMsgId, componentId); |
| 76 | + prev.conversationsOpen.set(componentId, rootMsgId); |
| 77 | + return { |
| 78 | + ...prev, |
| 79 | + conversationsOpen: prev.conversationsOpen, |
| 80 | + updatesFlag: !prev.updatesFlag, |
| 81 | + }; |
| 82 | + }, |
| 83 | + ); |
| 84 | + |
| 85 | + const oldConversationOpenedReducer$ = navSource |
| 86 | + .globalDidAppear(Screens.Conversation) |
| 87 | + .map( |
| 88 | + ev => |
| 89 | + function conversationOpenedReducer(prev: State): State { |
| 90 | + const props = ev.passProps as ConversationProps; |
| 91 | + if (!isExistingConversationProps(props)) return prev; |
| 92 | + const {rootMsgId} = props; |
| 93 | + const {conversationsOpen} = prev; |
| 94 | + prev.updates.delete(rootMsgId); |
| 95 | + if (conversationsOpen.has(rootMsgId)) { |
81 | 96 | return { |
82 | 97 | ...prev, |
83 | | - conversationsOpen, |
84 | 98 | updatesFlag: !prev.updatesFlag, |
85 | 99 | }; |
86 | | - } else { |
87 | | - return prev; |
88 | 100 | } |
| 101 | + |
| 102 | + conversationsOpen.set(rootMsgId, ev.componentId); |
| 103 | + conversationsOpen.set(ev.componentId, rootMsgId); |
| 104 | + return { |
| 105 | + ...prev, |
| 106 | + conversationsOpen, |
| 107 | + updatesFlag: !prev.updatesFlag, |
| 108 | + }; |
89 | 109 | }, |
90 | 110 | ); |
91 | 111 | |
92 | 112 | const conversationClosedReducer$ = navSource |
109 | 129 | |
110 | 130 | return xs.merge( |
111 | 131 | setPrivateFeedReducer$, |
112 | 132 | incUpdatesReducer$, |
113 | | - conversationOpenedReducer$, |
| 133 | + newConversationOpenedReducer$, |
| 134 | + oldConversationOpenedReducer$, |
114 | 135 | conversationClosedReducer$, |
115 | 136 | ); |
116 | 137 | } |