idlize
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
16import { 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*/
22export class GlobalStateManager {23private static current: StateManager | undefined = undefined24
25/**26* The current instance of a global state manager.
27* Note that it will be recreated after reset.
28*/
29static get instance(): StateManager {30if (GlobalStateManager.current === undefined) {31GlobalStateManager.current = createStateManager()32}33return GlobalStateManager.current!34}35
36/**37* Drops the current instance to recreate a global state manager.
38* @internal
39*/
40static reset() {41GlobalStateManager.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*/
51export function updateStateManager(manager: StateManager = GlobalStateManager.instance): void {52manager.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*/
61export function callScheduledCallbacks(manager: StateManager = GlobalStateManager.instance): void {62manager.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*/
70export function scheduleCallback(callback?: () => void) {71if (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*/
84export function mutableState<T>(value: T, equivalent?: Equivalent<T>, tracker?: ValueTracker<T>): MutableState<T> {85return GlobalStateManager.instance.mutableState<T>(value, undefined, equivalent, tracker)86}
87