MCPcopy Create free account
hub / github.com/TanStack/query / removeObserver

Method removeObserver

packages/query-core/src/query.ts:636–661  ·  view source on GitHub ↗

* Unsubscribes an observer from the query. * @internal * @param observer - The observer to remove. When the last observer is removed, the in-flight * fetch is cancelled if its abort signal was consumed (otherwise only its retries are stopped), * and garbage collection is scheduled.

(observer: QueryObserver<any, any, any, any, any>)

Source from the content-addressed store, hash-verified

634 * and garbage collection is scheduled.
635 */
636 removeObserver(observer: QueryObserver<any, any, any, any, any>): void {
637 const index = this.observers.indexOf(observer)
638 if (index !== -1) {
639 this.observers.splice(index, 1)
640
641 if (!this.observers.length) {
642 // If the transport layer does not support cancellation
643 // we'll let the query continue so the result can be cached
644 if (this.#retryer) {
645 if (
646 this.#abortSignalConsumed ||
647 (this.state.fetchStatus === 'paused' &&
648 this.state.status === 'pending')
649 ) {
650 this.#retryer.cancel({ revert: true })
651 } else {
652 this.#retryer.cancelRetry()
653 }
654 }
655
656 this.scheduleGc()
657 }
658
659 this.#cache.notify({ type: 'observerRemoved', query: this, observer })
660 }
661 }
662
663 /**
664 * Returns the number of observers currently subscribed to this query.

Callers 6

destroyMethod · 0.45
#updateQueryMethod · 0.45
onUnsubscribeMethod · 0.45
resetMethod · 0.45
mutateMethod · 0.45
query.test.tsxFile · 0.45

Calls 2

cancelMethod · 0.80
notifyMethod · 0.45

Tested by

no test coverage detected