@@ -1138,13 +1138,48 @@ export async function finish(difficultyFailed = false): Promise<void> {
11381138 ) ;
11391139 Result . updateTodayTracker ( ) ;
11401140
1141- if ( ! isAuthenticated ( ) ) {
1141+ let savingResultPromise : ReturnType < typeof saveResult > =
1142+ Promise . resolve ( null ) ;
1143+ const user = getAuthenticatedUser ( ) ;
1144+ if ( user !== null ) {
1145+ // logged in
1146+ if ( dontSave ) {
1147+ void AnalyticsController . log ( "testCompletedInvalid" ) ;
1148+ } else {
1149+ TestStats . resetIncomplete ( ) ;
1150+
1151+ if ( completedEvent . testDuration > 122 ) {
1152+ completedEvent . chartData = "toolong" ;
1153+ completedEvent . keySpacing = "toolong" ;
1154+ completedEvent . keyDuration = "toolong" ;
1155+ }
1156+
1157+ if ( ! completedEvent . bailedOut ) {
1158+ const challenge = ChallengeContoller . verify ( completedEvent ) ;
1159+ if ( challenge !== null ) completedEvent . challenge = challenge ;
1160+ }
1161+
1162+ completedEvent . uid = user . uid ;
1163+ completedEvent . hash = objectHash ( completedEvent ) ;
1164+
1165+ savingResultPromise = saveResult ( completedEvent , false ) ;
1166+ void savingResultPromise . then ( ( response ) => {
1167+ if ( response && response . status === 200 ) {
1168+ void AnalyticsController . log ( "testCompleted" ) ;
1169+ }
1170+ } ) ;
1171+ }
1172+ } else {
1173+ // logged out
11421174 void AnalyticsController . log ( "testCompletedNoLogin" ) ;
1143- if ( ! dontSave ) notSignedInLastResult = completedEvent ;
1175+ if ( ! dontSave ) {
1176+ // if its valid save it for later
1177+ notSignedInLastResult = completedEvent ;
1178+ }
11441179 dontSave = true ;
11451180 }
11461181
1147- await Result . update (
1182+ const resultUpdatePromise = Result . update (
11481183 completedEvent ,
11491184 difficultyFailed ,
11501185 failReason ,
@@ -1155,43 +1190,13 @@ export async function finish(difficultyFailed = false): Promise<void> {
11551190 dontSave ,
11561191 ) ;
11571192
1158- if ( dontSave ) {
1159- void AnalyticsController . log ( "testCompletedInvalid" ) ;
1160- return ;
1161- }
1162-
1163- // because of the dont save check above, we know the user is signed in
1164- // we check here again so that typescript doesnt complain
1165- const user = getAuthenticatedUser ( ) ;
1166- if ( ! user ) {
1167- return ;
1168- }
1169-
1170- // user is logged in
1171- TestStats . resetIncomplete ( ) ;
1172-
1173- completedEvent . uid = user . uid ;
1174-
1175- if ( completedEvent . testDuration > 122 ) {
1176- completedEvent . chartData = "toolong" ;
1177- completedEvent . keySpacing = "toolong" ;
1178- completedEvent . keyDuration = "toolong" ;
1179- }
1180-
1181- if ( ! completedEvent . bailedOut ) {
1182- const challenge = ChallengeContoller . verify ( completedEvent ) ;
1183- if ( challenge !== null ) completedEvent . challenge = challenge ;
1184- }
1185-
1186- completedEvent . hash = objectHash ( completedEvent ) ;
1187-
1188- await saveResult ( completedEvent , false ) ;
1193+ await Promise . all ( [ savingResultPromise , resultUpdatePromise ] ) ;
11891194}
11901195
11911196async function saveResult (
11921197 completedEvent : CompletedEvent ,
11931198 isRetrying : boolean ,
1194- ) : Promise < void > {
1199+ ) : Promise < null | Awaited < ReturnType < typeof Ape . results . add > > > {
11951200 AccountButton . loading ( true ) ;
11961201
11971202 if ( ! TestState . savingEnabled ) {
@@ -1201,7 +1206,7 @@ async function saveResult(
12011206 important : true ,
12021207 } ) ;
12031208 AccountButton . loading ( false ) ;
1204- return ;
1209+ return null ;
12051210 }
12061211
12071212 if ( ! ConnectionState . get ( ) ) {
@@ -1216,7 +1221,7 @@ async function saveResult(
12161221 if ( ! isRetrying ) {
12171222 retrySaving . completedEvent = completedEvent ;
12181223 }
1219- return ;
1224+ return null ;
12201225 }
12211226
12221227 const response = await Ape . results . add ( { body : { result : completedEvent } } ) ;
@@ -1244,7 +1249,7 @@ async function saveResult(
12441249 "Looks like your result data is using an incorrect schema. Please refresh the page to download the new update. If the problem persists, please contact support." ;
12451250 }
12461251 Notifications . add ( "Failed to save result" , - 1 , { response } ) ;
1247- return ;
1252+ return response ;
12481253 }
12491254
12501255 const data = response . body . data ;
@@ -1285,8 +1290,6 @@ async function saveResult(
12851290 dataToSave . result = result ;
12861291 }
12871292
1288- void AnalyticsController . log ( "testCompleted" ) ;
1289-
12901293 if ( data . isPb !== undefined && data . isPb ) {
12911294 //new pb
12921295 const localPb = await DB . getLocalPB (
@@ -1335,6 +1338,7 @@ async function saveResult(
13351338 Notifications . add ( "Result saved" , 1 , { important : true } ) ;
13361339 }
13371340 DB . saveLocalResult ( dataToSave ) ;
1341+ return response ;
13381342}
13391343
13401344export function fail ( reason : string ) : void {
0 commit comments