diff --git a/.changeset/hot-kangaroos-film.md b/.changeset/hot-kangaroos-film.md new file mode 100644 index 0000000..4f0b46c --- /dev/null +++ b/.changeset/hot-kangaroos-film.md @@ -0,0 +1,5 @@ +--- +"@upstash/react-databrowser": patch +--- + +fix bug with cursor parsing diff --git a/packages/react-databrowser/package.json b/packages/react-databrowser/package.json index f1efd16..e8335d3 100644 --- a/packages/react-databrowser/package.json +++ b/packages/react-databrowser/package.json @@ -58,6 +58,6 @@ "@radix-ui/react-toast": "^1.1.5", "@radix-ui/react-tooltip": "^1.0.7", "@tanstack/react-query": "^5.32.0", - "@upstash/redis": "^1.31.3" + "@upstash/redis": "^1.31.4" } } diff --git a/packages/react-databrowser/src/components/databrowser/hooks/useFetchPaginatedKeys.ts b/packages/react-databrowser/src/components/databrowser/hooks/useFetchPaginatedKeys.ts index d8c651d..49b36cf 100644 --- a/packages/react-databrowser/src/components/databrowser/hooks/useFetchPaginatedKeys.ts +++ b/packages/react-databrowser/src/components/databrowser/hooks/useFetchPaginatedKeys.ts @@ -32,8 +32,9 @@ class PaginatedRedis { // console.log("************** RESET"); } - cache: Record = Object.fromEntries( - dataTypes.map((type) => [type, { cursor: 0, keys: [] }]), + // Cursor is 0 initially, then it is set to -1 when we reach the end + cache: Record = Object.fromEntries( + dataTypes.map((type) => [type, { cursor: "0", keys: [] }]), ); targetCount = 0; @@ -57,32 +58,26 @@ class PaginatedRedis { while (true) { const cursor = this.cache[type].cursor; - if (cursor === -1 || this.getLength() >= this.targetCount) { + if (cursor === "-1" || this.getLength() >= this.targetCount) { break; } - const [nextCursorStr, newKeys] = await this.redis.scan(cursor, { + const [nextCursor, newKeys] = await this.redis.scan(cursor, { count: fetchCount, match: this.searchTerm, type: type, }); - const nextCursor = Number(nextCursorStr); - - const stringifiedKeys = (newKeys as unknown[]).map((key) => - typeof key === "string" ? key : JSON.stringify(key), - ); - fetchCount = Math.min(fetchCount * 2, MAX_FETCH_COUNT); // console.log("< scan", type, newKeys.length, nextCursor === 0 ? "END" : "MORE"); // Dedupe here because redis can and will return duplicates for example when // a key is deleted because of ttl etc. - const dedupedSet = new Set([...this.cache[type].keys, ...stringifiedKeys]); + const dedupedSet = new Set([...this.cache[type].keys, ...newKeys]); this.cache[type].keys = [...dedupedSet]; - this.cache[type].cursor = nextCursor === 0 ? -1 : nextCursor; + this.cache[type].cursor = nextCursor === "0" ? "-1" : nextCursor; } }; @@ -94,7 +89,7 @@ class PaginatedRedis { isFetching = false; private isAllEnded() { - return (this.typeFilter ? [this.typeFilter] : dataTypes).every((type) => this.cache[type].cursor === -1); + return (this.typeFilter ? [this.typeFilter] : dataTypes).every((type) => this.cache[type].cursor === "-1"); } async getPage(page: number) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f2657fa..637bb2a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -164,8 +164,8 @@ importers: specifier: ^5.32.0 version: 5.32.0(react@18.2.0) '@upstash/redis': - specifier: ^1.31.3 - version: 1.31.3 + specifier: ^1.31.4 + version: 1.31.4 devDependencies: '@types/node': specifier: ^18.15.11 @@ -1256,8 +1256,8 @@ packages: '@types/semver@7.5.8': resolution: {integrity: sha512-I8EUhyrgfLrcTkzV3TSsGyl1tSuPrEDzr0yd5m90UgNxQkyDXULk3b6MlQqTCpZpNtWe1K0hzclnZkTcLBe2UQ==} - '@upstash/redis@1.31.3': - resolution: {integrity: sha512-KtVgWBUEx/LGbR8oRwYexwzHh3s5DNqYW0bjkD+gjFZVOnREJITvK+hC4PjSSD+8D4qJ+Xbkfmy8ANADZ9EUFg==} + '@upstash/redis@1.31.4': + resolution: {integrity: sha512-roJjsdAOUWGVCuiA5R2ajkR41n0KrO4DGil2ZbRaj3m0F2bMUMZ3d5KaUGgDQshPpGkW2+nwFLzN5M5VQoR1pQ==} '@vitejs/plugin-react@4.2.1': resolution: {integrity: sha512-oojO9IDc4nCUUi8qIR11KoQm0XFFLIwsRBwHRR4d/88IWghn1y6ckz/bJ8GHDCsYEJee8mDzqtJxh15/cisJNQ==} @@ -4107,7 +4107,7 @@ snapshots: '@types/semver@7.5.8': {} - '@upstash/redis@1.31.3': + '@upstash/redis@1.31.4': dependencies: crypto-js: 4.2.0