Skip to content

feat: add useLensFrameMetrics hook#3

Merged
msilivonik-sc merged 2 commits intomainfrom
ms/fix-lens-cache-on-rebootstrap
Apr 3, 2026
Merged

feat: add useLensFrameMetrics hook#3
msilivonik-sc merged 2 commits intomainfrom
ms/fix-lens-cache-on-rebootstrap

Conversation

@msilivonik-sc
Copy link
Copy Markdown
Collaborator

Summary

  • Add useLensFrameMetrics hook that exposes lens rendering performance data (ComputedFrameMetrics) from the CameraKit SDK's session.metrics API
  • The hook manages the full LensPerformanceMeasurement lifecycle: begins measurement when a session is available, polls at a configurable interval, resets on lens change, and calls end() on cleanup to prevent memory leaks
  • Supports an enabled option (defaults to true) so consumers can toggle measurement without breaking rules of hooks

Usage

import { useLensFrameMetrics } from "@snap/react-camera-kit";

function PerformanceOverlay() {
  const metrics = useLensFrameMetrics({ interval: 500 });

  if (!metrics) return null;

  return (
    <div>
      <p>FPS: {metrics.avgFps.toFixed(1)}</p>
      <p>Frame time: {metrics.lensFrameProcessingTimeMsAvg.toFixed(1)}ms</p>
    </div>
  );
}

Test plan

  • 11 new unit tests covering: no session, begin/end lifecycle, polling, lens change reset, unmount cleanup, session re-bootstrap, enabled/disabled toggling, session loss
  • Full test suite passes (301 tests)
  • Build passes (ESM + CJS)
  • Verify in demo app: hook returns metrics when lens is active, returns undefined when no session

@msilivonik-sc msilivonik-sc force-pushed the ms/fix-lens-cache-on-rebootstrap branch from 6e25da0 to 9612cdd Compare April 3, 2026 03:27
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Add implementation plan for useLensPerformanceMetrics hook

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

test: add failing tests for useLensPerformanceMetrics hook

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

fix: resolve TypeScript errors in useLensPerformanceMetrics tests

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

fix: align test mock shapes with actual SDK types

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

feat: implement useLensPerformanceMetrics hook

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

fix: reset isFirstLensRender on session cleanup

Prevents spurious measurement.reset() call on the first lens
applied to a new session after re-bootstrap.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

feat: export useLensPerformanceMetrics and ComputedFrameMetrics type

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

feat: add enabled option to useLensPerformanceMetrics

Allows consumers to conditionally enable/disable measurement
without breaking rules of hooks. Defaults to true.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

fix: remove ComputedFrameMetrics re-export from index

Consumers should import SDK types directly from @snap/camera-kit,
consistent with how other SDK types (Lens, LensLaunchData, etc.)
are not re-exported.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

docs: add useLensPerformanceMetrics to README

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

refactor: rename useLensPerformanceMetrics to useLensFrameMetrics

Aligns with the SDK's ComputedFrameMetrics return type and is
more specific about what is being measured.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

chore: remove design and plan docs

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

refactor: remove lens cache fix from this branch

The lens cache clearing on re-bootstrap will go in a separate PR.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@msilivonik-sc msilivonik-sc force-pushed the ms/fix-lens-cache-on-rebootstrap branch from 9612cdd to d043cf4 Compare April 3, 2026 03:35
jciereszko-sc
jciereszko-sc previously approved these changes Apr 3, 2026
Changing the polling interval no longer restarts the measurement
and discards accumulated stats. The measurement lifecycle is now
keyed only on session/enabled, while the polling interval is
managed in a separate effect.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@msilivonik-sc msilivonik-sc merged commit a0ae0e6 into main Apr 3, 2026
2 checks passed
@msilivonik-sc msilivonik-sc deleted the ms/fix-lens-cache-on-rebootstrap branch April 3, 2026 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants