Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

ChartLoader fails if multiple charts are being placed on a page. #66

Open
ccrvincent opened this issue Apr 13, 2016 · 5 comments
Open

Comments

@ccrvincent
Copy link

We are using multiple different charts on a single page. Each one is a separate custom GWT Widget.

If more than one hits the ChartLoader concurrently, only the last callback is run.

The "official" GWT library solved this by keeping callbacks in a queue while loading is happening and emptying the queue after loading is complete.

https://github.com/googlearchive/gwt-google-apis/blob/master/apis/src/com/google/api/gwt/client/GoogleApiLoader.java

Easy enough to do in a loader wrapper (which is what I have done for now), but would be a good improvement to the actual ChartLoader.

@ccrvincent
Copy link
Author

Looks like this is possibly a duplicate/regression of #58 .

@sauldeleon
Copy link

Hello @ccrvincent

I am facing the same issue, and it is really annoying. I have two pie Charts which are generated at the same tame woth 2 data sources and only one gets painted.

Could you please provide some hints of your solution? I have trying to make it work using GoogleApiLoader with no success...

Thanks in advance!

Saúl

@ccrvincent
Copy link
Author

ccrvincent commented Feb 21, 2017

I no longer have access to the codebase where I was using this, so this is totally off of memory.

I wrote a class that either extended or wrapped ChartLoader and used it to manage the callbacks myself. Something like this (VERY loose psuedocode):

load(callback) {
if (!loaded) {
queue.add(callback);
if (!loading) {
ChartLoader.dowhateveritneedstodo(getLocalCallback());
loading = true;
}
} else {
callback.run();
}
}

LocalCallback.run() {
loaded = true;
for (callback : queue) {
callback.run();
}
}

GoogleApiLoader was only referenced here as an example of how to manage the loader. I never used it directly with gwt-charts.

@sauldeleon
Copy link

Thank you very much @ccrvincent for your support and effort looking for this in your memory. I will give a try.

Again, thanks a lot :)

@krullert
Copy link

fugly workaround;

replace all ChartLoaders with ConcurrentChartLoader;

public class ConcurrentChartLoader extends ChartLoader {

    private static final Queue<DelegatingLoaderRunnable> LOADING_QUEUE = new ArrayDeque<>();

    public ConcurrentChartLoader(ChartPackage... packages) {
        super(packages);
    }

    @Override
    public void loadApi(Runnable callback) {
        doLoadApi(this, callback);
    }

    protected void doLoadApi(ConcurrentChartLoader loader, Runnable callback) {
        // @see https://github.com/google/gwt-charts/issues/66
        if (LOADING_QUEUE.isEmpty()) {
            LOADING_QUEUE.add(new DelegatingLoaderRunnable(callback, loader));
            final Runnable delegatingCallback = new Runnable() {
                @Override
                public void run() {
                    try {
                        LOADING_QUEUE.poll().run();
                    } finally {
                        final DelegatingLoaderRunnable peek = LOADING_QUEUE.peek();
                        if (peek != null) {
                            peek.loader.doLoadApi(this);
                        }
                    }
                }
            };

            doLoadApi(delegatingCallback);
        } else {
            LOADING_QUEUE.add(new DelegatingLoaderRunnable(callback, loader));
        }
    }

    protected void doLoadApi(Runnable callback) {
        super.loadApi(callback);
    }

    protected static class DelegatingLoaderRunnable implements Runnable {

        private final Runnable delegate;
        private final ConcurrentChartLoader loader;

        public DelegatingLoaderRunnable(Runnable delegate, ConcurrentChartLoader loader) {
            this.delegate = delegate;
            this.loader = loader;
        }

        @Override
        public void run() {
            delegate.run();
        }
    }
}

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

No branches or pull requests

3 participants