@@ -30,6 +30,7 @@ import type {
30
30
31
31
type Props = {
32
32
firebaseDBRef : DatabaseReference ;
33
+ oldFirebaseDBRef : Object | any ;
33
34
component : Object ;
34
35
} ;
35
36
@@ -57,7 +58,8 @@ const emptyFunc = () => null;
57
58
const ChatProviderWrapper = (
58
59
firebaseDB : DatabaseReference ,
59
60
ComposedComponent : React . ComponentType < any > ,
60
- packageCount : number = MESSAGE_PACKAGE_COUNT
61
+ packageCount : number = MESSAGE_PACKAGE_COUNT ,
62
+ oldFirebaseDB : any
61
63
) => {
62
64
class ChatProvider extends React . Component < Props , State > {
63
65
state = chatDefaultState ;
@@ -75,20 +77,14 @@ const ChatProviderWrapper = (
75
77
( chatId : string , newChat ?: boolean ) => {
76
78
console . warn ( "chatListner" , chatId ) ;
77
79
const { initialLoad } = this . state ;
78
- const chatMessages = chatMessagesRef ( firebaseDB , chatId ) ;
79
- const orderByCreatedAt = orderByChild ( "createdAt" ) ;
80
- const limitToLastPackageCount = limitToLast ( packageCount ) ;
81
- const queryRef = query (
82
- chatMessages ,
83
- // orderByCreatedAt,
84
- limitToLastPackageCount
85
- ) ;
86
80
// we want to fetch first N messages at once
87
81
if ( ! newChat && initialLoad ) {
88
- onValue (
89
- queryRef ,
90
- ( messagesSnap ) => {
91
- console . warn ( "chatListner" , 1 , messagesSnap . val ( ) , messagesSnap ) ;
82
+ oldFirebaseDB
83
+ . child ( `chat-messages/${ chatId } ` )
84
+ . orderByChild ( "createdAt" )
85
+ . limitToLast ( packageCount )
86
+ . once ( "value" )
87
+ . then ( ( messagesSnap ) => {
92
88
/*
93
89
* Here we fetch first batch of the messages at once and process them for the chat
94
90
* component. After that we push them to state, mark initial load as done and unsubsribe
@@ -115,44 +111,42 @@ const ChatProviderWrapper = (
115
111
processedMessages . length - this . state . messagesCount ,
116
112
initialLoad : false ,
117
113
} ) ;
118
- console . warn ( "chatListner" , 2 ) ;
119
- } ,
120
- // ensure to read data only once
121
- {
122
- onlyOnce : true ,
123
- }
124
- ) ;
114
+ } ) ;
125
115
} else {
126
- onChildAdded ( queryRef , ( messageSnippet ) => {
127
- console . warn ( "chatListner" , 3 , messageSnippet . val ( ) ) ;
128
- const message = listnerSingleMessageTransform (
129
- messageSnippet . key ,
130
- participants
131
- ) ( messageSnippet . val ( ) ) ;
132
-
133
- // We have to check for duplicates since we get already fetched node as well after
134
- // initial load
135
- /* eslint-disable no-underscore-dangle */
136
- if (
137
- this . state . messages . some ( ( m : Message ) => m . _id === message . _id )
138
- ) {
139
- console . warn ( "chatListner" , 3.5 ) ;
140
- return ;
141
- }
116
+ oldFirebaseDB
117
+ . child ( `chat-messages/${ chatId } ` )
118
+ . orderByChild ( "createdAt" )
119
+ . limitToLast ( packageCount )
120
+ . on ( "child_added" , ( messageSnippet ) => {
121
+ console . warn ( "chatListner" , 3 , messageSnippet . val ( ) ) ;
122
+ const message = listnerSingleMessageTransform (
123
+ messageSnippet . key ,
124
+ participants
125
+ ) ( messageSnippet . val ( ) ) ;
126
+
127
+ // We have to check for duplicates since we get already fetched node as well after
128
+ // initial load
129
+ /* eslint-disable no-underscore-dangle */
130
+ if (
131
+ this . state . messages . some ( ( m : Message ) => m . _id === message . _id )
132
+ ) {
133
+ console . warn ( "chatListner" , 3.5 ) ;
134
+ return ;
135
+ }
136
+
137
+ console . warn ( "chatListner" , 4 ) ;
138
+ const updatedMesseges = webMessageTransform
139
+ ? R . append ( message , this . state . messages )
140
+ : R . prepend ( message , this . state . messages ) ;
142
141
143
- console . warn ( "chatListner" , 4 ) ;
144
- const updatedMesseges = webMessageTransform
145
- ? R . append ( message , this . state . messages )
146
- : R . prepend ( message , this . state . messages ) ;
142
+ this . setState ( {
143
+ messages : updatedMesseges ,
144
+ messagesCount : this . state . messagesCount + 1 ,
145
+ initialLoad : false ,
146
+ } ) ;
147
147
148
- this . setState ( {
149
- messages : updatedMesseges ,
150
- messagesCount : this . state . messagesCount + 1 ,
151
- initialLoad : false ,
148
+ console . warn ( "chatListner" , 5 , updatedMesseges ) ;
152
149
} ) ;
153
-
154
- console . warn ( "chatListner" , 5 ) ;
155
- } ) ;
156
150
}
157
151
} ;
158
152
@@ -186,7 +180,7 @@ const ChatProviderWrapper = (
186
180
} ) ;
187
181
188
182
toSendMessage ( {
189
- firebaseDB,
183
+ firebaseDB : oldFirebaseDB ,
190
184
chatId : newChatId ,
191
185
userId : uid ,
192
186
messages,
@@ -219,7 +213,7 @@ const ChatProviderWrapper = (
219
213
} ) => {
220
214
console . warn ( "onSend" , chatId ) ;
221
215
toSendMessage ( {
222
- firebaseDB,
216
+ firebaseDB : oldFirebaseDB ,
223
217
chatId,
224
218
userId : uid ,
225
219
messages,
@@ -263,20 +257,14 @@ const ChatProviderWrapper = (
263
257
// fetch last 5/10/15/20/...
264
258
265
259
this . setState ( { isLoadingEarlier : true } , ( ) => {
266
- const chatMessages = chatMessagesRef ( firebaseDB , chatId ) ;
267
- const orderByCreatedAt = orderByChild ( "createdAt" ) ;
268
- const limitToLastPackageCount = limitToLast ( updatedMsgsCount ) ;
269
- const queryRef = query (
270
- chatMessages ,
271
- // orderByCreatedAt,
272
- limitToLastPackageCount
273
- ) ;
260
+ oldFirebaseDB
261
+ . child ( `chat-messages/${ chatId } ` )
262
+ . orderByChild ( "createdAt" )
263
+ . limitToLast ( updatedMsgsCount )
264
+ . once ( "value" ) // TODO via on and unsubscription
265
+ . then ( ( chatMsgs ) => {
266
+ const messagesFromDB = chatMsgs . val ( ) ;
274
267
275
- onValue (
276
- queryRef ,
277
- ( chatMsgs ) => {
278
- const messagesFromDB = chatMsgs . val ( ) as CollectionObject < any > ;
279
- console . warn ( "loadMoreMessages messagesFromDB" , messagesFromDB ) ;
280
268
if ( R . values ( messagesFromDB ) . length > this . state . messages . length ) {
281
269
// add to message's id to other message's object
282
270
chatMsgs . forEach ( ( item ) => {
@@ -300,12 +288,7 @@ const ChatProviderWrapper = (
300
288
callBack || emptyFunc
301
289
) ;
302
290
}
303
- } ,
304
- // ensure to read data only once
305
- {
306
- onlyOnce : true ,
307
- }
308
- ) ;
291
+ } ) ;
309
292
} ) ;
310
293
} ;
311
294
@@ -316,7 +299,7 @@ const ChatProviderWrapper = (
316
299
uid : string ;
317
300
prevMessages : Message [ ] ;
318
301
} ) => {
319
- console . warn ( "markMessagesRead" , uid ) ;
302
+ console . warn ( "markMessagesRead" , uid , prevMessages ) ;
320
303
const { messages } = this . state ;
321
304
const messagesIds = getMessagesIds ( messages ) ;
322
305
const prevMessagesIds = getMessagesIds ( prevMessages ) ;
@@ -336,7 +319,7 @@ const ChatProviderWrapper = (
336
319
{ }
337
320
) ;
338
321
339
- update ( firebaseDB , unreadMessagesToDeleteUpdate ) ;
322
+ oldFirebaseDB . update ( unreadMessagesToDeleteUpdate ) ;
340
323
}
341
324
} ;
342
325
@@ -348,10 +331,7 @@ const ChatProviderWrapper = (
348
331
theUserId : string ;
349
332
uid : string ;
350
333
eventId : string ;
351
- } ) => {
352
- console . warn ( "checkForChat" , theUserId ) ;
353
- return checkForChatExistence ( firebaseDB , theUserId , uid , eventId ) ;
354
- } ;
334
+ } ) => checkForChatExistence ( firebaseDB , theUserId , uid , eventId ) ;
355
335
356
336
getChatParticipantsDetails = async (
357
337
participants : CollectionObject < true >
@@ -385,10 +365,8 @@ const ChatProviderWrapper = (
385
365
return allChatsParticipants ;
386
366
} ;
387
367
388
- getGroupChatsByEvent = ( eventId : string ) => {
389
- console . warn ( "getGroupChatsByEvent" , eventId ) ;
390
- return getGroupChatsByEvent ( firebaseDB , eventId ) ;
391
- } ;
368
+ getGroupChatsByEvent = ( eventId : string ) =>
369
+ getGroupChatsByEvent ( oldFirebaseDB , eventId ) ;
392
370
393
371
render ( ) {
394
372
return (
0 commit comments