• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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
17/**
18 * Generic Binding class to encapsulate read-only data binding.
19 *
20 * @template T - The type of the value to be bound.
21*/
22class Binding<T> {
23    private getter_: () => T;
24
25    /**
26     * Constructs a new Binding instance.
27     *
28     * @param getter - A function that returns the current value of type T.
29     */
30    constructor(getter: () => T) {
31        this.getter_ = getter;
32    }
33
34    /**
35     * Gets the current value of the binding.
36     *
37     * @returns The current value of type T.
38     */
39    public get value(): T {
40        return this.getter_();
41    }
42}
43
44/**
45 * Generic MutableBinding class to encapsulate both read and write operations.
46 *
47 * @template T - The type of the value to be bound.
48 */
49class MutableBinding<T> {
50    private getter_: () => T;
51    private setter_: (newValue: T) => void;
52
53    /**
54     * Constructs a new MutableBinding instance.
55     *
56     * @param getter - A function that returns the current value of type T.
57     * @param setter - A function that sets a new value of type T.
58     */
59    constructor(getter: () => T, setter: (newValue: T) => void) {
60        this.getter_ = getter;
61        this.setter_ = setter;
62    }
63
64    /**
65     * Sets a new value for the binding.
66     *
67     * @param newValue - The new value to be set of type T.
68     */
69    set value(newValue: T) {
70        this.setter_(newValue);
71    }
72
73    /**
74     * Gets the current value of the binding.
75     *
76     * @returns The current value of type T.
77     */
78    get value(): T {
79        return this.getter_();
80    }
81}