@tanstack/query-core
Version:
The framework agnostic core that powers TanStack Query
1 lines • 2.81 kB
Source Map (JSON)
{"version":3,"file":"removable.cjs","names":["isValidTimeout","timeoutManager","isServerEnvironment"],"sources":["../../src/removable.ts"],"sourcesContent":["import { timeoutManager } from './timeoutManager'\nimport { isServer as isServerEnvironment } from './environmentManager'\nimport { isValidTimeout } from './utils'\nimport type { ManagedTimerId } from './timeoutManager'\n\n/**\n * The base class for cache entries that are garbage collected once nothing is using them —\n * `Query` and `Mutation` both extend it. `gcTime` controls how long an unused entry is kept.\n */\nexport abstract class Removable {\n gcTime!: number\n #gcTimeout?: ManagedTimerId\n\n /**\n * Clears the pending garbage collection timeout, so the entry is no longer scheduled for removal.\n * A subclass may override this to release what it holds on to as well — `Query` also cancels any\n * in-flight fetch.\n */\n destroy(): void {\n this.clearGcTimeout()\n }\n\n /**\n * Schedules the entry to be removed from its cache after `gcTime`, replacing any timeout scheduled\n * before. Nothing is scheduled if `gcTime` is `Infinity`.\n */\n protected scheduleGc(): void {\n this.clearGcTimeout()\n\n if (isValidTimeout(this.gcTime)) {\n this.#gcTimeout = timeoutManager.setTimeout(() => {\n this.optionalRemove()\n }, this.gcTime)\n }\n }\n\n /**\n * Updates `gcTime`, keeping the longest one seen so far. Without a value, it defaults to 5 minutes\n * on the client and `Infinity` on the server.\n * @param newGcTime - The `gcTime` from the latest options, if any.\n */\n protected updateGcTime(newGcTime: number | undefined): void {\n // Default to 5 minutes (Infinity for server-side) if no gcTime is set\n this.gcTime = Math.max(\n this.gcTime || 0,\n newGcTime ?? (isServerEnvironment() ? Infinity : 5 * 60 * 1000),\n )\n }\n\n /**\n * Cancels the scheduled removal, if there is one.\n */\n protected clearGcTimeout() {\n if (this.#gcTimeout !== undefined) {\n timeoutManager.clearTimeout(this.#gcTimeout)\n this.#gcTimeout = undefined\n }\n }\n\n /**\n * Called when the garbage collection timeout fires. Subclasses remove the entry from its cache\n * unless it is still in use, e.g. because it has observers.\n */\n protected abstract optionalRemove(): void\n}\n"],"mappings":";;;;;;;;;AASA,IAAsB,YAAtB,MAAgC;CAE9B;;;;;;CAOA,UAAgB;EACd,KAAK,eAAe;CACtB;;;;;CAMA,aAA6B;EAC3B,KAAK,eAAe;EAEpB,IAAIA,cAAAA,eAAe,KAAK,MAAM,GAC5B,KAAK,aAAaC,uBAAAA,eAAe,iBAAiB;GAChD,KAAK,eAAe;EACtB,GAAG,KAAK,MAAM;CAElB;;;;;;CAOA,aAAuB,WAAqC;EAE1D,KAAK,SAAS,KAAK,IACjB,KAAK,UAAU,GACf,cAAcC,2BAAAA,SAAoB,IAAI,WAAW,IACnD;CACF;;;;CAKA,iBAA2B;EACzB,IAAI,KAAK,eAAe,KAAA,GAAW;GACjC,uBAAA,eAAe,aAAa,KAAK,UAAU;GAC3C,KAAK,aAAa,KAAA;EACpB;CACF;AAOF"}