Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ios native player desctruction #446

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.1.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

- Fixed a memory leak on iOS, caused by the wrapping ViewController that was keeping a strong reference to the THEOplayerRCTView.

## [8.8.1] - 24-11-20

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions ios/THEOplayerRCTBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ @interface RCT_EXTERN_MODULE(THEOplayerRCTViewManager, RCTViewManager)

RCT_EXPORT_VIEW_PROPERTY(onNativeCastEvent, RCTDirectEventBlock);

RCT_EXTERN_METHOD(destroy:(nonnull NSNumber *)node);

@end

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -114,6 +112,8 @@ @interface RCT_EXTERN_REMAP_MODULE(THEORCTPlayerModule, THEOplayerRCTPlayerAPI,
RCT_EXTERN_METHOD(setTextTrackStyle:(nonnull NSNumber *)node
textTrackStyle:(NSDictionary)textTrackStyle)

RCT_EXTERN_METHOD(destroyPlayer:(nonnull NSNumber *)node);

@end

// ----------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions ios/THEOplayerRCTPlayerAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -356,5 +356,14 @@ class THEOplayerRCTPlayerAPI: NSObject, RCTBridgeModule {
}
}
}

@objc(destroyPlayer:)
func destroyPlayer(_ node: NSNumber) -> Void {
DispatchQueue.main.async {
if let theView = self.bridge.uiManager.view(forReactTag: node) as? THEOplayerRCTView {
theView.destroyPlayer()
}
}
}

}
5 changes: 4 additions & 1 deletion ios/THEOplayerRCTView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public class THEOplayerRCTView: UIView {

// MARK: - Destroy Player

func destroyPlayer() {
public func destroyPlayer() {
self.mainEventHandler.destroy()
self.textTrackEventHandler.destroy()
self.mediaTrackEventHandler.destroy()
Expand All @@ -154,6 +154,9 @@ public class THEOplayerRCTView: UIView {
self.player?.destroy()
self.player = nil
if DEBUG_THEOPLAYER_INTERACTION { PrintUtils.printLog(logText: "[NATIVE] THEOplayer instance destroyed.") }

self.theoPlayerViewController.view = nil
self.theoPlayerViewController.removeFromParent()
}

func processMetadataTracks(metadataTrackDescriptions: [TextTrackDescription]?) {
Expand Down
5 changes: 5 additions & 0 deletions src/api/player/THEOplayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ export interface THEOplayer extends EventDispatcher<PlayerEventMap> {
*/
pause(): void;

/**
* destroy the player.
*/
destroy(): void;

/**
* Whether the player is paused.
*/
Expand Down
8 changes: 1 addition & 7 deletions src/internal/THEOplayerView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,9 @@ export class THEOplayerView extends PureComponent<React.PropsWithChildren<THEOpl
if (onPlayerDestroy) {
onPlayerDestroy(this._facade);
}
this._facade.destroy();
this._facade.dispatchEvent(new BaseEvent(PlayerEventType.DESTROY));
this._dimensionsHandler?.remove();

if (Platform.OS === 'ios') {
// TODO: move to native module
// on iOS, we trigger an explicit 'destroy' to clean up the underlying THEOplayer
const command = (UIManager as { [index: string]: any })['THEOplayerRCTView'].Commands.destroy;
UIManager.dispatchViewManagerCommand(findNodeHandle(this._root.current), command, []);
}
this._facade.clearEventListeners();
}

Expand Down
6 changes: 6 additions & 0 deletions src/internal/adapter/THEOplayerAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,12 @@ export class THEOplayerAdapter extends DefaultEventDispatcher<PlayerEventMap> im
}
}

destroy(): void {
if (Platform.OS === 'ios') {
NativePlayerModule.destroyPlayer(this._view.nativeHandle);
}
}

public get version(): PlayerVersion {
return this._playerVersion;
}
Expand Down
Loading