idlize

Форк
0
/
GlobalStateManager.ts 
86 строк · 3.1 Кб
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 { Equivalent, MutableState, StateManager, ValueTracker, createStateManager } from "./State"
17

18
/**
19
 * This class provides an access to the global state manager of the application.
20
 * @internal
21
 */
22
export class GlobalStateManager {
23
    private static current: StateManager | undefined = undefined
24

25
    /**
26
     * The current instance of a global state manager.
27
     * Note that it will be recreated after reset.
28
     */
29
    static get instance(): StateManager {
30
        if (GlobalStateManager.current === undefined) {
31
            GlobalStateManager.current = createStateManager()
32
        }
33
        return GlobalStateManager.current!
34
    }
35

36
    /**
37
     * Drops the current instance to recreate a global state manager.
38
     * @internal
39
     */
40
    static reset() {
41
        GlobalStateManager.current?.reset()
42
    }
43
}
44

45
/**
46
 * Updates all states in the specified state manager.
47
 * If the manager is not specified, the global state manager will be updated.
48
 * @param manager - a state manager to update
49
 * @internal
50
 */
51
export function updateStateManager(manager: StateManager = GlobalStateManager.instance): void {
52
    manager.updateSnapshot()
53
}
54

55
/**
56
 * Calls all scheduled callbacks in the specified state manager.
57
 * If the manager is not specified, the global state manager will be used.
58
 * @param manager - a state manager to use
59
 * @internal
60
 */
61
export function callScheduledCallbacks(manager: StateManager = GlobalStateManager.instance): void {
62
    manager.callCallbacks()
63
}
64

65
/**
66
 * Performs the specified callback function later
67
 * (before the next recomposition and after the current one).
68
 * @param callback - a function to perform between recompositions
69
 */
70
export function scheduleCallback(callback?: () => void) {
71
    if (callback !== undefined) GlobalStateManager.instance.scheduleCallback(callback)
72
}
73

74
/**
75
 * Creates new mutable state in the global state manager.
76
 * This state is valid until it is manually detached from the manager.
77
 * It will be detached automatically if it is in the {@link remember}.
78
 * Note that thoughtless state disposing can lead to memory leaks.
79
 * @param value - initial value to initialize the created state
80
 * @param equivalent - optional value comparator for a state
81
 * @param tracker - optional tracker of values assigned to a state
82
 * @returns new mutable state trackable by memo-functions
83
 */
84
export function mutableState<T>(value: T, equivalent?: Equivalent<T>, tracker?: ValueTracker<T>): MutableState<T> {
85
    return GlobalStateManager.instance.mutableState<T>(value, undefined, equivalent, tracker)
86
}
87

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

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

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

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