@celljs/ta-geo 4.31.0 → 4.32.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.
@@ -12,18 +12,21 @@ export interface GeoTileSource {
12
12
  export declare class OpenFreeMapTiles implements GeoTileSource {
13
13
  protected readonly fetcher: Fetcher;
14
14
  protected readonly base: string;
15
+ protected readonly wait: number;
15
16
  readonly maxZoom = 14;
16
17
  protected version?: Promise<{
17
18
  version: string;
18
19
  template: string;
19
20
  }>;
20
21
  protected readonly kept: Map<string, Promise<Map<string, MvtLayer> | undefined>>;
21
- constructor(fetcher?: Fetcher, base?: string);
22
+ constructor(fetcher?: Fetcher, base?: string, wait?: number);
22
23
  latest(): Promise<string>;
23
24
  tile(tile: MapTile, version: string): Promise<Map<string, MvtLayer> | undefined>;
24
- protected load(tile: MapTile, version: string): Promise<Map<string, MvtLayer> | undefined>;
25
+ protected load(tile: MapTile, version: string, signal: AbortSignal): Promise<Map<string, MvtLayer> | undefined>;
25
26
  protected read(): Promise<{
26
27
  version: string;
27
28
  template: string;
28
29
  }>;
30
+ /** The work, given up when not done in time: its request aborted, the promise failed. */
31
+ protected within<T>(work: (signal: AbortSignal) => Promise<T>, what: string): Promise<T>;
29
32
  }
package/esm/geo-tiles.js CHANGED
@@ -2,6 +2,8 @@ import { GeoMvt } from './geo-mvt.js';
2
2
  const GZIP = [0x1f, 0x8b];
3
3
  /** Tiles kept decoded, the latest read. */
4
4
  const KEPT = 8;
5
+ /** A request not answered by then is given up (and asked again by the caller): a phone's network may hold one for ever. */
6
+ const WAIT_MS = 15000;
5
7
  async function gunzip(bytes) {
6
8
  if (bytes[0] !== GZIP[0] || bytes[1] !== GZIP[1]) {
7
9
  return bytes;
@@ -10,9 +12,10 @@ async function gunzip(bytes) {
10
12
  return new Uint8Array(await new Response(stream).arrayBuffer());
11
13
  }
12
14
  export class OpenFreeMapTiles {
13
- constructor(fetcher = (url, init) => fetch(url, init), base = 'https://tiles.openfreemap.org/planet') {
15
+ constructor(fetcher = (url, init) => fetch(url, init), base = 'https://tiles.openfreemap.org/planet', wait = WAIT_MS) {
14
16
  this.fetcher = fetcher;
15
17
  this.base = base;
18
+ this.wait = wait;
16
19
  this.maxZoom = 14;
17
20
  this.kept = new Map();
18
21
  }
@@ -27,7 +30,7 @@ export class OpenFreeMapTiles {
27
30
  this.kept.set(key, kept);
28
31
  return kept;
29
32
  }
30
- const made = this.load(tile, version);
33
+ const made = this.within(signal => this.load(tile, version, signal), `Map tile ${tile.z}/${tile.x}/${tile.y}`);
31
34
  made.catch(() => this.kept.delete(key));
32
35
  this.kept.set(key, made);
33
36
  while (this.kept.size > KEPT) {
@@ -35,13 +38,13 @@ export class OpenFreeMapTiles {
35
38
  }
36
39
  return made;
37
40
  }
38
- async load(tile, version) {
41
+ async load(tile, version, signal) {
39
42
  const { template, version: latest } = await this.read();
40
43
  const url = (of) => template.replace(latest, of).replace('{z}', String(tile.z)).replace('{x}', String(tile.x)).replace('{y}', String(tile.y));
41
- let response = await this.fetcher(url(version));
44
+ let response = await this.fetcher(url(version), { signal });
42
45
  // A version the source no longer keeps: the latest, which differs a little at most.
43
46
  if (response.status === 404 && version !== latest) {
44
- response = await this.fetcher(url(latest));
47
+ response = await this.fetcher(url(latest), { signal });
45
48
  }
46
49
  if (response.status === 204 || response.status === 404) {
47
50
  return undefined;
@@ -54,7 +57,7 @@ export class OpenFreeMapTiles {
54
57
  }
55
58
  read() {
56
59
  var _a;
57
- (_a = this.version) !== null && _a !== void 0 ? _a : (this.version = this.fetcher(this.base)
60
+ (_a = this.version) !== null && _a !== void 0 ? _a : (this.version = this.within(signal => this.fetcher(this.base, { signal }), 'Map source')
58
61
  .then(response => {
59
62
  if (!response.ok) {
60
63
  throw new Error(`Map source: HTTP ${response.status}`);
@@ -75,5 +78,22 @@ export class OpenFreeMapTiles {
75
78
  });
76
79
  return this.version;
77
80
  }
81
+ /** The work, given up when not done in time: its request aborted, the promise failed. */
82
+ async within(work, what) {
83
+ const controller = new AbortController();
84
+ let timer;
85
+ const late = new Promise((_, reject) => {
86
+ timer = setTimeout(() => {
87
+ controller.abort();
88
+ reject(new Error(`${what}: no answer in ${Math.round(this.wait / 1000)} s`));
89
+ }, this.wait);
90
+ });
91
+ try {
92
+ return await Promise.race([work(controller.signal), late]);
93
+ }
94
+ finally {
95
+ clearTimeout(timer);
96
+ }
97
+ }
78
98
  }
79
99
  //# sourceMappingURL=geo-tiles.js.map
@@ -12,19 +12,22 @@ export interface GeoTileSource {
12
12
  export declare class OpenFreeMapTiles implements GeoTileSource {
13
13
  protected readonly fetcher: Fetcher;
14
14
  protected readonly base: string;
15
+ protected readonly wait: number;
15
16
  readonly maxZoom = 14;
16
17
  protected version?: Promise<{
17
18
  version: string;
18
19
  template: string;
19
20
  }>;
20
21
  protected readonly kept: Map<string, Promise<Map<string, MvtLayer> | undefined>>;
21
- constructor(fetcher?: Fetcher, base?: string);
22
+ constructor(fetcher?: Fetcher, base?: string, wait?: number);
22
23
  latest(): Promise<string>;
23
24
  tile(tile: MapTile, version: string): Promise<Map<string, MvtLayer> | undefined>;
24
- protected load(tile: MapTile, version: string): Promise<Map<string, MvtLayer> | undefined>;
25
+ protected load(tile: MapTile, version: string, signal: AbortSignal): Promise<Map<string, MvtLayer> | undefined>;
25
26
  protected read(): Promise<{
26
27
  version: string;
27
28
  template: string;
28
29
  }>;
30
+ /** The work, given up when not done in time: its request aborted, the promise failed. */
31
+ protected within<T>(work: (signal: AbortSignal) => Promise<T>, what: string): Promise<T>;
29
32
  }
30
33
  //# sourceMappingURL=geo-tiles.d.ts.map
package/lib/geo-tiles.js CHANGED
@@ -5,6 +5,8 @@ const geo_mvt_1 = require("./geo-mvt");
5
5
  const GZIP = [0x1f, 0x8b];
6
6
  /** Tiles kept decoded, the latest read. */
7
7
  const KEPT = 8;
8
+ /** A request not answered by then is given up (and asked again by the caller): a phone's network may hold one for ever. */
9
+ const WAIT_MS = 15000;
8
10
  async function gunzip(bytes) {
9
11
  if (bytes[0] !== GZIP[0] || bytes[1] !== GZIP[1]) {
10
12
  return bytes;
@@ -13,9 +15,10 @@ async function gunzip(bytes) {
13
15
  return new Uint8Array(await new Response(stream).arrayBuffer());
14
16
  }
15
17
  class OpenFreeMapTiles {
16
- constructor(fetcher = (url, init) => fetch(url, init), base = 'https://tiles.openfreemap.org/planet') {
18
+ constructor(fetcher = (url, init) => fetch(url, init), base = 'https://tiles.openfreemap.org/planet', wait = WAIT_MS) {
17
19
  this.fetcher = fetcher;
18
20
  this.base = base;
21
+ this.wait = wait;
19
22
  this.maxZoom = 14;
20
23
  this.kept = new Map();
21
24
  }
@@ -30,7 +33,7 @@ class OpenFreeMapTiles {
30
33
  this.kept.set(key, kept);
31
34
  return kept;
32
35
  }
33
- const made = this.load(tile, version);
36
+ const made = this.within(signal => this.load(tile, version, signal), `Map tile ${tile.z}/${tile.x}/${tile.y}`);
34
37
  made.catch(() => this.kept.delete(key));
35
38
  this.kept.set(key, made);
36
39
  while (this.kept.size > KEPT) {
@@ -38,13 +41,13 @@ class OpenFreeMapTiles {
38
41
  }
39
42
  return made;
40
43
  }
41
- async load(tile, version) {
44
+ async load(tile, version, signal) {
42
45
  const { template, version: latest } = await this.read();
43
46
  const url = (of) => template.replace(latest, of).replace('{z}', String(tile.z)).replace('{x}', String(tile.x)).replace('{y}', String(tile.y));
44
- let response = await this.fetcher(url(version));
47
+ let response = await this.fetcher(url(version), { signal });
45
48
  // A version the source no longer keeps: the latest, which differs a little at most.
46
49
  if (response.status === 404 && version !== latest) {
47
- response = await this.fetcher(url(latest));
50
+ response = await this.fetcher(url(latest), { signal });
48
51
  }
49
52
  if (response.status === 204 || response.status === 404) {
50
53
  return undefined;
@@ -57,7 +60,7 @@ class OpenFreeMapTiles {
57
60
  }
58
61
  read() {
59
62
  var _a;
60
- (_a = this.version) !== null && _a !== void 0 ? _a : (this.version = this.fetcher(this.base)
63
+ (_a = this.version) !== null && _a !== void 0 ? _a : (this.version = this.within(signal => this.fetcher(this.base, { signal }), 'Map source')
61
64
  .then(response => {
62
65
  if (!response.ok) {
63
66
  throw new Error(`Map source: HTTP ${response.status}`);
@@ -78,6 +81,23 @@ class OpenFreeMapTiles {
78
81
  });
79
82
  return this.version;
80
83
  }
84
+ /** The work, given up when not done in time: its request aborted, the promise failed. */
85
+ async within(work, what) {
86
+ const controller = new AbortController();
87
+ let timer;
88
+ const late = new Promise((_, reject) => {
89
+ timer = setTimeout(() => {
90
+ controller.abort();
91
+ reject(new Error(`${what}: no answer in ${Math.round(this.wait / 1000)} s`));
92
+ }, this.wait);
93
+ });
94
+ try {
95
+ return await Promise.race([work(controller.signal), late]);
96
+ }
97
+ finally {
98
+ clearTimeout(timer);
99
+ }
100
+ }
81
101
  }
82
102
  exports.OpenFreeMapTiles = OpenFreeMapTiles;
83
103
  //# sourceMappingURL=geo-tiles.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-geo",
3
- "version": "4.31.0",
3
+ "version": "4.32.1",
4
4
  "description": "Real places for Ta's extensions: a grid of plots over the earth, vector tiles of OpenStreetMap decoded, places searched, the device's location, and what is on the ground of a plot as a grid",
5
5
  "homepage": "https://github.com/cellbang/ta",
6
6
  "bugs": {