idlize

Форк
0
57 строк · 2.0 Кб
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 { __context, __id } from "../internals"
17
import { State } from "../states/State"
18

19
/**
20
 * @param name - a name of a context state
21
 * @returns the named context state or `undefined` if it does not exist
22
 * @memo:intrinsic
23
 */
24
export function contextLocal<Value>(name: string): State<Value> | undefined {
25
    return __context().stateBy<Value>(name, undefined)
26
}
27

28
/**
29
 * @param name - a name of a context state
30
 * @returns a value of a context state
31
 * @throws Error if a named context state does not exist
32
 * @memo:intrinsic
33
 */
34
export function contextLocalValue<Value>(name: string): Value {
35
    return __context().valueBy<Value>(name, undefined)
36
}
37

38
/**
39
 * Creates a named state with the specified value in the context scope.
40
 * The specified value will be propagated immediately during recomposition.
41
 * Note that the state name must not be dynamically changed.
42
 *
43
 * @param name - a name of a context state
44
 * @param value - a value of a named state to share within the given content
45
 * @param content - a scope content in which a named state is available
46
 * @memo:intrinsic
47
 */
48
export function contextLocalScope<Value>(
49
    name: string,
50
    value: Value,
51
    /** @memo */
52
    content: () => void
53
) {
54
    const scope = __context().scope<void>(__id(), 1, undefined, undefined, undefined, undefined)
55
    scope.param(0, value, undefined, name, true) // can be found by name
56
    return scope.unchanged ? scope.cached : scope.recache(content())
57
}
58

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

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

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

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