@cloudparse/up-miniapps-sdk 0.6.7 → 0.6.8

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.cjs CHANGED
@@ -785,7 +785,7 @@ var MapDrawer = (0, import_react2.forwardRef)(
785
785
  const dragDelta = touchEndY - dragStartY;
786
786
  const drawerHeightFromBottom = dragBaseHeightPxRef.current - dragDelta;
787
787
  const distToClosed = Math.abs(drawerHeightFromBottom - closedPx);
788
- let targetState;
788
+ let targetState2;
789
789
  if (currentState === "auto") {
790
790
  const autoMaxPx2 = resolveHeightToPixels(
791
791
  drawerAutoMaxHeight || "100%",
@@ -793,22 +793,22 @@ var MapDrawer = (0, import_react2.forwardRef)(
793
793
  );
794
794
  const autoPx2 = Math.min(autoMaxPx2, autoContentPx);
795
795
  const distToAuto = Math.abs(drawerHeightFromBottom - autoPx2);
796
- targetState = distToAuto <= distToClosed ? "auto" : "closed";
796
+ targetState2 = distToAuto <= distToClosed ? "auto" : "closed";
797
797
  } else {
798
798
  const distToFull = Math.abs(drawerHeightFromBottom - fullPx2);
799
799
  const distToPeek = Math.abs(drawerHeightFromBottom - peekPx2);
800
800
  const minDist = Math.min(distToFull, distToPeek, distToClosed);
801
801
  if (minDist === distToFull) {
802
- targetState = "full";
802
+ targetState2 = "full";
803
803
  } else if (minDist === distToPeek) {
804
- targetState = "peek";
804
+ targetState2 = "peek";
805
805
  } else {
806
- targetState = "closed";
806
+ targetState2 = "closed";
807
807
  }
808
808
  }
809
809
  setTouchStartY(null);
810
810
  setCurrentTouchY(null);
811
- onChangeState(targetState);
811
+ onChangeState(targetState2);
812
812
  requestAnimationFrame(() => {
813
813
  if (parentContainer) {
814
814
  parentContainer.style.setProperty("--drag-offset", "0px");
@@ -2085,115 +2085,142 @@ var GlobalEvent = /* @__PURE__ */ ((GlobalEvent2) => {
2085
2085
  // src/components/Map/hooks/useUserLocation.ts
2086
2086
  var import_react6 = require("react");
2087
2087
  var LERP_FACTOR = 0.12;
2088
- var useUserLocation = () => {
2089
- const [locationState, setLocationState] = (0, import_react6.useState)({
2090
- accuracy: null,
2091
- coords: null,
2092
- heading: null
2093
- });
2094
- const targetRef = (0, import_react6.useRef)({
2095
- accuracy: null,
2096
- coords: null,
2097
- heading: null
2098
- });
2099
- const currentRef = (0, import_react6.useRef)({
2100
- accuracy: null,
2101
- coords: null,
2102
- heading: null
2103
- });
2104
- const mapPayload = (0, import_react6.useCallback)((payload) => {
2105
- const coords = payload.location?.coords || payload.coords;
2106
- const headingData = payload.heading;
2107
- const newCoords = coords ? [coords.longitude, coords.latitude] : null;
2108
- let heading = null;
2109
- if (headingData?.trueHeading !== void 0 && headingData.trueHeading >= 0) {
2110
- heading = headingData.trueHeading;
2111
- } else if (headingData?.magneticHeading !== void 0 && headingData.magneticHeading >= 0) {
2112
- heading = headingData.magneticHeading;
2113
- } else if (headingData?.heading !== null && headingData?.heading >= 0) {
2114
- heading = headingData.heading;
2115
- }
2116
- return {
2117
- accuracy: coords?.accuracy || null,
2118
- coords: newCoords,
2119
- heading
2120
- };
2121
- }, []);
2122
- (0, import_react6.useEffect)(() => {
2123
- let rafId;
2124
- const animate = () => {
2125
- let changed = false;
2126
- if (targetRef.current.coords) {
2127
- if (!currentRef.current.coords) {
2128
- currentRef.current.coords = [...targetRef.current.coords];
2129
- changed = true;
2130
- } else {
2131
- const [tLng, tLat] = targetRef.current.coords;
2132
- const [cLng, cLat] = currentRef.current.coords;
2133
- const nLng = cLng + (tLng - cLng) * LERP_FACTOR;
2134
- const nLat = cLat + (tLat - cLat) * LERP_FACTOR;
2135
- if (Math.abs(nLng - cLng) > 1e-7 || Math.abs(nLat - cLat) > 1e-7) {
2136
- currentRef.current.coords = [nLng, nLat];
2137
- changed = true;
2138
- }
2139
- }
2140
- }
2141
- if (targetRef.current.heading !== null) {
2142
- if (currentRef.current.heading === null) {
2143
- currentRef.current.heading = targetRef.current.heading;
2144
- changed = true;
2145
- } else {
2146
- const target = targetRef.current.heading;
2147
- const current = currentRef.current.heading;
2148
- let delta = target - current;
2149
- if (delta > 180) delta -= 360;
2150
- if (delta < -180) delta += 360;
2151
- const nHeading = current + delta * LERP_FACTOR;
2152
- const normalized = (nHeading % 360 + 360) % 360;
2153
- if (Math.abs(delta) > 0.1) {
2154
- currentRef.current.heading = normalized;
2155
- changed = true;
2156
- }
2157
- }
2158
- }
2159
- if (changed) {
2160
- setLocationState({
2161
- accuracy: targetRef.current.accuracy,
2162
- coords: currentRef.current.coords ? [...currentRef.current.coords] : null,
2163
- heading: currentRef.current.heading
2164
- });
2165
- }
2166
- rafId = requestAnimationFrame(animate);
2167
- };
2168
- rafId = requestAnimationFrame(animate);
2169
- sdk.location.getCurrentPosition().then((data) => {
2170
- const payload = mapPayload(data);
2171
- targetRef.current = payload;
2172
- }).catch((e) => console.warn("Initial location fetch failed", e));
2173
- const unsubscribeStream = sdk.on(
2174
- "LOCATION_STREAM_UPDATE" /* LOCATION_STREAM_UPDATE */,
2175
- (payload) => {
2176
- targetRef.current = mapPayload(payload);
2088
+ var EMPTY_STATE = {
2089
+ accuracy: null,
2090
+ coords: null,
2091
+ heading: null
2092
+ };
2093
+ function mapPayload(payload) {
2094
+ const coords = payload.location?.coords || payload.coords;
2095
+ const headingData = payload.heading;
2096
+ const newCoords = coords ? [coords.longitude, coords.latitude] : null;
2097
+ let heading = null;
2098
+ if (headingData?.trueHeading !== void 0 && headingData.trueHeading >= 0) {
2099
+ heading = headingData.trueHeading;
2100
+ } else if (headingData?.magneticHeading !== void 0 && headingData.magneticHeading >= 0) {
2101
+ heading = headingData.magneticHeading;
2102
+ } else if (headingData?.heading !== null && headingData?.heading >= 0) {
2103
+ heading = headingData.heading;
2104
+ }
2105
+ return {
2106
+ accuracy: coords?.accuracy || null,
2107
+ coords: newCoords,
2108
+ heading
2109
+ };
2110
+ }
2111
+ var refCount = 0;
2112
+ var generation = 0;
2113
+ var subscribers = /* @__PURE__ */ new Set();
2114
+ var targetState = { ...EMPTY_STATE };
2115
+ var currentInterpolated = { coords: null, heading: null };
2116
+ var rafId = null;
2117
+ var unsubscribeStream = null;
2118
+ var unsubscribeLegacy = null;
2119
+ function getSnapshot() {
2120
+ return {
2121
+ accuracy: targetState.accuracy,
2122
+ coords: currentInterpolated.coords ? [...currentInterpolated.coords] : null,
2123
+ heading: currentInterpolated.heading
2124
+ };
2125
+ }
2126
+ function notify() {
2127
+ const snapshot = getSnapshot();
2128
+ for (const callback of subscribers) callback(snapshot);
2129
+ }
2130
+ function animate() {
2131
+ let changed = false;
2132
+ if (targetState.coords) {
2133
+ if (!currentInterpolated.coords) {
2134
+ currentInterpolated.coords = [...targetState.coords];
2135
+ changed = true;
2136
+ } else {
2137
+ const [tLng, tLat] = targetState.coords;
2138
+ const [cLng, cLat] = currentInterpolated.coords;
2139
+ const nLng = cLng + (tLng - cLng) * LERP_FACTOR;
2140
+ const nLat = cLat + (tLat - cLat) * LERP_FACTOR;
2141
+ if (Math.abs(nLng - cLng) > 1e-7 || Math.abs(nLat - cLat) > 1e-7) {
2142
+ currentInterpolated.coords = [nLng, nLat];
2143
+ changed = true;
2177
2144
  }
2178
- );
2179
- const unsubscribeLegacy = sdk.on(
2180
- "LOCATION_UPDATE" /* LOCATION_UPDATE */,
2181
- (payload) => {
2182
- targetRef.current = mapPayload(payload);
2145
+ }
2146
+ }
2147
+ if (targetState.heading !== null) {
2148
+ if (currentInterpolated.heading === null) {
2149
+ currentInterpolated.heading = targetState.heading;
2150
+ changed = true;
2151
+ } else {
2152
+ const target = targetState.heading;
2153
+ const current = currentInterpolated.heading;
2154
+ let delta = target - current;
2155
+ if (delta > 180) delta -= 360;
2156
+ if (delta < -180) delta += 360;
2157
+ const nHeading = current + delta * LERP_FACTOR;
2158
+ const normalized = (nHeading % 360 + 360) % 360;
2159
+ if (Math.abs(delta) > 0.1) {
2160
+ currentInterpolated.heading = normalized;
2161
+ changed = true;
2183
2162
  }
2184
- );
2185
- sdk.location.startStream({
2186
- distanceFilter: 1,
2187
- headingThreshold: 2
2188
- }).catch((e) => console.warn("Failed to start location stream", e));
2163
+ }
2164
+ }
2165
+ if (changed) notify();
2166
+ rafId = requestAnimationFrame(animate);
2167
+ }
2168
+ function startSharedStream() {
2169
+ rafId = requestAnimationFrame(animate);
2170
+ generation++;
2171
+ const thisGeneration = generation;
2172
+ sdk.location.getCurrentPosition().then((data) => {
2173
+ if (thisGeneration !== generation) return;
2174
+ targetState = mapPayload(data);
2175
+ }).catch((e) => console.warn("Initial location fetch failed", e));
2176
+ unsubscribeStream = sdk.on(
2177
+ "LOCATION_STREAM_UPDATE" /* LOCATION_STREAM_UPDATE */,
2178
+ (payload) => {
2179
+ targetState = mapPayload(payload);
2180
+ }
2181
+ );
2182
+ unsubscribeLegacy = sdk.on(
2183
+ "LOCATION_UPDATE" /* LOCATION_UPDATE */,
2184
+ (payload) => {
2185
+ targetState = mapPayload(payload);
2186
+ }
2187
+ );
2188
+ sdk.location.startStream({
2189
+ distanceFilter: 1,
2190
+ headingThreshold: 2
2191
+ }).catch((e) => console.warn("Failed to start location stream", e));
2192
+ }
2193
+ function stopSharedStream() {
2194
+ generation++;
2195
+ if (rafId !== null) cancelAnimationFrame(rafId);
2196
+ rafId = null;
2197
+ unsubscribeStream?.();
2198
+ unsubscribeLegacy?.();
2199
+ unsubscribeStream = null;
2200
+ unsubscribeLegacy = null;
2201
+ sdk.location.stopStream().catch(() => {
2202
+ });
2203
+ targetState = { ...EMPTY_STATE };
2204
+ currentInterpolated = { coords: null, heading: null };
2205
+ }
2206
+ var useUserLocation = () => {
2207
+ const [locationState, setLocationState] = (0, import_react6.useState)(getSnapshot);
2208
+ (0, import_react6.useEffect)(() => {
2209
+ refCount++;
2210
+ if (refCount === 1) {
2211
+ startSharedStream();
2212
+ } else {
2213
+ setLocationState(getSnapshot());
2214
+ }
2215
+ subscribers.add(setLocationState);
2189
2216
  return () => {
2190
- cancelAnimationFrame(rafId);
2191
- unsubscribeStream();
2192
- unsubscribeLegacy();
2193
- sdk.location.stopStream().catch(() => {
2194
- });
2217
+ subscribers.delete(setLocationState);
2218
+ refCount--;
2219
+ if (refCount === 0) {
2220
+ stopSharedStream();
2221
+ }
2195
2222
  };
2196
- }, [mapPayload]);
2223
+ }, []);
2197
2224
  return locationState;
2198
2225
  };
2199
2226
 
@@ -3765,7 +3792,7 @@ var RoutingService = {
3765
3792
  };
3766
3793
 
3767
3794
  // package.json
3768
- var version = "0.6.7";
3795
+ var version = "0.6.8";
3769
3796
 
3770
3797
  // src/sdk.ts
3771
3798
  function deepFreeze(obj) {
@@ -4176,7 +4203,6 @@ var import_react14 = require("react");
4176
4203
  var import_jsx_runtime16 = require("react/jsx-runtime");
4177
4204
  var AVATAR_MOUTH_VARIANTS = [
4178
4205
  "variant03",
4179
- "variant06",
4180
4206
  "variant07",
4181
4207
  "variant08",
4182
4208
  "variant09",