Skip to content

feat: expose player methods and events in ReactNative - #1

Merged
syed-tp merged 2 commits into
mainfrom
add/player_method_support
Jun 3, 2025
Merged

feat: expose player methods and events in ReactNative#1
syed-tp merged 2 commits into
mainfrom
add/player_method_support

Conversation

@syed-tp

@syed-tp syed-tp commented Jun 3, 2025

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Introduced a React component for the player with an imperative API, allowing play, pause, seek, and playback speed control from React.
    • Added asynchronous methods to query current position, duration, playback state, and speed, with results returned via promises.
    • Native player events are now sent to React Native, enabling real-time updates for playback state and metrics.
  • Chores

    • Updated the package version to 0.2.0.

@coderabbitai

coderabbitai Bot commented Jun 3, 2025

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This update introduces a React Native player component with an imperative API for playback control and state queries, bridging native Android functionality to JavaScript. The native view and its manager expose new methods and events for play, pause, seek, playback speed, and state queries, while the JavaScript layer handles event-driven promise resolution and command dispatching.

Changes

File(s) Change Summary
android/src/main/java/com/tpstreams/TPStreamsRNPlayerView.kt Added public methods for play, pause, seek, set playback speed, and state queries; emits events to React Native.
android/src/main/java/com/tpstreams/TPStreamsRNPlayerViewManager.kt Added overrides to expose control/query methods and event registration for new player events.
src/TPStreamsPlayerViewNativeComponent.ts Exported/extended NativeProps with new event handlers; added commands interface and exported commands object.
src/TPStreamsPlayer.tsx New component wrapping the native view; exposes imperative API; handles async state queries via events/promises.
src/index.tsx Added default export for TPStreamsPlayer and its ref type; no changes to existing logic.
package.json Bumped version from 0.1.0 to 0.2.0.

Sequence Diagram(s)

sequenceDiagram
    participant JS as TPStreamsPlayer (JS)
    participant NativeView as TPStreamsRNPlayerView (Android)
    participant Manager as TPStreamsRNPlayerViewManager (Android)

    JS->>NativeView: play() / pause() / seekTo() / setPlaybackSpeed()
    JS->>NativeView: getCurrentPosition() / getDuration() / isPlaying() / getPlaybackSpeed()
    NativeView-->>JS: Emits event (onCurrentPosition, onDuration, etc.) with value
    JS->>JS: Resolves corresponding Promise via event handler
Loading

Poem

A hop, a skip, a playback leap,
Now React and native secrets keep.
With pause and play, and seek anew,
Events and queries hopping through!
Version bumped, the code’s in tune—
This player’s ready, none too soon!
🐇🎶


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codetortoiseai

codetortoiseai Bot commented Jun 3, 2025

Copy link
Copy Markdown

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 3, 2025

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (3)
src/TPStreamsPlayer.tsx (2)

41-50: Remove redundant null check for cleaner code.

The event handler performs a redundant null check after retrieving the handler from the promise map.

 const onCurrentPosition = useCallback((event: any) => {
   const key = `position-${instanceId.current}`;
-  if (promiseMap.current[key]) {
-    const handler = promiseMap.current[key];
-    if (handler) {
-      handler.resolve(event.nativeEvent.position);
-      delete promiseMap.current[key];
-    }
-  }
+  const handler = promiseMap.current[key];
+  if (handler) {
+    handler.resolve(event.nativeEvent.position);
+    delete promiseMap.current[key];
+  }
 }, []);

This pattern should be applied to all similar event handlers (lines 52-83).


8-8: Consider potential overflow of global instance counter.

While unlikely in practice, the global nextInstanceId counter could theoretically overflow in long-running applications with many component instances.

Consider using a more robust ID generation:

-let nextInstanceId = 0;
+import { nanoid } from 'nanoid'; // or use crypto.randomUUID() if available

-const instanceId = useRef<number>(nextInstanceId++);
+const instanceId = useRef<string>(nanoid());

This would also require updating the key generation in event handlers to use string concatenation instead of template literals with numbers.

android/src/main/java/com/tpstreams/TPStreamsRNPlayerViewManager.kt (1)

61-63: Potential precision loss in seekTo conversion.

Converting Double to Long for positionMs could lose precision, especially for fractional millisecond values.

Consider using toLong() with proper rounding:

  override fun seekTo(view: TPStreamsRNPlayerView, positionMs: Double) {
-      view.seekTo(positionMs.toLong())
+      view.seekTo(positionMs.roundToLong())
  }

Alternatively, change the interface to accept Long directly if fractional precision isn't needed.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7053155 and 28a6407.

