From e6850b9d57cbbd81a7b9a022625451765b91f61f Mon Sep 17 00:00:00 2001 From: Art <4998038+Alorel@users.noreply.github.com> Date: Tue, 17 Oct 2023 14:21:30 +0100 Subject: [PATCH] fix: Add missing documentation to published package --- src/core.ts | 30 ++++++++++++++++++++++++++++-- src/index.ts | 5 +++++ tsconfig.base.json | 1 - 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/core.ts b/src/core.ts index 7c4a833..8787089 100644 --- a/src/core.ts +++ b/src/core.ts @@ -16,7 +16,12 @@ function applyRename(origFn: F, label: string, newFn: F): F } /** - * Direct access to a memoised method's cache. Only appears after the method's been called at least once. + * Cache associated with this function/method if it's been processed with one of: + * + * - {@link Memoise} + * - {@link MemoiseAll} + * - {@link memoiseFunction} + * - {@link memoiseArglessFunction} */ export const MEMOISE_CACHE: unique symbol = Symbol('Memoise cache'); @@ -33,19 +38,35 @@ export type Decorator = ( ctx: ClassMethodDecoratorContext> ) => undefined | Fn; +/** @see {MEMOISE_CACHE} */ export interface Cache { + + /** Clear the cache */ clear(): void; + /** + * Delete a specific cache entry. + * @param key The result of passing the method call args through the associated {@link SerialiserFn serialiser fn} + */ delete(key: any): boolean; + /** + * Check if a specific cache entry exists. + * @param key See {@link Cache#delete delete()} + */ has(key: any): boolean; } -/** The default serialiser */ +/** The default cache key {@link SerialiserFn serialiser}. */ export function defaultSerialiser(...args: any[]): string { return JSON.stringify(args); } +/** + * Memoise the function's return value based on call arguments + * @param fn The function to memoise + * @param serialiser Serialiser to use for generating the cache key. Defaults to {@link defaultSerialiser}. + */ export function memoiseFunction( fn: Fn, serialiser: SerialiserFn = defaultSerialiser @@ -69,6 +90,11 @@ export function memoiseFunction( return memoisedFunction; } +/** + * Memoise the function's return value disregarding call arguments, + * effectively turning it into a lazily-evaluated value + * @param fn The function to memoise + */ export function memoiseArglessFunction(fn: Fn): Fn { let firstCall = true; let returnValue: R; diff --git a/src/index.ts b/src/index.ts index b333b2c..b29028e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,15 @@ import type {Cache, Decorator, SerialiserFn} from './core'; import {applyDecorator, defaultSerialiser, MEMOISE_CACHE, memoiseArglessFunction, memoiseFunction} from './core'; +/** + * Memoise the method's return value based on call arguments + * @param serialiser Serialiser to use for generating the cache key. Defaults to {@link defaultSerialiser}. + */ function Memoise(serialiser?: SerialiserFn): Decorator { return applyDecorator(memoiseFunction, serialiser); } +/** Memoise the method's return value disregarding call arguments */ function MemoiseAll(): Decorator { return applyDecorator(memoiseArglessFunction); } diff --git a/tsconfig.base.json b/tsconfig.base.json index 65315f6..e5d4fa2 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -16,7 +16,6 @@ "allowUnusedLabels": false, "useUnknownInCatchVariables": false, "outDir": "dist", - "removeComments": true, "forceConsistentCasingInFileNames": true, "skipLibCheck": true, "noImplicitAny": true,