Skip to content

Commit

Permalink
Merge pull request #111 from microlinkhq/emit-errors
Browse files Browse the repository at this point in the history
fix: associate `emitErrors` with keyv instance
  • Loading branch information
Kikobeats authored May 22, 2022
2 parents d08005c + 18f7e86 commit 6b9b6ce
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
17 changes: 5 additions & 12 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,20 @@ const EventEmitter = require('events')
const JSONB = require('json-buffer')

class Keyv extends EventEmitter {
constructor ({ emitErrors = true, ...options } = {}) {
constructor (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.store.on('error', error => this.emit('error', error))
}

const generateIterator = iterator =>
Expand Down
11 changes: 7 additions & 4 deletions packages/offline/test/keyv-redis.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict'

const KeyvRedis = require('@keyvhq/redis')
const Keyv = require('@keyvhq/core')
const test = require('ava')

const keyvOffline = require('../src')

const keyvRedis = new KeyvRedis({
uri: 'redis://user:pass@localhost:1337',
maxRetriesPerRequest: 0,
emitErrors: false
const keyvRedis = new Keyv({
store: new KeyvRedis({
uri: 'redis://user:pass@localhost:1337',
maxRetriesPerRequest: 0,
emitErrors: false
})
})

test('.set return false if store is unreachable', async t => {
Expand Down
30 changes: 16 additions & 14 deletions packages/redis/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ 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, ...opts } = Object.assign(typeof input === 'string' ? { uri: input } : input, options)
const normalizedOpts = normalizeOptions(opts)
return [new Redis(uri, normalizedOpts), normalizedOpts]
}

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, { emitErrors }] = normalizeArguments(uri, options)

this.redis = redis

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

Expand Down

0 comments on commit 6b9b6ce

Please sign in to comment.