Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Nov 6, 2024
1 parent cd343bd commit e483f4c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
22 changes: 12 additions & 10 deletions lib/handler/cache-revalidation-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ const DecoratorHandler = require('../handler/decorator-handler')
class CacheRevalidationHandler extends DecoratorHandler {
#successful = false
/**
* @type {(() => void)}
* @type {((boolean) => void)}
*/
#successCallback
#callback
/**
* @type {(import('../../types/dispatcher.d.ts').default.DispatchHandlers)}
*/
#handler

/**
* @param {() => void} successCallback Function to call if the cached value is valid
* @param {(boolean) => void} callback Function to call if the cached value is valid
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandlers} handler
*/
constructor (successCallback, handler) {
if (typeof successCallback !== 'function') {
throw new TypeError('successCallback must be a function')
constructor (callback, handler) {
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function')
}

super(handler)

this.#successCallback = successCallback
this.#callback = callback
this.#handler = handler
}

Expand Down Expand Up @@ -107,9 +107,9 @@ class CacheRevalidationHandler extends DecoratorHandler {
* @param {string[] | null} rawTrailers
*/
onComplete (rawTrailers) {
if (this.#successful) {
this.#successCallback()
} else if (typeof this.#handler.onComplete === 'function') {
this.#callback(this.#successful)

if (!this.#successful && typeof this.#handler.onComplete === 'function') {
this.#handler.onComplete(rawTrailers)
}
}
Expand All @@ -120,6 +120,8 @@ class CacheRevalidationHandler extends DecoratorHandler {
* @param {Error} err
*/
onError (err) {
this.#callback(false)

if (typeof this.#handler.onError === 'function') {
this.#handler.onError(err)
}
Expand Down
9 changes: 8 additions & 1 deletion lib/interceptor/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,15 @@ module.exports = (opts = {}) => {
'if-modified-since': new Date(value.cachedAt).toUTCString()
}
},
// TODO (fix): This is suspect...
new CacheRevalidationHandler(
() => respondWithCachedValue(stream, value),
(success) => {
if (success) {
respondWithCachedValue(stream, value)
} else {
stream.on('error', () => {}).destroy()
}
},
new CacheHandler(globalOpts, opts, handler)
)
)
Expand Down

0 comments on commit e483f4c

Please sign in to comment.