Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Prefix to the watchdog #490 #494

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ async function connectCMD(shell:Shell, args: string[]) {
const firstGate = (await Preferences.get({key: "first_gate"})).value
const timeout = (firstGate == "nope") ? undefined : 10000

shell.startWatchdog(timeout).catch(e => gate.handleFailure(e))
shell.startWatchdog({ timeout }).catch(e => gate.handleFailure(e))
if (terminal7.activeG && !terminal7.isActive(gate)) {
terminal7.activeG.stopBoarding()
terminal7.activeG.blur()
Expand Down Expand Up @@ -638,7 +638,7 @@ async function subscribeCMD(shell: Shell) {
return
if (choice == "Restore Purchases") {
shell.t.writeln("Restoring purchases")
shell.startWatchdog(10000).catch(e => {
shell.startWatchdog({ timeout: 10000 }).catch(e => {
shell.t.writeln("Sorry, restore command timed out")
shell.t.writeln("Please try again or `support`")
throw e
Expand All @@ -661,7 +661,7 @@ async function subscribeCMD(shell: Shell) {
} else {
const p = packages.find(p => p.prompt == choice)
shell.t.writeln("Thank you 🙏 directing you to the store")
shell.startWatchdog(120000).catch(e => {
shell.startWatchdog({ timeout : 120000 }).catch(e => {
shell.t.writeln("Sorry, subscribe command timed out")
shell.t.writeln("Please try again or `support`")
throw e
Expand Down Expand Up @@ -939,7 +939,7 @@ export async function installCMD(shell: Shell, args: string[]) {
if (done) {
// wait for the gate to get an fp
let timedOut = false
shell.startWatchdog(terminal7.conf.net.timeout)
shell.startWatchdog({ timeout: terminal7.conf.net.timeout })
.catch(() => timedOut = true)
while (!gate.fp && ! timedOut)
await (new Promise(r => setTimeout(r, 100)))
Expand Down Expand Up @@ -1002,7 +1002,7 @@ async function supportCMD(shell: Shell) {
let description = await shell.askValue("Please explain how to recreate the issue: \n")
description += `\n\nOn: ${navigator.userAgent}\n`
shell.t.writeln("Sending...")
shell.startWatchdog(5000).catch(() => sendFailed())
shell.startWatchdog({ timeout: 5000 }).catch(() => sendFailed())
const res = await fetch(`${schema}://${terminal7.conf.net.peerbook}/support`, {
method: "POST",
body: JSON.stringify({
Expand Down Expand Up @@ -1081,7 +1081,7 @@ async function loginCMD(shell: Shell) {
}
shell.t.writeln(`PeerBook response: ${await res.text()}`)
let timedOut = false
shell.startWatchdog(180000).catch(() => timedOut = true)
shell.startWatchdog({ timeout: 180000 }).catch(() => timedOut = true)
while (!terminal7.pb.uid && !timedOut) {
await new Promise(r => setTimeout(r, 2000))
try {
Expand Down
2 changes: 1 addition & 1 deletion src/peerbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class PeerbookConnection {
this.echo("and use it to generate a One Time Password")
// verify ourselves - it's the first time and we were approved thanks
// to the revenuecat's user id
this.shell.startWatchdog(3000).catch(() => {
this.shell.startWatchdog({ timeout: 3000 }).catch(() => {
this.shell.t.writeln("Timed out waiting for OTP")
this.shell.printPrompt()
})
Expand Down
10 changes: 5 additions & 5 deletions src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,17 +306,17 @@ export class Shell {
/*
* Starts a watchdog that will reject if not stopped within the given time
*/
startWatchdog(timeout? : number): Promise<void> {
startWatchdog({ timeout, prefix = 'Waiting' }: { timeout?: number; prefix?: string } = {} ): Promise<void> {
if (!timeout)
timeout = terminal7.conf.net.timeout
// only one watchdog, the last one
if (this.watchdog)
this.stopWatchdog()
return new Promise((resolve, reject) => {
this.watchdogResolve = resolve
this.startHourglass(timeout)
this.startHourglass({timeout , prefix})
this.watchdog = terminal7.run(() => {
terminal7.log("shell watchdog timeout")
terminal7.log(`shell watchdog timeout`)
this.stopWatchdog()
reject(Failure.TimedOut)
}, timeout)
Expand All @@ -336,15 +336,15 @@ export class Shell {
this.printPrompt()
}

startHourglass(timeout: number) {
startHourglass({ timeout, prefix = 'Waiting' } : { timeout: number; prefix?: string } ) {
if (this.timer) return
this.map.showLog(true)
const len = 20,
interval = timeout / len
let i = 0
this.timer = window.setInterval(() => {
const dots = Math.max(0, len - i) // i should never be > len, but just in case
this.t.write(`\r\x1B[K${" ".repeat(i)}ᗧ${"·".repeat(dots)}🍒\x1B[?25l`)
this.t.write(`\r\x1B[K${prefix}${" ".repeat(i)}ᗧ${"·".repeat(dots)}🍒\x1B[?25l`)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Consider making parameter types consistent with startWatchdog.

While the changes implement the requested prefix feature, there's an inconsistency in how parameters are handled between startWatchdog and startHourglass:

  • In startWatchdog, both timeout and prefix are optional
  • In startHourglass, timeout is required but prefix is optional

This could lead to confusion since these methods are closely related.

Consider making both parameters optional in startHourglass to match startWatchdog:

-    startHourglass({ timeout, prefix = 'Waiting' } : { timeout: number; prefix?: string } ) {
+    startHourglass({ timeout, prefix = 'Waiting' } : { timeout?: number; prefix?: string } = {} ) {
         if (this.timer) return
         this.map.showLog(true)
         const len = 20,
-            interval = timeout / len
+            interval = (timeout || terminal7.conf.net.timeout) / len

Committable suggestion was skipped due to low confidence.

i++
}, interval)
}
Expand Down