Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 54cb0b5

Browse files
committed
Only load one copy of db file and share it if reused
1 parent d2c5ad5 commit 54cb0b5

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

runestone/activecode/js/activecode.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var isMouseDown = false;
77
document.onmousedown = function() { isMouseDown = true };
88
document.onmouseup = function() { isMouseDown = false };
99
var edList = {};
10+
var allDburls = {};
1011

1112
ActiveCode.prototype = new RunestoneBase();
1213
var socket, connection, doc;
@@ -2397,8 +2398,23 @@ SQLActiveCode.prototype.init = function(opts) {
23972398
if (! self.dburl.startsWith("http")) {
23982399
self.dburl = window.location.protocol + '//' + window.location.host + self.dburl;
23992400
}
2400-
var xhr = new XMLHttpRequest();
24012401
$(self.runButton).attr('disabled','disabled')
2402+
if (! (self.dburl in allDburls)) {
2403+
allDburls[self.dburl] = {status: 'loading', xWaitFor: jQuery.Deferred() };
2404+
} else {
2405+
if (allDburls[self.dburl].status == 'loading') {
2406+
allDburls[self.dburl].xWaitFor.done(function() {
2407+
self.db = new SQL.Database(allDburls[self.dburl].db);
2408+
$(self.runButton).removeAttr('disabled')
2409+
});
2410+
return;
2411+
}
2412+
self.db = new SQL.Database(allDburls[self.dburl].db);
2413+
$(self.runButton).removeAttr('disabled')
2414+
return;
2415+
}
2416+
var xhr = new XMLHttpRequest();
2417+
24022418
// For example: https://github.com/lerocha/chinook-database/raw/master/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite
24032419
xhr.open('GET', self.dburl, true);
24042420
xhr.responseType = 'arraybuffer';
@@ -2407,6 +2423,9 @@ SQLActiveCode.prototype.init = function(opts) {
24072423
var uInt8Array = new Uint8Array(xhr.response);
24082424
self.db = new SQL.Database(uInt8Array);
24092425
$(self.runButton).removeAttr('disabled')
2426+
allDburls[self.dburl].db = uInt8Array;
2427+
allDburls[self.dburl].status = 'ready';
2428+
allDburls[self.dburl].xWaitFor.resolve();
24102429
// contents is now [{columns:['col1','col2',...], values:[[first row], [second row], ...]}]
24112430
};
24122431
xhr.send();

0 commit comments

Comments
 (0)