• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"use strict";
2/*
3 * Copyright (c) 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.callCallback = exports.disposeCallback = exports.wrapSystemCallback = exports.wrapCallback = void 0;
18class CallbackRecord {
19    constructor(callback, autoDisposable) {
20        this.callback = callback;
21        this.autoDisposable = autoDisposable;
22    }
23}
24class CallbackRegistry {
25    constructor() {
26        this.callbacks = new Map();
27        this.id = 1024;
28        this.callbacks.set(0, new CallbackRecord((args, length) => {
29            console.log(`Callback 0 called with args = ${args} and length = ${length}`);
30            throw new Error(`Null callback called`);
31        }, false));
32    }
33    wrap(callback, autoDisposable) {
34        const id = this.id++;
35        this.callbacks.set(id, new CallbackRecord(callback, autoDisposable));
36        return id;
37    }
38    wrapSystem(id, callback, autoDisposable) {
39        this.callbacks.set(id, new CallbackRecord(callback, autoDisposable));
40        return id;
41    }
42    call(id, args, length) {
43        const record = this.callbacks.get(id);
44        if (!record) {
45            console.log(`Callback ${id} is not known`);
46            // throw new Error(`Disposed or unwrapped callback called (id = ${id})`)
47            return 0; // todo
48        }
49        if (record.autoDisposable) {
50            this.dispose(id);
51        }
52        return record.callback(args, length);
53    }
54    dispose(id) {
55        this.callbacks.delete(id);
56    }
57}
58CallbackRegistry.INSTANCE = new CallbackRegistry();
59function wrapCallback(callback, autoDisposable = true) {
60    return CallbackRegistry.INSTANCE.wrap(callback, autoDisposable);
61}
62exports.wrapCallback = wrapCallback;
63function wrapSystemCallback(id, callback) {
64    return CallbackRegistry.INSTANCE.wrapSystem(id, callback, false);
65}
66exports.wrapSystemCallback = wrapSystemCallback;
67function disposeCallback(id) {
68    CallbackRegistry.INSTANCE.dispose(id);
69}
70exports.disposeCallback = disposeCallback;
71function callCallback(id, args, length) {
72    return CallbackRegistry.INSTANCE.call(id, args, length);
73}
74exports.callCallback = callCallback;
75//# sourceMappingURL=InteropOps.js.map