@nextgis/leaflet-map-adapter 4.0.2 → 4.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- /** Bundle of @nextgis/leaflet-map-adapter; version: 4.0.2; author: NextGIS */
1
+ /** Bundle of @nextgis/leaflet-map-adapter; version: 4.1.1; author: NextGIS */
2
2
  function getElement(el) {
3
3
  if (typeof el === "string") {
4
4
  let el_ = document.getElementById(el);
@@ -15468,6 +15468,8 @@ function filterGeometries(data, type) {
15468
15468
  _data.geometries = _data.geometries.filter(
15469
15469
  (g) => geometryFilter(g.type, type)
15470
15470
  );
15471
+ } else if (!geometryFilter(data.type, type)) {
15472
+ return false;
15471
15473
  }
15472
15474
  return data;
15473
15475
  }
@@ -16019,6 +16021,9 @@ class GeoJsonAdapter extends BaseAdapter {
16019
16021
  }
16020
16022
  setData(data) {
16021
16023
  this.clearLayer();
16024
+ if (!this.options.type) {
16025
+ this.type = void 0;
16026
+ }
16022
16027
  this.addData(data);
16023
16028
  }
16024
16029
  addData(data) {
@@ -16033,15 +16038,20 @@ class GeoJsonAdapter extends BaseAdapter {
16033
16038
  } else {
16034
16039
  type = options.type;
16035
16040
  }
16041
+ if (!type) {
16042
+ return;
16043
+ }
16036
16044
  if (this.type && this.type !== type) {
16037
16045
  console.warn("no other data type than layer can be added");
16038
16046
  return;
16039
16047
  }
16040
16048
  this.type = type;
16041
16049
  data = filterGeometries(data, type);
16042
- if (data) {
16043
- geoJsonOptions = this._getGeoJsonOptions(options, type);
16050
+ if (!data) {
16051
+ console.warn("no other data type than layer can be added");
16052
+ return;
16044
16053
  }
16054
+ geoJsonOptions = this._getGeoJsonOptions(options, type);
16045
16055
  }
16046
16056
  new leafletSrcExports.GeoJSON(data || void 0, geoJsonOptions);
16047
16057
  }
@@ -16494,15 +16504,31 @@ class Queue {
16494
16504
  /**
16495
16505
  * @param {PromiseQueueOptions} options - Configuration options for the queue.
16496
16506
  */
16497
- constructor({ concurrency = 1, delay = 0 } = {}) {
16507
+ constructor({
16508
+ delay = 0,
16509
+ limit,
16510
+ timeout,
16511
+ debounce,
16512
+ concurrency = 1
16513
+ } = {}) {
16498
16514
  __publicField$4(this, "queue", []);
16515
+ __publicField$4(this, "activeRequests", []);
16499
16516
  __publicField$4(this, "concurrency");
16500
- __publicField$4(this, "activeCount", 0);
16501
16517
  __publicField$4(this, "timeoutId");
16518
+ __publicField$4(this, "resumeTimer");
16502
16519
  __publicField$4(this, "delay");
16503
- __publicField$4(this, "toAbort", []);
16504
- this.concurrency = concurrency;
16505
- this.delay = delay;
16520
+ __publicField$4(this, "timeout");
16521
+ __publicField$4(this, "paused", false);
16522
+ __publicField$4(this, "idleResolvers", []);
16523
+ __publicField$4(this, "debounce");
16524
+ this.concurrency = limit != null ? limit : concurrency;
16525
+ this.delay = debounce != null ? debounce : delay;
16526
+ this.debounce = this.delay;
16527
+ this.timeout = timeout;
16528
+ }
16529
+ setLimit(limit) {
16530
+ this.concurrency = limit;
16531
+ this._next();
16506
16532
  }
16507
16533
  /**
16508
16534
  * Adds a task to the queue, which can be either a simple asynchronous function or an array containing a function and its abort handler.
@@ -16521,7 +16547,18 @@ class Queue {
16521
16547
  * ```
16522
16548
  */
16523
16549
  add(promise, abort) {
16524
- this.queue.push({ promise, abort });
16550
+ var _a, _b;
16551
+ const options = typeof abort === "function" ? { abort } : abort;
16552
+ if ((options == null ? void 0 : options.id) !== void 0) {
16553
+ this.removeById(options.id);
16554
+ }
16555
+ this.queue.push({
16556
+ id: (_a = options == null ? void 0 : options.id) != null ? _a : Math.random().toString(36).slice(2),
16557
+ promise,
16558
+ abort: options == null ? void 0 : options.abort,
16559
+ abortController: new AbortController(),
16560
+ timeout: (_b = options == null ? void 0 : options.timeout) != null ? _b : this.timeout
16561
+ });
16525
16562
  this.debouncedNext();
16526
16563
  }
16527
16564
  /**
@@ -16531,60 +16568,167 @@ class Queue {
16531
16568
  * queue.abort(); // Aborts all abortable tasks in the queue.
16532
16569
  */
16533
16570
  abort() {
16534
- for (const abort of this.toAbort) {
16535
- abort();
16571
+ for (const item of [...this.queue, ...this.activeRequests]) {
16572
+ this.abortRequest(item);
16573
+ this.clearRequestTimeout(item);
16574
+ item.completed = true;
16575
+ }
16576
+ this.queue.length = 0;
16577
+ this.activeRequests.length = 0;
16578
+ this._clearTimeout();
16579
+ this.checkIdle();
16580
+ }
16581
+ remove(id) {
16582
+ this.removeById(id);
16583
+ }
16584
+ clearQueue() {
16585
+ for (const item of this.queue) {
16586
+ this.abortRequest(item);
16587
+ item.completed = true;
16536
16588
  }
16537
- this.toAbort = [];
16538
- this.queue = [];
16589
+ this.queue.length = 0;
16539
16590
  this._clearTimeout();
16591
+ this.checkIdle();
16592
+ }
16593
+ waitAll() {
16594
+ return new Promise((resolve) => {
16595
+ if (this.isIdle()) {
16596
+ resolve();
16597
+ } else {
16598
+ this.idleResolvers.push(resolve);
16599
+ }
16600
+ });
16601
+ }
16602
+ pause(pauseDuration) {
16603
+ this.paused = true;
16604
+ this._clearTimeout();
16605
+ this._clearResumeTimer();
16606
+ if (pauseDuration) {
16607
+ this.resumeTimer = setTimeout(() => this.resume(), pauseDuration);
16608
+ }
16609
+ }
16610
+ resume() {
16611
+ if (!this.paused) return;
16612
+ this.paused = false;
16613
+ this._clearResumeTimer();
16614
+ this._next();
16540
16615
  }
16541
16616
  debouncedNext() {
16617
+ if (this.paused) return;
16618
+ if (this.activeRequests.length) {
16619
+ this._next();
16620
+ return;
16621
+ }
16542
16622
  this._clearTimeout();
16543
16623
  this.timeoutId = setTimeout(() => this._next(), this.delay);
16544
16624
  }
16545
16625
  _clearTimeout() {
16546
16626
  if (this.timeoutId) {
16547
16627
  clearTimeout(this.timeoutId);
16628
+ this.timeoutId = void 0;
16629
+ }
16630
+ }
16631
+ _clearResumeTimer() {
16632
+ if (this.resumeTimer) {
16633
+ clearTimeout(this.resumeTimer);
16634
+ this.resumeTimer = void 0;
16548
16635
  }
16549
16636
  }
16550
16637
  _next() {
16551
- while (this.activeCount < this.concurrency && this.queue.length > 0) {
16552
- const { promise, abort } = this.queue.shift();
16553
- this.activeCount++;
16554
- let toAbort = abort;
16555
- let request;
16556
- const execute = promise();
16557
- if (Array.isArray(execute)) {
16558
- request = execute[0];
16559
- toAbort = execute[1];
16560
- } else {
16561
- request = execute;
16562
- }
16563
- if (toAbort) {
16564
- this.toAbort.push(toAbort);
16638
+ if (this.paused) return;
16639
+ while (this.activeRequests.length < this.concurrency && this.queue.length) {
16640
+ const item = this.queue.shift();
16641
+ if (item.abortController.signal.aborted) continue;
16642
+ this.activeRequests.push(item);
16643
+ if (item.timeout && item.timeout > 0) {
16644
+ item.timeoutId = setTimeout(() => {
16645
+ this.abortRequest(
16646
+ item,
16647
+ new DOMException("Request timed out", "TimeoutError")
16648
+ );
16649
+ this.finishRequest(item);
16650
+ }, item.timeout);
16565
16651
  }
16566
- request.finally(() => {
16567
- this.activeCount--;
16568
- if (toAbort) {
16569
- this.toAbort = this.toAbort.filter((a) => a !== toAbort);
16652
+ try {
16653
+ const execute = item.promise({ signal: item.abortController.signal });
16654
+ const request = Array.isArray(execute) ? execute[0] : execute;
16655
+ if (Array.isArray(execute)) {
16656
+ const abort = item.abort;
16657
+ item.abort = abort ? () => {
16658
+ abort();
16659
+ execute[1]();
16660
+ } : execute[1];
16570
16661
  }
16571
- this.debouncedNext();
16572
- });
16662
+ Promise.resolve(request).then(
16663
+ () => this.finishRequest(item),
16664
+ () => this.finishRequest(item)
16665
+ );
16666
+ } catch (error) {
16667
+ this.finishRequest(item);
16668
+ console.error(error);
16669
+ }
16670
+ }
16671
+ this.checkIdle();
16672
+ }
16673
+ removeById(id) {
16674
+ this.queue = this.queue.filter((item) => {
16675
+ if (item.id !== id) return true;
16676
+ this.abortRequest(item);
16677
+ item.completed = true;
16678
+ return false;
16679
+ });
16680
+ for (const item of [...this.activeRequests]) {
16681
+ if (item.id === id) {
16682
+ this.abortRequest(item);
16683
+ this.finishRequest(item);
16684
+ }
16685
+ }
16686
+ this.checkIdle();
16687
+ }
16688
+ abortRequest(item, reason) {
16689
+ var _a;
16690
+ if (!item.abortController.signal.aborted) {
16691
+ item.abortController.abort(reason);
16692
+ }
16693
+ (_a = item.abort) == null ? void 0 : _a.call(item);
16694
+ }
16695
+ clearRequestTimeout(item) {
16696
+ if (item.timeoutId) {
16697
+ clearTimeout(item.timeoutId);
16698
+ item.timeoutId = void 0;
16699
+ }
16700
+ }
16701
+ finishRequest(item) {
16702
+ if (item.completed) return;
16703
+ item.completed = true;
16704
+ this.clearRequestTimeout(item);
16705
+ this.activeRequests = this.activeRequests.filter((x) => x !== item);
16706
+ this.checkIdle();
16707
+ this._next();
16708
+ }
16709
+ isIdle() {
16710
+ return this.queue.length === 0 && this.activeRequests.length === 0;
16711
+ }
16712
+ checkIdle() {
16713
+ if (this.isIdle()) {
16714
+ this.idleResolvers.splice(0).forEach((resolve) => resolve());
16573
16715
  }
16574
16716
  }
16575
16717
  }
16576
16718
 
16577
- const imageQueue = new Queue({ concurrency: 6, delay: 200 });
16719
+ const imageQueue = new Queue({ limit: 6, debounce: 150 });
16578
16720
 
16579
16721
  var __defProp$6 = Object.defineProperty;
16580
16722
  var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
16581
16723
  var __publicField$3 = (obj, key, value) => __defNormalProp$6(obj, key + "" , value);
16724
+ const transparentImage = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEAAAAALAAAAAABAAEAAAIBAAA=";
16582
16725
  class ImageOverlay extends leafletSrcExports.ImageOverlay {
16583
16726
  constructor(imageUrl, bounds, options) {
16584
16727
  super(imageUrl, bounds, options);
16585
16728
  __publicField$3(this, "_aborter");
16586
16729
  }
16587
16730
  cancelLoad() {
16731
+ imageQueue.remove(leafletSrcExports.Util.stamp(this));
16588
16732
  if (this._aborter) {
16589
16733
  this._aborter();
16590
16734
  this._aborter = void 0;
@@ -16596,19 +16740,27 @@ class ImageOverlay extends leafletSrcExports.ImageOverlay {
16596
16740
  const img = this._image;
16597
16741
  const src = img.src;
16598
16742
  img.src = "";
16599
- imageQueue.add(() => {
16600
- const [promise, abortFunc] = callAjax({
16601
- src,
16602
- headers,
16603
- request,
16604
- withCredentials
16605
- });
16606
- promise.then((imgUrl) => {
16607
- img.src = imgUrl;
16608
- });
16609
- this._aborter = abortFunc;
16610
- return promise;
16611
- });
16743
+ imageQueue.add(
16744
+ ({ signal }) => {
16745
+ const [promise, abortFunc] = callAjax({
16746
+ src,
16747
+ headers,
16748
+ request,
16749
+ withCredentials
16750
+ });
16751
+ void promise.then((imgUrl) => {
16752
+ if (!signal.aborted) img.src = imgUrl || transparentImage;
16753
+ }).catch((error) => {
16754
+ if (!signal.aborted) {
16755
+ console.error(error);
16756
+ img.src = transparentImage;
16757
+ }
16758
+ });
16759
+ this._aborter = abortFunc;
16760
+ return [promise, abortFunc];
16761
+ },
16762
+ { id: leafletSrcExports.Util.stamp(this) }
16763
+ );
16612
16764
  }
16613
16765
  }
16614
16766
 
@@ -16678,6 +16830,12 @@ class ImageLayer extends leafletSrcExports.Layer {
16678
16830
  map.removeLayer(this._currentOverlay);
16679
16831
  delete this._currentOverlay;
16680
16832
  }
16833
+ if (this._loadedOverlay) {
16834
+ this._loadedOverlay.cancelLoad();
16835
+ this._loadedOverlay.off("load");
16836
+ map.removeLayer(this._loadedOverlay);
16837
+ this._loadedOverlay = void 0;
16838
+ }
16681
16839
  if (this._currentUrl) {
16682
16840
  delete this._currentUrl;
16683
16841
  }
@@ -16703,6 +16861,7 @@ class ImageLayer extends leafletSrcExports.Layer {
16703
16861
  if (this._loadedOverlay) {
16704
16862
  this._loadedOverlay.cancelLoad();
16705
16863
  this._loadedOverlay.off("load");
16864
+ this._map.removeLayer(this._loadedOverlay);
16706
16865
  this._loadedOverlay = void 0;
16707
16866
  }
16708
16867
  const overlay = new ImageOverlay(url, bounds, {
@@ -16753,6 +16912,7 @@ class ImageLayer extends leafletSrcExports.Layer {
16753
16912
  if (this._currentOverlay) {
16754
16913
  this._currentOverlay.setOpacity(opacity);
16755
16914
  }
16915
+ this.fire("opacitychange", { opacity });
16756
16916
  }
16757
16917
  bringToBack() {
16758
16918
  this.options.isBack = true;
@@ -17363,10 +17523,10 @@ const _LeafletMapAdapter = class _LeafletMapAdapter {
17363
17523
  }
17364
17524
  const stop2 = () => {
17365
17525
  if (locationfound !== void 0) {
17366
- map.off("locationfound", locationFound);
17526
+ map.off("locationfound", locationFound, this);
17367
17527
  }
17368
17528
  if (locationerror) {
17369
- map.off("locationerror", locationerror);
17529
+ map.off("locationerror", locationerror, this);
17370
17530
  }
17371
17531
  };
17372
17532
  return { stop: stop2 };