From 753a468fcc2d51ecb4c7511a16c4caaf23bf17ea Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Thu, 29 Jul 2021 17:14:53 +0000 Subject: [PATCH] refactor: remove unnecessary promises interfaces --- packages/core/src/index.js | 105 +++++++++++++------------------------ 1 file changed, 35 insertions(+), 70 deletions(-) diff --git a/packages/core/src/index.js b/packages/core/src/index.js index 3e3f8ca..57a3480 100644 --- a/packages/core/src/index.js +++ b/packages/core/src/index.js @@ -6,18 +6,21 @@ const JSONB = require('json-buffer') class Keyv extends EventEmitter { constructor ({ emitErrors = true, ...options } = {}) { super() - this.options = Object.assign( + + const normalizeOptions = Object.assign( { namespace: 'keyv', serialize: JSONB.stringify, deserialize: JSONB.parse, - emitErrors: true + emitErrors: true, + store: new Map() }, options ) - this.store = this.options.store || new Map() - this.store.namespace = this.options.namespace + Object.keys(normalizeOptions).forEach( + key => (this[key] = normalizeOptions[key]) + ) if (typeof this.store.on === 'function' && emitErrors) { this.store.on('error', error => this.emit('error', error)) @@ -28,12 +31,8 @@ class Keyv extends EventEmitter { for await (const [key, raw] of typeof iterator === 'function' ? iterator() : iterator) { - const data = - typeof raw === 'string' ? this.options.deserialize(raw) : raw - if ( - !key.includes(this.options.namespace) || - typeof data !== 'object' - ) { + const data = typeof raw === 'string' ? this.deserialize(raw) : raw + if (!key.includes(this.namespace) || typeof data !== 'object') { continue } @@ -60,11 +59,11 @@ class Keyv extends EventEmitter { } _getKeyPrefix (key) { - return this.options.namespace ? `${this.options.namespace}:${key}` : key + return this.namespace ? `${this.namespace}:${key}` : key } _getKeyUnprefix (key) { - return this.options.namespace + return this.namespace ? key .split(':') .splice(1) @@ -72,75 +71,41 @@ class Keyv extends EventEmitter { : key } - get (key, options) { - const keyPrefixed = this._getKeyPrefix(key) - const store = this.store - return Promise.resolve() - .then(() => store.get(keyPrefixed)) - .then(data => { - return typeof data === 'string' ? this.options.deserialize(data) : data - }) - .then(data => { - if (data === undefined) { - return undefined - } + async get (key, { raw: asRaw = false } = {}) { + const raw = await this.store.get(this._getKeyPrefix(key)) + const data = typeof raw === 'string' ? await this.deserialize(raw) : raw - if (typeof data.expires === 'number' && Date.now() > data.expires) { - this.delete(key) - return undefined - } + if (typeof data.expires === 'number' && Date.now() > data.expires) { + await this.delete(key) + return undefined + } - return options && options.raw ? data : data.value - }) + return asRaw ? data : data.value } - has (key) { - const keyPrefixed = this._getKeyPrefix(key) - const store = this.store - if (typeof store.has === 'function') { - return Promise.resolve().then(() => store.has(keyPrefixed)) - } - - return Promise.resolve() - .then(() => store.get(keyPrefixed)) - .then(data => data !== undefined) + async has (key) { + return typeof this.store.has === 'function' + ? this.store.has(this._getKeyPrefix(key)) + : (await this.store.get(this._getKeyPrefix(key))) !== undefined } - set (key, value, ttl) { + async set (key, value, ttl) { if (value === undefined) return false - const keyPrefixed = this._getKeyPrefix(key) - if (typeof ttl === 'undefined') { - ttl = this.options.ttl - } - - if (ttl === 0) { - ttl = undefined - } - - const store = this.store - return Promise.resolve() - .then(() => { - const expires = typeof ttl === 'number' ? Date.now() + ttl : null - value = { value, expires } - return this.options.serialize(value) - }) - .then(value => store.set(keyPrefixed, value, ttl)) - .then(() => true) + if (ttl === undefined) ttl = this.ttl + if (ttl === 0) ttl = undefined + const expires = typeof ttl === 'number' ? Date.now() + ttl : null + const valueSerialized = await this.serialize({ value, expires }) + await this.store.set(this._getKeyPrefix(key), valueSerialized, ttl) + return true } - delete (key) { - const keyPrefixed = this._getKeyPrefix(key) - const store = this.store - return Promise.resolve().then(() => store.delete(keyPrefixed)) + async delete (key) { + return this.store.delete(this._getKeyPrefix(key)) } - clear () { - if (!this.options.namespace) { - return Promise.resolve().then(() => undefined) - } - - const store = this.store - return Promise.resolve().then(() => store.clear()) + async clear () { + if (!this.namespace) return undefined + return this.store.clear() } } module.exports = Keyv