@accelint/map-toolkit 0.3.1 → 0.4.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.
Files changed (104) hide show
  1. package/CHANGELOG.md +49 -24
  2. package/catalog-info.yaml +5 -7
  3. package/dist/cursor-coordinates/index.d.ts +14 -3
  4. package/dist/cursor-coordinates/index.js +16 -3
  5. package/dist/cursor-coordinates/use-cursor-coordinates.d.ts +20 -6
  6. package/dist/cursor-coordinates/use-cursor-coordinates.js +247 -128
  7. package/dist/cursor-coordinates/use-cursor-coordinates.js.map +1 -1
  8. package/dist/deckgl/base-map/constants.d.ts +14 -12
  9. package/dist/deckgl/base-map/constants.js +26 -12
  10. package/dist/deckgl/base-map/constants.js.map +1 -1
  11. package/dist/deckgl/base-map/events.d.ts +6 -4
  12. package/dist/deckgl/base-map/events.js +18 -4
  13. package/dist/deckgl/base-map/events.js.map +1 -1
  14. package/dist/deckgl/base-map/index.d.ts +45 -18
  15. package/dist/deckgl/base-map/index.js +216 -148
  16. package/dist/deckgl/base-map/index.js.map +1 -1
  17. package/dist/deckgl/base-map/provider.d.ts +48 -32
  18. package/dist/deckgl/base-map/provider.js +122 -11
  19. package/dist/deckgl/base-map/provider.js.map +1 -1
  20. package/dist/deckgl/base-map/types.d.ts +49 -39
  21. package/dist/deckgl/base-map/types.js +11 -2
  22. package/dist/deckgl/index.d.ts +18 -13
  23. package/dist/deckgl/index.js +19 -6
  24. package/dist/deckgl/symbol-layer/fiber.d.ts +21 -10
  25. package/dist/deckgl/symbol-layer/fiber.js +18 -3
  26. package/dist/deckgl/symbol-layer/fiber.js.map +1 -1
  27. package/dist/deckgl/symbol-layer/index.d.ts +68 -54
  28. package/dist/deckgl/symbol-layer/index.js +105 -85
  29. package/dist/deckgl/symbol-layer/index.js.map +1 -1
  30. package/dist/deckgl/text-layer/character-sets.d.ts +19 -17
  31. package/dist/deckgl/text-layer/character-sets.js +40 -19
  32. package/dist/deckgl/text-layer/character-sets.js.map +1 -1
  33. package/dist/deckgl/text-layer/default-settings.d.ts +16 -2
  34. package/dist/deckgl/text-layer/default-settings.js +42 -18
  35. package/dist/deckgl/text-layer/default-settings.js.map +1 -1
  36. package/dist/deckgl/text-layer/fiber.d.ts +38 -27
  37. package/dist/deckgl/text-layer/fiber.js +18 -3
  38. package/dist/deckgl/text-layer/fiber.js.map +1 -1
  39. package/dist/deckgl/text-layer/index.d.ts +39 -25
  40. package/dist/deckgl/text-layer/index.js +47 -29
  41. package/dist/deckgl/text-layer/index.js.map +1 -1
  42. package/dist/decorators/deckgl.d.ts +16 -2
  43. package/dist/decorators/deckgl.js +25 -7
  44. package/dist/decorators/deckgl.js.map +1 -1
  45. package/dist/map-cursor/events.d.ts +16 -0
  46. package/dist/map-cursor/events.js +27 -0
  47. package/dist/map-cursor/events.js.map +1 -0
  48. package/dist/map-cursor/index.d.ts +17 -0
  49. package/dist/map-cursor/index.js +18 -0
  50. package/dist/map-cursor/store.d.ts +93 -0
  51. package/dist/map-cursor/store.js +351 -0
  52. package/dist/map-cursor/store.js.map +1 -0
  53. package/dist/map-cursor/types.d.ts +81 -0
  54. package/dist/map-cursor/types.js +12 -0
  55. package/dist/map-cursor/use-map-cursor.d.ts +99 -0
  56. package/dist/map-cursor/use-map-cursor.js +116 -0
  57. package/dist/map-cursor/use-map-cursor.js.map +1 -0
  58. package/dist/map-mode/events.d.ts +11 -9
  59. package/dist/map-mode/events.js +43 -9
  60. package/dist/map-mode/events.js.map +1 -1
  61. package/dist/map-mode/index.d.ts +17 -6
  62. package/dist/map-mode/index.js +18 -5
  63. package/dist/map-mode/store.d.ts +26 -3
  64. package/dist/map-mode/store.js +329 -265
  65. package/dist/map-mode/store.js.map +1 -1
  66. package/dist/map-mode/types.d.ts +49 -35
  67. package/dist/map-mode/types.js +11 -2
  68. package/dist/map-mode/use-map-mode.d.ts +21 -7
  69. package/dist/map-mode/use-map-mode.js +66 -23
  70. package/dist/map-mode/use-map-mode.js.map +1 -1
  71. package/dist/maplibre/constants.d.ts +10 -8
  72. package/dist/maplibre/constants.js +22 -8
  73. package/dist/maplibre/constants.js.map +1 -1
  74. package/dist/maplibre/hooks/use-maplibre.d.ts +17 -2
  75. package/dist/maplibre/hooks/use-maplibre.js +77 -31
  76. package/dist/maplibre/hooks/use-maplibre.js.map +1 -1
  77. package/dist/maplibre/index.d.ts +15 -3
  78. package/dist/maplibre/index.js +17 -4
  79. package/dist/viewport/constants.d.ts +8 -6
  80. package/dist/viewport/constants.js +20 -6
  81. package/dist/viewport/constants.js.map +1 -1
  82. package/dist/viewport/index.d.ts +18 -13
  83. package/dist/viewport/index.js +19 -6
  84. package/dist/viewport/types.d.ts +27 -17
  85. package/dist/viewport/types.js +11 -2
  86. package/dist/viewport/use-viewport-state.d.ts +29 -14
  87. package/dist/viewport/use-viewport-state.js +200 -87
  88. package/dist/viewport/use-viewport-state.js.map +1 -1
  89. package/dist/viewport/utils.d.ts +25 -10
  90. package/dist/viewport/utils.js +67 -37
  91. package/dist/viewport/utils.js.map +1 -1
  92. package/dist/viewport/viewport-size.d.ts +27 -15
  93. package/dist/viewport/viewport-size.js +54 -11
  94. package/dist/viewport/viewport-size.js.map +1 -1
  95. package/package.json +107 -78
  96. package/dist/cursor-coordinates/index.js.map +0 -1
  97. package/dist/deckgl/base-map/types.js.map +0 -1
  98. package/dist/deckgl/index.js.map +0 -1
  99. package/dist/map-mode/index.js.map +0 -1
  100. package/dist/map-mode/types.js.map +0 -1
  101. package/dist/maplibre/index.js.map +0 -1
  102. package/dist/metafile-esm.json +0 -1
  103. package/dist/viewport/index.js.map +0 -1
  104. package/dist/viewport/types.js.map +0 -1
