2
* Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
16
import { KoalaCallsiteKey, KoalaCallsiteKeys, KoalaProfiler } from "@koalaui/common"
17
import { GlobalStateManager } from "../states/GlobalStateManager"
18
import { ComputableState, StateContext } from "../states/State"
19
import { IncrementalNode } from "../tree/IncrementalNode"
22
* @param manager - a state manager
23
* @param node - a root node for the composition
24
* @param update - a memo-function to build and update the composition
25
* @returns updatable state with the given node as the composition root
28
export function memoRoot<Node extends IncrementalNode>(
31
update: (node: Node) => void,
32
): ComputableState<Node> {
33
const manager = GlobalStateManager.instance
34
return manager.updatableNode(node, (context: StateContext) => {
35
KoalaProfiler.counters?.buildRootEnter()
36
const frozen = manager.frozen
37
manager.frozen = true // states are frozen during recomposition
38
memoEntry1<Node, void>(context, KoalaCallsiteKeys.empty, update, node)
39
manager.frozen = frozen
40
KoalaProfiler.counters?.buildRootExit()
45
* This is where regular code can enter the memo realm.
46
* This function allows to run a memo function in a non-memo context.
50
export function memoEntry<R>(
51
__memo_context: StateContext,
52
__memo_id: KoalaCallsiteKey,
60
* This is where regular code can enter the memo realm.
61
* This function allows to run a memo function in a non-memo context.
65
export function memoEntry1<T, R>(
66
__memo_context: StateContext,
67
__memo_id: KoalaCallsiteKey,
76
* This is where regular code can enter the memo realm.
77
* This function allows to run a memo function in a non-memo context.
81
export function memoEntry2<T1, T2, R>(
82
__memo_context: StateContext,
83
__memo_id: KoalaCallsiteKey,
85
entry: (arg1: T1, arg2: T2) => R,
89
return entry(arg1, arg2)