UNPKG

@rocketmakers/api-swr

Version:

Rocketmakers front-end library for parsing a generated Typescript API client into a set of configurable React hooks for fetching and mutating data.

30 lines (29 loc) 1.16 kB
"use strict"; 'use client'; Object.defineProperty(exports, "__esModule", { value: true }); exports.useClearCache = void 0; const tslib_1 = require("tslib"); const React = tslib_1.__importStar(require("react")); const swr_1 = require("swr"); /** * A custom hook that provides a function to clear the SWR cache. * * @example * const clearCache = useClearCache(); * clearCache(); // Clears the SWR cache */ const useClearCache = () => { const { mutate, cache } = (0, swr_1.useSWRConfig)(); React.useEffect(() => { return () => { // show deprecation warning // eslint-disable-next-line no-console -- legitimate deprecation warning console.warn('useClearCache is deprecated and will be removed in the next major version of swr. Please use the `clearAll` function from the `useCacheManager` hook instead.'); }; }, []); const clearCache = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { yield Promise.all(Array.from(cache.keys()).map((key) => mutate(key, undefined, { revalidate: false }))); }); return clearCache; }; exports.useClearCache = useClearCache;