Skip to content

Commit

Permalink
Rename mapCatch -> mapError
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Sep 15, 2024
1 parent fd1f4b4 commit c010e73
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,20 +229,20 @@ db.getRange({ start, end, offset: 10, limit: 10 }) // skip first 10 and get next
If you want to get a true array from the range results, the `asArray` property will return the results as an array.

### Catching Errors in Range Iteration
With an array, `map` and `filter` callbacks are immediately executed, but with range iterators, they are executed during iteration, so if an error occurs during iteration, the error will be thrown when the iteration is attempted. It is also critical that when an iteration is finished, the cursor is closed, so by default, if an error occurs during iteration, the cursor will immediately be closed. However, if you want to catch errors that occur in `map` (and `flatMap`) callbacks during iteration, you can use the `mapCatch` method to catch errors that occur during iteration, and allow iteration to continue (without closing the cursor). For example:
With an array, `map` and `filter` callbacks are immediately executed, but with range iterators, they are executed during iteration, so if an error occurs during iteration, the error will be thrown when the iteration is attempted. It is also critical that when an iteration is finished, the cursor is closed, so by default, if an error occurs during iteration, the cursor will immediately be closed. However, if you want to catch errors that occur in `map` (and `flatMap`) callbacks during iteration, you can use the `mapError` method to catch errors that occur during iteration, and allow iteration to continue (without closing the cursor). For example:

```js
let mapped = db.getRange({ start, end }).map(({ key, value }) => {
return thisMightThrowError(value);
}).mapCatch((error) => {
}).mapError((error) => {
// rather than letting the error terminate the iteration, we can catch it here and return a value to continue iterating:
return 'error occurred';
})
for (let entry of mapped) {
...
}
```
A `mapCatch` callback can return a value to continue iterating, or throw an error to terminate the iteration.
A `mapError` callback can return a value to continue iterating, or throw an error to terminate the iteration.

#### Snapshots
By default, a range iterator will use a database snapshot, using a single read transaction that remains open and gives a consistent view of the database at the time it was started, for the duration of iterating through the range. However, if the iteration will take place over a long period of time, keeping a read transaction open for a long time can interfere with LMDB's free space collection and reuse and increase the database size. If you will be using a long duration iterator, you can specify `snapshot: false` flag in the range options to indicate that it snapshotting is not necessary, and it can reset and renew read transactions while iterating, to allow LMDB to collect any space that was freed during iteration.
Expand Down
4 changes: 2 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1967,7 +1967,7 @@ describe('lmdb-js', function () {
if (v === 4) throw new Error('test');
return [v, v + 1];
})
.mapCatch((error) => {
.mapError((error) => {
return { error: error.toString() };
});
all = [];
Expand Down Expand Up @@ -2006,7 +2006,7 @@ describe('lmdb-js', function () {
}
}).to.throw();
expect(finished).to.be.equal(1);
let aMappedWithCaught = aMappedWithError.mapCatch((error) => {
let aMappedWithCaught = aMappedWithError.mapError((error) => {
return { error: error.toString() };
});
all = [];
Expand Down
16 changes: 8 additions & 8 deletions util/RangeIterable.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ export class RangeIterable {
throw error; // throw to next catch to handle
}
} catch (error) {
if (iterable.mapError) {
// if we have mapError, we can use it to further handle errors
if (iterable.handleError) {
// if we have handleError, we can use it to further handle errors
try {
result = iterable.mapError(error, i);
} catch (mapError) {
return this.throw(mapError);
result = iterable.handleError(error, i);
} catch (error2) {
return this.throw(error2);
}
} else return this.throw(error);
}
Expand Down Expand Up @@ -212,7 +212,7 @@ export class RangeIterable {
try {
let result = iterator.next();
if (result.then) {
if (!async)
if (!options.async)
throw new Error(
'Can not synchronously iterate with promises as iterator results',
);
Expand Down Expand Up @@ -355,7 +355,7 @@ export class RangeIterable {
}
return element;
});
iterable.mapError = (error, i) => {
iterable.handleError = (error, i) => {
if (i < start) return SKIP;
if (i >= end) {
return DONE;
Expand All @@ -364,7 +364,7 @@ export class RangeIterable {
};
return iterable;
}
mapCatch(catch_callback) {
mapError(catch_callback) {
let iterable = this.map((element) => {
return element;
});
Expand Down

0 comments on commit c010e73

Please sign in to comment.