• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict";
2/*
3 * Copyright (c) 2022-2025 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16Object.defineProperty(exports, "__esModule", { value: true });
17exports.ResourceHolder = void 0;
18class ResourceHolder {
19    constructor() {
20        this.resources = new Map();
21    }
22    static instance() {
23        if (ResourceHolder._instance == undefined) {
24            ResourceHolder._instance = new ResourceHolder();
25        }
26        return ResourceHolder._instance;
27    }
28    hold(resourceId) {
29        if (!this.resources.has(resourceId))
30            throw new Error(`Resource ${resourceId} does not exists, can not hold`);
31        this.resources.get(resourceId).holdersCount++;
32    }
33    release(resourceId) {
34        if (!this.resources.has(resourceId))
35            throw new Error(`Resource ${resourceId} does not exists, can not release`);
36        const resource = this.resources.get(resourceId);
37        resource.holdersCount--;
38        if (resource.holdersCount <= 0)
39            this.resources.delete(resourceId);
40    }
41    registerAndHold(resource) {
42        const resourceId = ResourceHolder.nextResourceId++;
43        this.resources.set(resourceId, {
44            resource: resource,
45            holdersCount: 1,
46        });
47        return resourceId;
48    }
49    get(resourceId) {
50        if (!this.resources.has(resourceId))
51            throw new Error(`Resource ${resourceId} does not exists`);
52        return this.resources.get(resourceId).resource;
53    }
54    has(resourceId) {
55        return this.resources.has(resourceId);
56    }
57}
58exports.ResourceHolder = ResourceHolder;
59ResourceHolder.nextResourceId = 100;
60ResourceHolder._instance = undefined;
61//# sourceMappingURL=ResourceManager.js.map