This repository has been archived by the owner on Oct 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
120 lines (120 loc) · 4.82 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var RaananW;
(function (RaananW) {
var WorkedIDBTest;
(function (WorkedIDBTest) {
function generateData(length, maxDelay) {
if (maxDelay === void 0) { maxDelay = 25; }
var data = [];
for (var i = 0; i < length; ++i) {
data.push((Math.random() * maxDelay) >> 0);
}
return data;
}
WorkedIDBTest.generateData = generateData;
function openDatabase(dbName, dbVersion, deleteDatabase, successCallback) {
if (deleteDatabase) {
indexedDB.deleteDatabase(dbName);
}
var request = indexedDB.open(dbName, dbVersion);
request.onupgradeneeded = function (event) {
var db = event.target['result'];
var objStore = db.createObjectStore("numbers", { autoIncrement: true });
successCallback(db, objStore);
};
}
function storeInDb(objectStore, data, asArray, successCallback) {
var clearReq = objectStore.clear();
clearReq.onsuccess = function () {
if (asArray) {
var req = objectStore.add(data, "array");
req.onsuccess = function () {
successCallback();
};
}
else {
var added = 0;
for (var i = 0; i < data.length; ++i) {
var req = objectStore.add(data[i]);
req.onsuccess = function () {
if (++added == data.length) {
successCallback();
}
};
}
}
};
}
function getData(db, asArray, successCallback) {
var trans = db.transaction("numbers", IDBTransaction.READ_ONLY);
var store = trans.objectStore("numbers");
var items = [];
trans.oncomplete = function (evt) {
successCallback(items);
};
if (asArray) {
var req = store.get("array");
req.onsuccess = function (e) {
successCallback(e.target['result']);
};
}
else {
var cursorRequest = store.openCursor();
cursorRequest.onerror = function (error) {
console.log(error);
};
cursorRequest.onsuccess = function (evt) {
var cursor = evt.target['result'];
if (cursor) {
items.push(cursor.value);
cursor.continue();
}
};
}
}
function runSingleTest(data, asArray, webworker, successCallback, progress) {
if (!webworker) {
WorkedIDBTest.doWork(data, progress, function () {
successCallback(data);
});
}
else {
var db = openDatabase("webworkerTest", 1, true, function (db, objStore) {
console.log("db opened");
storeInDb(objStore, data, asArray, function () {
webworker.onmessage = function (ev) {
if (progress)
progress(ev.data);
if (ev.data === "finished") {
getData(db, asArray, function (sortedData) {
db.close();
successCallback();
webworker.onmessage = null;
});
}
};
webworker.postMessage({ dbName: "webworkerTest", dbVersion: 1, asArray: asArray });
});
});
}
}
WorkedIDBTest.runSingleTest = runSingleTest;
})(WorkedIDBTest = RaananW.WorkedIDBTest || (RaananW.WorkedIDBTest = {}));
})(RaananW || (RaananW = {}));
$(function () {
var data;
var webworker = new Worker('webworker.js');
$("#generateData").click(function () {
data = RaananW.WorkedIDBTest.generateData($("#numberOfItems").val(), $("#maxDelay").val());
}).click();
$(".runTest").click(function () {
var worker = $(this).data("worker") == true ? webworker : null;
$("#progressBar").width("0").removeClass("progress-bar-success");
RaananW.WorkedIDBTest.runSingleTest(data, false, worker, function () {
$("#progressBar").width("100%").addClass("progress-bar-success");
;
}, function (progress) {
$("#progressBar").width(progress + "%");
});
});
});
//# sourceMappingURL=app.js.map