idlize

Форк
0
/
callback_registry.ts 
76 строк · 2.3 Кб
1
/*
2
 * Copyright (c) 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
import { int32 } from "@koalaui/common"
17

18
export type CallbackType = (args: Uint8Array, length: int32) => int32
19

20
class CallbackRecord {
21
    constructor(
22
        public readonly callback: CallbackType,
23
        public readonly autoDisposable: boolean
24
    ) { }
25
}
26

27
class CallbackRegistry {
28

29
    static INSTANCE = new CallbackRegistry()
30

31
    private callbacks = new Map<int32, CallbackRecord>()
32
    private id = 1
33

34
    constructor() {
35
        this.callbacks.set(0, new CallbackRecord(
36
            (args: Uint8Array, length: int32): int32 => {
37
                console.log(`Callback 0 called with args = ${args} and length = ${length}`)
38
                throw new Error(`Null callback called`)
39
            }, false)
40
        )
41
    }
42

43
    wrap(callback: CallbackType, autoDisposable: boolean): int32 {
44
        const id = this.id++
45
        this.callbacks.set(id, new CallbackRecord(callback, autoDisposable))
46
        return id
47
    }
48

49
    call(id: int32, args: Uint8Array, length: int32): int32 {
50
        const record = this.callbacks.get(id)
51
        if (!record) {
52
            console.log(`Callback ${id} is not known`)
53
            throw new Error(`Disposed or unwrapped callback called (id = ${id})`)
54
        }
55
        if (record.autoDisposable) {
56
            this.dispose(id)
57
        }
58
        return record.callback(args, length)
59
    }
60

61
    dispose(id: int32) {
62
        this.callbacks.delete(id)
63
    }
64
}
65

66
export function wrapCallback(callback: CallbackType, autoDisposable: boolean = true): int32 {
67
    return CallbackRegistry.INSTANCE.wrap(callback, autoDisposable)
68
}
69

70
export function disposeCallback(id: int32) {
71
    CallbackRegistry.INSTANCE.dispose(id)
72
}
73

74
export function callCallback(id: int32, args: Uint8Array, length: int32): int32 {
75
    return CallbackRegistry.INSTANCE.call(id, args, length)
76
}

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

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

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

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