async cluster layer

by Vladimir Vershinin

HTML

<script src="https://unpkg.com/[email protected]/lodash.js"></script>
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io/en/v5.3.0/build/ol.js"></script>
<script src="https://unpkg.com/[email protected]/lib/index.umd.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/lib/style.css">
<div id="app">
  <vl-map data-projection="EPSG:4326">
    <vl-view :zoom.sync="zoom" center.sync="center" />
    
    <vl-layer-tile>
      <vl-source-osm />
    </vl-layer-tile>
    
    <vl-layer-vector id="points" render-mode="image">
      <vl-source-cluster :distance="5">
        <vl-source-vector :features="counterFeatures"/>
      </vl-source-cluster>
      <vl-style-func :factory="scptIconStyle"/>
    </vl-layer-vector>
  </vl-map>
</div>

CSS

html, body, #app {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

JavaScript

new Vue({
	el: '#app',
  data () {
  	return {
    	zoom: 2,
      center: [0, 0],
      counterFeatures: [],
    }
  },
  computed: {
  	scptIconStyle() {
      // bind to the strokeColor changes
      return () => {
        return feature => {
        	let features = feature.get('features') || [] 
          let size = features.length;
          if(size == 1){
            size = "";
          }

          let colorDef = ""
          for(let item of features) {
            let color = item.get("counterColor");
            if(colorDef != ""){
              if(colorDef === "green" && (color === "yellow" || color === "orange" || color === "red" || color === "black")){
                colorDef = color;
              } else if (colorDef === "yellow" && (color === "orange" || color === "red" || color === "black")){
                colorDef = color;
              } else if (colorDef === "orange" && (color === "red" || color === "black")){
                colorDef = color;
              } else if (colorDef === "red" && color === "black"){
                colorDef = color;
              }
            } else {
              colorDef = color;
            }
          }
          if (!colorDef) {
          	colorDef = 'blue'
          }

          return [
            new ol.style.Style({
              image: new ol.style.Circle({
                radius: 10,
                stroke: new ol.style.Stroke({
                  color: colorDef,
                }),
                fill: new ol.style.Fill({
                  color: '#fff',
                }),
              }),
              text: new ol.style.Text({
                font: "light 10px Arial",
                text: "" + size,
                fill: new ol.style.Fill({ color: "black" }),
                stroke: new ol.style.Stroke({ color: "black", width: 0.5 })
              })
            })
          ];
        };
      };
    }
  },
  mounted () {
  	this.loadAllFeatures()
  	setInterval(() => {
   ...