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.
17
* A marker for objects that require some work done for cleaning up.
19
export interface Disposable {
21
* Returns `true` if the object should no longer be used.
23
readonly disposed: boolean
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.
32
* @param array - an array whose elements will be disposed from the first to the last
34
export function disposeContentForward<Type extends Disposable>(array: ReadonlyArray<Type | undefined>) {
35
const length = array.length
36
for (let i = 0; i < length; i++) {
42
* @param array - an array whose elements will be disposed from the last to the first
44
export function disposeContentBackward<Type extends Disposable>(array: ReadonlyArray<Type | undefined>) {
52
* @param it - an iterator that provides elements to dispose one by one
54
export function disposeContent<Type extends Disposable>(it: IterableIterator<Type | undefined>) {
56
const result = it.next()
57
if (result.done) break
58
result.value?.dispose()