// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { IndexMap } from '../../shared/index-map.js';
/**
 * Keeps the background-thread worklet ctx alive while main thread still holds
 * a handle to one of its background functions.
 */
export class BackgroundFunctionExecMap extends IndexMap {
    add(worklet) {
        const execId = super.add(worklet);
        worklet._execId = execId;
        return execId;
    }
    findJsFnHandle(execId, fnId) {
        const worklet = this.get(execId);
        if (!worklet) {
            return undefined;
        }
        return this.findJsFnHandleInValue(worklet, fnId);
    }
    findJsFnHandleInValue(value, fnId) {
        if (value === null || typeof value !== 'object') {
            return undefined;
        }
        const obj = value;
        if ('_jsFnId' in obj && obj['_jsFnId'] === fnId) {
            return obj;
        }
        for (const i in obj) {
            const result = this.findJsFnHandleInValue(obj[i], fnId);
            if (result) {
                return result;
            }
        }
        return undefined;
    }
}
//# sourceMappingURL=exec-map.js.map