UNPKG

@jitl/notion-api

Version:

The missing companion library for the official Notion public API.

66 lines (65 loc) 2.62 kB
/** * This file implements a cache and cache implementation helpers. * @category Cache * @module */ import { AssetRequest } from '..'; import { Asset } from './assets'; import { Block, BlockWithChildren, Page, PageWithChildren } from './notion-api'; /** * @category Cache * @source */ export declare type CacheBehavior = /** Read from the cache, but don't update it */ 'read-only' /** Read from the cache, or update it if needed. */ | 'fill' /** Don't read from the cache, and update it with new values */ | 'refresh'; /** * Either returns a value by calling `fromCache`, or by calling `fromScratch`, * depending on `cacheBehavior`. * @category Cache * @param cacheBehavior `"fill"` by default. * @param fromCache Function to read the value from the cache. * @param fromScratch Function to compute the value from scratch. * @returns `[value, hit]` where `hit` is `true` if the value was fetched from the cache. */ export declare function getFromCache<T1, T2>(cacheBehavior: CacheBehavior | undefined, fromCache: () => T1 | undefined, fromScratch: () => Promise<T2>): [T1, true] | Promise<[T2, false]>; /** * Possibly call `fill` to fill the cache, depending on `cacheBehavior`. * @param cacheBehavior `"fill"` by default. * @param fill Function to fill the cache. * @category Cache */ export declare function fillCache(cacheBehavior: CacheBehavior | undefined, fill: () => void): void; /** * Possibly call `fill` to fill the cache, depending on `cacheBehavior` and `hit`. * If `hit` is true, or `cacheBehavior` is `"read-only"`, then `fill` is not called. * @param cacheBehavior `"fill"` by default. * @param fill Function to fill the cache. * @category Cache */ export declare function fillCache(cacheBehavior: CacheBehavior | undefined, hit: boolean, fill: () => void): void; /** * Stores values from the Notion API. * @category Cache */ export declare class NotionObjectIndex { /** Whole pages */ page: Map<string, Page>; pageWithChildren: Map<string, PageWithChildren>; /** Whole blocks */ block: Map<string, Block>; blockWithChildren: Map<string, BlockWithChildren>; /** Assets inside a block, page, etc. These are keyed by `getAssetRequestKey`. */ asset: Map<string, Asset>; /** Parent block ID, may also be a page ID. */ parentId: Map<string, string>; /** Parent page ID. */ parentPageId: Map<string, string | undefined>; addBlock(block: Block | BlockWithChildren, parent: Block | Page | string | undefined): void; addPage(page: Page | PageWithChildren): void; addAsset(request: AssetRequest, asset: Asset): void; }