Skip to content

Commit 2151cba

Browse files
committed
WIP
1 parent cd343bd commit 2151cba

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

‎lib/handler/cache-revalidation-handler.js‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@ const DecoratorHandler = require('../handler/decorator-handler')
1919
class CacheRevalidationHandler extends DecoratorHandler {
2020
#successful = false
2121
/**
22-
* @type {(() => void)}
22+
* @type {((boolean) => void)}
2323
*/
24-
#successCallback
24+
#callback
2525
/**
2626
* @type {(import('../../types/dispatcher.d.ts').default.DispatchHandlers)}
2727
*/
2828
#handler
2929

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

3939
super(handler)
4040

41-
this.#successCallback = successCallback
41+
this.#callback = callback
4242
this.#handler = handler
4343
}
4444

@@ -107,9 +107,9 @@ class CacheRevalidationHandler extends DecoratorHandler {
107107
* @param {string[] | null} rawTrailers
108108
*/
109109
onComplete (rawTrailers) {
110-
if (this.#successful) {
111-
this.#successCallback()
112-
} else if (typeof this.#handler.onComplete === 'function') {
110+
this.#callback(this.#successful)
111+
112+
if (!this.#successful && typeof this.#handler.onComplete === 'function') {
113113
this.#handler.onComplete(rawTrailers)
114114
}
115115
}
@@ -120,6 +120,8 @@ class CacheRevalidationHandler extends DecoratorHandler {
120120
* @param {Error} err
121121
*/
122122
onError (err) {
123+
this.#callback(false)
124+
123125
if (typeof this.#handler.onError === 'function') {
124126
this.#handler.onError(err)
125127
}

‎lib/interceptor/cache.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,13 @@ module.exports = (opts = {}) => {
146146
}
147147
},
148148
new CacheRevalidationHandler(
149-
() => respondWithCachedValue(stream, value),
149+
(success) => {
150+
if (success) {
151+
respondWithCachedValue(stream, value)
152+
} else {
153+
stream.on('error', () => {}).destroy()
154+
}
155+
},
150156
new CacheHandler(globalOpts, opts, handler)
151157
)
152158
)

0 commit comments

Comments
 (0)