📒 Files selected for processing (6)
  • android/src/main/java/com/tpstreams/TPStreamsRNPlayerView.kt (2 hunks)
  • android/src/main/java/com/tpstreams/TPStreamsRNPlayerViewManager.kt (3 hunks)
  • package.json (1 hunks)
  • src/TPStreamsPlayer.tsx (1 hunks)
  • src/TPStreamsPlayerViewNativeComponent.ts (1 hunks)
  • src/index.tsx (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/TPStreamsPlayer.tsx (2)
src/index.tsx (1)
  • TPStreamsPlayerRef (6-6)
src/TPStreamsPlayerViewNativeComponent.ts (2)
  • NativeProps (8-17)
  • Commands (40-51)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: build-android
  • GitHub Check: build-ios
🔇 Additional comments (12)
package.json (1)

3-3: LGTM! Appropriate version bump for new feature.

The version increment from 0.1.0 to 0.2.0 correctly follows semantic versioning for the addition of new player methods functionality.

src/index.tsx (1)

5-6: LGTM! Clean export additions.

The new exports for TPStreamsPlayer and TPStreamsPlayerRef follow the existing patterns and properly expose the new player component functionality.

src/TPStreamsPlayer.tsx (1)

181-191: LGTM! Clean component composition and rendering.

The component properly merges event handlers with incoming props and renders the native component with the correct ref. The pattern follows React best practices.

src/TPStreamsPlayerViewNativeComponent.ts (4)

3-6: LGTM! Proper imports for React Native codegen.

The additional imports for Double, Float, DirectEventHandler, and codegenNativeCommands are correctly added to support the new event handlers and commands functionality.


8-17: LGTM! Well-typed event handler props.

The event handler props are properly typed with DirectEventHandler and include the correct payload types (Double, Float, boolean) that align with the native implementation expectations.


19-38: LGTM! Comprehensive command interface definition.

The TPStreamsPlayerViewCommands interface properly defines all player control methods with correct parameter types and follows React Native patterns for native command interfaces.


40-51: LGTM! Proper command registration with codegen.

The Commands export correctly uses codegenNativeCommands with all supported commands listed, enabling the bridge between JavaScript and native implementations.

android/src/main/java/com/tpstreams/TPStreamsRNPlayerView.kt (2)

5-9: LGTM! Proper imports and context storage for event emission.

The additional imports and ReactContext storage are correctly implemented for the event emission functionality.

Also applies to: 17-17


49-63: LGTM! Clean player control delegation.

The player control methods follow a proper null-safe delegation pattern to the underlying player instance.

android/src/main/java/com/tpstreams/TPStreamsRNPlayerViewManager.kt (3)

11-11: LGTM! Proper event registration for React Native.

The custom direct event types are correctly registered using MapBuilder, enabling proper event communication between native and JavaScript layers.

Also applies to: 30-37


53-67: LGTM! Clean delegation pattern for control methods.

The player control methods properly delegate to the view instance with appropriate type handling.


69-83:

Details

✅ Verification successful

Consider the event-driven architecture implications.

The getter methods in the manager don't return values but trigger event emission through the view. This is unusual for getter methods but appears intentional for the event-driven architecture.

Verify that this event-driven approach aligns with the JavaScript implementation and that the calling code doesn't expect synchronous return values from these methods.


🏁 Script executed:

#!/bin/bash
# Description: Check how these getter methods are used in the JavaScript layer
# Expected: JavaScript code should use event listeners rather than return values

rg -A 10 -B 5 "getCurrentPosition|getDuration|isPlaying|getPlaybackSpeed" --type ts --type tsx --type js --type jsx

Length of output: 145


🏁 Script executed:

#!/bin/bash
# Re-run search across JS/TS files without relying on rg’s built-in types
rg -g '*.js' -g '*.jsx' -g '*.ts' -g '*.tsx' -e "getCurrentPosition" -e "getDuration" -e "isPlaying" -e "getPlaybackSpeed" -A 5 -B 5

Length of output: 8632


Event-driven getters align with JS promise-based API

The native getCurrentPosition, getDuration, isPlaying, and getPlaybackSpeed methods fire events that the JS layer listens for (via onCurrentPosition, onDuration, onIsPlaying, onPlaybackSpeed) and resolves as Promises. The React wrapper in TPStreamsPlayer.tsx correctly invokes these commands without expecting synchronous returns. No changes needed.

Comment thread src/TPStreamsPlayer.tsx
Comment thread src/TPStreamsPlayer.tsx Outdated
Comment thread android/src/main/java/com/tpstreams/TPStreamsRNPlayerView.kt
@syed-tp syed-tp changed the title feat: expose player methods in ReactNative feat: expose player methods and events in ReactNative Jun 3, 2025
@syed-tp
syed-tp merged commit 49b7fe1 into main Jun 3, 2025
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.

1 participant