mps

Executing a non ui blocking task in MPS and changing the model after task completion


In Jetbrains MPS I want to execute a web request when the user hits a shortcut or an action from the context menu.

After successfully executing this request I want to show the user the response by manipulating the MPS model.

When I execute the web request within the UI Thread, the ui will be blocked until I get a response or the request times out.

This is of cause a bad ux experience.

How can I execute the request asynchronously?


Solution

  • The web request has to be executed in a separate thread - afaik MPS doesn't have full JDK8 support for ForkJoinPools s; I use the old fashioned approach in this example.

    Furthermore, you need a reference to the ModelAccess in order to change the MPS model. The model changing code has to executed in the UI Thread.

    The picture show a behavior method that executes an async task and changes the model after some 2Sek.

    The SwinUtilities.invokeAndWait is used to execute the code within the UI thread and the modelAccess.executeCommand wraps the model changing code.

    The this.asyncUpdate is a property of the concept and displayed by the concept editor.

    The task in action:

    <iframe src='https://gfycat.com/ifr/DisfiguredAdeptAmericanbittern' frameborder='0' scrolling='no' allowfullscreen width='640' height='213'></iframe><p> <a href="https://gfycat.com/disfiguredadeptamericanbittern-mps">via Gfycat</a></p>