Skip to content

How to wait for async open file action? #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jose1711 opened this issue Jan 2, 2023 · 1 comment
Open

How to wait for async open file action? #100

jose1711 opened this issue Jan 2, 2023 · 1 comment

Comments

@jose1711
Copy link

jose1711 commented Jan 2, 2023

The goal is to have a script open a local file, then manipulate the layers. Issue I am facing here is that I cannot find a way to tell script "wait until the file is loaded and only then continue execute the remaining tasks". As a result layer count is not updated in the code below:

Jython:

from javax.swing import JOptionPane
from org.openstreetmap.josm.gui import MainApplication
from org.openstreetmap.josm.actions import OpenFileAction
import java.io.File as File
from java.lang import Thread

# JOSM is started, no layer is opened
layerManager = MainApplication.getLayerManager()
all_layers = layerManager.getLayers()
JOptionPane.showMessageDialog(MainApplication.getMainFrame(), 'Layer count: {}'.format(len(all_layers)))
# Layer count: 0

# async open of a single file, we now (should) have 1 layer opened
# but we need this task to finish first
open_file_action = OpenFileAction.openFiles([File('/path/to/local/file.osm')])

'''
# this blocks forever:
while True:
    if open_file_action.isDone():
        break
    Thread.sleep(300);

# this blocks forever too:
open_file_action.get()
'''

# this works but a user must click [OK] to continue
JOptionPane.showMessageDialog(MainApplication.getMainFrame(), 'wait for open')

all_layers = layerManager.getLayers()
JOptionPane.showMessageDialog(MainApplication.getMainFrame(), 'Layer count: {}'.format(len(all_layers)))
# Layer count: 1

JS (courtesy of @zdila)

import josm from 'josm' 

const OpenFileAction = Java.type('org.openstreetmap.josm.actions.OpenFileAction');
const File = Java.type('java.io.File');

const future = OpenFileAction.openFiles([new File("/path/to/local/file.osm")]);

josm.console.println("LOOP ");

// no setTimeout available so let's try it hardcore
for (let i = 0; i < 100000; i++) {
  if (future.isDone()) {
    break;
  }
  
  josm.console.println("Layers: " + josm.layers.length + " " + i);
}

josm.console.println("DONE");
@jose1711
Copy link
Author

jose1711 commented Jan 4, 2023

It is rather interesting that almost the same code works fine inside gradle's tests. Could it be that JOSM's GUI interferes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant