@accelint/map-toolkit 0.6.0 → 1.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.
- package/CHANGELOG.md +81 -0
- package/catalog-info.yaml +7 -6
- package/dist/camera/events.js.map +1 -1
- package/dist/camera/index.d.ts +2 -2
- package/dist/camera/index.js +2 -2
- package/dist/camera/store.d.ts +120 -0
- package/dist/camera/store.js +279 -0
- package/dist/camera/store.js.map +1 -0
- package/dist/cursor-coordinates/index.d.ts +4 -2
- package/dist/cursor-coordinates/index.js +3 -2
- package/dist/cursor-coordinates/store.d.ts +48 -0
- package/dist/cursor-coordinates/store.js +92 -0
- package/dist/cursor-coordinates/store.js.map +1 -0
- package/dist/cursor-coordinates/types.d.ts +87 -0
- package/dist/cursor-coordinates/types.js +12 -0
- package/dist/cursor-coordinates/use-cursor-coordinates.d.ts +41 -37
- package/dist/cursor-coordinates/use-cursor-coordinates.js +131 -202
- package/dist/cursor-coordinates/use-cursor-coordinates.js.map +1 -1
- package/dist/deckgl/base-map/constants.d.ts +1 -6
- package/dist/deckgl/base-map/constants.js +1 -6
- package/dist/deckgl/base-map/constants.js.map +1 -1
- package/dist/deckgl/base-map/controls.js +2 -0
- package/dist/deckgl/base-map/controls.js.map +1 -1
- package/dist/deckgl/base-map/events.js.map +1 -1
- package/dist/deckgl/base-map/index.d.ts +2 -2
- package/dist/deckgl/base-map/index.js +10 -11
- package/dist/deckgl/base-map/index.js.map +1 -1
- package/dist/deckgl/base-map/provider.d.ts +2 -2
- package/dist/deckgl/base-map/provider.js +1 -1
- package/dist/deckgl/base-map/provider.js.map +1 -1
- package/dist/deckgl/index.d.ts +4 -4
- package/dist/deckgl/index.js +4 -4
- package/dist/deckgl/saved-viewports/index.js.map +1 -1
- package/dist/deckgl/saved-viewports/storage.js +10 -2
- package/dist/deckgl/saved-viewports/storage.js.map +1 -1
- package/dist/deckgl/shapes/display-shape-layer/constants.js +5 -8
- package/dist/deckgl/shapes/display-shape-layer/constants.js.map +1 -1
- package/dist/deckgl/shapes/display-shape-layer/fiber.js.map +1 -1
- package/dist/deckgl/shapes/display-shape-layer/index.d.ts +18 -14
- package/dist/deckgl/shapes/display-shape-layer/index.js +63 -30
- package/dist/deckgl/shapes/display-shape-layer/index.js.map +1 -1
- package/dist/deckgl/shapes/display-shape-layer/shape-label-layer.js +2 -16
- package/dist/deckgl/shapes/display-shape-layer/shape-label-layer.js.map +1 -1
- package/dist/deckgl/shapes/display-shape-layer/store.js +58 -272
- package/dist/deckgl/shapes/display-shape-layer/store.js.map +1 -1
- package/dist/deckgl/shapes/display-shape-layer/types.d.ts +22 -11
- package/dist/deckgl/shapes/display-shape-layer/{use-shape-selection.d.ts → use-select-shape.d.ts} +9 -9
- package/dist/deckgl/shapes/display-shape-layer/{use-shape-selection.js → use-select-shape.js} +12 -12
- package/dist/deckgl/shapes/display-shape-layer/use-select-shape.js.map +1 -0
- package/dist/deckgl/shapes/display-shape-layer/utils/display-style.js +5 -66
- package/dist/deckgl/shapes/display-shape-layer/utils/display-style.js.map +1 -1
- package/dist/deckgl/shapes/display-shape-layer/utils/labels.d.ts +2 -65
- package/dist/deckgl/shapes/display-shape-layer/utils/labels.js +3 -121
- package/dist/deckgl/shapes/display-shape-layer/utils/labels.js.map +1 -1
- package/dist/deckgl/shapes/draw-shape-layer/constants.js +46 -0
- package/dist/deckgl/shapes/draw-shape-layer/constants.js.map +1 -0
- package/dist/deckgl/shapes/draw-shape-layer/events.d.ts +92 -0
- package/dist/deckgl/shapes/draw-shape-layer/events.js +56 -0
- package/dist/deckgl/shapes/draw-shape-layer/events.js.map +1 -0
- package/dist/deckgl/shapes/draw-shape-layer/fiber.d.ts +11 -0
- package/dist/{maplibre/constants.js → deckgl/shapes/draw-shape-layer/fiber.js} +6 -12
- package/dist/deckgl/shapes/draw-shape-layer/fiber.js.map +1 -0
- package/dist/deckgl/shapes/draw-shape-layer/index.d.ts +53 -0
- package/dist/deckgl/shapes/draw-shape-layer/index.js +95 -0
- package/dist/deckgl/shapes/draw-shape-layer/index.js.map +1 -0
- package/dist/deckgl/shapes/draw-shape-layer/modes/draw-circle-mode-with-tooltip.js +51 -0
- package/dist/deckgl/shapes/draw-shape-layer/modes/draw-circle-mode-with-tooltip.js.map +1 -0
- package/dist/deckgl/shapes/draw-shape-layer/modes/draw-ellipse-mode-with-tooltip.js +73 -0
- package/dist/deckgl/shapes/draw-shape-layer/modes/draw-ellipse-mode-with-tooltip.js.map +1 -0
- package/dist/deckgl/shapes/draw-shape-layer/modes/draw-line-string-mode-with-tooltip.js +87 -0
- package/dist/deckgl/shapes/draw-shape-layer/modes/draw-line-string-mode-with-tooltip.js.map +1 -0
- package/dist/deckgl/shapes/draw-shape-layer/modes/draw-polygon-mode-with-tooltip.js +88 -0
- package/dist/deckgl/shapes/draw-shape-layer/modes/draw-polygon-mode-with-tooltip.js.map +1 -0
- package/dist/deckgl/shapes/draw-shape-layer/modes/draw-rectangle-mode-with-tooltip.js +77 -0
- package/dist/deckgl/shapes/draw-shape-layer/modes/draw-rectangle-mode-with-tooltip.js.map +1 -0
- package/dist/deckgl/shapes/draw-shape-layer/modes/index.js +64 -0
- package/dist/deckgl/shapes/draw-shape-layer/modes/index.js.map +1 -0
- package/dist/deckgl/shapes/draw-shape-layer/store.js +175 -0
- package/dist/deckgl/shapes/draw-shape-layer/store.js.map +1 -0
- package/dist/deckgl/shapes/draw-shape-layer/types.d.ts +86 -0
- package/dist/{viewport/constants.js → deckgl/shapes/draw-shape-layer/types.js} +1 -12
- package/dist/deckgl/shapes/draw-shape-layer/use-draw-shape.d.ts +82 -0
- package/dist/deckgl/shapes/draw-shape-layer/use-draw-shape.js +112 -0
- package/dist/deckgl/shapes/draw-shape-layer/use-draw-shape.js.map +1 -0
- package/dist/deckgl/shapes/draw-shape-layer/utils/feature-conversion.js +147 -0
- package/dist/deckgl/shapes/draw-shape-layer/utils/feature-conversion.js.map +1 -0
- package/dist/deckgl/shapes/edit-shape-layer/constants.js +41 -0
- package/dist/deckgl/shapes/edit-shape-layer/constants.js.map +1 -0
- package/dist/deckgl/shapes/edit-shape-layer/events.d.ts +92 -0
- package/dist/deckgl/shapes/edit-shape-layer/events.js +56 -0
- package/dist/deckgl/shapes/edit-shape-layer/events.js.map +1 -0
- package/dist/deckgl/shapes/edit-shape-layer/fiber.d.ts +13 -0
- package/dist/deckgl/shapes/edit-shape-layer/fiber.js +14 -0
- package/dist/deckgl/shapes/edit-shape-layer/index.d.ts +63 -0
- package/dist/deckgl/shapes/edit-shape-layer/index.js +162 -0
- package/dist/deckgl/shapes/edit-shape-layer/index.js.map +1 -0
- package/dist/deckgl/shapes/edit-shape-layer/modes/base-transform-mode.js +154 -0
- package/dist/deckgl/shapes/edit-shape-layer/modes/base-transform-mode.js.map +1 -0
- package/dist/deckgl/shapes/edit-shape-layer/modes/bounding-transform-mode.js +147 -0
- package/dist/deckgl/shapes/edit-shape-layer/modes/bounding-transform-mode.js.map +1 -0
- package/dist/deckgl/shapes/edit-shape-layer/modes/circle-transform-mode.js +87 -0
- package/dist/deckgl/shapes/edit-shape-layer/modes/circle-transform-mode.js.map +1 -0
- package/dist/deckgl/shapes/edit-shape-layer/modes/index.js +61 -0
- package/dist/deckgl/shapes/edit-shape-layer/modes/index.js.map +1 -0
- package/dist/deckgl/shapes/edit-shape-layer/modes/rotate-mode-with-snap.js +109 -0
- package/dist/deckgl/shapes/edit-shape-layer/modes/rotate-mode-with-snap.js.map +1 -0
- package/dist/deckgl/shapes/edit-shape-layer/modes/scale-mode-with-free-transform.js +289 -0
- package/dist/deckgl/shapes/edit-shape-layer/modes/scale-mode-with-free-transform.js.map +1 -0
- package/dist/deckgl/shapes/edit-shape-layer/modes/vertex-transform-mode.js +121 -0
- package/dist/deckgl/shapes/edit-shape-layer/modes/vertex-transform-mode.js.map +1 -0
- package/dist/deckgl/shapes/edit-shape-layer/store.js +194 -0
- package/dist/deckgl/shapes/edit-shape-layer/store.js.map +1 -0
- package/dist/deckgl/shapes/edit-shape-layer/types.d.ts +93 -0
- package/dist/deckgl/shapes/edit-shape-layer/types.js +14 -0
- package/dist/deckgl/shapes/edit-shape-layer/use-edit-shape.d.ts +82 -0
- package/dist/deckgl/shapes/edit-shape-layer/use-edit-shape.js +114 -0
- package/dist/deckgl/shapes/edit-shape-layer/use-edit-shape.js.map +1 -0
- package/dist/deckgl/shapes/index.d.ts +15 -6
- package/dist/deckgl/shapes/index.js +12 -5
- package/dist/deckgl/shapes/shared/constants.d.ts +27 -32
- package/dist/deckgl/shapes/shared/constants.js +189 -25
- package/dist/deckgl/shapes/shared/constants.js.map +1 -1
- package/dist/deckgl/shapes/shared/events.d.ts +1 -20
- package/dist/deckgl/shapes/shared/events.js +1 -31
- package/dist/deckgl/shapes/shared/events.js.map +1 -1
- package/dist/deckgl/shapes/shared/hooks/use-shift-zoom-disable.js +84 -0
- package/dist/deckgl/shapes/shared/hooks/use-shift-zoom-disable.js.map +1 -0
- package/dist/deckgl/shapes/shared/types.d.ts +187 -28
- package/dist/deckgl/shapes/shared/types.js +55 -1
- package/dist/deckgl/shapes/shared/types.js.map +1 -1
- package/dist/deckgl/shapes/shared/utils/geometry-measurements.js +128 -0
- package/dist/deckgl/shapes/shared/utils/geometry-measurements.js.map +1 -0
- package/dist/deckgl/shapes/shared/utils/layer-config.js +50 -0
- package/dist/deckgl/shapes/shared/utils/layer-config.js.map +1 -0
- package/dist/deckgl/shapes/shared/utils/mode-utils.js +113 -0
- package/dist/deckgl/shapes/shared/utils/mode-utils.js.map +1 -0
- package/dist/deckgl/shapes/shared/utils/pick-filtering.js +57 -0
- package/dist/deckgl/shapes/shared/utils/pick-filtering.js.map +1 -0
- package/dist/deckgl/shapes/shared/utils/style-utils.d.ts +64 -0
- package/dist/deckgl/shapes/shared/utils/style-utils.js +101 -0
- package/dist/deckgl/shapes/shared/utils/style-utils.js.map +1 -0
- package/dist/deckgl/symbol-layer/fiber.js.map +1 -1
- package/dist/deckgl/symbol-layer/index.js.map +1 -1
- package/dist/deckgl/text-layer/character-sets.js.map +1 -1
- package/dist/deckgl/text-layer/default-settings.js +4 -24
- package/dist/deckgl/text-layer/default-settings.js.map +1 -1
- package/dist/deckgl/text-layer/fiber.js.map +1 -1
- package/dist/deckgl/text-layer/index.js.map +1 -1
- package/dist/deckgl/text-settings.d.ts +77 -0
- package/dist/deckgl/text-settings.js +83 -0
- package/dist/deckgl/text-settings.js.map +1 -0
- package/dist/map-cursor/events.js.map +1 -1
- package/dist/map-cursor/index.d.ts +2 -2
- package/dist/map-cursor/index.js +2 -2
- package/dist/map-cursor/store.d.ts +32 -61
- package/dist/map-cursor/store.js +165 -294
- package/dist/map-cursor/store.js.map +1 -1
- package/dist/map-cursor/use-map-cursor.d.ts +5 -2
- package/dist/map-cursor/use-map-cursor.js +33 -15
- package/dist/map-cursor/use-map-cursor.js.map +1 -1
- package/dist/map-mode/events.js.map +1 -1
- package/dist/map-mode/index.d.ts +2 -2
- package/dist/map-mode/index.js +2 -2
- package/dist/map-mode/store.d.ts +36 -37
- package/dist/map-mode/store.js +131 -237
- package/dist/map-mode/store.js.map +1 -1
- package/dist/map-mode/use-map-mode.js +6 -5
- package/dist/map-mode/use-map-mode.js.map +1 -1
- package/dist/maplibre/hooks/use-maplibre.js.map +1 -1
- package/dist/maplibre/index.d.ts +2 -2
- package/dist/maplibre/index.js +2 -2
- package/dist/shared/constants.d.ts +19 -0
- package/dist/shared/constants.js +33 -0
- package/dist/shared/constants.js.map +1 -0
- package/dist/shared/create-map-store.d.ts +202 -0
- package/dist/shared/create-map-store.js +223 -0
- package/dist/shared/create-map-store.js.map +1 -0
- package/dist/shared/units.d.ts +39 -0
- package/dist/shared/units.js +49 -0
- package/dist/shared/units.js.map +1 -0
- package/dist/viewport/index.d.ts +3 -3
- package/dist/viewport/index.js +3 -3
- package/dist/viewport/store.d.ts +69 -0
- package/dist/viewport/store.js +125 -0
- package/dist/viewport/store.js.map +1 -0
- package/dist/viewport/types.d.ts +2 -2
- package/dist/viewport/utils.js +2 -2
- package/dist/viewport/utils.js.map +1 -1
- package/dist/viewport/viewport-size.d.ts +2 -2
- package/dist/viewport/viewport-size.js +2 -2
- package/dist/viewport/viewport-size.js.map +1 -1
- package/package.json +39 -19
- package/dist/camera/use-camera-state.d.ts +0 -153
- package/dist/camera/use-camera-state.js +0 -418
- package/dist/camera/use-camera-state.js.map +0 -1
- package/dist/deckgl/shapes/display-shape-layer/constants.d.ts +0 -44
- package/dist/deckgl/shapes/display-shape-layer/shape-label-layer.d.ts +0 -66
- package/dist/deckgl/shapes/display-shape-layer/store.d.ts +0 -87
- package/dist/deckgl/shapes/display-shape-layer/use-shape-selection.js.map +0 -1
- package/dist/deckgl/shapes/display-shape-layer/utils/display-style.d.ts +0 -61
- package/dist/maplibre/constants.d.ts +0 -13
- package/dist/maplibre/constants.js.map +0 -1
- package/dist/viewport/constants.d.ts +0 -11
- package/dist/viewport/constants.js.map +0 -1
- package/dist/viewport/use-viewport-state.d.ts +0 -100
- package/dist/viewport/use-viewport-state.js +0 -222
- package/dist/viewport/use-viewport-state.js.map +0 -1
package/dist/map-cursor/store.js
CHANGED
|
@@ -11,98 +11,72 @@
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
|
|
14
|
+
import { createMapStore, mapDelete, mapSet } from "../shared/create-map-store.js";
|
|
14
15
|
import { MapModeEvents } from "../map-mode/events.js";
|
|
15
|
-
import { getCurrentModeOwner } from "../map-mode/store.js";
|
|
16
16
|
import { MapCursorEvents } from "./events.js";
|
|
17
17
|
import { Broadcast } from "@accelint/bus";
|
|
18
18
|
|
|
19
19
|
//#region src/map-cursor/store.ts
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* Map Cursor Store
|
|
22
|
+
*
|
|
23
|
+
* Manages cursor state with ownership-based priority.
|
|
24
|
+
*
|
|
25
|
+
* Priority order:
|
|
26
|
+
* 1. Mode owner's cursor (if mode is owned)
|
|
27
|
+
* 2. Most recent cursor request
|
|
28
|
+
* 3. Default cursor
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```tsx
|
|
32
|
+
* import { cursorStore } from '@accelint/map-toolkit/map-cursor';
|
|
33
|
+
*
|
|
34
|
+
* function MapContainer({ mapId }) {
|
|
35
|
+
* const cursor = cursorStore.useSelector(mapId, (s) => getEffectiveCursor(mapId, s));
|
|
36
|
+
* return <div style={{ cursor }}>...</div>;
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* // Request cursor change from a layer:
|
|
40
|
+
* cursorStore.actions(mapId).requestCursorChange('crosshair', 'draw-layer');
|
|
41
|
+
* ```
|
|
22
42
|
*/
|
|
23
43
|
const DEFAULT_CURSOR = "default";
|
|
44
|
+
const cursorBus = Broadcast.getInstance();
|
|
45
|
+
const modeBus = Broadcast.getInstance();
|
|
24
46
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
*
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
|
47
|
+
* Lazy import to avoid circular dependency between cursor and mode stores.
|
|
48
|
+
* The mode store doesn't depend on cursor store, but importing it synchronously
|
|
49
|
+
* at module load time can cause initialization order issues.
|
|
50
|
+
*
|
|
51
|
+
* This pattern ensures the import is resolved before first use (bus listeners
|
|
52
|
+
* are set up on first subscriber, not at module load time).
|
|
53
|
+
*/
|
|
54
|
+
let getModeOwnerFn = null;
|
|
55
|
+
let isRegisteredModeOwnerFn = null;
|
|
56
|
+
let importPromise = null;
|
|
57
|
+
let importFailed = false;
|
|
58
|
+
function ensureModeStoreImported() {
|
|
59
|
+
if (getModeOwnerFn !== null || importFailed) return;
|
|
60
|
+
if (importPromise === null) importPromise = import("../map-mode/store.js").then((mod) => {
|
|
61
|
+
getModeOwnerFn = mod.getCurrentModeOwner;
|
|
62
|
+
isRegisteredModeOwnerFn = mod.isRegisteredModeOwner;
|
|
63
|
+
}).catch((error) => {
|
|
64
|
+
importFailed = true;
|
|
65
|
+
if (process.env.NODE_ENV !== "production") console.error("[MapCursor] Failed to import mode store:", error);
|
|
91
66
|
});
|
|
92
|
-
|
|
67
|
+
}
|
|
68
|
+
ensureModeStoreImported();
|
|
69
|
+
function getModeOwner(mapId) {
|
|
70
|
+
return getModeOwnerFn?.(mapId);
|
|
71
|
+
}
|
|
72
|
+
function isRegisteredOwner(mapId, owner) {
|
|
73
|
+
return isRegisteredModeOwnerFn?.(mapId, owner) ?? false;
|
|
93
74
|
}
|
|
94
75
|
/**
|
|
95
|
-
*
|
|
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
|
|
76
|
+
* Calculate effective cursor based on priority
|
|
101
77
|
*/
|
|
102
|
-
function
|
|
103
|
-
const
|
|
104
|
-
if (!state) return DEFAULT_CURSOR;
|
|
105
|
-
const modeOwner = getCurrentModeOwner(instanceId);
|
|
78
|
+
function getEffectiveCursor(mapId, state) {
|
|
79
|
+
const modeOwner = getModeOwner?.(mapId);
|
|
106
80
|
if (modeOwner) {
|
|
107
81
|
const modeOwnerCursor = state.cursorOwners.get(modeOwner);
|
|
108
82
|
if (modeOwnerCursor) return modeOwnerCursor;
|
|
@@ -113,239 +87,136 @@ function getSnapshot(instanceId) {
|
|
|
113
87
|
return DEFAULT_CURSOR;
|
|
114
88
|
}
|
|
115
89
|
/**
|
|
116
|
-
*
|
|
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
|
|
90
|
+
* Cursor store
|
|
124
91
|
*/
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
92
|
+
const cursorStore = createMapStore({
|
|
93
|
+
defaultState: {
|
|
94
|
+
cursorOwners: /* @__PURE__ */ new Map(),
|
|
95
|
+
currentCursor: null,
|
|
96
|
+
currentOwner: null
|
|
97
|
+
},
|
|
98
|
+
actions: (mapId, { get, set }) => ({
|
|
99
|
+
requestCursorChange: (cursor, owner) => {
|
|
100
|
+
const trimmedCursor = cursor.trim();
|
|
101
|
+
const trimmedOwner = owner.trim();
|
|
102
|
+
if (!trimmedCursor) throw new Error("requestCursorChange requires non-empty cursor");
|
|
103
|
+
if (!trimmedOwner) throw new Error("requestCursorChange requires non-empty owner");
|
|
104
|
+
cursorBus.emit(MapCursorEvents.changeRequest, {
|
|
105
|
+
cursor: trimmedCursor,
|
|
106
|
+
owner: trimmedOwner,
|
|
107
|
+
id: mapId
|
|
140
108
|
});
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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);
|
|
109
|
+
},
|
|
110
|
+
clearCursor: (owner) => {
|
|
111
|
+
const state = get();
|
|
112
|
+
if (state.cursorOwners.has(owner)) {
|
|
113
|
+
const updates = { cursorOwners: mapDelete(state.cursorOwners, owner) };
|
|
114
|
+
if (state.currentOwner === owner) {
|
|
115
|
+
updates.currentCursor = null;
|
|
116
|
+
updates.currentOwner = null;
|
|
117
|
+
}
|
|
118
|
+
set(updates);
|
|
119
|
+
}
|
|
173
120
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
121
|
+
}),
|
|
122
|
+
bus: (mapId, { get, set }) => {
|
|
123
|
+
const unsubRequest = cursorBus.on(MapCursorEvents.changeRequest, (event) => {
|
|
124
|
+
const { cursor, owner: requestOwner, id } = event.payload;
|
|
125
|
+
if (id !== mapId) return;
|
|
126
|
+
const state = get();
|
|
127
|
+
const previousCursor = getEffectiveCursor(mapId, state);
|
|
128
|
+
if (state.cursorOwners.get(requestOwner) === cursor) return;
|
|
129
|
+
const newCursorOwners = mapSet(state.cursorOwners, requestOwner, cursor);
|
|
130
|
+
const currentModeOwner = getModeOwner?.(mapId);
|
|
131
|
+
const isOwnerless = !currentModeOwner;
|
|
132
|
+
const isCurrentModeOwner = requestOwner === currentModeOwner;
|
|
133
|
+
const isAnyModeOwner = isRegisteredOwner(mapId, requestOwner);
|
|
134
|
+
if (isOwnerless || isCurrentModeOwner) {
|
|
135
|
+
const newState = {
|
|
136
|
+
cursorOwners: newCursorOwners,
|
|
137
|
+
currentCursor: cursor,
|
|
138
|
+
currentOwner: requestOwner
|
|
139
|
+
};
|
|
140
|
+
const newCursor = getEffectiveCursor(mapId, newState);
|
|
141
|
+
if (previousCursor !== newCursor) {
|
|
142
|
+
set(newState);
|
|
143
|
+
cursorBus.emit(MapCursorEvents.changed, {
|
|
144
|
+
previousCursor,
|
|
145
|
+
currentCursor: newCursor,
|
|
146
|
+
owner: requestOwner,
|
|
147
|
+
id: mapId
|
|
148
|
+
});
|
|
149
|
+
} else set(newState);
|
|
150
|
+
} else if (isAnyModeOwner) {
|
|
151
|
+
set({ cursorOwners: newCursorOwners });
|
|
152
|
+
cursorBus.emit(MapCursorEvents.rejected, {
|
|
153
|
+
rejectedCursor: cursor,
|
|
154
|
+
rejectedOwner: requestOwner,
|
|
155
|
+
currentOwner: state.currentOwner || currentModeOwner || "unknown",
|
|
156
|
+
reason: "not-current-owner",
|
|
157
|
+
id: mapId
|
|
183
158
|
});
|
|
184
|
-
|
|
185
|
-
|
|
159
|
+
} else cursorBus.emit(MapCursorEvents.rejected, {
|
|
160
|
+
rejectedCursor: cursor,
|
|
161
|
+
rejectedOwner: requestOwner,
|
|
162
|
+
currentOwner: state.currentOwner || currentModeOwner || "unknown",
|
|
163
|
+
reason: "not-owner",
|
|
164
|
+
id: mapId
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
const unsubMode = modeBus.on(MapModeEvents.changed, (event) => {
|
|
168
|
+
if (event.payload.id !== mapId) return;
|
|
169
|
+
const previousCursor = getEffectiveCursor(mapId, get());
|
|
170
|
+
if (event.payload.currentMode === "default" || event.payload.previousMode !== event.payload.currentMode) set({
|
|
171
|
+
currentCursor: null,
|
|
172
|
+
currentOwner: null
|
|
173
|
+
});
|
|
174
|
+
queueMicrotask(() => {
|
|
175
|
+
const newCursor = getEffectiveCursor(mapId, get());
|
|
176
|
+
if (previousCursor !== newCursor) {
|
|
177
|
+
const newModeOwner = getModeOwner?.(mapId) || "system";
|
|
178
|
+
cursorBus.emit(MapCursorEvents.changed, {
|
|
179
|
+
previousCursor,
|
|
180
|
+
currentCursor: newCursor,
|
|
181
|
+
owner: newModeOwner,
|
|
182
|
+
id: mapId
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
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
187
|
return () => {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
cleanupBusListenerIfNeeded(instanceId);
|
|
188
|
+
unsubRequest();
|
|
189
|
+
unsubMode();
|
|
231
190
|
};
|
|
232
|
-
}
|
|
233
|
-
|
|
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
|
-
}
|
|
191
|
+
}
|
|
192
|
+
});
|
|
248
193
|
/**
|
|
249
|
-
*
|
|
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
|
|
194
|
+
* Get effective cursor (computed from state)
|
|
255
195
|
*/
|
|
256
|
-
function
|
|
257
|
-
|
|
258
|
-
serverSnapshotCache.set(instanceId, serverSnapshot);
|
|
259
|
-
return serverSnapshot;
|
|
196
|
+
function getCursor(mapId) {
|
|
197
|
+
return getEffectiveCursor(mapId, cursorStore.get(mapId));
|
|
260
198
|
}
|
|
261
199
|
/**
|
|
262
|
-
*
|
|
263
|
-
* This maintains referential stability for the function reference.
|
|
200
|
+
* Hook for effective cursor value.
|
|
264
201
|
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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.
|
|
202
|
+
* **Internal use only** - not exported from the public API.
|
|
203
|
+
* Use `useMapCursor` instead, which provides:
|
|
204
|
+
* - MapContext integration (auto-resolves mapId inside MapProvider)
|
|
205
|
+
* - Actions (requestCursorChange, clearCursor)
|
|
206
|
+
* - Better ergonomics for consumers
|
|
286
207
|
*
|
|
287
|
-
*
|
|
288
|
-
* @returns A clearCursor function for this instance
|
|
208
|
+
* This hook exists for internal composition (used by useMapCursor).
|
|
289
209
|
*/
|
|
290
|
-
function
|
|
291
|
-
|
|
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;
|
|
210
|
+
function useCursor(mapId) {
|
|
211
|
+
return cursorStore.useSelector(mapId, (state) => getEffectiveCursor(mapId, state));
|
|
304
212
|
}
|
|
305
213
|
/**
|
|
306
|
-
*
|
|
307
|
-
* @internal - For internal map-toolkit use only
|
|
308
|
-
*
|
|
309
|
-
* @param instanceId - The unique identifier for the map instance
|
|
310
|
-
* @returns The current cursor
|
|
214
|
+
* Clear cursor state
|
|
311
215
|
*/
|
|
312
|
-
function
|
|
313
|
-
|
|
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) };
|
|
216
|
+
function clearCursorState(mapId) {
|
|
217
|
+
cursorStore.clear(mapId);
|
|
347
218
|
}
|
|
348
219
|
|
|
349
220
|
//#endregion
|
|
350
|
-
export { clearCursorState,
|
|
221
|
+
export { clearCursorState, cursorStore, getCursor, useCursor };
|
|
351
222
|
//# sourceMappingURL=store.js.map
|
|
@@ -1 +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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,MAAM,MAAM;AAEjC,MAAI,CAAC,cACH,OAAM,IAAI,MAAM,gDAAgD;AAElE,MAAI,CAAC,aACH,OAAM,IAAI,MAAM,+CAA+C;AAGjE,eAAa,KAAK,gBAAgB,eAAe;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"}
|
|
1
|
+
{"version":3,"file":"store.js","names":["DEFAULT_CURSOR: CSSCursorType","getModeOwnerFn: ((mapId: UniqueId) => string | undefined) | null","isRegisteredModeOwnerFn:\n | ((mapId: UniqueId, owner: string) => boolean)\n | null","importPromise: Promise<void> | null","updates: Partial<CursorState>","newState: CursorState"],"sources":["../../src/map-cursor/store.ts"],"sourcesContent":["/*\n * Copyright 2026 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\n/**\n * Map Cursor Store\n *\n * Manages cursor state with ownership-based priority.\n *\n * Priority order:\n * 1. Mode owner's cursor (if mode is owned)\n * 2. Most recent cursor request\n * 3. Default cursor\n *\n * @example\n * ```tsx\n * import { cursorStore } from '@accelint/map-toolkit/map-cursor';\n *\n * function MapContainer({ mapId }) {\n * const cursor = cursorStore.useSelector(mapId, (s) => getEffectiveCursor(mapId, s));\n * return <div style={{ cursor }}>...</div>;\n * }\n *\n * // Request cursor change from a layer:\n * cursorStore.actions(mapId).requestCursorChange('crosshair', 'draw-layer');\n * ```\n */\n\nimport { Broadcast } from '@accelint/bus';\nimport { MapModeEvents } from '../map-mode/events';\nimport { createMapStore, mapDelete, mapSet } from '../shared/create-map-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\nconst DEFAULT_CURSOR: CSSCursorType = 'default';\n\n/**\n * State shape for map cursor\n */\ntype CursorState = {\n /** Map of owner -> cursor */\n cursorOwners: Map<string, CSSCursorType>;\n /** Current active cursor (for priority tracking) */\n currentCursor: CSSCursorType | null;\n /** Current cursor owner (for priority tracking) */\n currentOwner: string | null;\n};\n\n/**\n * Actions for cursor management\n */\ntype CursorActions = {\n /** Request a cursor change */\n requestCursorChange: (cursor: CSSCursorType, owner: string) => void;\n /** Clear cursor for an owner */\n clearCursor: (owner: string) => void;\n};\n\nconst cursorBus = Broadcast.getInstance<MapCursorEventType>();\nconst modeBus = Broadcast.getInstance<ModeChangedEvent>();\n\n/**\n * Lazy import to avoid circular dependency between cursor and mode stores.\n * The mode store doesn't depend on cursor store, but importing it synchronously\n * at module load time can cause initialization order issues.\n *\n * This pattern ensures the import is resolved before first use (bus listeners\n * are set up on first subscriber, not at module load time).\n */\nlet getModeOwnerFn: ((mapId: UniqueId) => string | undefined) | null = null;\nlet isRegisteredModeOwnerFn:\n | ((mapId: UniqueId, owner: string) => boolean)\n | null = null;\nlet importPromise: Promise<void> | null = null;\nlet importFailed = false;\n\nfunction ensureModeStoreImported(): void {\n if (getModeOwnerFn !== null || importFailed) {\n return;\n }\n if (importPromise === null) {\n importPromise = import('../map-mode/store')\n .then((mod) => {\n getModeOwnerFn = mod.getCurrentModeOwner;\n isRegisteredModeOwnerFn = mod.isRegisteredModeOwner;\n })\n .catch((error) => {\n importFailed = true;\n // Log error in development only - in production this is a silent fallback\n if (process.env.NODE_ENV !== 'production') {\n console.error('[MapCursor] Failed to import mode store:', error);\n }\n });\n }\n}\n\n// Start the import immediately so it's likely resolved by first use\nensureModeStoreImported();\n\nfunction getModeOwner(mapId: UniqueId): string | undefined {\n // getModeOwnerFn will be available by the time bus listeners are set up\n // (which happens on first React subscriber, not at module load)\n return getModeOwnerFn?.(mapId);\n}\n\nfunction isRegisteredOwner(mapId: UniqueId, owner: string): boolean {\n return isRegisteredModeOwnerFn?.(mapId, owner) ?? false;\n}\n\n/**\n * Calculate effective cursor based on priority\n */\nfunction getEffectiveCursor(\n mapId: UniqueId,\n state: CursorState,\n): CSSCursorType {\n // Priority 1: Mode owner's cursor\n const modeOwner = getModeOwner?.(mapId);\n if (modeOwner) {\n const modeOwnerCursor = state.cursorOwners.get(modeOwner);\n if (modeOwnerCursor) {\n return modeOwnerCursor;\n }\n }\n\n // Priority 2: Current cursor (if owner still has entry)\n if (state.currentCursor && state.currentOwner) {\n if (state.cursorOwners.has(state.currentOwner)) {\n return state.currentCursor;\n }\n }\n\n // Priority 3: Default\n return DEFAULT_CURSOR;\n}\n\n/**\n * Cursor store\n */\nexport const cursorStore = createMapStore<CursorState, CursorActions>({\n defaultState: {\n cursorOwners: new Map(),\n currentCursor: null,\n currentOwner: null,\n },\n\n actions: (mapId, { get, set }) => ({\n requestCursorChange: (cursor: CSSCursorType, owner: string) => {\n const trimmedCursor = cursor.trim() as CSSCursorType;\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 cursorBus.emit(MapCursorEvents.changeRequest, {\n cursor: trimmedCursor,\n owner: trimmedOwner,\n id: mapId,\n });\n },\n\n clearCursor: (owner: string) => {\n const state = get();\n const hadCursor = state.cursorOwners.has(owner);\n\n if (hadCursor) {\n // Clear current tracking if this was the owner\n const updates: Partial<CursorState> = {\n cursorOwners: mapDelete(state.cursorOwners, owner),\n };\n if (state.currentOwner === owner) {\n updates.currentCursor = null;\n updates.currentOwner = null;\n }\n\n set(updates);\n }\n },\n }),\n\n bus: (mapId, { get, set }) => {\n // Handle cursor change requests\n const unsubRequest = cursorBus.on(\n MapCursorEvents.changeRequest,\n (event) => {\n const { cursor, owner: requestOwner, id } = event.payload;\n if (id !== mapId) {\n return;\n }\n\n const state = get();\n const previousCursor = getEffectiveCursor(mapId, state);\n\n // Skip if same cursor already stored for this owner\n if (state.cursorOwners.get(requestOwner) === cursor) {\n return;\n }\n\n // Create new Map for immutable update\n const newCursorOwners = mapSet(\n state.cursorOwners,\n requestOwner,\n cursor,\n );\n\n // Check ownership\n const currentModeOwner = getModeOwner?.(mapId);\n const isOwnerless = !currentModeOwner;\n const isCurrentModeOwner = requestOwner === currentModeOwner;\n const isAnyModeOwner = isRegisteredOwner(mapId, requestOwner);\n\n if (isOwnerless || isCurrentModeOwner) {\n // Accept: apply cursor with immutable update\n const newState: CursorState = {\n cursorOwners: newCursorOwners,\n currentCursor: cursor,\n currentOwner: requestOwner,\n };\n\n // Calculate new cursor with updated state\n const newCursor = getEffectiveCursor(mapId, newState);\n\n if (previousCursor !== newCursor) {\n set(newState);\n cursorBus.emit(MapCursorEvents.changed, {\n previousCursor,\n currentCursor: newCursor,\n owner: requestOwner,\n id: mapId,\n });\n } else {\n // Still need to update state even if cursor didn't change visually\n set(newState);\n }\n } else if (isAnyModeOwner) {\n // Store but don't apply: requester owns a different mode (pending or not current).\n // When their mode becomes active, getEffectiveCursor will find their cursor.\n set({ cursorOwners: newCursorOwners });\n\n cursorBus.emit(MapCursorEvents.rejected, {\n rejectedCursor: cursor,\n rejectedOwner: requestOwner,\n currentOwner: state.currentOwner || currentModeOwner || 'unknown',\n reason: 'not-current-owner',\n id: mapId,\n });\n } else {\n // Reject: don't store. Non-owners should only set cursor in default mode.\n cursorBus.emit(MapCursorEvents.rejected, {\n rejectedCursor: cursor,\n rejectedOwner: requestOwner,\n currentOwner: state.currentOwner || currentModeOwner || 'unknown',\n reason: 'not-owner',\n id: mapId,\n });\n }\n },\n );\n\n // Handle mode changes\n const unsubMode = modeBus.on(MapModeEvents.changed, (event) => {\n if (event.payload.id !== mapId) {\n return;\n }\n\n const state = get();\n const previousCursor = getEffectiveCursor(mapId, state);\n\n // Clear current tracking on mode change with immutable update\n if (\n event.payload.currentMode === 'default' ||\n event.payload.previousMode !== event.payload.currentMode\n ) {\n set({\n currentCursor: null,\n currentOwner: null,\n });\n }\n\n // Defer check until new mode owner is registered\n queueMicrotask(() => {\n // Re-get state after microtask since it may have changed\n const currentState = get();\n const newCursor = getEffectiveCursor(mapId, currentState);\n if (previousCursor !== newCursor) {\n const newModeOwner = getModeOwner?.(mapId) || 'system';\n cursorBus.emit(MapCursorEvents.changed, {\n previousCursor,\n currentCursor: newCursor,\n owner: newModeOwner,\n id: mapId,\n });\n }\n });\n });\n\n return () => {\n unsubRequest();\n unsubMode();\n };\n },\n});\n\n// =============================================================================\n// Convenience exports\n// =============================================================================\n\n/**\n * Get effective cursor (computed from state)\n */\nexport function getCursor(mapId: UniqueId): CSSCursorType {\n return getEffectiveCursor(mapId, cursorStore.get(mapId));\n}\n\n/**\n * Hook for effective cursor value.\n *\n * **Internal use only** - not exported from the public API.\n * Use `useMapCursor` instead, which provides:\n * - MapContext integration (auto-resolves mapId inside MapProvider)\n * - Actions (requestCursorChange, clearCursor)\n * - Better ergonomics for consumers\n *\n * This hook exists for internal composition (used by useMapCursor).\n */\nexport function useCursor(mapId: UniqueId): CSSCursorType {\n return cursorStore.useSelector(mapId, (state) =>\n getEffectiveCursor(mapId, state),\n );\n}\n\n/**\n * Clear cursor state\n */\nexport function clearCursorState(mapId: UniqueId): void {\n cursorStore.clear(mapId);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CA,MAAMA,iBAAgC;AAwBtC,MAAM,YAAY,UAAU,aAAiC;AAC7D,MAAM,UAAU,UAAU,aAA+B;;;;;;;;;AAUzD,IAAIC,iBAAmE;AACvE,IAAIC,0BAEO;AACX,IAAIC,gBAAsC;AAC1C,IAAI,eAAe;AAEnB,SAAS,0BAAgC;AACvC,KAAI,mBAAmB,QAAQ,aAC7B;AAEF,KAAI,kBAAkB,KACpB,iBAAgB,OAAO,wBACpB,MAAM,QAAQ;AACb,mBAAiB,IAAI;AACrB,4BAA0B,IAAI;GAC9B,CACD,OAAO,UAAU;AAChB,iBAAe;AAEf,MAAI,QAAQ,IAAI,aAAa,aAC3B,SAAQ,MAAM,4CAA4C,MAAM;GAElE;;AAKR,yBAAyB;AAEzB,SAAS,aAAa,OAAqC;AAGzD,QAAO,iBAAiB,MAAM;;AAGhC,SAAS,kBAAkB,OAAiB,OAAwB;AAClE,QAAO,0BAA0B,OAAO,MAAM,IAAI;;;;;AAMpD,SAAS,mBACP,OACA,OACe;CAEf,MAAM,YAAY,eAAe,MAAM;AACvC,KAAI,WAAW;EACb,MAAM,kBAAkB,MAAM,aAAa,IAAI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;AAc1D,SAAgB,UAAU,OAAgC;AACxD,QAAO,YAAY,YAAY,QAAQ,UACrC,mBAAmB,OAAO,MAAM,CACjC;;;;;AAMH,SAAgB,iBAAiB,OAAuB;AACtD,aAAY,MAAM,MAAM"}
|
|
@@ -80,7 +80,10 @@ declare function useMapCursor(id?: UniqueId): UseMapCursorReturn;
|
|
|
80
80
|
* This hook automatically requests a cursor when mounted and clears it when unmounted.
|
|
81
81
|
* Useful for components that need to consistently show a specific cursor.
|
|
82
82
|
*
|
|
83
|
-
*
|
|
83
|
+
* NOTE: This hook does NOT re-render when cursor state changes. If you need to read
|
|
84
|
+
* the current cursor value, use `useMapCursor` instead.
|
|
85
|
+
*
|
|
86
|
+
* @param cursorType - The cursor to request
|
|
84
87
|
* @param owner - The owner identifier
|
|
85
88
|
* @param id - Optional map instance ID
|
|
86
89
|
*
|
|
@@ -93,7 +96,7 @@ declare function useMapCursor(id?: UniqueId): UseMapCursorReturn;
|
|
|
93
96
|
* }
|
|
94
97
|
* ```
|
|
95
98
|
*/
|
|
96
|
-
declare function useMapCursorEffect(
|
|
99
|
+
declare function useMapCursorEffect(cursorType: CSSCursorType, owner: string, id?: UniqueId): void;
|
|
97
100
|
//#endregion
|
|
98
101
|
export { UseMapCursorReturn, useMapCursor, useMapCursorEffect };
|
|
99
102
|
//# sourceMappingURL=use-map-cursor.d.ts.map
|