@runhuman/sensor 0.2.3 → 0.2.5

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
@@ -186,10 +186,6 @@ declare function RunhumanOverlay({ children, position }: RunhumanOverlayProps):
186
186
  /**
187
187
  * React hook for subscribing to sensor state changes.
188
188
  * Uses useSyncExternalStore for tear-safe subscriptions.
189
- *
190
- * Handles the case where Runhuman.init() hasn't been called yet
191
- * (e.g., during first render before useEffect fires) by polling
192
- * until the instance is available.
193
189
  */
194
190
 
195
191
  interface SensorState {
@@ -199,8 +195,7 @@ interface SensorState {
199
195
  }
200
196
  /**
201
197
  * Subscribe to the sensor's session state.
202
- * Safe to call before Runhuman.init() — returns idle state and
203
- * polls every 100ms until the instance becomes available.
198
+ * Safe to call before Runhuman.init().
204
199
  */
205
200
  declare function useSensorState(): SensorState;
206
201
 
package/dist/index.d.ts CHANGED
@@ -186,10 +186,6 @@ declare function RunhumanOverlay({ children, position }: RunhumanOverlayProps):
186
186
  /**
187
187
  * React hook for subscribing to sensor state changes.
188
188
  * Uses useSyncExternalStore for tear-safe subscriptions.
189
- *
190
- * Handles the case where Runhuman.init() hasn't been called yet
191
- * (e.g., during first render before useEffect fires) by polling
192
- * until the instance is available.
193
189
  */
194
190
 
195
191
  interface SensorState {
@@ -199,8 +195,7 @@ interface SensorState {
199
195
  }
200
196
  /**
201
197
  * Subscribe to the sensor's session state.
202
- * Safe to call before Runhuman.init() — returns idle state and
203
- * polls every 100ms until the instance becomes available.
198
+ * Safe to call before Runhuman.init().
204
199
  */
205
200
  declare function useSensorState(): SensorState;
206
201
 
package/dist/index.js CHANGED
@@ -1191,28 +1191,39 @@ function tryGetInstance() {
1191
1191
  }
1192
1192
  }
1193
1193
  function useSensorState() {
1194
+ const cached = (0, import_react.useRef)(IDLE_STATE);
1194
1195
  const subscribe = (0, import_react.useCallback)((onStoreChange) => {
1195
1196
  const instance = tryGetInstance();
1196
- if (!instance) {
1197
- const interval = setInterval(() => {
1198
- if (tryGetInstance()) {
1199
- clearInterval(interval);
1200
- onStoreChange();
1201
- }
1202
- }, 100);
1203
- return () => clearInterval(interval);
1197
+ if (instance) {
1198
+ return instance.getSessionManager().subscribe(onStoreChange);
1204
1199
  }
1205
- return instance.getSessionManager().subscribe(onStoreChange);
1200
+ let unsub = null;
1201
+ const interval = setInterval(() => {
1202
+ const inst = tryGetInstance();
1203
+ if (inst) {
1204
+ clearInterval(interval);
1205
+ unsub = inst.getSessionManager().subscribe(onStoreChange);
1206
+ onStoreChange();
1207
+ }
1208
+ }, 100);
1209
+ return () => {
1210
+ clearInterval(interval);
1211
+ if (unsub) unsub();
1212
+ };
1206
1213
  }, []);
1207
1214
  const getSnapshot = (0, import_react.useCallback)(() => {
1208
1215
  const instance = tryGetInstance();
1209
1216
  if (!instance) return IDLE_STATE;
1210
1217
  const sm = instance.getSessionManager();
1211
- return {
1212
- state: sm.getSnapshot(),
1213
- activeJobId: sm.getActiveJobId(),
1214
- sessionId: sm.getSessionId()
1215
- };
1218
+ const state = sm.getSnapshot();
1219
+ const activeJobId = sm.getActiveJobId();
1220
+ const sessionId = sm.getSessionId();
1221
+ const prev = cached.current;
1222
+ if (prev.state === state && prev.activeJobId === activeJobId && prev.sessionId === sessionId) {
1223
+ return prev;
1224
+ }
1225
+ cached.current = { state, activeJobId, sessionId };
1226
+ return cached.current;
1216
1227
  }, []);
1217
1228
  return (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
1218
1229
  }