@classic-homes/maps-core 0.4.1 → 0.5.2
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.
- package/dist/{chunk-EXGGK4H7.js → chunk-ASIFMIU7.js} +300 -289
- package/dist/index.cjs +600 -592
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +8 -11
- package/dist/route/index.cjs +15 -5
- package/dist/route/index.d.cts +10 -6
- package/dist/route/index.d.ts +10 -6
- package/dist/route/index.js +15 -5
- package/dist/utils/index.cjs +300 -289
- package/dist/utils/index.js +1 -1
- package/dist/workers/index.cjs +29 -6
- package/dist/workers/index.d.cts +6 -0
- package/dist/workers/index.d.ts +6 -0
- package/dist/workers/index.js +29 -6
- package/dist/workers/spatial.worker.js +13 -10
- package/package.json +3 -2
package/dist/workers/index.cjs
CHANGED
|
@@ -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 =
|
|
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.
|
|
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.
|
|
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;
|
package/dist/workers/index.d.cts
CHANGED
|
@@ -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
|
*/
|
package/dist/workers/index.d.ts
CHANGED
|
@@ -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
|
*/
|
package/dist/workers/index.js
CHANGED
|
@@ -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 =
|
|
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.
|
|
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.
|
|
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 =
|
|
508
|
-
const maxCol =
|
|
509
|
-
const minRow =
|
|
510
|
-
const maxRow =
|
|
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 =
|
|
608
|
-
const maxCol =
|
|
609
|
-
const minRow =
|
|
610
|
-
const maxRow =
|
|
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.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Core utilities and theme for Classic Theme maps",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"maps",
|
|
@@ -96,7 +96,8 @@
|
|
|
96
96
|
"@types/rbush": "^4.0.0",
|
|
97
97
|
"maplibre-gl": "^5.0.0",
|
|
98
98
|
"tsup": "^8.0.0",
|
|
99
|
-
"typescript": "^5.3.0"
|
|
99
|
+
"typescript": "^5.3.0",
|
|
100
|
+
"vitest": "^4.1.10"
|
|
100
101
|
},
|
|
101
102
|
"publishConfig": {
|
|
102
103
|
"access": "public"
|