@classic-homes/maps-core 0.5.0 → 0.5.3

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.
@@ -9,6 +9,7 @@ var SpatialWorkerClient = class _SpatialWorkerClient {
9
9
  this.initialized = false;
10
10
  this.ready = false;
11
11
  this.readyResolve = null;
12
+ this.readyReject = null;
12
13
  this.options = {
13
14
  requestTimeout: options.requestTimeout ?? 3e4,
14
15
  useRTree: options.useRTree ?? true,
@@ -17,9 +18,7 @@ var SpatialWorkerClient = class _SpatialWorkerClient {
17
18
  workerFactory: options.workerFactory,
18
19
  workerUrl: options.workerUrl
19
20
  };
20
- this.readyPromise = new Promise((resolve) => {
21
- this.readyResolve = resolve;
22
- });
21
+ this.readyPromise = this.createReadyPromise();
23
22
  this.createWorker();
24
23
  }
25
24
  /**
@@ -77,9 +76,21 @@ var SpatialWorkerClient = class _SpatialWorkerClient {
77
76
  this.worker.onmessage = this.handleMessage.bind(this);
78
77
  this.worker.onerror = this.handleError.bind(this);
79
78
  this.ready = false;
80
- this.readyPromise = new Promise((resolve) => {
79
+ this.readyReject?.(new Error("Worker replaced"));
80
+ this.readyPromise = this.createReadyPromise();
81
+ }
82
+ /**
83
+ * Create the promise that settles when the worker posts 'ready', or rejects
84
+ * when the worker fails before that (or is terminated/replaced)
85
+ */
86
+ createReadyPromise() {
87
+ const promise = new Promise((resolve, reject) => {
81
88
  this.readyResolve = resolve;
89
+ this.readyReject = reject;
90
+ });
91
+ promise.catch(() => {
82
92
  });
93
+ return promise;
83
94
  }
84
95
  /**
85
96
  * Handle messages from the worker
@@ -108,6 +119,7 @@ var SpatialWorkerClient = class _SpatialWorkerClient {
108
119
  */
109
120
  handleError(error) {
110
121
  console.error("[maps-core] Spatial worker error:", error);
122
+ this.readyReject?.(new Error(`Worker error: ${error.message}`));
111
123
  for (const [requestId, pending] of this.pendingRequests) {
112
124
  clearTimeout(pending.timeout);
113
125
  pending.reject(new Error(`Worker error: ${error.message}`));
@@ -127,7 +139,6 @@ var SpatialWorkerClient = class _SpatialWorkerClient {
127
139
  if (!this.worker) {
128
140
  throw new Error("Worker not available");
129
141
  }
130
- await this.readyPromise;
131
142
  return new Promise((resolve, reject) => {
132
143
  const timeout = setTimeout(() => {
133
144
  this.pendingRequests.delete(request.requestId);
@@ -138,7 +149,18 @@ var SpatialWorkerClient = class _SpatialWorkerClient {
138
149
  reject,
139
150
  timeout
140
151
  });
141
- this.worker.postMessage(request);
152
+ this.readyPromise.then(
153
+ () => {
154
+ if (!this.pendingRequests.has(request.requestId)) return;
155
+ this.worker?.postMessage(request);
156
+ },
157
+ (error) => {
158
+ if (!this.pendingRequests.has(request.requestId)) return;
159
+ clearTimeout(timeout);
160
+ this.pendingRequests.delete(request.requestId);
161
+ reject(error);
162
+ }
163
+ );
142
164
  });
143
165
  }
144
166
  /**
@@ -374,6 +396,7 @@ var SpatialWorkerClient = class _SpatialWorkerClient {
374
396
  pending.reject(new Error("Worker terminated"));
375
397
  this.pendingRequests.delete(requestId);
376
398
  }
399
+ this.readyReject?.(new Error("Worker terminated"));
377
400
  if (this.worker) {
378
401
  this.worker.terminate();
379
402
  this.worker = null;
@@ -112,6 +112,7 @@ declare class SpatialWorkerClient {
112
112
  private ready;
113
113
  private readyPromise;
114
114
  private readyResolve;
115
+ private readyReject;
115
116
  constructor(options?: SpatialWorkerClientOptions);
116
117
  /**
117
118
  * Check if Web Workers are supported
@@ -135,6 +136,11 @@ declare class SpatialWorkerClient {
135
136
  * ```
136
137
  */
137
138
  setWorker(worker: Worker): void;
139
+ /**
140
+ * Create the promise that settles when the worker posts 'ready', or rejects
141
+ * when the worker fails before that (or is terminated/replaced)
142
+ */
143
+ private createReadyPromise;
138
144
  /**
139
145
  * Handle messages from the worker
140
146
  */
@@ -112,6 +112,7 @@ declare class SpatialWorkerClient {
112
112
  private ready;
113
113
  private readyPromise;
114
114
  private readyResolve;
115
+ private readyReject;
115
116
  constructor(options?: SpatialWorkerClientOptions);
116
117
  /**
117
118
  * Check if Web Workers are supported
@@ -135,6 +136,11 @@ declare class SpatialWorkerClient {
135
136
  * ```
136
137
  */
137
138
  setWorker(worker: Worker): void;
139
+ /**
140
+ * Create the promise that settles when the worker posts 'ready', or rejects
141
+ * when the worker fails before that (or is terminated/replaced)
142
+ */
143
+ private createReadyPromise;
138
144
  /**
139
145
  * Handle messages from the worker
140
146
  */
@@ -7,6 +7,7 @@ var SpatialWorkerClient = class _SpatialWorkerClient {
7
7
  this.initialized = false;
8
8
  this.ready = false;
9
9
  this.readyResolve = null;
10
+ this.readyReject = null;
10
11
  this.options = {
11
12
  requestTimeout: options.requestTimeout ?? 3e4,
12
13
  useRTree: options.useRTree ?? true,
@@ -15,9 +16,7 @@ var SpatialWorkerClient = class _SpatialWorkerClient {
15
16
  workerFactory: options.workerFactory,
16
17
  workerUrl: options.workerUrl
17
18
  };
18
- this.readyPromise = new Promise((resolve) => {
19
- this.readyResolve = resolve;
20
- });
19
+ this.readyPromise = this.createReadyPromise();
21
20
  this.createWorker();
22
21
  }
23
22
  /**
@@ -75,9 +74,21 @@ var SpatialWorkerClient = class _SpatialWorkerClient {
75
74
  this.worker.onmessage = this.handleMessage.bind(this);
76
75
  this.worker.onerror = this.handleError.bind(this);
77
76
  this.ready = false;
78
- this.readyPromise = new Promise((resolve) => {
77
+ this.readyReject?.(new Error("Worker replaced"));
78
+ this.readyPromise = this.createReadyPromise();
79
+ }
80
+ /**
81
+ * Create the promise that settles when the worker posts 'ready', or rejects
82
+ * when the worker fails before that (or is terminated/replaced)
83
+ */
84
+ createReadyPromise() {
85
+ const promise = new Promise((resolve, reject) => {
79
86
  this.readyResolve = resolve;
87
+ this.readyReject = reject;
88
+ });
89
+ promise.catch(() => {
80
90
  });
91
+ return promise;
81
92
  }
82
93
  /**
83
94
  * Handle messages from the worker
@@ -106,6 +117,7 @@ var SpatialWorkerClient = class _SpatialWorkerClient {
106
117
  */
107
118
  handleError(error) {
108
119
  console.error("[maps-core] Spatial worker error:", error);
120
+ this.readyReject?.(new Error(`Worker error: ${error.message}`));
109
121
  for (const [requestId, pending] of this.pendingRequests) {
110
122
  clearTimeout(pending.timeout);
111
123
  pending.reject(new Error(`Worker error: ${error.message}`));
@@ -125,7 +137,6 @@ var SpatialWorkerClient = class _SpatialWorkerClient {
125
137
  if (!this.worker) {
126
138
  throw new Error("Worker not available");
127
139
  }
128
- await this.readyPromise;
129
140
  return new Promise((resolve, reject) => {
130
141
  const timeout = setTimeout(() => {
131
142
  this.pendingRequests.delete(request.requestId);
@@ -136,7 +147,18 @@ var SpatialWorkerClient = class _SpatialWorkerClient {
136
147
  reject,
137
148
  timeout
138
149
  });
139
- this.worker.postMessage(request);
150
+ this.readyPromise.then(
151
+ () => {
152
+ if (!this.pendingRequests.has(request.requestId)) return;
153
+ this.worker?.postMessage(request);
154
+ },
155
+ (error) => {
156
+ if (!this.pendingRequests.has(request.requestId)) return;
157
+ clearTimeout(timeout);
158
+ this.pendingRequests.delete(request.requestId);
159
+ reject(error);
160
+ }
161
+ );
140
162
  });
141
163
  }
142
164
  /**
@@ -372,6 +394,7 @@ var SpatialWorkerClient = class _SpatialWorkerClient {
372
394
  pending.reject(new Error("Worker terminated"));
373
395
  this.pendingRequests.delete(requestId);
374
396
  }
397
+ this.readyReject?.(new Error("Worker terminated"));
375
398
  if (this.worker) {
376
399
  this.worker.terminate();
377
400
  this.worker = null;
@@ -490,8 +490,8 @@ function buildSpatialIndex(geojson, options = {}) {
490
490
  };
491
491
  }
492
492
  const [[minLng, minLat], [maxLng, maxLat]] = bounds;
493
- const width = maxLng - minLng;
494
- const height = maxLat - minLat;
493
+ const width = Math.max(maxLng - minLng, minCellSize);
494
+ const height = Math.max(maxLat - minLat, minCellSize);
495
495
  const aspectRatio = width / height;
496
496
  let cols = Math.ceil(Math.sqrt(targetCells * aspectRatio));
497
497
  let rows = Math.ceil(targetCells / cols);
@@ -504,10 +504,10 @@ function buildSpatialIndex(geojson, options = {}) {
504
504
  const featureBounds = getFeatureBounds(feature);
505
505
  if (!featureBounds) continue;
506
506
  const [[fMinLng, fMinLat], [fMaxLng, fMaxLat]] = featureBounds;
507
- const minCol = Math.max(0, Math.floor((fMinLng - minLng) / cellWidth));
508
- const maxCol = Math.min(cols - 1, Math.floor((fMaxLng - minLng) / cellWidth));
509
- const minRow = Math.max(0, Math.floor((fMinLat - minLat) / cellHeight));
510
- const maxRow = Math.min(rows - 1, Math.floor((fMaxLat - minLat) / cellHeight));
507
+ const minCol = cellIndex(fMinLng, minLng, cellWidth, cols);
508
+ const maxCol = cellIndex(fMaxLng, minLng, cellWidth, cols);
509
+ const minRow = cellIndex(fMinLat, minLat, cellHeight, rows);
510
+ const maxRow = cellIndex(fMaxLat, minLat, cellHeight, rows);
511
511
  for (let col = minCol; col <= maxCol; col++) {
512
512
  for (let row = minRow; row <= maxRow; row++) {
513
513
  const key = `${col},${row}`;
@@ -528,6 +528,9 @@ function buildSpatialIndex(geojson, options = {}) {
528
528
  featureCount: geojson.features.length
529
529
  };
530
530
  }
531
+ function cellIndex(value, origin, size, count) {
532
+ return Math.min(count - 1, Math.max(0, Math.floor((value - origin) / size)));
533
+ }
531
534
  function getFeatureBounds(feature) {
532
535
  const geometry = feature.geometry;
533
536
  if (!geometry) return null;
@@ -604,10 +607,10 @@ function queryByBoundsWithStats(index, queryBounds, options = {}) {
604
607
  maxLng += actualBuffer;
605
608
  maxLat += actualBuffer;
606
609
  const [[idxMinLng, idxMinLat]] = index.bounds;
607
- const minCol = Math.max(0, Math.floor((minLng - idxMinLng) / index.cellWidth));
608
- const maxCol = Math.min(index.cols - 1, Math.floor((maxLng - idxMinLng) / index.cellWidth));
609
- const minRow = Math.max(0, Math.floor((minLat - idxMinLat) / index.cellHeight));
610
- const maxRow = Math.min(index.rows - 1, Math.floor((maxLat - idxMinLat) / index.cellHeight));
610
+ const minCol = cellIndex(minLng, idxMinLng, index.cellWidth, index.cols);
611
+ const maxCol = cellIndex(maxLng, idxMinLng, index.cellWidth, index.cols);
612
+ const minRow = cellIndex(minLat, idxMinLat, index.cellHeight, index.rows);
613
+ const maxRow = cellIndex(maxLat, idxMinLat, index.cellHeight, index.rows);
611
614
  const seen = /* @__PURE__ */ new Set();
612
615
  const features = [];
613
616
  let cellsChecked = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/maps-core",
3
- "version": "0.5.0",
3
+ "version": "0.5.3",
4
4
  "description": "Core utilities and theme for Classic Theme maps",
5
5
  "keywords": [
6
6
  "maps",
@@ -20,39 +20,74 @@
20
20
  "types": "dist/index.d.ts",
21
21
  "exports": {
22
22
  ".": {
23
- "types": "./dist/index.d.ts",
24
- "import": "./dist/index.js",
25
- "require": "./dist/index.cjs"
23
+ "import": {
24
+ "types": "./dist/index.d.ts",
25
+ "default": "./dist/index.js"
26
+ },
27
+ "require": {
28
+ "types": "./dist/index.d.cts",
29
+ "default": "./dist/index.cjs"
30
+ }
26
31
  },
27
32
  "./theme": {
28
- "types": "./dist/theme/index.d.ts",
29
- "import": "./dist/theme/index.js",
30
- "require": "./dist/theme/index.cjs"
33
+ "import": {
34
+ "types": "./dist/theme/index.d.ts",
35
+ "default": "./dist/theme/index.js"
36
+ },
37
+ "require": {
38
+ "types": "./dist/theme/index.d.cts",
39
+ "default": "./dist/theme/index.cjs"
40
+ }
31
41
  },
32
42
  "./types": {
33
- "types": "./dist/types/index.d.ts",
34
- "import": "./dist/types/index.js",
35
- "require": "./dist/types/index.cjs"
43
+ "import": {
44
+ "types": "./dist/types/index.d.ts",
45
+ "default": "./dist/types/index.js"
46
+ },
47
+ "require": {
48
+ "types": "./dist/types/index.d.cts",
49
+ "default": "./dist/types/index.cjs"
50
+ }
36
51
  },
37
52
  "./route": {
38
- "types": "./dist/route/index.d.ts",
39
- "import": "./dist/route/index.js",
40
- "require": "./dist/route/index.cjs"
53
+ "import": {
54
+ "types": "./dist/route/index.d.ts",
55
+ "default": "./dist/route/index.js"
56
+ },
57
+ "require": {
58
+ "types": "./dist/route/index.d.cts",
59
+ "default": "./dist/route/index.cjs"
60
+ }
41
61
  },
42
62
  "./geocoding": {
43
- "types": "./dist/geocoding/index.d.ts",
44
- "import": "./dist/geocoding/index.js",
45
- "require": "./dist/geocoding/index.cjs"
63
+ "import": {
64
+ "types": "./dist/geocoding/index.d.ts",
65
+ "default": "./dist/geocoding/index.js"
66
+ },
67
+ "require": {
68
+ "types": "./dist/geocoding/index.d.cts",
69
+ "default": "./dist/geocoding/index.cjs"
70
+ }
46
71
  },
47
72
  "./utils": {
48
- "types": "./dist/utils/index.d.ts",
49
- "import": "./dist/utils/index.js",
50
- "require": "./dist/utils/index.cjs"
73
+ "import": {
74
+ "types": "./dist/utils/index.d.ts",
75
+ "default": "./dist/utils/index.js"
76
+ },
77
+ "require": {
78
+ "types": "./dist/utils/index.d.cts",
79
+ "default": "./dist/utils/index.cjs"
80
+ }
51
81
  },
52
82
  "./workers": {
53
- "types": "./dist/workers/index.d.ts",
54
- "import": "./dist/workers/index.js",
55
- "require": "./dist/workers/index.cjs"
83
+ "import": {
84
+ "types": "./dist/workers/index.d.ts",
85
+ "default": "./dist/workers/index.js"
86
+ },
87
+ "require": {
88
+ "types": "./dist/workers/index.d.cts",
89
+ "default": "./dist/workers/index.cjs"
90
+ }
56
91
  },
57
92
  "./workers/spatial.worker": {
58
93
  "import": "./dist/workers/spatial.worker.js"
@@ -96,7 +131,8 @@
96
131
  "@types/rbush": "^4.0.0",
97
132
  "maplibre-gl": "^5.0.0",
98
133
  "tsup": "^8.0.0",
99
- "typescript": "^5.3.0"
134
+ "typescript": "^5.3.0",
135
+ "vitest": "^4.1.10"
100
136
  },
101
137
  "publishConfig": {
102
138
  "access": "public"