Skip to content

Commit 834e6e2

Browse files
authored
refactor pxt.Cloud.markdownAsync (microsoft#7431)
* if > a week old, wait for new markdown side effect, never use cached content in pxt serve * switch from nested promise to async / await in markdown async * add ticks for markdown updates
1 parent 3750546 commit 834e6e2

1 file changed

Lines changed: 38 additions & 26 deletions

File tree

pxtlib/emitter/cloud.ts

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -117,35 +117,47 @@ namespace pxt.Cloud {
117117
const MARKDOWN_EXPIRATION = pxt.BrowserUtils.isLocalHostDev() ? 0 : 1 * 60 * 60 * 1000;
118118
// 1w check don't use cached version and wait for new content
119119
const FORCE_MARKDOWN_UPDATE = MARKDOWN_EXPIRATION * 24 * 7;
120-
export function markdownAsync(docid: string, locale?: string): Promise<string> {
120+
export async function markdownAsync(docid: string, locale?: string): Promise<string> {
121121
locale = locale || pxt.Util.userLanguage();
122122
const live = pxt.Util.localizeLive;
123123
const branch = "";
124-
return pxt.BrowserUtils.translationDbAsync()
125-
.then(db => db.getAsync(locale, docid, "")
126-
.then(entry => {
127-
const timeDiff = Date.now() - (entry?.time || 0);
128-
const shouldFetchInBackground = timeDiff > MARKDOWN_EXPIRATION;
129-
const shouldWaitForNewContent = timeDiff > FORCE_MARKDOWN_UPDATE;
130-
131-
if (!shouldWaitForNewContent && entry) {
132-
if (entry && shouldFetchInBackground) {
133-
// background update,
134-
downloadMarkdownAsync(docid, locale, live, entry.etag)
135-
.then(r => db.setAsync(locale, docid, branch, r.etag, undefined, r.md || entry.md))
136-
.catch(() => { }) // swallow errors
137-
.done();
138-
}
139-
// return cached entry
140-
if (entry.md)
141-
return entry.md;
142-
}
143-
// download and cache
144-
return downloadMarkdownAsync(docid, locale, live)
145-
.then(r => db.setAsync(locale, docid, branch, r.etag, undefined, r.md)
146-
.then(() => r.md))
147-
.catch(() => ""); // no translation
148-
}))
124+
125+
const db = await pxt.BrowserUtils.translationDbAsync();
126+
const entry = await db.getAsync(locale, docid, branch);
127+
128+
const downloadAndSetMarkdownAsync = async () => {
129+
try {
130+
const r = await downloadMarkdownAsync(docid, locale, live, entry?.etag);
131+
await db.setAsync(locale, docid, branch, r.etag, undefined, r.md || entry?.md);
132+
return r.md;
133+
} catch {
134+
return ""; // no translation
135+
}
136+
};
137+
138+
if (entry) {
139+
const timeDiff = Date.now() - entry.time;
140+
const shouldFetchInBackground = timeDiff > MARKDOWN_EXPIRATION;
141+
const shouldWaitForNewContent = timeDiff > FORCE_MARKDOWN_UPDATE;
142+
143+
if (!shouldWaitForNewContent) {
144+
if (shouldFetchInBackground) {
145+
pxt.tickEvent("markdown.update.background");
146+
// background update, do not wait
147+
downloadAndSetMarkdownAsync();
148+
}
149+
150+
// return cached entry
151+
if (entry.md) {
152+
return entry.md;
153+
}
154+
} else {
155+
pxt.tickEvent("markdown.update.wait");
156+
}
157+
}
158+
159+
// download and cache
160+
return downloadAndSetMarkdownAsync();
149161
}
150162

151163
function downloadMarkdownAsync(docid: string, locale?: string, live?: boolean, etag?: string): Promise<{ md: string; etag?: string; }> {

0 commit comments

Comments
 (0)