UNPKG

studiocms

Version:

Astro Native CMS for AstroDB. Built from the ground up by the Astro community.

137 lines (136 loc) 5.41 kB
import { Effect, genLogger } from "../../../effect.js"; import { _ClearUnknownError, CacheContext, isCacheEnabled } from "../utils.js"; class SDKCore_CLEAR extends Effect.Service()( "studiocms/sdk/SDKCore/modules/clear", { dependencies: [], effect: genLogger("studiocms/sdk/SDKCore/modules/clear/effect")(function* () { const { pages, FolderList, folderTree, pageFolderTree, version } = yield* CacheContext; const CLEAR = { page: { /** * Clears a cached page by its ID. * @param id - The ID of the page to clear from the cache. * @returns An Effect that resolves when the operation is complete. * @throws {UnknownException} If an error occurs during the clearing process. */ byId: (id) => Effect.gen(function* () { const status = yield* isCacheEnabled; if (!status) return; pages.delete(id); return; }).pipe( Effect.catchTags({ UnknownException: (cause) => _ClearUnknownError("CLEAR.page.byId", cause) }) ), /** * Clears cached pages by their slug. * @param slug - The slug of the page to clear from the cache. * @returns An Effect that resolves when the operation is complete. * @throws {UnknownException} If an error occurs during the clearing process. */ bySlug: (slug) => Effect.gen(function* () { const status = yield* isCacheEnabled; if (!status) return; const keyIndex = []; for (const [key, cachedObject] of pages) { if (cachedObject.data.slug === slug) { keyIndex.push(key); } } for (const key of keyIndex) { pages.delete(key); } return; }).pipe( Effect.catchTags({ UnknownException: (cause) => _ClearUnknownError("CLEAR.page.bySlug", cause) }) ) }, /** * Clears all cached pages, folder trees, and folder lists. * @remarks * This method checks if the cache is enabled before clearing the pages, folder tree, and folder list. * If the cache is not enabled, it simply returns without performing any action. * @returns An Effect that resolves when the operation is complete. * @throws {UnknownException} If an error occurs during the clearing process. */ pages: () => Effect.gen(function* () { const status = yield* isCacheEnabled; if (!status) return; pages.clear(); pageFolderTree.clear(); folderTree.clear(); FolderList.clear(); return; }).pipe( Effect.catchTags({ UnknownException: (cause) => _ClearUnknownError("CLEAR.pages", cause) }) ), /** * Clears the cached latest version information. * @remarks * This method checks if the cache is enabled before clearing the latest version. * If the cache is not enabled, it simply returns without performing any action. * @returns An Effect that resolves when the operation is complete. * @throws {UnknownException} If an error occurs during the clearing process. */ latestVersion: () => Effect.gen(function* () { const status = yield* isCacheEnabled; if (!status) return; version.clear(); return; }).pipe( Effect.catchTags({ UnknownException: (cause) => _ClearUnknownError("CLEAR.latestVersion", cause) }) ), /** * Clears the cached folder tree and page folder tree. * @remarks * This method checks if the cache is enabled before clearing the folder tree and page folder tree. * If the cache is not enabled, it simply returns without performing any action. * @returns An Effect that resolves when the operation is complete. * @throws {UnknownException} If an error occurs during the clearing process. */ folderTree: () => Effect.gen(function* () { const status = yield* isCacheEnabled; if (!status) return; folderTree.clear(); pageFolderTree.clear(); return; }).pipe( Effect.catchTags({ UnknownException: (cause) => _ClearUnknownError("CLEAR.folderTree", cause) }) ), /** * Clears the cached folder list. * @remarks * This method checks if the cache is enabled before clearing the folder list. * If the cache is not enabled, it simply returns without performing any action. * @returns An Effect that resolves when the operation is complete. * @throws {UnknownException} If an error occurs during the clearing process. */ folderList: () => Effect.gen(function* () { const status = yield* isCacheEnabled; if (!status) return; FolderList.clear(); return; }).pipe( Effect.catchTags({ UnknownException: (cause) => _ClearUnknownError("CLEAR.folderList", cause) }) ) }; return CLEAR; }) } ) { } export { SDKCore_CLEAR };