Skip to content

Commit 2fd241e

Browse files
committed
♻️ Move define signal property to a separate function
1 parent d202427 commit 2fd241e

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

src/core/query.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -383,16 +383,23 @@ export class Query<
383383
meta: this.meta,
384384
}
385385

386-
Object.defineProperty(queryFnContext, 'signal', {
387-
enumerable: true,
388-
get: () => {
389-
if (abortController) {
390-
this.abortSignalConsumed = true
391-
return abortController.signal
392-
}
393-
return undefined
394-
},
395-
})
386+
// Adds an enumerable signal property to the object that
387+
// which sets abortSignalConsumed to true when the signal
388+
// is read.
389+
const addSignalProperty = (object: unknown) => {
390+
Object.defineProperty(object, 'signal', {
391+
enumerable: true,
392+
get: () => {
393+
if (abortController) {
394+
this.abortSignalConsumed = true
395+
return abortController.signal
396+
}
397+
return undefined
398+
},
399+
})
400+
}
401+
402+
addSignalProperty(queryFnContext)
396403

397404
// Create fetch function
398405
const fetchFn = () => {
@@ -413,16 +420,7 @@ export class Query<
413420
meta: this.meta,
414421
}
415422

416-
Object.defineProperty(context, 'signal', {
417-
enumerable: true,
418-
get: () => {
419-
if (abortController) {
420-
this.abortSignalConsumed = true
421-
return abortController.signal
422-
}
423-
return undefined
424-
},
425-
})
423+
addSignalProperty(context)
426424

427425
if (this.options.behavior?.onFetch) {
428426
this.options.behavior?.onFetch(context)

0 commit comments

Comments
 (0)