Skip to content
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

Making canvasFunction() async #8027

Closed
3 of 4 tasks
nyurik opened this issue Mar 25, 2018 · 3 comments
Closed
3 of 4 tasks

Making canvasFunction() async #8027

nyurik opened this issue Mar 25, 2018 · 3 comments

Comments

@nyurik
Copy link
Contributor

nyurik commented Mar 25, 2018

The ImageCanvas.canvasFunction is synchronous, making it cumbersome to work with in async environment. I think it would be great to allow consumers to return observable and thenable objects in addition to pure canvas. The ImageCanvas implementation should function as follows:

function setInternalCanvasAndRefresh(newCanvas) {
  // set cached image to the given canvas
}

const result = obj.canvasFunction(...);
if (typeof result.then === 'function') {
  // promise
  result.then(setInternalCanvasAndRefresh);
} else if (typeof result.subscribe === 'function') {
  // observable
  result.subscribe({
    next: setInternalCanvasAndRefresh
  });
} else {
  // assuming this is the canvas itself
  setInternalCanvasAndRefresh(result);
}

  • I am submitting a bug or feature request, not a usage question. Go to https://stackoverflow.com/questions/tagged/openlayers for questions.
  • I have searched GitHub to see if a similar bug or feature request has already been reported.
  • I have verified that the issue is present in the latest version of OpenLayers (see 'LATEST' on https://openlayers.org/).
  • If reporting a bug, I have created a CodePen or prepared a stack trace (using the latest version and unminified code, so e.g. ol-debug.js, not ol.js) that shows the issue.
@ahocevar
Copy link
Member

I agree that the ol/source/ImageCanvas API should support asynchronous canvas generators. To be more in line with the rest of the library (and agnostic about the way async is implemented), the API could look like this:

const source = new ImageCanvas({
  canvasFunction: (extent, res, pixelRatio, size, proj) => {
    source.setLoader(callback => {
      myAsyncCanvasFunction(ext, res, pixelRatio, size, proj)
        .then(result => {
          source.setCanvas(result);
          callback()
        })
        .catch(e => callback(e));
    });
  }
});

@nyurik
Copy link
Contributor Author

nyurik commented Mar 27, 2018

@ahocevar I'm not sure I understood your example. My code was the implementation of the ImageCanvas side. The API would simply allow user's canvasFunction to return a promise or an observable instead of the canvas object:

Current usage:

const source = new ImageCanvas({
  canvasFunction: (...) => { return myCanvas; }  // synchronous usage
}

New capabilities:

const source = new ImageCanvas({
  // Asynchronous usage - returns a promise
  canvasFunction: (...) => {
    return new Promise((accept) => {
       accept(myCanvas)
    });
  }
}
const source = new ImageCanvas({
  // Asynchronous usage - returns an observable object
  canvasFunction: (...) => ({
    subscribe: (o) => {
      // user calls o.next(myCanvas) as often as needed to refresh
    }
  })
}

Basically with promise it's a OL-initiated, async request for canvas, and with observable, OL subscribes to a stream of canvas updates initiated by the user.

@stale
Copy link

stale bot commented May 22, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

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

No branches or pull requests

2 participants