refactor: using RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD for sync method call

This commit is contained in:
Kyle Fang
2020-09-27 22:22:07 +08:00
parent 59f8ad5705
commit d4ce3adbf4
6 changed files with 44 additions and 66 deletions

View File

@@ -7,7 +7,6 @@ Use following libraries to do the heavy lifting.
- iOS: https://github.com/ChangbaDevs/KTVHTTPCache
- Android: https://github.com/danikula/AndroidVideoCache
## Getting started
`$ yarn add react-native-video-cache`
@@ -17,10 +16,9 @@ Use following libraries to do the heavy lifting.
`$ react-native link react-native-video-cache`
## Usage
```javascript
import convertToProxyURL from 'react-native-video-cache';
...
const localProxiedURL = await convertToProxyURL(originalURL);
...
<Video source={{uri: localProxiedURL}} />
<Video source={{uri: convertToProxyURL(originalURL)}} />
```

View File

@@ -21,13 +21,11 @@ public class VideoCacheModule extends ReactContextBaseJavaModule {
return "VideoCache";
}
@ReactMethod
public void convert(
String url,
Promise promise) {
@ReactMethod(isBlockingSynchronousMethod = true)
public void convert(String url) {
if (this.proxy == null) {
this.proxy = new HttpProxyCacheServer(this.reactContext);
}
promise.resolve(this.proxy.getProxyUrl(url));
return this.proxy.getProxyUrl(url);
}
}

View File

@@ -1,57 +1,36 @@
/**
* Sample React Native App
*
* adapted from App.js generated by the following command:
*
* react-native init example
*
* https://github.com/facebook/react-native
*/
import React, { Component } from "react";
import { StyleSheet, Text, View } from "react-native";
import convertToCache from "react-native-video-cache";
import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View } from 'react-native';
import convertToCache from 'react-native-video-cache';
export default class App extends Component<{}> {
state = {
status: 'starting',
message: '--'
};
componentDidMount() {
convertToCache('https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-720p.mp4').then((message) => {
this.setState({
status: 'native callback received',
message
});
});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>VideoCache example</Text>
<Text style={styles.instructions}>STATUS: {this.state.status}</Text>
<Text style={styles.welcome}>NATIVE CALLBACK MESSAGE</Text>
<Text style={styles.instructions}>{this.state.message}</Text>
</View>
);
}
export default function App() {
const url =
"https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-720p.mp4";
return (
<View style={styles.container}>
<Text style={styles.welcome}>Original URL</Text>
<Text style={styles.instructions}>{url}</Text>
<Text style={styles.welcome}>Proxy URL for Video Component</Text>
<Text style={styles.instructions}>{convertToCache(url)}</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F5FCFF",
padding: 20
},
welcome: {
fontSize: 20,
textAlign: 'center',
textAlign: "center",
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
textAlign: "center",
color: "#333333",
marginBottom: 5,
},
});

View File

@@ -129,9 +129,9 @@ SPEC CHECKSUMS:
KTVCocoaHTTPServer: df8d7b861e603ff8037e9b2138aca2563a6b768d
KTVHTTPCache: 588c3eb16f6bd1e6fde1e230dabfb7bd4e490a4d
React: 36d0768f9e93be2473b37e7fa64f92c1d5341eef
react-native-video-cache: a9b3bb7c7ad2c33348a35ed5617ac93cf179c9f9
react-native-video-cache: 90b8857673151d2618fc61030f14d755d1e92d4e
yoga: 684513b14b03201579ba3cee20218c9d1298b0cc
PODFILE CHECKSUM: 8cb65c707639127200176815b46fe7b0846ce649
COCOAPODS: 1.8.4
COCOAPODS: 1.9.3

View File

@@ -1,25 +1,20 @@
#import "VideoCache.h"
#import <KTVHTTPCache/KTVHTTPCache.h>
@implementation VideoCache
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(convert:(NSString *)url
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(convert:(NSString *)url)
{
if (!KTVHTTPCache.proxyIsRunning) {
NSError *error;
[KTVHTTPCache proxyStart:&error];
if (error) {
reject(@"init.error", @"failed to start proxy server", error);
return;
if (!KTVHTTPCache.proxyIsRunning) {
NSError *error;
[KTVHTTPCache proxyStart:&error];
if (error) {
return url;
}
}
}
resolve([KTVHTTPCache proxyURLWithOriginalURL:[NSURL URLWithString:url]].absoluteString);
return [KTVHTTPCache proxyURLWithOriginalURL:[NSURL URLWithString:url]].absoluteString;
}
@end

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>