API reference · generated from source
Function: fnMemo()#
Call Signature#
function fnMemo<This, MethodArgs, Value>(target, context): FnMemoMethod<This, MethodArgs, Value>;Defined in: decorators.ts:629
Decorator that memoizes a method return value on a ReactiveNode.
Type Parameters#
| Type Parameter |
|---|
This extends ReactiveNode |
MethodArgs extends unknown[] |
Value |
Parameters#
| Parameter | Type |
|---|---|
target | FnMemoMethod<This, MethodArgs, Value> |
context | ClassMethodDecoratorContext<This, FnMemoMethod<This, MethodArgs, Value>> |
Returns#
FnMemoMethod<This, MethodArgs, Value>
Remarks#
Use @fnMemo or @fnMemo() for automatic dependency trapping. Pass a
function only when you want finer cache-key control; the function receives
the current instance and method arguments, so the values are read fresh each
time the method is called. The method arguments are always shallow-compared.
Cache semantics:
- No argument:
@fnMemoand@fnMemo()are interchangeable. Both run the method under automatic dependency trapping and recompute when the arguments or one of the trapped reads changes. - Returns
undefined: recompute when the arguments change, or on every reproxy of the ReactiveNode. - Returns
[]: recompute only when the arguments change. - Returns
[a, b, ...]: recompute when the arguments or comparisons shallow-change.
Each decorated method stores one cache cell per instance, containing the last argument list and return value.
@fnMemo is a cache, not a subscription. It does not emit Retree events or
trigger React renders by itself. Keep decorated methods deterministic for
the same arguments and dependency values.
Do not apply @fnMemo to static methods or methods that intentionally
perform side effects.
Example#
class ListFilter extends ReactiveNode {
list: Card[] = [];
searchText = "";
@fnMemo
filterBy(limit: number) {
return this.list
.filter((c) => c.text === this.searchText)
.slice(0, limit);
}
get dependencies() { return [this.dependency(this.list)]; }
}Pass explicit comparisons when the automatic trapper is broader than you want:
@fnMemo((self: ListFilter, limit: number) => [
self.list,
self.searchText,
limit,
])
filterByWithExplicitComparisons(limit: number) {
return this.list
.filter((c) => c.text === this.searchText)
.slice(0, limit);
}Call Signature#
function fnMemo<This, ComparisonArgs>(getComparisons?): <MethodArgs, Value>(target, context) => FnMemoMethod<This, MethodArgs, Value>;Defined in: decorators.ts:641
Decorator that memoizes a method return value on a ReactiveNode.
Type Parameters#
| Type Parameter | Default type |
|---|---|
This extends ReactiveNode | - |
ComparisonArgs extends unknown[] | [] |
Parameters#
| Parameter | Type |
|---|---|
getComparisons? | (self, ...args) => unknown[] | undefined |
Returns#
<MethodArgs, Value>(target, context) => FnMemoMethod<This, MethodArgs, Value>
Remarks#
Use @fnMemo or @fnMemo() for automatic dependency trapping. Pass a
function only when you want finer cache-key control; the function receives
the current instance and method arguments, so the values are read fresh each
time the method is called. The method arguments are always shallow-compared.
Cache semantics:
- No argument:
@fnMemoand@fnMemo()are interchangeable. Both run the method under automatic dependency trapping and recompute when the arguments or one of the trapped reads changes. - Returns
undefined: recompute when the arguments change, or on every reproxy of the ReactiveNode. - Returns
[]: recompute only when the arguments change. - Returns
[a, b, ...]: recompute when the arguments or comparisons shallow-change.
Each decorated method stores one cache cell per instance, containing the last argument list and return value.
@fnMemo is a cache, not a subscription. It does not emit Retree events or
trigger React renders by itself. Keep decorated methods deterministic for
the same arguments and dependency values.
Do not apply @fnMemo to static methods or methods that intentionally
perform side effects.
Example#
class ListFilter extends ReactiveNode {
list: Card[] = [];
searchText = "";
@fnMemo
filterBy(limit: number) {
return this.list
.filter((c) => c.text === this.searchText)
.slice(0, limit);
}
get dependencies() { return [this.dependency(this.list)]; }
}Pass explicit comparisons when the automatic trapper is broader than you want:
@fnMemo((self: ListFilter, limit: number) => [
self.list,
self.searchText,
limit,
])
filterByWithExplicitComparisons(limit: number) {
return this.list
.filter((c) => c.text === this.searchText)
.slice(0, limit);
}