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

repl: only save new history entries, use transaction #664

Merged
merged 2 commits into from
Oct 12, 2024
Merged
Changes from 1 commit
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
18 changes: 14 additions & 4 deletions src/js/run-repl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ import { Database } from 'tjs:sqlite';

var historyDb;
var history = [];
var history_load_index = 0;
var history_index;
var clip_board = "";
var pstate = "";
Expand Down Expand Up @@ -1511,10 +1512,12 @@ import { Database } from 'tjs:sqlite';
function clear_history() {
try {
historyDb.exec('DELETE FROM history');
historyDb.exec('VACUUM');
} catch (_) {}

history = [];
history_index = 0;
history_load_index = 0;
}

function exit(e) {
Expand Down Expand Up @@ -1881,10 +1884,16 @@ import { Database } from 'tjs:sqlite';

function save_history() {
if (historyDb) {
for (const str of history) {
try {
historyDb.prepare('INSERT INTO history (entry) VALUES(?)').run(str);
} catch (_) {}
try {
const insert = historyDb.prepare('INSERT INTO history (entry) VALUES(?)');
const insertMany = historyDb.transaction(entries => {
for (const str of entries) {
insert.run(str);
}
});
insertMany(history.slice(history_load_index));
} catch (e) {
tjs.stderr.write(encoder.encode(`Failed to save history: ${e}\n`));
}
}
}
Expand Down Expand Up @@ -1919,6 +1928,7 @@ import { Database } from 'tjs:sqlite';
const data = historyDb.prepare('SELECT entry from history').all();

history = data.map(row => row.entry);
history_load_index = data.length;
}
function load_config() {
var m, s = std.getenv("COLORFGBG");
Expand Down
Loading