@@ -0,0 +1,351 @@
1
+ /*
2
+ * Copyright 2025 Hypergiant Galactic Systems Inc. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at https://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+
14
+ import { MapModeEvents } from "../map-mode/events.js";
15
+ import { getCurrentModeOwner } from "../map-mode/store.js";
16
+ import { MapCursorEvents } from "./events.js";
17
+ import { Broadcast } from "@accelint/bus";
18
+
19
+ //#region src/map-cursor/store.ts
20
+ /**
21
+ * Default cursor value
22
+ */
23
+ const DEFAULT_CURSOR = "default";
24
+ /**
25
+ * Typed event bus instance for map cursor events.
26
+ * Provides type-safe event emission and listening for all map cursor state changes.
27
+ */
28
+ const mapCursorBus = Broadcast.getInstance();
29
+ /**
30
+ * Store for map cursor state keyed by instanceId
31
+ */
32
+ const cursorStore = /* @__PURE__ */ new Map();
33
+ /**
34
+ * Track React component subscribers per instanceId (for fan-out notifications).
35
+ * Each Set contains onStoreChange callbacks from useSyncExternalStore.
36
+ */
37
+ const componentSubscribers = /* @__PURE__ */ new Map();
38
+ /**
39
+ * Cache of bus unsubscribe functions (1 per instanceId).
40
+ * This ensures we only have one bus listener per map cursor instance, regardless of
41
+ * how many React components subscribe to it.
42
+ */
43
+ const busUnsubscribers = /* @__PURE__ */ new Map();
44
+ /**
45
+ * Cache of subscription functions per instanceId to avoid recreating on every render
46
+ */
47
+ const subscriptionCache = /* @__PURE__ */ new Map();
48
+ /**
49
+ * Cache of snapshot functions per instanceId to maintain referential stability
50
+ */
51
+ const snapshotCache = /* @__PURE__ */ new Map();
52
+ /**
53
+ * Cache of server snapshot functions per instanceId to maintain referential stability.
54
+ * Server snapshots always return default cursor since cursor state is client-only.
55
+ */
56
+ const serverSnapshotCache = /* @__PURE__ */ new Map();
57
+ /**
58
+ * Cache of requestCursorChange functions per instanceId to maintain referential stability
59
+ */
60
+ const requestCursorChangeCache = /* @__PURE__ */ new Map();
61
+ /**
62
+ * Cache of clearCursor functions per instanceId to maintain referential stability
63
+ */
64
+ const clearCursorCache = /* @__PURE__ */ new Map();
65
+ /**
66
+ * All state caches that need cleanup when an instance is removed
67
+ */
68
+ const stateCaches = [
69
+ cursorStore,
70
+ componentSubscribers,
71
+ subscriptionCache,
72
+ snapshotCache,
73
+ serverSnapshotCache,
74
+ requestCursorChangeCache,
75
+ clearCursorCache
76
+ ];
77
+ /**
78
+ * Clear all cached state for a given instanceId
79
+ */
80
+ function clearAllCaches(instanceId) {
81
+ stateCaches.map((cache) => cache.delete(instanceId));
82
+ }
83
+ /**
84
+ * Get or create cursor state for a given instanceId
85
+ */
86
+ function getOrCreateState(instanceId) {
87
+ if (!cursorStore.has(instanceId)) cursorStore.set(instanceId, {
88
+ cursorOwners: /* @__PURE__ */ new Map(),
89
+ currentCursor: null,
90
+ currentOwner: null
91
+ });
92
+ return cursorStore.get(instanceId);
93
+ }
94
+ /**
95
+ * Get current cursor snapshot for a given instance (for useSyncExternalStore)
96
+ *
97
+ * Priority order:
98
+ * 1. Mode owner's cursor (if exists and mode is owned)
99
+ * 2. Most recent cursor (only if currentOwner still has an entry in storage)
100
+ * 3. Default cursor
101
+ */
102
+ function getSnapshot(instanceId) {
103
+ const state = cursorStore.get(instanceId);
104
+ if (!state) return DEFAULT_CURSOR;
105
+ const modeOwner = getCurrentModeOwner(instanceId);
106
+ if (modeOwner) {
107
+ const modeOwnerCursor = state.cursorOwners.get(modeOwner);
108
+ if (modeOwnerCursor) return modeOwnerCursor;
109
+ }
110
+ if (state.currentCursor && state.currentOwner) {
111
+ if (state.cursorOwners.has(state.currentOwner)) return state.currentCursor;
112
+ }
113
+ return DEFAULT_CURSOR;
114
+ }
115
+ /**
116
+ * Notify all React subscribers for a given instanceId
117
+ */
118
+ function notifySubscribers(instanceId) {
119
+ const subscribers = componentSubscribers.get(instanceId);
120
+ if (subscribers) for (const onStoreChange of subscribers) onStoreChange();
121
+ }
122
+ /**
123
+ * Handle cursor change request for a given instance
124
+ */
125
+ function handleCursorChangeRequest(instanceId, state, cursor, requestOwner) {
126
+ const previousCursor = getSnapshot(instanceId);
127
+ if (state.cursorOwners.get(requestOwner) === cursor) return;
128
+ state.cursorOwners.set(requestOwner, cursor);
129
+ const modeOwner = getCurrentModeOwner(instanceId);
130
+ if (!modeOwner || requestOwner === modeOwner) {
131
+ state.currentCursor = cursor;
132
+ state.currentOwner = requestOwner;
133
+ const newCursor = getSnapshot(instanceId);
134
+ if (previousCursor !== newCursor) {
135
+ mapCursorBus.emit(MapCursorEvents.changed, {
136
+ previousCursor,
137
+ currentCursor: newCursor,
138
+ owner: requestOwner,
139
+ id: instanceId
140
+ });
141
+ notifySubscribers(instanceId);
142
+ }
143
+ } else mapCursorBus.emit(MapCursorEvents.rejected, {
144
+ rejectedCursor: cursor,
145
+ rejectedOwner: requestOwner,
146
+ currentOwner: state.currentOwner || modeOwner || "unknown",
147
+ reason: "not-owner",
148
+ id: instanceId
149
+ });
150
+ }
151
+ /**
152
+ * Ensures a single bus listener exists for the given instanceId.
153
+ * All React subscribers will be notified via fan-out when the bus events fire.
154
+ * This prevents creating N bus listeners for N React components.
155
+ *
156
+ * @param instanceId - The unique identifier for the map cursor instance
157
+ */
158
+ function ensureBusListener(instanceId) {
159
+ if (busUnsubscribers.has(instanceId)) return;
160
+ const state = getOrCreateState(instanceId);
161
+ const unsubRequest = mapCursorBus.on(MapCursorEvents.changeRequest, (event) => {
162
+ const { cursor, owner: requestOwner, id } = event.payload;
163
+ if (id !== instanceId) return;
164
+ handleCursorChangeRequest(instanceId, state, cursor, requestOwner);
165
+ });
166
+ const unsubModeChange = Broadcast.getInstance().on(MapModeEvents.changed, (event) => {
167
+ if (event.payload.id !== instanceId) return;
168
+ const previousCursor = getSnapshot(instanceId);
169
+ if (event.payload.currentMode === "default" || event.payload.previousMode !== event.payload.currentMode) {
170
+ state.currentCursor = null;
171
+ state.currentOwner = null;
172
+ notifySubscribers(instanceId);
173
+ }
174
+ queueMicrotask(() => {
175
+ const newCursor = getSnapshot(instanceId);
176
+ if (previousCursor !== newCursor) {
177
+ const newModeOwner = getCurrentModeOwner(instanceId) || "system";
178
+ mapCursorBus.emit(MapCursorEvents.changed, {
179
+ previousCursor,
180
+ currentCursor: newCursor,
181
+ owner: newModeOwner,
182
+ id: instanceId
183
+ });
184
+ notifySubscribers(instanceId);
185
+ }
186
+ });
187
+ });
188
+ busUnsubscribers.set(instanceId, () => {
189
+ unsubRequest();
190
+ unsubModeChange();
191
+ });
192
+ }
193
+ /**
194
+ * Cleans up the bus listener if no React subscribers remain.
195
+ *
196
+ * @param instanceId - The unique identifier for the map cursor instance
197
+ */
198
+ function cleanupBusListenerIfNeeded(instanceId) {
199
+ const subscribers = componentSubscribers.get(instanceId);
200
+ if (!subscribers || subscribers.size === 0) {
201
+ const unsub = busUnsubscribers.get(instanceId);
202
+ if (unsub) {
203
+ unsub();
204
+ busUnsubscribers.delete(instanceId);
205
+ }
206
+ clearAllCaches(instanceId);
207
+ }
208
+ }
209
+ /**
210
+ * Creates or retrieves a cached subscription function for a given instanceId.
211
+ * Uses a fan-out pattern: 1 bus listener -> N React subscribers.
212
+ * Automatically cleans up cursor state when the last subscriber unsubscribes.
213
+ *
214
+ * @param instanceId - The unique identifier for the map cursor instance
215
+ * @returns A subscription function for useSyncExternalStore
216
+ */
217
+ function getOrCreateSubscription(instanceId) {
218
+ const subscription = subscriptionCache.get(instanceId) ?? ((onStoreChange) => {
219
+ getOrCreateState(instanceId);
220
+ ensureBusListener(instanceId);
221
+ let subscriberSet = componentSubscribers.get(instanceId);
222
+ if (!subscriberSet) {
223
+ subscriberSet = /* @__PURE__ */ new Set();
224
+ componentSubscribers.set(instanceId, subscriberSet);
225
+ }
226
+ subscriberSet.add(onStoreChange);
227
+ return () => {
228
+ const currentSubscriberSet = componentSubscribers.get(instanceId);
229
+ if (currentSubscriberSet) currentSubscriberSet.delete(onStoreChange);
230
+ cleanupBusListenerIfNeeded(instanceId);
231
+ };
232
+ });
233
+ subscriptionCache.set(instanceId, subscription);
234
+ return subscription;
235
+ }
236
+ /**
237
+ * Creates or retrieves a cached snapshot function for a given instanceId.
238
+ * The cursor returned gets equality checked, so it needs to be stable or React re-renders unnecessarily.
239
+ *
240
+ * @param instanceId - The unique identifier for the map cursor instance
241
+ * @returns A snapshot function for useSyncExternalStore
242
+ */
243
+ function getOrCreateSnapshot(instanceId) {
244
+ const snapshot = snapshotCache.get(instanceId) ?? (() => getSnapshot(instanceId));
245
+ snapshotCache.set(instanceId, snapshot);
246
+ return snapshot;
247
+ }
248
+ /**
249
+ * Creates or retrieves a cached server snapshot function for a given instanceId.
250
+ * Server snapshots always return the default cursor since cursor state is client-only.
251
+ * Required for SSR/RSC compatibility with useSyncExternalStore.
252
+ *
253
+ * @param instanceId - The unique identifier for the map cursor instance
254
+ * @returns A server snapshot function for useSyncExternalStore
255
+ */
256
+ function getOrCreateServerSnapshot(instanceId) {
257
+ const serverSnapshot = serverSnapshotCache.get(instanceId) ?? (() => DEFAULT_CURSOR);
258
+ serverSnapshotCache.set(instanceId, serverSnapshot);
259
+ return serverSnapshot;
260
+ }
261
+ /**
262
+ * Creates or retrieves a cached requestCursorChange function for a given instanceId.
263
+ * This maintains referential stability for the function reference.
264
+ *
265
+ * @param instanceId - The unique identifier for the map cursor instance
266
+ * @returns A requestCursorChange function for this instance
267
+ */
268
+ function getOrCreateRequestCursorChange(instanceId) {
269
+ const requestCursorChange = requestCursorChangeCache.get(instanceId) ?? ((cursor, owner) => {
270
+ const trimmedCursor = cursor.trim();
271
+ const trimmedOwner = owner.trim();
272
+ if (!trimmedCursor) throw new Error("requestCursorChange requires non-empty cursor");
273
+ if (!trimmedOwner) throw new Error("requestCursorChange requires non-empty owner");
274
+ mapCursorBus.emit(MapCursorEvents.changeRequest, {
275
+ cursor: trimmedCursor,
276
+ owner: trimmedOwner,
277
+ id: instanceId
278
+ });
279
+ });
280
+ requestCursorChangeCache.set(instanceId, requestCursorChange);
281
+ return requestCursorChange;
282
+ }
283
+ /**
284
+ * Creates or retrieves a cached clearCursor function for a given instanceId.
285
+ * This maintains referential stability for the function reference.
286
+ *
287
+ * @param instanceId - The unique identifier for the map cursor instance
288
+ * @returns A clearCursor function for this instance
289
+ */
290
+ function getOrCreateClearCursor(instanceId) {
291
+ const clearCursor = clearCursorCache.get(instanceId) ?? ((owner) => {
292
+ const state = cursorStore.get(instanceId);
293
+ if (!state) return;
294
+ const hadCursor = state.cursorOwners.has(owner);
295
+ state.cursorOwners.delete(owner);
296
+ if (state.currentOwner === owner) {
297
+ state.currentCursor = null;
298
+ state.currentOwner = null;
299
+ }
300
+ if (hadCursor) notifySubscribers(instanceId);
301
+ });
302
+ clearCursorCache.set(instanceId, clearCursor);
303
+ return clearCursor;
304
+ }
305
+ /**
306
+ * Get the current cursor for a given instance (direct access, not reactive).
307
+ * @internal - For internal map-toolkit use only
308
+ *
309
+ * @param instanceId - The unique identifier for the map instance
310
+ * @returns The current cursor
311
+ */
312
+ function getCursor(instanceId) {
313
+ return getSnapshot(instanceId);
314
+ }
315
+ /**
316
+ * Manually clear all cursor state for a specific instanceId.
317
+ * This is typically not needed as cleanup happens automatically when all subscribers unmount.
318
+ * Use this only in advanced scenarios where manual cleanup is required.
319
+ *
320
+ * @param instanceId - The unique identifier for the map cursor instance to clear
321
+ *
322
+ * @example
323
+ * ```tsx
324
+ * // Manual cleanup (rarely needed)
325
+ * clearCursorState('my-map-instance');
326
+ * ```
327
+ */
328
+ function clearCursorState(instanceId) {
329
+ const unsub = busUnsubscribers.get(instanceId);
330
+ if (unsub) {
331
+ unsub();
332
+ busUnsubscribers.delete(instanceId);
333
+ }
334
+ clearAllCaches(instanceId);
335
+ }
336
+ /** @deprecated Use module functions directly */
337
+ function getOrCreateStore(id) {
338
+ return { getSnapshot: () => getSnapshot(id) };
339
+ }
340
+ /** @deprecated Use clearCursorState instead */
341
+ function destroyStore(id) {
342
+ clearCursorState(id);
343
+ }
344
+ /** @deprecated Use direct function calls instead */
345
+ function getStore(id) {
346
+ return { getSnapshot: () => getSnapshot(id) };
347
+ }
348
+
349
+ //#endregion
350
+ export { clearCursorState, destroyStore, getCursor, getOrCreateClearCursor, getOrCreateRequestCursorChange, getOrCreateServerSnapshot, getOrCreateSnapshot, getOrCreateStore, getOrCreateSubscription, getStore };
351
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.js","names":["DEFAULT_CURSOR: CSSCursorType"],"sources":["../../src/map-cursor/store.ts"],"sourcesContent":["/*\n * Copyright 2025 Hypergiant Galactic Systems Inc. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport { Broadcast } from '@accelint/bus';\nimport { MapModeEvents } from '../map-mode/events';\nimport { getCurrentModeOwner } from '../map-mode/store';\nimport { MapCursorEvents } from './events';\nimport type { UniqueId } from '@accelint/core';\nimport type { ModeChangedEvent } from '../map-mode/types';\nimport type { CSSCursorType, MapCursorEventType } from './types';\n\n/**\n * Default cursor value\n */\nconst DEFAULT_CURSOR: CSSCursorType = 'default';\n\n/**\n * Typed event bus instance for map cursor events.\n * Provides type-safe event emission and listening for all map cursor state changes.\n */\nconst mapCursorBus = Broadcast.getInstance<MapCursorEventType>();\n\n/**\n * Type representing the state for a single map cursor instance\n */\ntype MapCursorState = {\n /** Map of owner-to-cursor mappings */\n cursorOwners: Map<string, CSSCursorType>;\n /** Current active cursor */\n currentCursor: CSSCursorType | null;\n /** Current cursor owner */\n currentOwner: string | null;\n};\n\n/**\n * Store for map cursor state keyed by instanceId\n */\nconst cursorStore = new Map<UniqueId, MapCursorState>();\n\n/**\n * Track React component subscribers per instanceId (for fan-out notifications).\n * Each Set contains onStoreChange callbacks from useSyncExternalStore.\n */\nconst componentSubscribers = new Map<UniqueId, Set<() => void>>();\n\n/**\n * Cache of bus unsubscribe functions (1 per instanceId).\n * This ensures we only have one bus listener per map cursor instance, regardless of\n * how many React components subscribe to it.\n */\nconst busUnsubscribers = new Map<UniqueId, () => void>();\n\ntype Subscription = (onStoreChange: () => void) => () => void;\n/**\n * Cache of subscription functions per instanceId to avoid recreating on every render\n */\nconst subscriptionCache = new Map<UniqueId, Subscription>();\n\n/**\n * Cache of snapshot functions per instanceId to maintain referential stability\n */\nconst snapshotCache = new Map<UniqueId, () => CSSCursorType>();\n\n/**\n * Cache of server snapshot functions per instanceId to maintain referential stability.\n * Server snapshots always return default cursor since cursor state is client-only.\n */\nconst serverSnapshotCache = new Map<UniqueId, () => CSSCursorType>();\n\n/**\n * Cache of requestCursorChange functions per instanceId to maintain referential stability\n */\nconst requestCursorChangeCache = new Map<\n UniqueId,\n (cursor: CSSCursorType, owner: string) => void\n>();\n\n/**\n * Cache of clearCursor functions per instanceId to maintain referential stability\n */\nconst clearCursorCache = new Map<UniqueId, (owner: string) => void>();\n\n/**\n * All state caches that need cleanup when an instance is removed\n */\nconst stateCaches = [\n cursorStore,\n componentSubscribers,\n subscriptionCache,\n snapshotCache,\n serverSnapshotCache,\n requestCursorChangeCache,\n clearCursorCache,\n] as const;\n\n/**\n * Clear all cached state for a given instanceId\n */\nfunction clearAllCaches(instanceId: UniqueId): void {\n stateCaches.map((cache) => cache.delete(instanceId));\n}\n\n/**\n * Get or create cursor state for a given instanceId\n */\nfunction getOrCreateState(instanceId: UniqueId): MapCursorState {\n if (!cursorStore.has(instanceId)) {\n cursorStore.set(instanceId, {\n cursorOwners: new Map(),\n currentCursor: null,\n currentOwner: null,\n });\n }\n // biome-ignore lint/style/noNonNullAssertion: State guaranteed to exist after has() check above\n return cursorStore.get(instanceId)!;\n}\n\n/**\n * Get current cursor snapshot for a given instance (for useSyncExternalStore)\n *\n * Priority order:\n * 1. Mode owner's cursor (if exists and mode is owned)\n * 2. Most recent cursor (only if currentOwner still has an entry in storage)\n * 3. Default cursor\n */\nfunction getSnapshot(instanceId: UniqueId): CSSCursorType {\n const state = cursorStore.get(instanceId);\n if (!state) {\n return DEFAULT_CURSOR;\n }\n\n // Priority 1: Mode owner's cursor\n const modeOwner = getCurrentModeOwner(instanceId);\n if (modeOwner) {\n const modeOwnerCursor = state.cursorOwners.get(modeOwner);\n if (modeOwnerCursor) {\n return modeOwnerCursor;\n }\n }\n\n // Priority 2: Most recent cursor (only if owner entry still exists)\n if (state.currentCursor && state.currentOwner) {\n if (state.cursorOwners.has(state.currentOwner)) {\n return state.currentCursor;\n }\n }\n\n // Priority 3: Default cursor\n return DEFAULT_CURSOR;\n}\n\n/**\n * Notify all React subscribers for a given instanceId\n */\nfunction notifySubscribers(instanceId: UniqueId): void {\n const subscribers = componentSubscribers.get(instanceId);\n if (subscribers) {\n for (const onStoreChange of subscribers) {\n onStoreChange();\n }\n }\n}\n\n/**\n * Handle cursor change request for a given instance\n */\nfunction handleCursorChangeRequest(\n instanceId: UniqueId,\n state: MapCursorState,\n cursor: CSSCursorType,\n requestOwner: string,\n): void {\n const previousCursor = getSnapshot(instanceId);\n\n // Skip if same cursor is already stored for this owner\n if (state.cursorOwners.get(requestOwner) === cursor) {\n return;\n }\n\n // Store cursor for this owner\n state.cursorOwners.set(requestOwner, cursor);\n\n // Check if requester is the mode owner or if mode is ownerless\n const modeOwner = getCurrentModeOwner(instanceId);\n const isOwnerlessMode = !modeOwner;\n const isRequestOwnerModeOwner = requestOwner === modeOwner;\n\n // Grant or reject based on ownership\n if (isOwnerlessMode || isRequestOwnerModeOwner) {\n // Accept: store and apply cursor\n state.currentCursor = cursor;\n state.currentOwner = requestOwner;\n\n const newCursor = getSnapshot(instanceId);\n\n // Emit changed event only if cursor actually changed\n if (previousCursor !== newCursor) {\n mapCursorBus.emit(MapCursorEvents.changed, {\n previousCursor,\n currentCursor: newCursor,\n owner: requestOwner,\n id: instanceId,\n });\n notifySubscribers(instanceId);\n }\n } else {\n // Reject: stored for future auto-apply, but emit rejection event\n mapCursorBus.emit(MapCursorEvents.rejected, {\n rejectedCursor: cursor,\n rejectedOwner: requestOwner,\n currentOwner: state.currentOwner || modeOwner || 'unknown',\n reason: 'not-owner',\n id: instanceId,\n });\n }\n}\n\n/**\n * Ensures a single bus listener exists for the given instanceId.\n * All React subscribers will be notified via fan-out when the bus events fire.\n * This prevents creating N bus listeners for N React components.\n *\n * @param instanceId - The unique identifier for the map cursor instance\n */\nfunction ensureBusListener(instanceId: UniqueId): void {\n if (busUnsubscribers.has(instanceId)) {\n return; // Already listening\n }\n\n const state = getOrCreateState(instanceId);\n\n // Listen for cursor change requests\n const unsubRequest = mapCursorBus.on(\n MapCursorEvents.changeRequest,\n (event) => {\n const { cursor, owner: requestOwner, id } = event.payload;\n if (id !== instanceId) {\n return;\n }\n handleCursorChangeRequest(instanceId, state, cursor, requestOwner);\n },\n );\n\n // Listen for mode changes\n const modeBus = Broadcast.getInstance<ModeChangedEvent>();\n const unsubModeChange = modeBus.on(MapModeEvents.changed, (event) => {\n if (event.payload.id !== instanceId) {\n return;\n }\n\n const previousCursor = getSnapshot(instanceId);\n\n // When mode changes, clear current cursor tracking (but keep owner's cursor in storage)\n // This allows the cursor to fall back to state-based default until a new owner sets it\n if (\n event.payload.currentMode === 'default' ||\n event.payload.previousMode !== event.payload.currentMode\n ) {\n state.currentCursor = null;\n state.currentOwner = null;\n notifySubscribers(instanceId);\n }\n\n // Defer cursor change check until after new mode owner is registered\n queueMicrotask(() => {\n const newCursor = getSnapshot(instanceId);\n if (previousCursor !== newCursor) {\n const newModeOwner = getCurrentModeOwner(instanceId) || 'system';\n mapCursorBus.emit(MapCursorEvents.changed, {\n previousCursor,\n currentCursor: newCursor,\n owner: newModeOwner,\n id: instanceId,\n });\n notifySubscribers(instanceId);\n }\n });\n });\n\n // Store composite cleanup function\n busUnsubscribers.set(instanceId, () => {\n unsubRequest();\n unsubModeChange();\n });\n}\n\n/**\n * Cleans up the bus listener if no React subscribers remain.\n *\n * @param instanceId - The unique identifier for the map cursor instance\n */\nfunction cleanupBusListenerIfNeeded(instanceId: UniqueId): void {\n const subscribers = componentSubscribers.get(instanceId);\n\n if (!subscribers || subscribers.size === 0) {\n // No more React subscribers - clean up bus listener\n const unsub = busUnsubscribers.get(instanceId);\n if (unsub) {\n unsub();\n busUnsubscribers.delete(instanceId);\n }\n\n // Clean up all state\n clearAllCaches(instanceId);\n }\n}\n\n/**\n * Creates or retrieves a cached subscription function for a given instanceId.\n * Uses a fan-out pattern: 1 bus listener -> N React subscribers.\n * Automatically cleans up cursor state when the last subscriber unsubscribes.\n *\n * @param instanceId - The unique identifier for the map cursor instance\n * @returns A subscription function for useSyncExternalStore\n */\nexport function getOrCreateSubscription(\n instanceId: UniqueId,\n): (onStoreChange: () => void) => () => void {\n const subscription =\n subscriptionCache.get(instanceId) ??\n ((onStoreChange: () => void) => {\n // Ensure state exists\n getOrCreateState(instanceId);\n\n // Ensure single bus listener exists for this instanceId\n ensureBusListener(instanceId);\n\n // Get or create the subscriber set for this map instance, then add this component's callback\n let subscriberSet = componentSubscribers.get(instanceId);\n if (!subscriberSet) {\n subscriberSet = new Set();\n componentSubscribers.set(instanceId, subscriberSet);\n }\n subscriberSet.add(onStoreChange);\n\n // Return cleanup function to remove this component's subscription\n return () => {\n const currentSubscriberSet = componentSubscribers.get(instanceId);\n if (currentSubscriberSet) {\n currentSubscriberSet.delete(onStoreChange);\n }\n\n // Clean up bus listener if this was the last React subscriber\n cleanupBusListenerIfNeeded(instanceId);\n };\n });\n\n subscriptionCache.set(instanceId, subscription);\n\n return subscription;\n}\n\n/**\n * Creates or retrieves a cached snapshot function for a given instanceId.\n * The cursor returned gets equality checked, so it needs to be stable or React re-renders unnecessarily.\n *\n * @param instanceId - The unique identifier for the map cursor instance\n * @returns A snapshot function for useSyncExternalStore\n */\nexport function getOrCreateSnapshot(instanceId: UniqueId): () => CSSCursorType {\n const snapshot =\n snapshotCache.get(instanceId) ?? (() => getSnapshot(instanceId));\n\n snapshotCache.set(instanceId, snapshot);\n\n return snapshot;\n}\n\n/**\n * Creates or retrieves a cached server snapshot function for a given instanceId.\n * Server snapshots always return the default cursor since cursor state is client-only.\n * Required for SSR/RSC compatibility with useSyncExternalStore.\n *\n * @param instanceId - The unique identifier for the map cursor instance\n * @returns A server snapshot function for useSyncExternalStore\n */\nexport function getOrCreateServerSnapshot(\n instanceId: UniqueId,\n): () => CSSCursorType {\n const serverSnapshot =\n serverSnapshotCache.get(instanceId) ?? (() => DEFAULT_CURSOR);\n\n serverSnapshotCache.set(instanceId, serverSnapshot);\n\n return serverSnapshot;\n}\n\n/**\n * Creates or retrieves a cached requestCursorChange function for a given instanceId.\n * This maintains referential stability for the function reference.\n *\n * @param instanceId - The unique identifier for the map cursor instance\n * @returns A requestCursorChange function for this instance\n */\nexport function getOrCreateRequestCursorChange(\n instanceId: UniqueId,\n): (cursor: CSSCursorType, owner: string) => void {\n const requestCursorChange =\n requestCursorChangeCache.get(instanceId) ??\n ((cursor: CSSCursorType, owner: string) => {\n const trimmedCursor = cursor.trim();\n const trimmedOwner = owner.trim();\n\n if (!trimmedCursor) {\n throw new Error('requestCursorChange requires non-empty cursor');\n }\n if (!trimmedOwner) {\n throw new Error('requestCursorChange requires non-empty owner');\n }\n\n mapCursorBus.emit(MapCursorEvents.changeRequest, {\n cursor: trimmedCursor as CSSCursorType,\n owner: trimmedOwner,\n id: instanceId,\n });\n });\n\n requestCursorChangeCache.set(instanceId, requestCursorChange);\n\n return requestCursorChange;\n}\n\n/**\n * Creates or retrieves a cached clearCursor function for a given instanceId.\n * This maintains referential stability for the function reference.\n *\n * @param instanceId - The unique identifier for the map cursor instance\n * @returns A clearCursor function for this instance\n */\nexport function getOrCreateClearCursor(\n instanceId: UniqueId,\n): (owner: string) => void {\n const clearCursor =\n clearCursorCache.get(instanceId) ??\n ((owner: string) => {\n const state = cursorStore.get(instanceId);\n if (!state) {\n return;\n }\n\n const hadCursor = state.cursorOwners.has(owner);\n state.cursorOwners.delete(owner);\n\n // If this was the current owner, clear current tracking\n if (state.currentOwner === owner) {\n state.currentCursor = null;\n state.currentOwner = null;\n }\n\n // Only notify if cursor changed\n if (hadCursor) {\n notifySubscribers(instanceId);\n }\n });\n\n clearCursorCache.set(instanceId, clearCursor);\n\n return clearCursor;\n}\n\n/**\n * Get the current cursor for a given instance (direct access, not reactive).\n * @internal - For internal map-toolkit use only\n *\n * @param instanceId - The unique identifier for the map instance\n * @returns The current cursor\n */\nexport function getCursor(instanceId: UniqueId): CSSCursorType {\n return getSnapshot(instanceId);\n}\n\n/**\n * Manually clear all cursor state for a specific instanceId.\n * This is typically not needed as cleanup happens automatically when all subscribers unmount.\n * Use this only in advanced scenarios where manual cleanup is required.\n *\n * @param instanceId - The unique identifier for the map cursor instance to clear\n *\n * @example\n * ```tsx\n * // Manual cleanup (rarely needed)\n * clearCursorState('my-map-instance');\n * ```\n */\nexport function clearCursorState(instanceId: UniqueId): void {\n // Unsubscribe from bus if listening\n const unsub = busUnsubscribers.get(instanceId);\n if (unsub) {\n unsub();\n busUnsubscribers.delete(instanceId);\n }\n\n // Clear all state\n clearAllCaches(instanceId);\n}\n\n// Legacy API compatibility (for tests and existing code)\n/** @deprecated Use module functions directly */\nexport function getOrCreateStore(id: UniqueId): {\n getSnapshot: () => CSSCursorType;\n} {\n return {\n getSnapshot: () => getSnapshot(id),\n };\n}\n\n/** @deprecated Use clearCursorState instead */\nexport function destroyStore(id: UniqueId): void {\n clearCursorState(id);\n}\n\n/** @deprecated Use direct function calls instead */\nexport function getStore(\n id: UniqueId,\n): { getSnapshot: () => CSSCursorType } | undefined {\n // Always return a store object since we don't track \"existence\" anymore\n return {\n getSnapshot: () => getSnapshot(id),\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAuBA,MAAMA,iBAAgC;;;;;AAMtC,MAAM,eAAe,UAAU,aAAiC;;;;AAiBhE,MAAM,8BAAc,IAAI,KAA+B;;;;;A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GAC/C,QAAQ;GACR,OAAO;GACP,IAAI;GACL,CAAC;;AAGN,0BAAyB,IAAI,YAAY,oBAAoB;AAE7D,QAAO;;;;;;;;;AAUT,SAAgB,uBACd,YACyB;CACzB,MAAM,cACJ,iBAAiB,IAAI,WAAW,MAC9B,UAAkB;EAClB,MAAM,QAAQ,YAAY,IAAI,WAAW;AACzC,MAAI,CAAC,MACH;EAGF,MAAM,YAAY,MAAM,aAAa,IAAI,MAAM;AAC/C,QAAM,aAAa,OAAO,MAAM;AAGhC,MAAI,MAAM,iBAAiB,OAAO;AAChC,SAAM,gBAAgB;AACtB,SAAM,eAAe;;AAIvB,MAAI,UACF,mBAAkB,WAAW;;AAInC,kBAAiB,IAAI,YAAY,YAAY;AAE7C,QAAO;;;;;;;;;AAUT,SAAgB,UAAU,YAAqC;AAC7D,QAAO,YAAY,WAAW;;;;;;;;;;;;;;;AAgBhC,SAAgB,iBAAiB,YAA4B;CAE3D,MAAM,QAAQ,iBAAiB,IAAI,WAAW;AAC9C,KAAI,OAAO;AACT,SAAO;AACP,mBAAiB,OAAO,WAAW;;AAIrC,gBAAe,WAAW;;;AAK5B,SAAgB,iBAAiB,IAE/B;AACA,QAAO,EACL,mBAAmB,YAAY,GAAG,EACnC;;;AAIH,SAAgB,aAAa,IAAoB;AAC/C,kBAAiB,GAAG;;;AAItB,SAAgB,SACd,IACkD;AAElD,QAAO,EACL,mBAAmB,YAAY,GAAG,EACnC"}
@@ -0,0 +1,81 @@
1
+ /*
2
+ * Copyright 2025 Hypergiant Galactic Systems Inc. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at https://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import { Payload } from "@accelint/bus";
14
+ import React from "react";
15
+ import { UniqueId } from "@accelint/core";
16
+
17
+ //#region src/map-cursor/types.d.ts
18
+ /**
19
+ * CSS cursor type from React's CSSProperties
20
+ * Includes all standard cursor keywords and custom url() syntax
21
+ * @see https://developer.mozilla.org/en-US/docs/Web/CSS/cursor
22
+ */
23
+ type CSSCursorType = NonNullable<React.CSSProperties['cursor']>;
24
+ /**
25
+ * Payload for cursor change request events
26
+ */
27
+ type CursorChangeRequestPayload = {
28
+ /** The desired cursor CSS string */
29
+ cursor: CSSCursorType;
30
+ /** The unique identifier of the requester */
31
+ owner: string;
32
+ /** The map instance ID this request targets */
33
+ id: UniqueId;
34
+ };
35
+ /**
36
+ * Payload for cursor changed events
37
+ */
38
+ type CursorChangedPayload = {
39
+ /** The previous cursor value */
40
+ previousCursor: CSSCursorType;
41
+ /** The new cursor value */
42
+ currentCursor: CSSCursorType;
43
+ /** The owner of the new cursor */
44
+ owner: string;
45
+ /** The map instance ID */
46
+ id: UniqueId;
47
+ };
48
+ /**
49
+ * Payload for cursor rejection events
50
+ */
51
+ type CursorRejectionPayload = {
52
+ /** The cursor that was rejected */
53
+ rejectedCursor: CSSCursorType;
54
+ /** The owner whose request was rejected */
55
+ rejectedOwner: string;
56
+ /** The current cursor owner */
57
+ currentOwner: string;
58
+ /** Reason for rejection */
59
+ reason: string;
60
+ /** The map instance ID */
61
+ id: UniqueId;
62
+ };
63
+ /**
64
+ * Typed event for cursor change request
65
+ */
66
+ type CursorChangeRequestEvent = Payload<'cursor:change-request', CursorChangeRequestPayload>;
67
+ /**
68
+ * Typed event for cursor changed
69
+ */
70
+ type CursorChangedEvent = Payload<'cursor:changed', CursorChangedPayload>;
71
+ /**
72
+ * Typed event for cursor rejected
73
+ */
74
+ type CursorRejectedEvent = Payload<'cursor:rejected', CursorRejectionPayload>;
75
+ /**
76
+ * Union of all cursor event payloads
77
+ */
78
+ type MapCursorEventType = CursorChangeRequestEvent | CursorChangedEvent | CursorRejectedEvent;
79
+ //#endregion
80
+ export { CSSCursorType, CursorChangeRequestEvent, CursorChangeRequestPayload, CursorChangedEvent, CursorChangedPayload, CursorRejectedEvent, CursorRejectionPayload, MapCursorEventType };
81
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,12 @@
1
+ /*
2
+ * Copyright 2025 Hypergiant Galactic Systems Inc. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at https://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
@@ -0,0 +1,99 @@
1
+ /*
2
+ * Copyright 2025 Hypergiant Galactic Systems Inc. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at https://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import { CSSCursorType } from "./types.js";
14
+ import { UniqueId } from "@accelint/core";
15
+
16
+ //#region src/map-cursor/use-map-cursor.d.ts
17
+ /**
18
+ * Return value for the useMapCursor hook
19
+ */
20
+ type UseMapCursorReturn = {
21
+ /** The current active cursor */
22
+ cursor: CSSCursorType;
23
+ /** Function to request a cursor change with ownership */
24
+ requestCursorChange: (cursor: CSSCursorType, requestOwner: string) => void;
25
+ /** Function to clear cursor for an owner */
26
+ clearCursor: (owner: string) => void;
27
+ };
28
+ /**
29
+ * Hook to access the map cursor state and actions.
30
+ *
31
+ * This hook uses `useSyncExternalStore` to subscribe to map cursor state changes,
32
+ * providing concurrent-safe cursor state updates. Uses a fan-out pattern where
33
+ * a single bus listener per map instance notifies N React component subscribers.
34
+ *
35
+ * **Owner-based Priority System:**
36
+ * - Mode owners (from MapModeStore) have highest priority
37
+ * - Non-owners can set cursors only in default/ownerless modes
38
+ * - Rejected requests emit events with rejection reasons
39
+ *
40
+ * @param id - Optional map instance ID. If not provided, will use the ID from `MapContext`.
41
+ * @returns The current cursor, requestCursorChange function, and clearCursor function
42
+ * @throws Error if no `id` is provided and hook is used outside of `MapProvider`
43
+ *
44
+ * @example
45
+ * ```tsx
46
+ * // Inside MapProvider (within BaseMap children)
47
+ * function CustomDeckLayer() {
48
+ * const { cursor, requestCursorChange, clearCursor } = useMapCursor();
49
+ *
50
+ * const handleHover = (info: PickingInfo) => {
51
+ * if (info.object) {
52
+ * requestCursorChange('pointer', 'custom-layer-id');
53
+ * } else {
54
+ * clearCursor('custom-layer-id');
55
+ * }
56
+ * };
57
+ *
58
+ * return <ScatterplotLayer onHover={handleHover} />;
59
+ * }
60
+ * ```
61
+ *
62
+ * @example
63
+ * ```tsx
64
+ * // Outside MapProvider - pass id directly
65
+ * function ExternalControl({ mapId }: { mapId: UniqueId }) {
66
+ * const { cursor, requestCursorChange } = useMapCursor(mapId);
67
+ *
68
+ * return (
69
+ * <button onClick={() => requestCursorChange('crosshair', 'external-control')}>
70
+ * Set Crosshair (current: {cursor})
71
+ * </button>
72
+ * );
73
+ * }
74
+ * ```
75
+ */
76
+ declare function useMapCursor(id?: UniqueId): UseMapCursorReturn;
77
+ /**
78
+ * Hook to automatically manage cursor for a component's lifecycle.
79
+ *
80
+ * This hook automatically requests a cursor when mounted and clears it when unmounted.
81
+ * Useful for components that need to consistently show a specific cursor.
82
+ *
83
+ * @param cursor - The cursor to request
84
+ * @param owner - The owner identifier
85
+ * @param id - Optional map instance ID
86
+ *
87
+ * @example
88
+ * ```tsx
89
+ * function DrawingMode() {
90
+ * useMapCursorEffect('crosshair', 'drawing-mode');
91
+ *
92
+ * return <div>Drawing mode active with crosshair cursor</div>;
93
+ * }
94
+ * ```
95
+ */
96
+ declare function useMapCursorEffect(cursor: CSSCursorType, owner: string, id?: UniqueId): void;
97
+ //#endregion
98
+ export { UseMapCursorReturn, useMapCursor, useMapCursorEffect };
99
+ //# sourceMappingURL=use-map-cursor.d.ts.map