UNPKG

1.51 kBTypeScriptView Raw
1import { IComputedValueOptions } from "mobx";
2export declare type IComputedFnOptions<F extends (...args: any[]) => any> = {
3 onCleanup?: (result: ReturnType<F> | undefined, ...args: Parameters<F>) => void;
4} & IComputedValueOptions<ReturnType<F>>;
5/**
6 * computedFn takes a function with an arbitrary amount of arguments,
7 * and memoizes the output of the function based on the arguments passed in.
8 *
9 * computedFn(fn) returns a function with the very same signature. There is no limit on the amount of arguments
10 * that is accepted. However, the amount of arguments must be constant and default arguments are not supported.
11 *
12 * By default the output of a function call will only be memoized as long as the
13 * output is being observed.
14 *
15 * The function passes into `computedFn` should be pure, not be an action and only be relying on
16 * observables.
17 *
18 * Setting `keepAlive` to `true` will cause the output to be forcefully cached forever.
19 * Note that this might introduce memory leaks!
20 *
21 * @example
22 * const store = observable({
23 a: 1,
24 b: 2,
25 c: 3,
26 m: computedFn(function(x) {
27 return this.a * this.b * x
28 })
29 })
30
31 const d = autorun(() => {
32 // store.m(3) will be cached as long as this autorun is running
33 console.log(store.m(3) * store.c)
34 })
35 *
36 * @param fn
37 * @param keepAliveOrOptions
38 */
39export declare function computedFn<T extends (...args: any[]) => any>(fn: T, keepAliveOrOptions?: IComputedFnOptions<T> | boolean): T;