Skip to content

Commit

Permalink
Also catch exceptions in user callbacks for key/value streams.
Browse files Browse the repository at this point in the history
  • Loading branch information
paberr committed Mar 15, 2018
1 parent 9ca7f30 commit ffcde0f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jungle-db",
"version": "0.4.1",
"version": "0.4.2",
"main": "dist/node.js",
"private": true,
"scripts": {
Expand Down
12 changes: 8 additions & 4 deletions src/main/platform/browser/IDBBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,14 @@ class IDBBackend {
openCursorRequest.onsuccess = event => {
const cursor = event.target.result;
if (cursor) {
if (callback(cursor.primaryKey)) {
cursor.continue();
} else {
resolve();
try {
if (callback(cursor.primaryKey)) {
cursor.continue();
} else {
resolve();
}
} catch (e) {
reject(e);
}
} else {
resolve();
Expand Down
18 changes: 15 additions & 3 deletions src/main/platform/browser/PersistentIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ class PersistentIndex {
request.onsuccess = event => {
const cursor = event.target.result;
if (cursor) {
results.push(this._objectStore.decode(cursor.value, cursor.primaryKey));
try {
results.push(this._objectStore.decode(cursor.value, cursor.primaryKey));
} catch (e) {
reject(e);
}
cursor.continue();
} else {
resolve(results);
Expand Down Expand Up @@ -130,7 +134,11 @@ class PersistentIndex {
}
// Only iterate until key changes.
if (cursor && maxKey === cursor.key) {
results.push(this._objectStore.decode(cursor.value, cursor.primaryKey));
try {
results.push(this._objectStore.decode(cursor.value, cursor.primaryKey));
} catch (e) {
reject(e);
}
cursor.continue();
} else {
resolve(results);
Expand Down Expand Up @@ -193,7 +201,11 @@ class PersistentIndex {
}
// Only iterate until key changes.
if (cursor && maxKey === cursor.key) {
results.push(this._objectStore.decode(cursor.value, cursor.primaryKey));
try {
results.push(this._objectStore.decode(cursor.value, cursor.primaryKey));
} catch (e) {
reject(e);
}
cursor.continue();
} else {
resolve(results);
Expand Down
12 changes: 8 additions & 4 deletions src/main/platform/nodejs/LevelDBBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,14 @@ class LevelDBBackend {
const stream = this._dbBackend.createReadStream(LevelDBTools.convertKeyRange(query, { 'values': false, 'keys': true, 'reverse': !ascending }));
let stopped = false;
stream.on('data', data => {
if (!callback(data)) {
stopped = true;
stream.pause();
stream.destroy();
try {
if (!callback(data)) {
stopped = true;
stream.pause();
stream.destroy();
}
} catch (e) {
error(e);
}
})
.on('error', err => {
Expand Down

0 comments on commit ffcde0f

Please sign in to comment.