@latlng/wasm 0.1.0

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.
@@ -0,0 +1,95 @@
1
+ import { type AreaQuery, type BoundingBox, type CollectionInfo, type CollectionStats, type GeofenceDef, type HookDef, type HookInfo, type LatLngObject, type LatLngWasmEventMap, type NearbyQuery, type SearchOptions, type SearchResults, type ServerInfo, type SetObjectOptions, type GeoObject } from "./types.js";
2
+ /** Configuration for `createLatLng`. */
3
+ export interface CreateLatLngOptions {
4
+ /**
5
+ * Explicit URL for `latlng_core_bg.wasm`.
6
+ *
7
+ * Use this when your bundler or CDN serves the wasm file from a custom
8
+ * location. If omitted, the bundled worker resolves `../wasm/latlng_core_bg.wasm`
9
+ * relative to its own emitted worker URL.
10
+ */
11
+ wasmUrl?: string | URL;
12
+ /**
13
+ * Custom worker factory for tests or advanced bundlers.
14
+ *
15
+ * Normal browser applications should omit this and let the package create its
16
+ * bundled module worker.
17
+ */
18
+ workerFactory?: () => Worker;
19
+ }
20
+ /**
21
+ * Browser-only latlng client backed by a dedicated Web Worker and WebAssembly engine.
22
+ *
23
+ * Every API method is asynchronous because requests are sent to the worker with
24
+ * `postMessage`. The main thread never owns or calls the wasm engine directly.
25
+ */
26
+ export declare class LatLngWasmClient {
27
+ #private;
28
+ /** Creates a client around a Web Worker. Prefer `createLatLng()` in application code. */
29
+ constructor(worker: Worker, options?: Pick<CreateLatLngOptions, "wasmUrl">);
30
+ /** Resolves when the worker has loaded and initialized the wasm engine. */
31
+ ready(): Promise<void>;
32
+ /** Creates a collection explicitly. Object writes still create missing collections implicitly. */
33
+ createCollection(collection: string): Promise<boolean>;
34
+ /** Drops a collection and all objects stored in it. */
35
+ dropCollection(collection: string): Promise<boolean>;
36
+ /** Lists collection names matching a glob pattern. Defaults to all collections. */
37
+ collections(pattern?: string): Promise<string[]>;
38
+ /** Returns collection bounds and statistics, or `null` if the collection does not exist. */
39
+ collectionInfo(collection: string): Promise<CollectionInfo | null>;
40
+ /** Stores or updates an object. */
41
+ setObject(collection: string, id: string, object: GeoObject, options?: SetObjectOptions): Promise<boolean>;
42
+ /** Stores or updates a point object. */
43
+ setPoint(collection: string, id: string, coordinates: {
44
+ lat: number;
45
+ lon: number;
46
+ z?: number | null;
47
+ }, options?: SetObjectOptions): Promise<boolean>;
48
+ /** Returns an object by ID, or `null` when it does not exist. */
49
+ getObject(collection: string, id: string, options?: {
50
+ withFields?: boolean;
51
+ }): Promise<LatLngObject | null>;
52
+ /** Deletes an object by ID. */
53
+ deleteObject(collection: string, id: string): Promise<boolean>;
54
+ /** Sets a TTL in seconds for an existing object. */
55
+ expire(collection: string, id: string, seconds: number): Promise<boolean>;
56
+ /** Removes TTL metadata from an existing object. */
57
+ persist(collection: string, id: string): Promise<boolean>;
58
+ /** Returns the remaining TTL in seconds, or `null` when no TTL is set or the object is missing. */
59
+ ttl(collection: string, id: string): Promise<number | null>;
60
+ /** Creates or replaces a browser geofence hook. Matching events are emitted as JS events. */
61
+ setHook(name: string, def: GeofenceDef): Promise<boolean>;
62
+ /** Deletes a browser geofence hook. */
63
+ deleteHook(name: string): Promise<boolean>;
64
+ /** Lists browser geofence hooks matching a glob pattern. */
65
+ hooks(pattern?: string): Promise<HookInfo[]>;
66
+ /** Returns a full browser geofence hook definition, or `null` when it does not exist. */
67
+ getHook(name: string): Promise<HookDef | null>;
68
+ /** Runs a nearby spatial query. */
69
+ nearby(collection: string, query: NearbyQuery): Promise<SearchResults>;
70
+ /** Runs a within spatial query. */
71
+ within(collection: string, query: AreaQuery): Promise<SearchResults>;
72
+ /** Runs an intersects spatial query. */
73
+ intersects(collection: string, query: AreaQuery): Promise<SearchResults>;
74
+ /** Scans a collection. */
75
+ scan(collection: string, options?: SearchOptions): Promise<SearchResults>;
76
+ /** Scans string objects in a collection. */
77
+ search(collection: string, options?: SearchOptions): Promise<SearchResults>;
78
+ /** Returns collection bounds, or `null` when the collection does not exist. */
79
+ bounds(collection: string): Promise<BoundingBox | null>;
80
+ /** Returns statistics for existing collections. */
81
+ stats(collections: string[]): Promise<CollectionStats[]>;
82
+ /** Returns engine metadata for the in-browser wasm engine. */
83
+ serverInfo(): Promise<ServerInfo>;
84
+ /** Registers a typed event listener for mutation or geofence events. */
85
+ addEventListener<K extends keyof LatLngWasmEventMap>(type: K, listener: (event: LatLngWasmEventMap[K]) => void, options?: AddEventListenerOptions): void;
86
+ /** Removes a previously registered typed event listener. */
87
+ removeEventListener<K extends keyof LatLngWasmEventMap>(type: K, listener: (event: LatLngWasmEventMap[K]) => void, options?: EventListenerOptions): void;
88
+ /** Convenience alias for `addEventListener`. */
89
+ on<K extends keyof LatLngWasmEventMap>(type: K, listener: (event: LatLngWasmEventMap[K]) => void): () => void;
90
+ /** Terminates the worker and rejects pending requests. The client cannot be reused afterward. */
91
+ close(): void;
92
+ }
93
+ /** Creates a worker-backed in-browser latlng engine. */
94
+ export declare function createLatLng(options?: CreateLatLngOptions): Promise<LatLngWasmClient>;
95
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAOA,OAAO,EASL,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,WAAW,EAEhB,KAAK,OAAO,EACZ,KAAK,QAAQ,EACb,KAAK,YAAY,EACjB,KAAK,kBAAkB,EACvB,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACf,MAAM,YAAY,CAAC;AAEpB,wCAAwC;AACxC,MAAM,WAAW,mBAAmB;IAClC;;;;;;OAMG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC;IACvB;;;;;OAKG;IACH,aAAa,CAAC,EAAE,MAAM,MAAM,CAAC;CAC9B;AAOD;;;;;GAKG;AACH,qBAAa,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O,CAAC,aAAa,CAAC;IAMxF,+EAA+E;IAClE,MAAM,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;IAIpE,mDAAmD;IACtC,KAAK,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAIrE,8DAA8D;IACjD,UAAU,IAAI,OAAO,CAAC,UAAU,CAAC;IAI9C,wEAAwE;IACjE,gBAAgB,CAAC,CAAC,SAAS,MAAM,kBAAkB,EACxD,IAAI,EAAE,CAAC,EACP,QAAQ,EAAE,CAAC,KAAK,EAAE,kBAAkB,CAAC,CAAC,CAAC,KAAK,IAAI,EAChD,OAAO,CAAC,EAAE,uBAAuB,GAChC,IAAI;IAIP,4DAA4D;IACrD,mBAAmB,CAAC,CAAC,SAAS,MAAM,kBAAkB,EAC3D,IAAI,EAAE,CAAC,EACP,QAAQ,EAAE,CAAC,KAAK,EAAE,kBAAkB,CAAC,CAAC,CAAC,KAAK,IAAI,EAChD,OAAO,CAAC,EAAE,oBAAoB,GAC7B,IAAI;IAIP,gDAAgD;IACzC,EAAE,CAAC,CAAC,SAAS,MAAM,kBAAkB,EAC1C,IAAI,EAAE,CAAC,EACP,QAAQ,EAAE,CAAC,KAAK,EAAE,kBAAkB,CAAC,CAAC,CAAC,KAAK,IAAI,GAC/C,MAAM,IAAI;IAKb,iGAAiG;IAC1F,KAAK,IAAI,IAAI;CA+DrB;AAED,wDAAwD;AACxD,wBAAsB,YAAY,CAChC,OAAO,GAAE,mBAAwB,GAChC,OAAO,CAAC,gBAAgB,CAAC,CAK3B"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=engine.worker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"engine.worker.d.ts","sourceRoot":"","sources":["../src/engine.worker.ts"],"names":[],"mappings":""}
@@ -0,0 +1,3 @@
1
+ export { createLatLng, LatLngWasmClient, type CreateLatLngOptions } from "./client.js";
2
+ export type { Area, AreaQuery, BoundingBox, CollectionInfo, CollectionStats, DetectType, FieldEntry, FieldMap, FieldValue, GeofenceDef, GeofenceEvent, GeofenceQuery, GeoObject, HookDef, HookInfo, JsonValue, LatLngObject, LatLngWasmEventMap, MutationCommand, MutationEvent, NearbyQuery, OutputFormat, RoamingInfo, SearchItem, SearchOptions, SearchResults, ServerInfo, SetObjectOptions, SortOrder, WhereComparison, WhereExprFilter, WhereFilter, WhereInFilter, } from "./types.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,KAAK,mBAAmB,EAAE,MAAM,aAAa,CAAC;AACvF,YAAY,EACV,IAAI,EACJ,SAAS,EACT,WAAW,EACX,cAAc,EACd,eAAe,EACf,UAAU,EACV,UAAU,EACV,QAAQ,EACR,UAAU,EACV,WAAW,EACX,aAAa,EACb,aAAa,EACb,SAAS,EACT,OAAO,EACP,QAAQ,EACR,SAAS,EACT,YAAY,EACZ,kBAAkB,EAClB,eAAe,EACf,aAAa,EACb,WAAW,EACX,YAAY,EACZ,WAAW,EACX,UAAU,EACV,aAAa,EACb,aAAa,EACb,UAAU,EACV,gBAAgB,EAChB,SAAS,EACT,eAAe,EACf,eAAe,EACf,WAAW,EACX,aAAa,GACd,MAAM,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,576 @@
1
+ //#region src/engine.worker.ts?worker
2
+ function e(e) {
3
+ return new Worker("/assets/engine.worker-CkKJqc5H.js", { name: e?.name });
4
+ }
5
+ //#endregion
6
+ //#region src/types.ts
7
+ function t(e, t) {
8
+ return {
9
+ object: p(e),
10
+ fields: l(t?.fields),
11
+ expire_seconds: t?.expireSeconds ?? null,
12
+ condition: C(t?.condition)
13
+ };
14
+ }
15
+ function n(e) {
16
+ return {
17
+ lat: e.lat,
18
+ lon: e.lon,
19
+ meters: e.meters,
20
+ options: i(e.options)
21
+ };
22
+ }
23
+ function r(e) {
24
+ return {
25
+ area: h(e.area),
26
+ options: i(e.options)
27
+ };
28
+ }
29
+ function i(e) {
30
+ let t = e ?? {};
31
+ return {
32
+ cursor: t.cursor ?? 0,
33
+ limit: t.limit ?? 100,
34
+ nofields: t.nofields ?? !1,
35
+ match_pattern: t.matchPattern ?? null,
36
+ sort: t.sort === "desc" ? "Desc" : "Asc",
37
+ where_filters: (t.whereFilters ?? []).map((e) => ({
38
+ field: e.field,
39
+ comparison: _(e.comparison)
40
+ })),
41
+ where_in_filters: t.whereInFilters ?? [],
42
+ where_expr_filters: t.whereExprFilters ?? [],
43
+ clip: t.clip ?? !1,
44
+ output: g(t.output)
45
+ };
46
+ }
47
+ function a(e) {
48
+ return {
49
+ collection: e.collection,
50
+ query: v(e.query),
51
+ detect: e.detect.map(E),
52
+ commands: e.commands.map(w)
53
+ };
54
+ }
55
+ function o(e) {
56
+ return e == null ? null : (k(e, "object response"), {
57
+ id: A(e.id, "object id"),
58
+ geo: m(e.geo),
59
+ fields: f(e.fields),
60
+ expires_at: typeof e.expires_at == "number" ? e.expires_at : null
61
+ });
62
+ }
63
+ function s(e) {
64
+ if (k(e, "search results"), !Array.isArray(e.results)) throw Error("search results must include results array");
65
+ return {
66
+ results: e.results.map((e) => (k(e, "search result item"), {
67
+ id: A(e.id, "result id"),
68
+ object: e.object === null || e.object === void 0 ? void 0 : m(e.object),
69
+ fields: e.fields === null || e.fields === void 0 ? void 0 : f(e.fields),
70
+ distance_meters: typeof e.distance_meters == "number" ? e.distance_meters : null
71
+ })),
72
+ cursor: Number(e.cursor ?? 0),
73
+ count: Number(e.count ?? 0)
74
+ };
75
+ }
76
+ function c(e) {
77
+ return e == null ? null : (k(e, "hook definition"), {
78
+ name: A(e.name, "hook name"),
79
+ endpoint: A(e.endpoint, "hook endpoint"),
80
+ def: y(e.def)
81
+ });
82
+ }
83
+ function l(e) {
84
+ return e ? (Array.isArray(e) ? e : Object.entries(e).map(([e, t]) => ({
85
+ name: e,
86
+ value: t
87
+ }))).map((e) => ({
88
+ name: e.name,
89
+ value: u(e.value)
90
+ })) : [];
91
+ }
92
+ function u(e) {
93
+ switch (e.type) {
94
+ case "number": return {
95
+ type: "number",
96
+ value: e.value
97
+ };
98
+ case "text": return {
99
+ type: "text",
100
+ value: e.value
101
+ };
102
+ case "json": return {
103
+ type: "json",
104
+ value: JSON.stringify(e.value)
105
+ };
106
+ }
107
+ }
108
+ function d(e) {
109
+ if (k(e, "field value"), e.type === "number") return {
110
+ type: "number",
111
+ value: Number(e.value)
112
+ };
113
+ if (e.type === "text") return {
114
+ type: "text",
115
+ value: String(e.value)
116
+ };
117
+ if (e.type === "json") {
118
+ let t = String(e.value);
119
+ try {
120
+ return {
121
+ type: "json",
122
+ value: JSON.parse(t)
123
+ };
124
+ } catch {
125
+ return {
126
+ type: "json",
127
+ value: t
128
+ };
129
+ }
130
+ }
131
+ throw Error("unknown field value shape");
132
+ }
133
+ function f(e) {
134
+ return !O(e) || !O(e.values) ? {} : Object.fromEntries(Object.entries(e.values).map(([e, t]) => [e, d(t)]));
135
+ }
136
+ function p(e) {
137
+ switch (e.type) {
138
+ case "point": return { Point: {
139
+ lat: e.lat,
140
+ lon: e.lon,
141
+ z: e.z ?? null
142
+ } };
143
+ case "bounds": return { Bounds: e.bounds };
144
+ case "hash": return { Hash: e.value };
145
+ case "geojson": return { GeoJson: e.value };
146
+ case "string": return { String: e.value };
147
+ }
148
+ }
149
+ function m(e) {
150
+ if (k(e, "geo object"), "Point" in e) {
151
+ let t = e.Point;
152
+ return k(t, "point"), {
153
+ type: "point",
154
+ lat: Number(t.lat),
155
+ lon: Number(t.lon),
156
+ z: typeof t.z == "number" || t.z === null ? t.z : void 0
157
+ };
158
+ }
159
+ if ("Bounds" in e) return {
160
+ type: "bounds",
161
+ bounds: e.Bounds
162
+ };
163
+ if ("Hash" in e) return {
164
+ type: "hash",
165
+ value: String(e.Hash)
166
+ };
167
+ if ("GeoJson" in e) return {
168
+ type: "geojson",
169
+ value: e.GeoJson
170
+ };
171
+ if ("String" in e) return {
172
+ type: "string",
173
+ value: String(e.String)
174
+ };
175
+ throw Error("unknown geo object shape");
176
+ }
177
+ function h(e) {
178
+ switch (e.type) {
179
+ case "circle": return { Circle: {
180
+ lat: e.lat,
181
+ lon: e.lon,
182
+ meters: e.meters
183
+ } };
184
+ case "bounds": return { Bounds: e.bounds };
185
+ case "hash": return { Hash: e.value };
186
+ case "geojson": return { GeoJson: e.value };
187
+ case "tile": return { Tile: {
188
+ x: e.x,
189
+ y: e.y,
190
+ z: e.z
191
+ } };
192
+ case "quadkey": return { Quadkey: e.value };
193
+ case "sector": return { Sector: {
194
+ lat: e.lat,
195
+ lon: e.lon,
196
+ meters: e.meters,
197
+ bearing1: e.bearing1,
198
+ bearing2: e.bearing2
199
+ } };
200
+ case "reference": return { Reference: {
201
+ collection: e.collection,
202
+ id: e.id
203
+ } };
204
+ }
205
+ }
206
+ function g(e) {
207
+ return !e || e === "objects" ? "Objects" : e === "points" ? "Points" : e === "bounds" ? "Bounds" : e === "ids" ? "Ids" : e === "count" ? "Count" : { Hashes: { precision: e.hashes.precision } };
208
+ }
209
+ function _(e) {
210
+ switch (e.type) {
211
+ case "range": return { Range: {
212
+ min: e.min,
213
+ max: e.max
214
+ } };
215
+ case "equalsText": return { EqualsText: e.value };
216
+ case "regex": return { Regex: e.value };
217
+ }
218
+ }
219
+ function v(e) {
220
+ switch (e.type) {
221
+ case "nearby": return { Nearby: {
222
+ lat: e.lat,
223
+ lon: e.lon,
224
+ meters: e.meters,
225
+ options: i(e.options)
226
+ } };
227
+ case "within": return { Within: {
228
+ area: h(e.area),
229
+ options: i(e.options)
230
+ } };
231
+ case "intersects": return { Intersects: {
232
+ area: h(e.area),
233
+ options: i(e.options)
234
+ } };
235
+ case "roam": return { Roam: {
236
+ target_collection: e.targetCollection,
237
+ target_pattern: e.targetPattern,
238
+ meters: e.meters,
239
+ options: i(e.options),
240
+ nodwell: e.nodwell ?? !1
241
+ } };
242
+ }
243
+ }
244
+ function y(e) {
245
+ k(e, "geofence definition");
246
+ let t = e.detect, n = e.commands;
247
+ if (!Array.isArray(t) || !Array.isArray(n)) throw Error("geofence detect and commands must be arrays");
248
+ return {
249
+ collection: A(e.collection, "geofence collection"),
250
+ query: b(e.query),
251
+ detect: t.map(D),
252
+ commands: n.map(T)
253
+ };
254
+ }
255
+ function b(e) {
256
+ if (k(e, "geofence query"), "Nearby" in e) {
257
+ let t = e.Nearby;
258
+ return k(t, "nearby query"), {
259
+ type: "nearby",
260
+ lat: Number(t.lat),
261
+ lon: Number(t.lon),
262
+ meters: Number(t.meters),
263
+ options: S(t.options)
264
+ };
265
+ }
266
+ if ("Within" in e) {
267
+ let t = e.Within;
268
+ return k(t, "within query"), {
269
+ type: "within",
270
+ area: x(t.area),
271
+ options: S(t.options)
272
+ };
273
+ }
274
+ if ("Intersects" in e) {
275
+ let t = e.Intersects;
276
+ return k(t, "intersects query"), {
277
+ type: "intersects",
278
+ area: x(t.area),
279
+ options: S(t.options)
280
+ };
281
+ }
282
+ if ("Roam" in e) {
283
+ let t = e.Roam;
284
+ return k(t, "roam query"), {
285
+ type: "roam",
286
+ targetCollection: A(t.target_collection, "roam target collection"),
287
+ targetPattern: A(t.target_pattern, "roam target pattern"),
288
+ meters: Number(t.meters),
289
+ options: S(t.options),
290
+ nodwell: typeof t.nodwell == "boolean" ? t.nodwell : void 0
291
+ };
292
+ }
293
+ throw Error("unknown geofence query shape");
294
+ }
295
+ function x(e) {
296
+ if (k(e, "area"), "Circle" in e) {
297
+ let t = e.Circle;
298
+ return k(t, "circle area"), {
299
+ type: "circle",
300
+ lat: Number(t.lat),
301
+ lon: Number(t.lon),
302
+ meters: Number(t.meters)
303
+ };
304
+ }
305
+ if ("Bounds" in e) return {
306
+ type: "bounds",
307
+ bounds: e.Bounds
308
+ };
309
+ if ("Hash" in e) return {
310
+ type: "hash",
311
+ value: String(e.Hash)
312
+ };
313
+ if ("GeoJson" in e) return {
314
+ type: "geojson",
315
+ value: e.GeoJson
316
+ };
317
+ if ("Tile" in e) {
318
+ let t = e.Tile;
319
+ return k(t, "tile area"), {
320
+ type: "tile",
321
+ x: Number(t.x),
322
+ y: Number(t.y),
323
+ z: Number(t.z)
324
+ };
325
+ }
326
+ if ("Quadkey" in e) return {
327
+ type: "quadkey",
328
+ value: String(e.Quadkey)
329
+ };
330
+ if ("Sector" in e) {
331
+ let t = e.Sector;
332
+ return k(t, "sector area"), {
333
+ type: "sector",
334
+ lat: Number(t.lat),
335
+ lon: Number(t.lon),
336
+ meters: Number(t.meters),
337
+ bearing1: Number(t.bearing1),
338
+ bearing2: Number(t.bearing2)
339
+ };
340
+ }
341
+ if ("Reference" in e) {
342
+ let t = e.Reference;
343
+ return k(t, "reference area"), {
344
+ type: "reference",
345
+ collection: A(t.collection, "reference collection"),
346
+ id: A(t.id, "reference id")
347
+ };
348
+ }
349
+ throw Error("unknown area shape");
350
+ }
351
+ function S(e) {
352
+ if (O(e)) return {
353
+ cursor: typeof e.cursor == "number" ? e.cursor : void 0,
354
+ limit: typeof e.limit == "number" ? e.limit : void 0,
355
+ nofields: typeof e.nofields == "boolean" ? e.nofields : void 0,
356
+ matchPattern: typeof e.match_pattern == "string" ? e.match_pattern : void 0,
357
+ sort: e.sort === "Desc" ? "desc" : e.sort === "Asc" ? "asc" : void 0,
358
+ clip: typeof e.clip == "boolean" ? e.clip : void 0
359
+ };
360
+ }
361
+ function C(e) {
362
+ switch (e) {
363
+ case "nx": return "Nx";
364
+ case "xx": return "Xx";
365
+ case "always":
366
+ case void 0: return "Always";
367
+ }
368
+ }
369
+ function w(e) {
370
+ switch (e) {
371
+ case "set": return "Set";
372
+ case "del": return "Del";
373
+ case "drop": return "Drop";
374
+ case "fset": return "Fset";
375
+ }
376
+ }
377
+ function T(e) {
378
+ switch (e) {
379
+ case "Set":
380
+ case "set": return "set";
381
+ case "Del":
382
+ case "del": return "del";
383
+ case "Drop":
384
+ case "drop": return "drop";
385
+ case "Fset":
386
+ case "fset": return "fset";
387
+ default: throw Error(`unknown mutation command: ${String(e)}`);
388
+ }
389
+ }
390
+ function E(e) {
391
+ switch (e) {
392
+ case "inside": return "Inside";
393
+ case "outside": return "Outside";
394
+ case "enter": return "Enter";
395
+ case "exit": return "Exit";
396
+ case "cross": return "Cross";
397
+ case "roam": return "Roam";
398
+ }
399
+ }
400
+ function D(e) {
401
+ switch (e) {
402
+ case "Inside":
403
+ case "inside": return "inside";
404
+ case "Outside":
405
+ case "outside": return "outside";
406
+ case "Enter":
407
+ case "enter": return "enter";
408
+ case "Exit":
409
+ case "exit": return "exit";
410
+ case "Cross":
411
+ case "cross": return "cross";
412
+ case "Roam":
413
+ case "roam": return "roam";
414
+ default: throw Error(`unknown detect type: ${String(e)}`);
415
+ }
416
+ }
417
+ function O(e) {
418
+ return typeof e == "object" && !!e && !Array.isArray(e);
419
+ }
420
+ function k(e, t) {
421
+ if (!O(e)) throw Error(`${t} must be an object`);
422
+ }
423
+ function A(e, t) {
424
+ if (typeof e != "string") throw Error(`${t} must be a string`);
425
+ return e;
426
+ }
427
+ //#endregion
428
+ //#region src/client.ts
429
+ var j = class {
430
+ #e;
431
+ #t = new EventTarget();
432
+ #n = /* @__PURE__ */ new Map();
433
+ #r;
434
+ #i = 1;
435
+ #a = !1;
436
+ constructor(e, t = {}) {
437
+ this.#e = e, this.#e.addEventListener("message", this.#s), this.#e.addEventListener("error", this.#c), this.#r = this.#o("init", t.wasmUrl ? String(t.wasmUrl) : null).then(() => void 0);
438
+ }
439
+ ready() {
440
+ return this.#r;
441
+ }
442
+ async createCollection(e) {
443
+ return !!await this.#o("createCollection", e);
444
+ }
445
+ async dropCollection(e) {
446
+ return !!await this.#o("dropCollection", e);
447
+ }
448
+ async collections(e = "*") {
449
+ return await this.#o("collections", e);
450
+ }
451
+ async collectionInfo(e) {
452
+ return await this.#o("collectionInfo", e);
453
+ }
454
+ async setObject(e, n, r, i) {
455
+ return !!await this.#o("setObject", e, n, t(r, i));
456
+ }
457
+ async setPoint(e, t, n, r) {
458
+ return this.setObject(e, t, {
459
+ type: "point",
460
+ lat: n.lat,
461
+ lon: n.lon,
462
+ z: n.z
463
+ }, r);
464
+ }
465
+ async getObject(e, t, n = {}) {
466
+ return o(await this.#o("getObject", e, t, n.withFields ?? !0));
467
+ }
468
+ async deleteObject(e, t) {
469
+ return !!await this.#o("deleteObject", e, t);
470
+ }
471
+ async expire(e, t, n) {
472
+ return !!await this.#o("expire", e, t, n);
473
+ }
474
+ async persist(e, t) {
475
+ return !!await this.#o("persist", e, t);
476
+ }
477
+ async ttl(e, t) {
478
+ let n = await this.#o("ttl", e, t);
479
+ return typeof n == "number" ? n : null;
480
+ }
481
+ async setHook(e, t) {
482
+ return !!await this.#o("setHook", e, { def: a(t) });
483
+ }
484
+ async deleteHook(e) {
485
+ return !!await this.#o("deleteHook", e);
486
+ }
487
+ async hooks(e = "*") {
488
+ return await this.#o("hooks", e);
489
+ }
490
+ async getHook(e) {
491
+ return c(await this.#o("getHook", e));
492
+ }
493
+ async nearby(e, t) {
494
+ return s(await this.#o("nearby", e, n(t)));
495
+ }
496
+ async within(e, t) {
497
+ return s(await this.#o("within", e, r(t)));
498
+ }
499
+ async intersects(e, t) {
500
+ return s(await this.#o("intersects", e, r(t)));
501
+ }
502
+ async scan(e, t) {
503
+ return s(await this.#o("scan", e, i(t)));
504
+ }
505
+ async search(e, t) {
506
+ return s(await this.#o("search", e, i(t)));
507
+ }
508
+ async bounds(e) {
509
+ return await this.#o("bounds", e);
510
+ }
511
+ async stats(e) {
512
+ return await this.#o("stats", e);
513
+ }
514
+ async serverInfo() {
515
+ return await this.#o("serverInfo");
516
+ }
517
+ addEventListener(e, t, n) {
518
+ this.#t.addEventListener(e, t, n);
519
+ }
520
+ removeEventListener(e, t, n) {
521
+ this.#t.removeEventListener(e, t, n);
522
+ }
523
+ on(e, t) {
524
+ return this.addEventListener(e, t), () => this.removeEventListener(e, t);
525
+ }
526
+ close() {
527
+ if (!this.#a) {
528
+ this.#a = !0, this.#e.removeEventListener("message", this.#s), this.#e.removeEventListener("error", this.#c), this.#e.terminate();
529
+ for (let e of this.#n.values()) e.reject(/* @__PURE__ */ Error("latlng wasm client is closed"));
530
+ this.#n.clear();
531
+ }
532
+ }
533
+ #o(e, ...t) {
534
+ if (this.#a) return Promise.reject(/* @__PURE__ */ Error("latlng wasm client is closed"));
535
+ let n = this.#i++, r = {
536
+ id: n,
537
+ method: e,
538
+ params: t
539
+ };
540
+ return new Promise((e, t) => {
541
+ this.#n.set(n, {
542
+ resolve: e,
543
+ reject: t
544
+ }), this.#e.postMessage(r);
545
+ });
546
+ }
547
+ #s = (e) => {
548
+ let t = e.data;
549
+ if ("type" in t && t.type === "event") {
550
+ if (t.eventType === "mutation") {
551
+ this.#t.dispatchEvent(new CustomEvent("mutation", { detail: t.event }));
552
+ return;
553
+ }
554
+ this.#t.dispatchEvent(new CustomEvent("geofence", { detail: t.event })), this.#t.dispatchEvent(new CustomEvent(`geofence:${t.event.detect}`, { detail: t.event }));
555
+ return;
556
+ }
557
+ this.#l(t);
558
+ };
559
+ #c = (e) => {
560
+ let t = Error(e.message || "latlng wasm worker failed");
561
+ for (let e of this.#n.values()) e.reject(t);
562
+ this.#n.clear();
563
+ };
564
+ #l(e) {
565
+ let t = this.#n.get(e.id);
566
+ t && (this.#n.delete(e.id), e.ok ? t.resolve(e.result) : t.reject(Error(e.error)));
567
+ }
568
+ };
569
+ async function M(t = {}) {
570
+ let n = new j(t.workerFactory?.() ?? new e(), { wasmUrl: t.wasmUrl });
571
+ return await n.ready(), n;
572
+ }
573
+ //#endregion
574
+ export { j as LatLngWasmClient, M as createLatLng };
575
+
576
+ //# sourceMappingURL=index.js.map