Skip to content

Commit 77fc505

Browse files
committed
fix(play): files sent unasked save nothing, one package is built at a time, and a late file does not start the activity again (#92)
Found in a review of the change: - Files the framing page sent while a preview played, unasked, saved a download nobody pressed for. They now make Download a link to the whole package, with nothing shown. - Files from the person and from the framing page at once built and saved two packages. One is built at a time. - A file that came after the package with its files in it was built, from a framing page still bringing what the person chose themselves meanwhile, started the activity again. It now changes nothing. - 'Try again' after a build that failed asked the framing page for bytes the page already held. - A count of a file on its way, sent in the moment the question for the file is held back, was dropped, so the question flashed up before the count. - An address given while a build with every file was running started that build again. - The preview's Download linked to '#download', a damaged link where opened in a new tab. It links to the page itself. - A build that failed is told a screen reader, and its bar goes.
1 parent 6b75f67 commit 77fc505

1 file changed

Lines changed: 73 additions & 29 deletions

File tree

‎src/h5p/play/play-page.ts‎

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,14 @@ let told = ''
8181
const links = new Map<string, string>()
8282
/** The names the source gives the files the package last built plays from an address: a preview. */
8383
let previewed: string[] = []
84-
/** Whether Download is waiting for the parent to bring the bytes of those files. */
84+
/** Whether Download was pressed on a preview, and its package is not saved yet. */
8585
let keeping = false
86+
/** Whether the parent was asked for the bytes of those files, and has not brought them yet. */
87+
let bringing = false
88+
/** Whether the package to keep is being built: one at a time. */
89+
let packing = false
90+
/** Whether the package that plays has its files in it. Files that come after that change nothing. */
91+
let whole = false
8692
/** Shows the question for the person's files a moment late, where the parent may bring them first. */
8793
let askTimer: ReturnType<typeof setTimeout> | undefined
8894

@@ -116,6 +122,8 @@ function busy(text: string): void {
116122
function fail(heading: string, text: string, fixable = false): void {
117123
errorTitle.textContent = heading
118124
errorText.textContent = text
125+
// Whatever was built does not play: files that come now are built from again.
126+
whole = false
119127
fix.hidden = !fixable
120128
// The headings above end in no full stop, and a message from elsewhere may be empty.
121129
show('error', text ? `${heading}. ${text}` : `${heading}.`)
@@ -207,8 +215,12 @@ function ask(missing: string[]): void {
207215
// A parent that holds the files answers within a moment: until then the loader stays, so the
208216
// question does not flash up and go.
209217
clearTimeout(askTimer)
218+
askTimer = undefined
210219
if (framed && first && !chosen.size) {
211-
askTimer = setTimeout(() => showAsk(listed, one), 1200)
220+
askTimer = setTimeout(() => {
221+
askTimer = undefined
222+
showAsk(listed, one)
223+
}, 1200)
212224
return
213225
}
214226
showAsk(listed, one)
@@ -227,7 +239,10 @@ function showAsk(listed: string, one: boolean): void {
227239
*/
228240
function fetching(name: string, received: number, total: number): void {
229241
if (keeping) return keepCount(name, received, total)
230-
if (main.dataset.state !== 'asking' && main.dataset.state !== 'fetching') return
242+
// Asked for, the question shown or still held back a moment: the file on its way is shown in its place.
243+
if (main.dataset.state !== 'asking' && main.dataset.state !== 'fetching' && askTimer === undefined) return
244+
clearTimeout(askTimer)
245+
askTimer = undefined
231246
const what = wanted.length === 1 ? called(name || wanted[0]!) : 'file'
232247
label.textContent = `Getting your ${what}… ${sized(received)}${total > 0 ? ` of ${sized(total)}` : ''}`
233248
note.textContent = 'From the chat to your browser, nowhere else.'
@@ -242,38 +257,53 @@ function fetching(name: string, received: number, total: number): void {
242257
/** Files given, by the person or by the parent: the activity is built once none is missing. */
243258
function take(files: File[]): void {
244259
if (!files.length || !wanted.length) return
260+
// A file that comes late, from a parent still bringing what the person chose themselves
261+
// meanwhile, must not start the activity again under them.
262+
if (whole) return
245263
// Where one file is named, a second choice replaces the first.
246264
if (wanted.length === 1) chosen.clear()
247265
for (const file of files) chosen.set(file.name, file)
248-
// While a preview plays, the files make the package to keep, and the player is left playing.
266+
// While a preview plays, the files make the package to keep, and the player is left playing:
267+
// saved where Download asked for them, and only made ready for it where the parent sent them unasked.
249268
if (previewed.length) void keep()
250269
else void play()
251270
}
252271

253272
/** The words for the files a preview plays from an address: "video", or "files". */
254273
const previewedAs = (): string => previewed.length === 1 ? called(previewed[0]!) : 'files'
255274

275+
/** The row under the stage while a download is made ready, with the choice of the file's own where it helps. */
276+
function preparing(text: string, choice: boolean): void {
277+
keepBox.hidden = false
278+
keepInstead.hidden = !choice
279+
keepLabel.textContent = text
280+
keepProgress.hidden = false
281+
keepProgress.dataset.indeterminate = ''
282+
keepProgress.removeAttribute('aria-valuenow')
283+
download.setAttribute('aria-disabled', 'true')
284+
downloadLabel.textContent = 'Preparing…'
285+
}
286+
256287
/**
257288
* Download of a preview: the parent is asked for the bytes of what it plays from an address, and
258289
* how much has come is shown under the stage while the activity plays on. The person can choose
259-
* the file from their computer instead, as when it was first asked for.
290+
* the file from their computer instead, as when it was first asked for. Files that are here
291+
* already, sent unasked or left by a build that failed, are built from at once.
260292
*/
261293
function fetchToKeep(): void {
262-
if (keeping) return
263294
keeping = true
264-
keepBox.hidden = false
265-
keepInstead.hidden = false
266-
keepLabel.textContent = `Getting your ${previewedAs()} from the chat for the download…`
267-
keepProgress.dataset.indeterminate = ''
268-
keepProgress.removeAttribute('aria-valuenow')
269-
download.setAttribute('aria-disabled', 'true')
270-
downloadLabel.textContent = 'Preparing…'
295+
if (packing) return preparing(`Building the .h5p with your ${previewedAs()} in it…`, false)
296+
if (bringing) return
297+
if (!filesFor(wanted).missing.length) return void keep()
298+
bringing = true
299+
preparing(`Getting your ${previewedAs()} from the chat for the download…`, true)
271300
announce(`Getting your ${previewedAs()} from the chat for the download.`)
272301
toParent({ type: 'needs-bytes', names: previewed.map(baseName) })
273302
}
274303

275304
/** How much of a file for the download has come. */
276305
function keepCount(name: string, received: number, total: number): void {
306+
if (!bringing) return
277307
const what = previewed.length === 1 ? called(name || previewed[0]!) : 'file'
278308
keepLabel.textContent = `Getting your ${what} for the download… ${sized(received)}${total > 0 ? ` of ${sized(total)}` : ''}`
279309
if (total > 0) {
@@ -284,51 +314,63 @@ function keepCount(name: string, received: number, total: number): void {
284314
}
285315
}
286316

287-
/** The package to keep, built with the files in it, saved, and given to the parent; the player plays on. */
317+
/**
318+
* The package to keep, built with the files in it and given to the parent, while the player plays
319+
* on. It is saved only where Download asked for it: files the parent sends unasked make Download
320+
* a link to it, with nothing shown and nothing saved.
321+
*/
288322
async function keep(): Promise<void> {
323+
if (packing) return
289324
const { files, missing } = filesFor(wanted)
290325
if (missing.length) {
291-
keepLabel.textContent = `Still needed for the download: ${new Intl.ListFormat('en-GB', { type: 'conjunction' }).format(missing.map(baseName))}.`
326+
if (keeping) keepLabel.textContent = `Still needed for the download: ${new Intl.ListFormat('en-GB', { type: 'conjunction' }).format(missing.map(baseName))}.`
292327
return
293328
}
294-
keeping = true
295-
keepBox.hidden = false
296-
keepInstead.hidden = true
297-
keepLabel.textContent = 'Building the .h5p with your file in it…'
298-
keepProgress.dataset.indeterminate = ''
329+
packing = true
330+
bringing = false
331+
if (keeping) preparing(`Building the .h5p with your ${previewedAs()} in it…`, false)
299332
try {
300333
opened ??= openPack(installPack)
301334
const result = await buildPackage(source, { libraries: await opened, files })
302335
const name = packageName(String(result.manifest.title ?? ''))
303336
if (packageUrl) URL.revokeObjectURL(packageUrl)
304337
packageUrl = URL.createObjectURL(result.blob)
338+
// Download may have been pressed while this was built.
339+
const asked = keeping
305340
previewed = []
306341
keeping = false
342+
whole = true
307343
keepBox.hidden = true
308344
download.href = packageUrl
309345
download.download = name
310346
download.removeAttribute('aria-disabled')
311347
downloadLabel.textContent = 'Download .h5p'
312348
about(result.manifest.title, result.builtWith, result.blob.size)
313349
toParent({ type: 'built', file: new File([result.blob], name, { type: 'application/zip' }), title: result.manifest.title, size: sized(result.blob.size) })
350+
if (!asked) return
314351
announce(`The .h5p is ready, ${sized(result.blob.size)}: downloading.`)
315352
// The download the person asked for. A frame its parent lets download nothing has given the
316353
// parent the file above, to save its own way.
317354
download.click()
318355
} catch (error) {
319-
keeping = false
356+
// Unasked, nothing is said: the preview plays on, and Download builds from the same files.
357+
if (!keeping) return
358+
const text = `The .h5p could not be built: ${error instanceof Error ? error.message : String(error)}`
320359
keepInstead.hidden = false
321-
delete keepProgress.dataset.indeterminate
322-
keepBar.style.setProperty('--done', '0%')
323-
keepLabel.textContent = `The .h5p could not be built: ${error instanceof Error ? error.message : String(error)}`
360+
keepProgress.hidden = true
361+
keepLabel.textContent = text
324362
download.removeAttribute('aria-disabled')
325363
downloadLabel.textContent = 'Try again'
364+
announce(text)
365+
} finally {
366+
packing = false
326367
}
327368
}
328369

329370
async function play(): Promise<void> {
330371
const build = ++builds
331372
clearTimeout(askTimer)
373+
askTimer = undefined
332374
const found = filesFor(wanted)
333375
const { linked, missing } = linksFor(found.missing)
334376
if (missing.length) return ask(missing)
@@ -344,11 +386,13 @@ async function play(): Promise<void> {
344386
download.hidden = false
345387
if (previewed.length) {
346388
// A preview is not a file to hand out: its address expires. Download fetches the bytes first.
389+
// Its link is this page's own, for a press that opens it elsewhere; a press here is taken below.
347390
packageUrl = null
348-
download.href = '#download'
391+
download.href = location.href
349392
download.removeAttribute('download')
350393
about(result.manifest.title, result.builtWith, `${previewedAs() === 'files' ? 'its files play' : `its ${previewedAs()} plays`} from the chat`)
351394
} else {
395+
whole = true
352396
packageUrl = URL.createObjectURL(result.blob)
353397
download.href = packageUrl
354398
download.download = name
@@ -401,7 +445,7 @@ keepInstead.addEventListener('click', () => filesInput.click())
401445
download.addEventListener('click', (event) => {
402446
if (!previewed.length) return
403447
event.preventDefault()
404-
if (!keeping) fetchToKeep()
448+
fetchToKeep()
405449
})
406450
filesInput.addEventListener('change', () => {
407451
const files = [...(filesInput.files ?? [])]
@@ -425,8 +469,8 @@ main.addEventListener('drop', (event) => {
425469

426470
/**
427471
* Addresses the parent gives for files it holds, by name: kept where they are http(s) and name a
428-
* video or a recording, and the activity built with them where it still waits for its files. While
429-
* a preview plays they are only kept: a new address for the same file is not a new activity.
472+
* video or a recording, and the activity built with them where a file of its own is still missing.
473+
* While a preview plays they are only kept: a new address for the same file is not a new activity.
430474
*/
431475
function takeLinks(given: Record<string, unknown>): void {
432476
let added = false
@@ -435,7 +479,7 @@ function takeLinks(given: Record<string, unknown>): void {
435479
links.set(baseName(name), address)
436480
added = true
437481
}
438-
if (added && wanted.length && !previewed.length && main.dataset.state !== 'ready') void play()
482+
if (added && !previewed.length && filesFor(wanted).missing.length) void play()
439483
}
440484

441485
/** A count of bytes as a message gives it: a number that is one, and not below nothing. */

0 commit comments

Comments
 (0)