Skip to content

Commit

Permalink
fix: associate emitErros with keyv instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 20, 2022
1 parent d08005c commit 3a88e0a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
14 changes: 4 additions & 10 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,18 @@ class Keyv extends EventEmitter {
constructor ({ emitErrors = true, ...options } = {}) {
super()

const normalizedOptions = Object.assign(
Object.entries(Object.assign(
{
serialize: JSONB.stringify,
deserialize: JSONB.parse,
emitErrors: true,
store: new Map()
},
options
)
)).forEach(([key, value]) => (this[key] = value))

Object.keys(normalizedOptions).forEach(
key => (this[key] = normalizedOptions[key])
)

if (typeof this.store.on === 'function' && emitErrors) {
this.store.on('error', error => {
this.emit('error', error)
})
if (typeof this.store.on === 'function' && this.emitErrors) {
this.store.on('error', error => this.emit('error', error))
}

const generateIterator = iterator =>
Expand Down
26 changes: 13 additions & 13 deletions packages/redis/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ const EventEmitter = require('events')
const pEvent = require('p-event')
const Redis = require('ioredis')

const normalizeOptions = (...args) => Object.assign({ emitErrors: true }, ...args)

const normalizeArguments = (input, options) => {
if (input instanceof Redis) return [input, normalizeOptions(options)]
const { uri, ...normalizedOptions } = normalizeOptions(typeof input === 'string' ? { uri: input } : input, options)
return [new Redis(uri, normalizedOptions), normalizedOptions]
}

class KeyvRedis extends EventEmitter {
constructor (uri, options) {
super()

if (uri instanceof Redis) {
this.redis = uri
} else {
options = Object.assign(
{
emitErrors: true
},
typeof uri === 'string' ? { uri } : uri,
options
)
this.redis = new Redis(options.uri, options)
}
const [redis, normalizedOptions] = normalizeArguments(uri, options)

this.redis = redis
Object.entries(normalizedOptions).forEach(([key, value]) => (this[key] = value))

if (options && options.emitErrors !== false) {
if (this.emitErrors) {
this.redis.on('error', error => this.emit('error', error))
}
}
Expand Down

0 comments on commit 3a88e0a

Please sign in to comment.