@@ -254,75 +254,78 @@ function loadJournalStream(db, req, res, user, lastEventId, done) {
254254 let changedKeywords = new Set ( ) ;
255255 let flaggedChanged = false ;
256256
257- let emitFlaggedCounter = next => {
257+ let emitFlaggedCounter = async next => {
258258 if ( ! flaggedChanged ) {
259259 return next ( ) ;
260260 }
261261
262- Promise . all ( [ tools . getFlaggedCounter ( db , user ) , tools . getFlaggedCounter ( db , user , 'unseen' ) ] )
263- . then ( ( [ total , unseen ] ) => {
264- res . write (
265- formatJournalData ( {
266- command : 'FLAGGED_COUNTER' ,
267- _id : lastEventId ,
268- total,
269- unseen
270- } )
271- ) ;
272- next ( ) ;
273- } )
274- . catch ( ( ) => next ( ) ) ;
262+ try {
263+ const [ total , unseen ] = await Promise . all ( [ tools . getFlaggedCounter ( db , user ) , tools . getFlaggedCounter ( db , user , 'unseen' ) ] ) ;
264+ res . write (
265+ formatJournalData ( {
266+ command : 'FLAGGED_COUNTER' ,
267+ _id : lastEventId ,
268+ total,
269+ unseen
270+ } )
271+ ) ;
272+ } catch {
273+ // ignore
274+ }
275+ next ( ) ;
275276 } ;
276277
277- let emitKeywordCounters = next => {
278+ let emitKeywordCounters = async next => {
278279 if ( ! changedKeywords . size ) {
279280 return next ( ) ;
280281 }
281282
282- const userKey = user . toString ( ) ;
283- Promise . all (
284- [ ...changedKeywords ] . map ( async keyword => {
285- const isCached = await db . redis . exists ( `kw:total:${ userKey } :${ keyword } ` ) ;
286- return isCached ? keyword : null ;
287- } )
288- )
289- . then ( async results => {
290- const toEmit = results . filter ( Boolean ) ;
291- if ( ! toEmit . length ) {
292- return next ( ) ;
293- }
283+ try {
284+ const userKey = user . toString ( ) ;
285+ const cachedResults = await Promise . all (
286+ [ ...changedKeywords ] . map ( async keyword => {
287+ const isCached = await db . redis . exists ( `kw:total:${ userKey } :${ keyword } ` ) ;
288+ return isCached ? keyword : null ;
289+ } )
290+ ) ;
291+
292+ const toEmit = cachedResults . filter ( Boolean ) ;
293+ if ( ! toEmit . length ) {
294+ return next ( ) ;
295+ }
294296
295- const keywordResults = await Promise . all (
296- toEmit . map ( async keyword => {
297- let total , unseen ;
298- try {
299- total = await tools . getKeywordCounter ( db , user , keyword ) ;
300- } catch {
301- total = 0 ;
302- }
303- try {
304- unseen = await tools . getKeywordCounter ( db , user , keyword , 'unseen' ) ;
305- } catch {
306- unseen = 0 ;
307- }
308- return { keyword, total, unseen } ;
297+ const keywordResults = await Promise . all (
298+ toEmit . map ( async keyword => {
299+ let total , unseen ;
300+ try {
301+ total = await tools . getKeywordCounter ( db , user , keyword ) ;
302+ } catch {
303+ total = 0 ;
304+ }
305+ try {
306+ unseen = await tools . getKeywordCounter ( db , user , keyword , 'unseen' ) ;
307+ } catch {
308+ unseen = 0 ;
309+ }
310+ return { keyword, total, unseen } ;
311+ } )
312+ ) ;
313+
314+ for ( const { keyword, total, unseen } of keywordResults ) {
315+ res . write (
316+ formatJournalData ( {
317+ command : 'KEYWORD_COUNTERS' ,
318+ _id : lastEventId ,
319+ keyword,
320+ total,
321+ unseen
309322 } )
310323 ) ;
311-
312- for ( const { keyword, total, unseen } of keywordResults ) {
313- res . write (
314- formatJournalData ( {
315- command : 'KEYWORD_COUNTERS' ,
316- _id : lastEventId ,
317- keyword,
318- total,
319- unseen
320- } )
321- ) ;
322- }
323- next ( ) ;
324- } )
325- . catch ( ( ) => next ( ) ) ;
324+ }
325+ } catch {
326+ // ignore
327+ }
328+ next ( ) ;
326329 } ;
327330
328331 let cursor = db . database . collection ( 'journal' ) . find ( query ) . sort ( { _id : 1 } ) ;
@@ -414,7 +417,7 @@ function loadJournalStream(db, req, res, user, lastEventId, done) {
414417 break ;
415418 }
416419
417- let checkKeywordsAndContinue = ( ) => {
420+ let writeEntryAndContinue = ( ) => {
418421 try {
419422 let data = formatJournalData ( e ) ;
420423 res . write ( data ) ;
@@ -439,7 +442,7 @@ function loadJournalStream(db, req, res, user, lastEventId, done) {
439442 }
440443 }
441444
442- checkKeywordsAndContinue ( ) ;
445+ writeEntryAndContinue ( ) ;
443446 } ) ;
444447 } ;
445448
0 commit comments