Skip to content

Commit

Permalink
refactor: avoid p-event dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Apr 28, 2024
1 parent e819f7a commit 662111a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
3 changes: 1 addition & 2 deletions packages/redis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
"value"
],
"dependencies": {
"ioredis": "~5.4.1",
"p-event": "~4.2.0"
"ioredis": "~5.4.1"
},
"devDependencies": {
"@keyvhq/core": "latest",
Expand Down
23 changes: 16 additions & 7 deletions packages/redis/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
'use strict'

const pEvent = require('p-event')
const Redis = require('ioredis')

const { promisify } = require('util')
const stream = require('stream')

const { Transform } = stream

const pipeline = promisify(stream.pipeline)

const normalizeArguments = (input, options) => {
if (input instanceof Redis) return input
const { uri, ...opts } = Object.assign(
Expand Down Expand Up @@ -37,13 +43,16 @@ class KeyvRedis {
async clear (namespace) {
const match = namespace ? `${namespace}:*` : '*'
const stream = this.redis.scanStream({ match })

const keys = []
stream.on('data', matchedKeys => keys.push(...matchedKeys))
await pEvent(stream, 'end')
if (keys.length > 0) {
await this.redis.unlink(keys)
}
const collectKeys = new Transform({
objectMode: true,
transform (chunk, _, next) {
keys.push.apply(keys, chunk)
next()
}
})
await pipeline(stream, collectKeys)
if (keys.length > 0) await this.redis.unlink(keys)
}

async * iterator (namespace) {
Expand Down

0 comments on commit 662111a

Please sign in to comment.