Ticket #24677: more-tightly-scoped.diff

File more-tightly-scoped.diff, 3.9 KB (added by dnet, 6 days ago)

A more tightly scoped patch that partially reverts the change to allow sync loading of assets for the rest of the runtime

  • src/org/openstreetmap/josm/gui/tagging/presets/TaggingPreset.java

     
    2121import java.util.Objects;
    2222import java.util.Set;
    2323import java.util.concurrent.CompletableFuture;
     24import java.util.concurrent.Executor;
    2425import java.util.function.Predicate;
    2526import java.util.stream.Collectors;
    2627
     
    277278        File arch = TaggingPresetReader.getZipIcons();
    278279        final Collection<String> s = TaggingPresets.ICON_SOURCES.get();
    279280        this.iconFuture = new CompletableFuture<>();
    280         new ImageProvider(iconName)
     281        final ImageProvider provider = new ImageProvider(iconName)
    281282            .setDirs(s)
    282283            .setId("presets")
    283284            .setArchive(arch)
    284             .setOptional(true)
    285             .getResourceAsync(result -> {
     285            .setOptional(true);
     286        final Executor fetcher = ImageProvider.getImageFetchExecutor();
     287        // Explicitly dispatch to the image fetch executor so that even local JAR-bundled icons
     288        // are loaded asynchronously, keeping expensive SVG pre-rendering off the startup thread.
     289        CompletableFuture.supplyAsync(provider::getResource, fetcher)
     290            .thenAcceptAsync(result -> {
    286291                if (result != null) {
    287292                    // Pre-render off EDT to avoid flooding the event queue with expensive SVG rendering
    288293                    ImageIcon small = result.getImageIcon(ImageProvider.ImageSizes.SMALLICON.getImageDimension());
     
    303308                    Logging.warn(toString() + ": " + PRESET_ICON_ERROR_MSG_PREFIX + iconName);
    304309                    iconFuture.complete(null);
    305310                }
    306             });
     311            }, fetcher);
    307312    }
    308313
    309314    /**
  • src/org/openstreetmap/josm/tools/ImageProvider.java

     
    4444import java.util.Objects;
    4545import java.util.concurrent.CompletableFuture;
    4646import java.util.concurrent.ConcurrentHashMap;
     47import java.util.concurrent.Executor;
    4748import java.util.concurrent.ExecutorService;
    4849import java.util.concurrent.Executors;
    4950import java.util.function.Consumer;
     
    717718    /**
    718719     * Load the image in a background thread.
    719720     * <p>
    720      * This method returns immediately and runs the image request asynchronously.
     721     * This method returns immediately and runs the image request asynchronously for remote resources.
     722     * For local resources, the request is executed synchronously in the current thread.
    721723     * @param action the action that will deal with the image
    722724     *
    723725     * @return the future of the requested image
     
    724726     * @since 13252
    725727     */
    726728    public CompletableFuture<Void> getResourceAsync(Consumer<? super ImageResource> action) {
    727         return CompletableFuture.supplyAsync(this::getResource, IMAGE_FETCHER).thenAcceptAsync(action, IMAGE_FETCHER);
     729        return isRemote()
     730                ? CompletableFuture.supplyAsync(this::getResource, IMAGE_FETCHER).thenAcceptAsync(action, IMAGE_FETCHER)
     731                : CompletableFuture.completedFuture(getResource()).thenAccept(action);
    728732    }
    729733
    730734    /**
     735     * Returns the executor used for background image fetching.
     736     * Callers that need to force asynchronous loading even for local resources
     737     * (e.g. to off-load expensive SVG pre-rendering) may use this executor directly.
     738     * @return the image fetch executor
     739     */
     740    public static Executor getImageFetchExecutor() {
     741        return IMAGE_FETCHER;
     742    }
     743
     744    /**
    731745     * Load an image with a given file name.
    732746     *
    733747     * @param subdir subdirectory the image lies in