Loading static/js/jslinux.api.mjs +3 −11 Original line number Diff line number Diff line /** API to interact with lib/jslinux **/ import { waitFor } from "./util.mjs" /** * Try to parse the given line as if it contained a prompt, maybe followed by user input. * Loading Loading @@ -160,17 +162,7 @@ function getCurrentPrompt() { * Returns the current prompt as soon as one is shown */ function prompt() { return new Promise((resolve)=>{ const intervalId = setInterval(checkAndResolve, 100) function checkAndResolve() { const currentPrompt = getCurrentPrompt() if(currentPrompt !== null) { clearInterval(intervalId) resolve(currentPrompt) } } }) return waitFor(getCurrentPrompt) } /** Loading static/js/util.mjs +18 −0 Original line number Diff line number Diff line Loading @@ -26,3 +26,21 @@ export function createElementWithClass(tagName, ...classes) { element.classList.add(...classes) return element } /** * Wait until "check" returns a truthy value, then, return it * @param {() => any} check A function that returns any value */ export async function waitFor(check) { return new Promise((resolve)=>{ const intervalId = setInterval(checkAndResolve, 100) function checkAndResolve() { const predicateValue = check() if(predicateValue) { clearInterval(intervalId) resolve(predicateValue) } } }) } Loading
static/js/jslinux.api.mjs +3 −11 Original line number Diff line number Diff line /** API to interact with lib/jslinux **/ import { waitFor } from "./util.mjs" /** * Try to parse the given line as if it contained a prompt, maybe followed by user input. * Loading Loading @@ -160,17 +162,7 @@ function getCurrentPrompt() { * Returns the current prompt as soon as one is shown */ function prompt() { return new Promise((resolve)=>{ const intervalId = setInterval(checkAndResolve, 100) function checkAndResolve() { const currentPrompt = getCurrentPrompt() if(currentPrompt !== null) { clearInterval(intervalId) resolve(currentPrompt) } } }) return waitFor(getCurrentPrompt) } /** Loading
static/js/util.mjs +18 −0 Original line number Diff line number Diff line Loading @@ -26,3 +26,21 @@ export function createElementWithClass(tagName, ...classes) { element.classList.add(...classes) return element } /** * Wait until "check" returns a truthy value, then, return it * @param {() => any} check A function that returns any value */ export async function waitFor(check) { return new Promise((resolve)=>{ const intervalId = setInterval(checkAndResolve, 100) function checkAndResolve() { const predicateValue = check() if(predicateValue) { clearInterval(intervalId) resolve(predicateValue) } } }) }