question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

StyleFunction should return Style or Promise<Style>

See original GitHub issue

It seems to be impossible to style a vector layer in asynchronous way. The current signature of the StyleFunction looks like this:

(feature: Feature, resolution: number) => (Style | Style[] | null)

The implementation is as follows (OL version 5.3.0):

    var render = function(feature) {
      var styles;
      var styleFunction = feature.getStyleFunction() || vectorLayer.getStyleFunction();
      if (styleFunction) {
        styles = styleFunction(feature, resolution);
      }
      if (styles) {
        var dirty = this.renderFeature(
          feature, resolution, pixelRatio, styles, replayGroup);
        this.dirty_ = this.dirty_ || dirty;
      }
    }.bind(this);

The styles are returned and applied immediately by the succeeding code (see renderFeature).

SOLUTION:

A promise of the style(s) can be returned.

New signature:

(feature: Feature, resolution: number) => (Style | Style[] | Promise<Style> | Promise<Style[]> | null)

Usage:

const styleFn = (feature, resolution) => {
 return Promise.resolve(new Style({
      stroke: new Stroke({
        color: 'rgb(255, 105, 180)',
        width: 1
      })
    }));
};

layer.setStyle(styleFn);

This feature will facilitate the involvement of promises, observables, workers and any kind of asynchronous stuff, respectively.

Note that the implementation of the style function like this does not solve the issue either because this would return a “constant” style for each feature (independent from the zoom level):

// computeStyle returnes a Promise<Style> for the passed feature
computeStyle(feature).then((style) => {
	feature.setStyle(style);
});

This means that the promisifying the styles is the only option to get both advantages: asynchronous calculation of the style + re-calculation of the style on zoom level etc.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:13
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mktcodecommented, Apr 18, 2019

We save some layer style settings in a database and they are fetched with a separate request. I don’t want to delay the rendering of the map on one hand and on the other hand I don’t want this request to be somewhere after map initialization but right where I configure my layers/styles.

new VectorLayer({
  style: () => {
    return new Promise((resolve, reject) => {
      http.get("...").then(style => resolve(style));
    });
  },
  ...
});

That would be nice!

0reactions
stale[bot]commented, Jul 25, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Converting a old style callback style function into a async ...
In javascript, there are 3 styles of asynchronous function: callback style, promise style, and async-await style.
Read more >
Declaring the type of a "promisifier" function in typescript or flow
I've got this function, which basically converts a (nodejs)callback-style function to a promise-style function. export const promisify : ...
Read more >
One change you'll have to make is to convert all callback-style ...
In this case fs.readFile is a callback-style function. You can convert it using node's util.promisify function like this: import fs ...
Read more >
Integrating callback and promise based function invocation ...
In this blog post, I will compare both function invocation styles and ... compared to the Node.js-style function invocation -- it returns an ......
Read more >
3 Functions | The tidyverse style guide
If a function argument can't fit on a single line, this is a sign you should rework the argument to keep it short...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found