Skip to content

Initialization of vscode services can only performed once! #988

@white10205

Description

@white10205

When clicking mount, the client was rendered normally for the first time, and when clicking uninstall, it was also uninstalled normally. However, when clicking mount again, the client could not be rendered normally and the console reported an error
Initialization of vscode services can only performed once!
Can it only be mounted once? Have you considered the scenario of needing to uninstall and then remount?

/* --------------------------------------------------------------------------------------------
 * Copyright (c) 2024 TypeFox and others.
 * Licensed under the MIT License. See LICENSE in the package root for license information.
* ------------------------------------------------------------------------------------------ */

import * as vscode from 'vscode';
import { type RegisterLocalProcessExtensionResult } from '@codingame/monaco-vscode-api/extensions';
import React, { useState } from 'react';
import ReactDOM from 'react-dom/client';
import { MonacoEditorReactComp } from '@typefox/monaco-editor-react';
import { MonacoEditorLanguageClientWrapper } from 'monaco-editor-wrapper';
import { createWrapperConfig } from './config.js';
import { configureDebugging } from '../../debugger/client/debugger.js';

export const runPythonReact = async () => {
    const appConfig = createWrapperConfig();
    
    const onLoad = async (wrapper: MonacoEditorLanguageClientWrapper) => {
        const result = wrapper.getExtensionRegisterResult('mlc-python-example') as RegisterLocalProcessExtensionResult;
        result.setAsDefaultApi();

        const initResult = wrapper.getExtensionRegisterResult('debugger-py-client') as RegisterLocalProcessExtensionResult | undefined;
        if (initResult !== undefined) {
            configureDebugging(await initResult.getApi(), appConfig.configParams);
        }

        await vscode.commands.executeCommand('workbench.view.explorer');
        await vscode.window.showTextDocument(appConfig.configParams.files.get('hello2.py')!.uri);
    };

    const root = ReactDOM.createRoot(document.getElementById('react-root')!);

    const App = () => {
        const [dispose, setDispose] = useState(false)
        return (
            <div style={{ 'backgroundColor': '#1f1f1f' }} >
                <div style={{ display: 'flex' }}>
                    <button onClick={() => setDispose(true)}>mount</button>
                    <button onClick={() => setDispose(false)}>unmount</button>
                </div>
                {
                    dispose ?
                        <MonacoEditorReactComp
                            wrapperConfig={appConfig.wrapperConfig}
                            style={{ 'height': '100%' }}
                            onLoad={onLoad}
                            onError={(e) => {
                                console.error(e);
                            }} /> : null
                }
            </div>
        );
    };
    root.render(<App />);
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions