Skip to content

Commit

Permalink
CLDR-16900 Avoid 304 Not Modified; instead use boolean json.unchanged (
Browse files Browse the repository at this point in the history
  • Loading branch information
btangmu authored Apr 3, 2024
1 parent c06479d commit f1401ba
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
18 changes: 5 additions & 13 deletions tools/cldr-apps/js/src/esm/cldrAnnounce.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,15 @@ async function refresh(viewCallbackSetData, viewCallbackSetCounts) {
.then(cldrAjax.handleFetchErrors)
.then((r) => r.json())
.then(setPosts)
.catch(handleErrorOrNotModified);
}

function handleErrorOrNotModified(e) {
if (e.message === "Not Modified") {
// 304
if (CLDR_ANNOUNCE_DEBUG) {
console.log("cldrAnnounce got " + e.message);
}
} else {
console.error(e);
}
.catch(console.error);
}

function setPosts(json) {
thePosts = json;
schedule.setResponseTime();
if (json.unchanged) {
return;
}
thePosts = json;
setAlreadyGotId(thePosts);
const totalCount = thePosts.announcements?.length || 0;
let checkedCount = 0;
Expand Down
8 changes: 8 additions & 0 deletions tools/cldr-apps/js/test/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This needs to be run from the cldr folder
# Optionally, to run this as cldrtestjs, you may have in .bash_profile:
# alias cldrtestjs='cd [path/to/cldr] && sh tools/cldr-apps/js-unittest/run.sh'
cd tools/cldr-apps/js
npm install
npm run build-test
cd ../../..
npx open-cli tools/cldr-apps/js/test/Test.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,29 @@ public Response getAnnouncements(
if (SurveyMain.isBusted() || !SurveyMain.wasInitCalled() || !SurveyMain.triedToStartUp()) {
return STError.surveyNotQuiteReady();
}
if (alreadyGotId != 0 && alreadyGotId == AnnouncementData.getMostRecentAnnouncementId()) {
return Response.notModified().build();
}
AnnouncementResponse response = new AnnouncementResponse(session.user);
Boolean unchanged =
(alreadyGotId != 0
&& alreadyGotId == AnnouncementData.getMostRecentAnnouncementId());
AnnouncementResponse response = new AnnouncementResponse(session.user, unchanged);
return Response.ok(response).build();
}

@Schema(description = "List of announcements")
public static final class AnnouncementResponse {
@Schema(description = "unchanged")
public boolean unchanged;

@Schema(description = "announcements")
public Announcement[] announcements;

public AnnouncementResponse(UserRegistry.User user) {
List<Announcement> announcementList = new ArrayList<>();
AnnouncementData.get(user, announcementList);
announcements = announcementList.toArray(new Announcement[0]);
public AnnouncementResponse(UserRegistry.User user, Boolean unchanged) {
this.unchanged = unchanged;

if (!unchanged) {
List<Announcement> announcementList = new ArrayList<>();
AnnouncementData.get(user, announcementList);
announcements = announcementList.toArray(new Announcement[0]);
}
}
}

Expand Down

0 comments on commit f1401ba

Please sign in to comment.