All files / src/web video-nula-notice.js

93.33% Statements 14/15
85.71% Branches 6/7
83.33% Functions 5/6
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44      1x   1x               7x 7x 7x                         1x 3x 3x 3x     2x 1x 1x   1x 1x        
import { noteFeature } from "./analytics.js";
import { toast } from "./toast.js";
 
export const VideoNulaFeature = "videonula";
 
const AfterSession = "One can be fitted once the shared session is over.";
 
/**
 * Tells the user the program wrote to a VideoNuLA the machine has not got, and offers to fit one, which restarts the
 * emulator. In a shared session fitting one would end the session, so it only says so.
 * @param {{inSession: boolean, fit: function(): void}} options
 */
export function offerVideoNula({ inSession, fit }) {
    noteFeature(VideoNulaFeature, "missing");
    const missing = "This program tried to use a VideoNuLA, which isn't fitted.";
    toast(inSession ? `${missing} ${AfterSession}` : missing, {
        title: "VideoNuLA",
        quietKey: "quietVideoNulaMissing",
        action: inSession ? null : { label: "Fit one and restart", run: fit },
    });
}
 
/**
 * The page's answer to a program writing to the missing VideoNuLA: an offer, made once the emulator has let go, since
 * the write is raised mid-instruction; `inSession()` says whether a shared session is running, and fitting one sets
 * it in `settings`, so the URL records it, and then `restart`s, unless a session has started since the offer was made.
 */
export const answerMissingVideoNula =
    ({ inSession, settings, restart = () => window.location.reload() }) =>
    () =>
        setTimeout(() =>
            offerVideoNula({
                inSession: inSession(),
                fit: () => {
                    if (inSession()) {
                        toast(AfterSession, { title: "VideoNuLA" });
                        return;
                    }
                    settings.set({ hasVideoNula: true });
                    restart();
                },
            }),
        );