Skip to content

Commit

Permalink
fix: Add missing documentation to published package
Browse files Browse the repository at this point in the history
  • Loading branch information
Alorel committed Oct 17, 2023
1 parent 31d21d0 commit bd6fba3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
30 changes: 28 additions & 2 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ function applyRename<F extends Function>(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');

Expand All @@ -33,19 +38,35 @@ export type Decorator<T, A extends any[], R> = (
ctx: ClassMethodDecoratorContext<T, Fn<T, A, R>>
) => undefined | Fn<T, A, R>;

/** @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<T, A extends [any, ...any[]], R>(
fn: Fn<T, A, R>,
serialiser: SerialiserFn<T, A> = defaultSerialiser
Expand All @@ -69,6 +90,11 @@ export function memoiseFunction<T, A extends [any, ...any[]], R>(
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<T, R>(fn: Fn<T, [], R>): Fn<T, [], R> {
let firstCall = true;
let returnValue: R;
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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<T, A extends [any, ...any[]], R>(serialiser?: SerialiserFn<T, A>): Decorator<T, A, R> {
return applyDecorator<T, A, R>(memoiseFunction, serialiser);
}

/** Memoise the method's return value disregarding call arguments */
function MemoiseAll<T, R>(): Decorator<T, [], R> {
return applyDecorator<T, R>(memoiseArglessFunction);
}
Expand Down
1 change: 0 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"allowUnusedLabels": false,
"useUnknownInCatchVariables": false,
"outDir": "dist",
"removeComments": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"noImplicitAny": true,
Expand Down

0 comments on commit bd6fba3

Please sign in to comment.