idlize

Форк
0
90 строк · 2.7 Кб
1
/*
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
6
 *
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 *
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.
14
 */
15

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"
20

21
/**
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
26
 * @internal
27
 */
28
export function memoRoot<Node extends IncrementalNode>(
29
    node: Node,
30
    /** @memo */
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()
41
    }, undefined)
42
}
43

44
/**
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.
47
 * @internal
48
 * @memo:entry
49
 */
50
export function memoEntry<R>(
51
    __memo_context: StateContext,
52
    __memo_id: KoalaCallsiteKey,
53
    /** @memo */
54
    entry: () => R,
55
): R {
56
    return entry()
57
}
58

59
/**
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.
62
 * @internal
63
 * @memo:entry
64
 */
65
export function memoEntry1<T, R>(
66
    __memo_context: StateContext,
67
    __memo_id: KoalaCallsiteKey,
68
    /** @memo */
69
    entry: (arg: T) => R,
70
    arg: T,
71
): R {
72
    return entry(arg)
73
}
74

75
/**
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.
78
 * @internal
79
 * @memo:entry
80
 */
81
export function memoEntry2<T1, T2, R>(
82
    __memo_context: StateContext,
83
    __memo_id: KoalaCallsiteKey,
84
    /** @memo */
85
    entry: (arg1: T1, arg2: T2) => R,
86
    arg1: T1,
87
    arg2: T2,
88
): R {
89
    return entry(arg1, arg2)
90
}
91

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.