Skip to content

Commit

Permalink
Fix for prompt totally blocking the event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed May 30, 2024
1 parent d8291c7 commit e577ed5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.15.4

- Fix for `prompt` totally blocking the event loop in the cli tool, preventing
synchronization.
- Make the watchdog slightly more invincible.

## 0.15.3

- Internal fix; Always unblock sync even if a vacuum fails
Expand Down
Binary file added db.db
Binary file not shown.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/kv",
"version": "0.15.3",
"version": "0.15.4",
"exports": {
".": "./mod.ts",
"./cli": "./src/cli/mod.ts"
Expand Down
10 changes: 9 additions & 1 deletion src/cli/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ export function registerCommand(command: string, handler: KVCliHandler) {
export async function main() {
const container: KVDBContainer = {};
let exit = false;

while (!exit) {
// Collect user input
const command = await prompt(Colors.blue(">"));
if (command === null) {
continue;
}

// Yield to event loop, allowing any scheduled tasks to be executed
await new Promise((resolve) => setTimeout(resolve, 0));

// Handle the user input
const commandSplit = command.split(" ");
const cmd = commandSplit[0].toLowerCase();
const startTime = performance.now(); // Start measuring time
Expand All @@ -42,5 +47,8 @@ export async function main() {
} else {
console.error(Colors.red("Invalid command.\n"));
}

// Yield to event loop, allowing any scheduled tasks to be executed
await new Promise((resolve) => setTimeout(resolve, 0));
}
}
4 changes: 3 additions & 1 deletion src/lib/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ export class KV extends EventEmitter {
this.watchdogTimer = setTimeout(
async () => {
// Make sure current run is done
await this.watchdogPromise;
try {
await this.watchdogPromise;
} catch (_e) { /* Ignore */ }

// Initiate a new run
this.watchdogPromise = this.watchdog();
Expand Down

0 comments on commit e577ed5

Please sign in to comment.