idlize

Форк
0
60 строк · 1.9 Кб
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
/**
17
 * A marker for objects that require some work done for cleaning up.
18
 */
19
export interface Disposable {
20
    /**
21
     * Returns `true` if the object should no longer be used.
22
     */
23
    readonly disposed: boolean
24
    /**
25
     * Invoked when all resources owned by the object can safely be released.
26
     * Once disposed of the object should no longer be considered to be usable.
27
     */
28
    dispose(): void
29
}
30

31
/**
32
 * @param array - an array whose elements will be disposed from the first to the last
33
 */
34
export function disposeContentForward<Type extends Disposable>(array: ReadonlyArray<Type | undefined>) {
35
    const length = array.length
36
    for (let i = 0; i < length; i++) {
37
        array[i]?.dispose()
38
    }
39
}
40

41
/**
42
 * @param array - an array whose elements will be disposed from the last to the first
43
 */
44
export function disposeContentBackward<Type extends Disposable>(array: ReadonlyArray<Type | undefined>) {
45
    let i = array.length
46
    while (0 < i--) {
47
        array[i]?.dispose()
48
    }
49
}
50

51
/**
52
 * @param it - an iterator that provides elements to dispose one by one
53
 */
54
export function disposeContent<Type extends Disposable>(it: IterableIterator<Type | undefined>) {
55
    while (true) {
56
        const result = it.next()
57
        if (result.done) break
58
        result.value?.dispose()
59
    }
60
}
61

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

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

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

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