@mapfirst.ai/react 0.0.45 → 0.0.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -238,8 +238,8 @@ type SmartFilter = {
238
238
  * ```
239
239
  */
240
240
  declare function useMapFirst(options: BaseMapFirstOptions): {
241
- instance: MapFirstCore | null;
242
- state: MapState | null;
241
+ instance: MapFirstCore;
242
+ state: MapState;
243
243
  setPrimaryType: (type: PropertyType) => void;
244
244
  setSelectedMarker: (id: number | null) => void;
245
245
  setUseApi: (useApi: boolean, autoLoad?: boolean) => void;
package/dist/index.d.ts CHANGED
@@ -238,8 +238,8 @@ type SmartFilter = {
238
238
  * ```
239
239
  */
240
240
  declare function useMapFirst(options: BaseMapFirstOptions): {
241
- instance: MapFirstCore | null;
242
- state: MapState | null;
241
+ instance: MapFirstCore;
242
+ state: MapState;
243
243
  setPrimaryType: (type: PropertyType) => void;
244
244
  setSelectedMarker: (id: number | null) => void;
245
245
  setUseApi: (useApi: boolean, autoLoad?: boolean) => void;
package/dist/index.js CHANGED
@@ -1376,9 +1376,6 @@ var SmartFilter = ({
1376
1376
  };
1377
1377
 
1378
1378
  // src/index.tsx
1379
- function updateStateField(setState, key, value) {
1380
- setState((prev) => prev ? { ...prev, [key]: value } : null);
1381
- }
1382
1379
  function forwardCallback(optionsRef, key, ...args) {
1383
1380
  var _a;
1384
1381
  const callback = (_a = optionsRef.current.callbacks) == null ? void 0 : _a[key];
@@ -1392,82 +1389,96 @@ function attachMapOnce(instanceRef, attachedRef, map, config) {
1392
1389
  attachedRef.current = true;
1393
1390
  }
1394
1391
  function useMapFirst(options) {
1395
- const instanceRef = import_react9.default.useRef(null);
1396
- const [state, setState] = import_react9.default.useState(null);
1397
1392
  const optionsRef = import_react9.default.useRef(options);
1398
1393
  import_react9.default.useEffect(() => {
1399
1394
  optionsRef.current = options;
1400
1395
  });
1401
- import_react9.default.useEffect(() => {
1402
- const opts = optionsRef.current;
1403
- const coreOptions = {
1396
+ const callbacksRef = import_react9.default.useRef({});
1397
+ const instanceRef = import_react9.default.useRef(null);
1398
+ if (instanceRef.current === null) {
1399
+ instanceRef.current = new import_core.MapFirstCore({
1404
1400
  adapter: null,
1405
- // Will be set when attachMap is called
1406
- ...opts,
1407
- callbacks: {
1408
- ...opts.callbacks,
1409
- // Add internal callbacks to trigger React re-renders
1410
- onPropertiesChange: (properties) => {
1411
- updateStateField(setState, "properties", properties);
1412
- forwardCallback(optionsRef, "onPropertiesChange", properties);
1413
- },
1414
- onSelectedPropertyChange: (id) => {
1415
- updateStateField(setState, "selectedPropertyId", id);
1416
- forwardCallback(optionsRef, "onSelectedPropertyChange", id);
1417
- },
1418
- onPrimaryTypeChange: (type) => {
1419
- updateStateField(setState, "primary", type);
1420
- forwardCallback(optionsRef, "onPrimaryTypeChange", type);
1421
- },
1422
- onFiltersChange: (filters) => {
1423
- updateStateField(setState, "filters", filters);
1424
- forwardCallback(optionsRef, "onFiltersChange", filters);
1425
- },
1426
- onBoundsChange: (bounds) => {
1427
- updateStateField(setState, "bounds", bounds);
1428
- forwardCallback(optionsRef, "onBoundsChange", bounds);
1429
- },
1430
- onPendingBoundsChange: (pendingBounds) => {
1431
- updateStateField(setState, "pendingBounds", pendingBounds);
1432
- forwardCallback(optionsRef, "onPendingBoundsChange", pendingBounds);
1433
- },
1434
- onCenterChange: (center, zoom) => {
1435
- setState((prev) => prev ? { ...prev, center, zoom } : null);
1436
- forwardCallback(optionsRef, "onCenterChange", center, zoom);
1437
- },
1438
- onZoomChange: (zoom) => {
1439
- updateStateField(setState, "zoom", zoom);
1440
- forwardCallback(optionsRef, "onZoomChange", zoom);
1441
- },
1442
- onActiveLocationChange: (location) => {
1443
- updateStateField(setState, "activeLocation", location);
1444
- forwardCallback(optionsRef, "onActiveLocationChange", location);
1445
- },
1446
- onLoadingStateChange: (loading) => {
1447
- updateStateField(setState, "initialLoading", loading);
1448
- forwardCallback(optionsRef, "onLoadingStateChange", loading);
1449
- },
1450
- onSearchingStateChange: (searching) => {
1451
- updateStateField(setState, "isSearching", searching);
1452
- forwardCallback(optionsRef, "onSearchingStateChange", searching);
1453
- },
1454
- onFirstCallDoneChange: (firstCallDone) => {
1455
- updateStateField(setState, "firstCallDone", firstCallDone);
1456
- forwardCallback(optionsRef, "onFirstCallDoneChange", firstCallDone);
1457
- },
1458
- onIsFlyToAnimatingChange: (animating) => {
1459
- updateStateField(setState, "isFlyToAnimating", animating);
1460
- forwardCallback(optionsRef, "onIsFlyToAnimatingChange", animating);
1461
- }
1462
- }
1401
+ ...optionsRef.current,
1402
+ callbacks: callbacksRef.current
1403
+ });
1404
+ }
1405
+ const [state, setState] = import_react9.default.useState(
1406
+ () => instanceRef.current.getState()
1407
+ );
1408
+ import_react9.default.useEffect(() => {
1409
+ if (!instanceRef.current) {
1410
+ callbacksRef.current = {};
1411
+ instanceRef.current = new import_core.MapFirstCore({
1412
+ adapter: null,
1413
+ ...optionsRef.current,
1414
+ callbacks: callbacksRef.current
1415
+ });
1416
+ setState(instanceRef.current.getState());
1417
+ mapLibreAttachedRef.current = false;
1418
+ googleMapsAttachedRef.current = false;
1419
+ mapboxAttachedRef.current = false;
1420
+ }
1421
+ const cb = callbacksRef.current;
1422
+ cb.onPropertiesChange = (properties) => {
1423
+ setState((prev) => ({ ...prev, properties }));
1424
+ forwardCallback(optionsRef, "onPropertiesChange", properties);
1425
+ };
1426
+ cb.onSelectedPropertyChange = (id) => {
1427
+ setState((prev) => ({ ...prev, selectedPropertyId: id }));
1428
+ forwardCallback(optionsRef, "onSelectedPropertyChange", id);
1429
+ };
1430
+ cb.onPrimaryTypeChange = (type) => {
1431
+ setState((prev) => ({ ...prev, primary: type }));
1432
+ forwardCallback(optionsRef, "onPrimaryTypeChange", type);
1433
+ };
1434
+ cb.onFiltersChange = (filters) => {
1435
+ setState((prev) => ({ ...prev, filters }));
1436
+ forwardCallback(optionsRef, "onFiltersChange", filters);
1437
+ };
1438
+ cb.onBoundsChange = (bounds) => {
1439
+ setState((prev) => ({ ...prev, bounds }));
1440
+ forwardCallback(optionsRef, "onBoundsChange", bounds);
1441
+ };
1442
+ cb.onPendingBoundsChange = (pendingBounds) => {
1443
+ setState((prev) => ({ ...prev, pendingBounds }));
1444
+ forwardCallback(optionsRef, "onPendingBoundsChange", pendingBounds);
1445
+ };
1446
+ cb.onCenterChange = (center, zoom) => {
1447
+ setState((prev) => ({ ...prev, center, zoom }));
1448
+ forwardCallback(optionsRef, "onCenterChange", center, zoom);
1449
+ };
1450
+ cb.onZoomChange = (zoom) => {
1451
+ setState((prev) => ({ ...prev, zoom }));
1452
+ forwardCallback(optionsRef, "onZoomChange", zoom);
1453
+ };
1454
+ cb.onActiveLocationChange = (location) => {
1455
+ setState((prev) => ({ ...prev, activeLocation: location }));
1456
+ forwardCallback(optionsRef, "onActiveLocationChange", location);
1457
+ };
1458
+ cb.onLoadingStateChange = (loading) => {
1459
+ setState((prev) => ({ ...prev, initialLoading: loading }));
1460
+ forwardCallback(optionsRef, "onLoadingStateChange", loading);
1461
+ };
1462
+ cb.onSearchingStateChange = (searching) => {
1463
+ setState((prev) => ({ ...prev, isSearching: searching }));
1464
+ forwardCallback(optionsRef, "onSearchingStateChange", searching);
1465
+ };
1466
+ cb.onFirstCallDoneChange = (firstCallDone) => {
1467
+ setState((prev) => ({ ...prev, firstCallDone }));
1468
+ forwardCallback(optionsRef, "onFirstCallDoneChange", firstCallDone);
1469
+ };
1470
+ cb.onIsFlyToAnimatingChange = (animating) => {
1471
+ setState((prev) => ({ ...prev, isFlyToAnimating: animating }));
1472
+ forwardCallback(optionsRef, "onIsFlyToAnimatingChange", animating);
1463
1473
  };
1464
- const instance = new import_core.MapFirstCore(coreOptions);
1465
- instanceRef.current = instance;
1466
- setState(instance.getState());
1467
1474
  return () => {
1468
- instance.destroy();
1475
+ var _a;
1476
+ const existingCb = callbacksRef.current;
1477
+ Object.keys(existingCb).forEach(
1478
+ (k) => delete existingCb[k]
1479
+ );
1480
+ (_a = instanceRef.current) == null ? void 0 : _a.destroy();
1469
1481
  instanceRef.current = null;
1470
- setState(null);
1471
1482
  };
1472
1483
  }, []);
1473
1484
  const setPrimaryType = import_react9.default.useCallback((type) => {
package/dist/index.mjs CHANGED
@@ -1336,9 +1336,6 @@ var SmartFilter = ({
1336
1336
  };
1337
1337
 
1338
1338
  // src/index.tsx
1339
- function updateStateField(setState, key, value) {
1340
- setState((prev) => prev ? { ...prev, [key]: value } : null);
1341
- }
1342
1339
  function forwardCallback(optionsRef, key, ...args) {
1343
1340
  var _a;
1344
1341
  const callback = (_a = optionsRef.current.callbacks) == null ? void 0 : _a[key];
@@ -1352,82 +1349,96 @@ function attachMapOnce(instanceRef, attachedRef, map, config) {
1352
1349
  attachedRef.current = true;
1353
1350
  }
1354
1351
  function useMapFirst(options) {
1355
- const instanceRef = React6.useRef(null);
1356
- const [state, setState] = React6.useState(null);
1357
1352
  const optionsRef = React6.useRef(options);
1358
1353
  React6.useEffect(() => {
1359
1354
  optionsRef.current = options;
1360
1355
  });
1361
- React6.useEffect(() => {
1362
- const opts = optionsRef.current;
1363
- const coreOptions = {
1356
+ const callbacksRef = React6.useRef({});
1357
+ const instanceRef = React6.useRef(null);
1358
+ if (instanceRef.current === null) {
1359
+ instanceRef.current = new MapFirstCore({
1364
1360
  adapter: null,
1365
- // Will be set when attachMap is called
1366
- ...opts,
1367
- callbacks: {
1368
- ...opts.callbacks,
1369
- // Add internal callbacks to trigger React re-renders
1370
- onPropertiesChange: (properties) => {
1371
- updateStateField(setState, "properties", properties);
1372
- forwardCallback(optionsRef, "onPropertiesChange", properties);
1373
- },
1374
- onSelectedPropertyChange: (id) => {
1375
- updateStateField(setState, "selectedPropertyId", id);
1376
- forwardCallback(optionsRef, "onSelectedPropertyChange", id);
1377
- },
1378
- onPrimaryTypeChange: (type) => {
1379
- updateStateField(setState, "primary", type);
1380
- forwardCallback(optionsRef, "onPrimaryTypeChange", type);
1381
- },
1382
- onFiltersChange: (filters) => {
1383
- updateStateField(setState, "filters", filters);
1384
- forwardCallback(optionsRef, "onFiltersChange", filters);
1385
- },
1386
- onBoundsChange: (bounds) => {
1387
- updateStateField(setState, "bounds", bounds);
1388
- forwardCallback(optionsRef, "onBoundsChange", bounds);
1389
- },
1390
- onPendingBoundsChange: (pendingBounds) => {
1391
- updateStateField(setState, "pendingBounds", pendingBounds);
1392
- forwardCallback(optionsRef, "onPendingBoundsChange", pendingBounds);
1393
- },
1394
- onCenterChange: (center, zoom) => {
1395
- setState((prev) => prev ? { ...prev, center, zoom } : null);
1396
- forwardCallback(optionsRef, "onCenterChange", center, zoom);
1397
- },
1398
- onZoomChange: (zoom) => {
1399
- updateStateField(setState, "zoom", zoom);
1400
- forwardCallback(optionsRef, "onZoomChange", zoom);
1401
- },
1402
- onActiveLocationChange: (location) => {
1403
- updateStateField(setState, "activeLocation", location);
1404
- forwardCallback(optionsRef, "onActiveLocationChange", location);
1405
- },
1406
- onLoadingStateChange: (loading) => {
1407
- updateStateField(setState, "initialLoading", loading);
1408
- forwardCallback(optionsRef, "onLoadingStateChange", loading);
1409
- },
1410
- onSearchingStateChange: (searching) => {
1411
- updateStateField(setState, "isSearching", searching);
1412
- forwardCallback(optionsRef, "onSearchingStateChange", searching);
1413
- },
1414
- onFirstCallDoneChange: (firstCallDone) => {
1415
- updateStateField(setState, "firstCallDone", firstCallDone);
1416
- forwardCallback(optionsRef, "onFirstCallDoneChange", firstCallDone);
1417
- },
1418
- onIsFlyToAnimatingChange: (animating) => {
1419
- updateStateField(setState, "isFlyToAnimating", animating);
1420
- forwardCallback(optionsRef, "onIsFlyToAnimatingChange", animating);
1421
- }
1422
- }
1361
+ ...optionsRef.current,
1362
+ callbacks: callbacksRef.current
1363
+ });
1364
+ }
1365
+ const [state, setState] = React6.useState(
1366
+ () => instanceRef.current.getState()
1367
+ );
1368
+ React6.useEffect(() => {
1369
+ if (!instanceRef.current) {
1370
+ callbacksRef.current = {};
1371
+ instanceRef.current = new MapFirstCore({
1372
+ adapter: null,
1373
+ ...optionsRef.current,
1374
+ callbacks: callbacksRef.current
1375
+ });
1376
+ setState(instanceRef.current.getState());
1377
+ mapLibreAttachedRef.current = false;
1378
+ googleMapsAttachedRef.current = false;
1379
+ mapboxAttachedRef.current = false;
1380
+ }
1381
+ const cb = callbacksRef.current;
1382
+ cb.onPropertiesChange = (properties) => {
1383
+ setState((prev) => ({ ...prev, properties }));
1384
+ forwardCallback(optionsRef, "onPropertiesChange", properties);
1385
+ };
1386
+ cb.onSelectedPropertyChange = (id) => {
1387
+ setState((prev) => ({ ...prev, selectedPropertyId: id }));
1388
+ forwardCallback(optionsRef, "onSelectedPropertyChange", id);
1389
+ };
1390
+ cb.onPrimaryTypeChange = (type) => {
1391
+ setState((prev) => ({ ...prev, primary: type }));
1392
+ forwardCallback(optionsRef, "onPrimaryTypeChange", type);
1393
+ };
1394
+ cb.onFiltersChange = (filters) => {
1395
+ setState((prev) => ({ ...prev, filters }));
1396
+ forwardCallback(optionsRef, "onFiltersChange", filters);
1397
+ };
1398
+ cb.onBoundsChange = (bounds) => {
1399
+ setState((prev) => ({ ...prev, bounds }));
1400
+ forwardCallback(optionsRef, "onBoundsChange", bounds);
1401
+ };
1402
+ cb.onPendingBoundsChange = (pendingBounds) => {
1403
+ setState((prev) => ({ ...prev, pendingBounds }));
1404
+ forwardCallback(optionsRef, "onPendingBoundsChange", pendingBounds);
1405
+ };
1406
+ cb.onCenterChange = (center, zoom) => {
1407
+ setState((prev) => ({ ...prev, center, zoom }));
1408
+ forwardCallback(optionsRef, "onCenterChange", center, zoom);
1409
+ };
1410
+ cb.onZoomChange = (zoom) => {
1411
+ setState((prev) => ({ ...prev, zoom }));
1412
+ forwardCallback(optionsRef, "onZoomChange", zoom);
1413
+ };
1414
+ cb.onActiveLocationChange = (location) => {
1415
+ setState((prev) => ({ ...prev, activeLocation: location }));
1416
+ forwardCallback(optionsRef, "onActiveLocationChange", location);
1417
+ };
1418
+ cb.onLoadingStateChange = (loading) => {
1419
+ setState((prev) => ({ ...prev, initialLoading: loading }));
1420
+ forwardCallback(optionsRef, "onLoadingStateChange", loading);
1421
+ };
1422
+ cb.onSearchingStateChange = (searching) => {
1423
+ setState((prev) => ({ ...prev, isSearching: searching }));
1424
+ forwardCallback(optionsRef, "onSearchingStateChange", searching);
1425
+ };
1426
+ cb.onFirstCallDoneChange = (firstCallDone) => {
1427
+ setState((prev) => ({ ...prev, firstCallDone }));
1428
+ forwardCallback(optionsRef, "onFirstCallDoneChange", firstCallDone);
1429
+ };
1430
+ cb.onIsFlyToAnimatingChange = (animating) => {
1431
+ setState((prev) => ({ ...prev, isFlyToAnimating: animating }));
1432
+ forwardCallback(optionsRef, "onIsFlyToAnimatingChange", animating);
1423
1433
  };
1424
- const instance = new MapFirstCore(coreOptions);
1425
- instanceRef.current = instance;
1426
- setState(instance.getState());
1427
1434
  return () => {
1428
- instance.destroy();
1435
+ var _a;
1436
+ const existingCb = callbacksRef.current;
1437
+ Object.keys(existingCb).forEach(
1438
+ (k) => delete existingCb[k]
1439
+ );
1440
+ (_a = instanceRef.current) == null ? void 0 : _a.destroy();
1429
1441
  instanceRef.current = null;
1430
- setState(null);
1431
1442
  };
1432
1443
  }, []);
1433
1444
  const setPrimaryType = React6.useCallback((type) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapfirst.ai/react",
3
- "version": "0.0.45",
3
+ "version": "0.0.46",
4
4
  "description": "React hooks for MapFirst SDK - Reactive state management for map properties",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",