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 { __context, __id } from "../internals"
17
import { State } from "../states/State"
20
* @param name - a name of a context state
21
* @returns the named context state or `undefined` if it does not exist
24
export function contextLocal<Value>(name: string): State<Value> | undefined {
25
return __context().stateBy<Value>(name, undefined)
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
34
export function contextLocalValue<Value>(name: string): Value {
35
return __context().valueBy<Value>(name, undefined)
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.
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
48
export function contextLocalScope<Value>(
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())