diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-06-25 13:18:09 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-06-25 13:18:09 +0200 |
commit | b3fc710d9a3b481196074ddd9a84a59be1f07fb3 (patch) | |
tree | 7d6efff1433b6db3bcb4547b12499002b66d7905 /packages/idb-bridge | |
parent | c84361d3cbf5485ee924272047a3fbd78bc508f6 (diff) |
idb: make unique cursor work
Diffstat (limited to 'packages/idb-bridge')
-rw-r--r-- | packages/idb-bridge/src/MemoryBackend.test.ts | 26 | ||||
-rw-r--r-- | packages/idb-bridge/src/MemoryBackend.ts | 37 |
2 files changed, 51 insertions, 12 deletions
diff --git a/packages/idb-bridge/src/MemoryBackend.test.ts b/packages/idb-bridge/src/MemoryBackend.test.ts index b5bc5dad2..2e322dfa6 100644 --- a/packages/idb-bridge/src/MemoryBackend.test.ts +++ b/packages/idb-bridge/src/MemoryBackend.test.ts @@ -188,6 +188,32 @@ test("Spec: Example 1 Part 3", async t => { t.is(cursor.value.author, "Fred"); cursor.continue(); + await promiseFromRequest(request5); + cursor = request5.result; + t.is(cursor.value.author, "Fred"); + cursor.continue(); + + await promiseFromRequest(request5); + cursor = request5.result; + t.is(cursor, null); + + const request6 = index5.openCursor(null, "nextunique"); + + await promiseFromRequest(request6); + cursor = request6.result; + t.is(cursor.value.author, "Barney"); + cursor.continue(); + + await promiseFromRequest(request6); + cursor = request6.result; + t.is(cursor.value.author, "Fred"); + t.is(cursor.value.isbn, 123456); + cursor.continue(); + + await promiseFromRequest(request6); + cursor = request6.result; + t.is(cursor, null); + db.close(); t.pass(); diff --git a/packages/idb-bridge/src/MemoryBackend.ts b/packages/idb-bridge/src/MemoryBackend.ts index c72431f28..786fa4cf6 100644 --- a/packages/idb-bridge/src/MemoryBackend.ts +++ b/packages/idb-bridge/src/MemoryBackend.ts @@ -832,19 +832,32 @@ export class MemoryBackend implements Backend { break; } } - if ( - unique && - indexKeys.length > 0 && - compareKeys(indexEntry.indexKey, indexKeys[indexKeys.length - 1]) === - 0 - ) { - // We only return the first result if subsequent index keys are the same. - continue; + + // Skip repeated index keys if unique results are requested. + let skip = false; + if (unique) { + if ( + indexKeys.length > 0 && + compareKeys( + indexEntry.indexKey, + indexKeys[indexKeys.length - 1], + ) === 0 + ) { + skip = true; + } + if ( + req.lastIndexPosition !== undefined && + compareKeys(indexPos, req.lastIndexPosition) === 0 + ) { + skip = true; + } } - indexKeys.push(indexEntry.indexKey); - primaryKeys.push(indexEntry.primaryKeys[primkeySubPos]); - numResults++; - primkeySubPos = forward ? 0 : indexEntry.primaryKeys.length - 1; + if (!skip) { + indexKeys.push(indexEntry.indexKey); + primaryKeys.push(indexEntry.primaryKeys[primkeySubPos]); + numResults++; + } + primkeySubPos += forward ? 1 : -1; } // Now we can collect the values based on the primary keys, |