@@ -54,7 +54,6 @@ export async function waitForItemInNotifications(
5454}
5555
5656export async function waitForUnreadNotifications (
57- unreadNotificationCount ,
5857 options = {
5958 retryCount : 0 ,
6059 delay : 0 ,
@@ -68,23 +67,59 @@ export async function waitForUnreadNotifications(
6867
6968 const responseJson = await response . clone ( ) . json ( ) ;
7069
71- const found = responseJson . count === unreadNotificationCount ;
70+ const unreadNotifications = responseJson . count > 0 ;
7271
73- if ( found ) {
74- return found ;
72+ if ( unreadNotifications ) {
73+ return true ;
74+ }
75+
76+ if ( options . retryCount === MAX_RETRIES ) {
77+ throw new Error (
78+ `Max retries reached (${ MAX_RETRIES } ) when waiting for unread notifications. No unread notifications found.` ,
79+ ) ;
80+ }
81+
82+ if ( options . delay > 0 ) {
83+ await new Promise ( ( resolve ) => setTimeout ( resolve , options . delay ) ) ;
84+ }
85+
86+ return await waitForUnreadNotifications ( {
87+ retryCount : options . retryCount + 1 ,
88+ delay : options . delay + 500 ,
89+ } ) ;
90+ }
91+
92+ export async function waitForZeroUnreadNotifications (
93+ options = {
94+ retryCount : 0 ,
95+ delay : 0 ,
96+ } ,
97+ ) {
98+ const MAX_RETRIES = 5 ;
99+
100+ const response = await fetchActivityPub (
101+ 'https://self.test/.ghost/activitypub/v1/notifications/unread/count' ,
102+ ) ;
103+
104+ const responseJson = await response . clone ( ) . json ( ) ;
105+
106+ const zeroUnreadNotifications = responseJson . count === 0 ;
107+
108+ if ( zeroUnreadNotifications ) {
109+ return true ;
75110 }
76111
77112 if ( options . retryCount === MAX_RETRIES ) {
78113 throw new Error (
79- `Max retries reached (${ MAX_RETRIES } ) when waiting for notifications count ${ unreadNotificationCount } . Notification count found ${ responseJson . count } ` ,
114+ `Max retries reached (${ MAX_RETRIES } ) when waiting for zero unread notifications. Unread notifications found: ${ responseJson . count } . ` ,
80115 ) ;
81116 }
82117
83118 if ( options . delay > 0 ) {
84119 await new Promise ( ( resolve ) => setTimeout ( resolve , options . delay ) ) ;
85120 }
86121
87- return await waitForUnreadNotifications ( unreadNotificationCount , {
122+ return await waitForZeroUnreadNotifications ( {
88123 retryCount : options . retryCount + 1 ,
89124 delay : options . delay + 500 ,
90125 } ) ;
0 commit comments