feat: add back async version

This commit is contained in:
Kyle Fang
2020-10-07 10:40:44 +08:00
parent a71b6da272
commit 3c72b43423
6 changed files with 37 additions and 3 deletions

View File

@@ -28,4 +28,13 @@ public class VideoCacheModule extends ReactContextBaseJavaModule {
}
return this.proxy.getProxyUrl(url);
}
@ReactMethod
public void convertAsync(String url, Promise promise) {
if (this.proxy == null) {
this.proxy = new HttpProxyCacheServer(this.reactContext);
}
promise.resolve(this.proxy.getProxyUrl(url));
}
}

View File

@@ -1,16 +1,22 @@
import React, { Component } from "react";
import React, {Component, useEffect, useState} from "react";
import { StyleSheet, Text, View } from "react-native";
import convertToCache from "react-native-video-cache";
import convertToCache, {convertAsync} from "react-native-video-cache";
export default function App() {
const url =
"https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-720p.mp4";
const [asyncVersion, setAsyncVersion] = useState();
useEffect(() => {
convertAsync(url).then(setAsyncVersion)
}, [])
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>
<Text style={styles.welcome}>Async Proxy URL</Text>
<Text style={styles.instructions}>{asyncVersion}</Text>
</View>
);
}

View File

@@ -4720,7 +4720,7 @@ react-is@^16.8.1, react-is@^16.8.3, react-is@^16.8.4:
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
"react-native-video-cache@file:..":
version "1.0.0"
version "2.0.2"
react-native@0.59:
version "0.59.10"

View File

@@ -6,3 +6,6 @@ export default (url) => {
}
return NativeModules.VideoCache.convert(url)
};
export const convertAsync = VideoCache.convertAsync;

View File

@@ -1 +1,2 @@
export default (url) => url;
export const convertAsync = Promise.resolve;

View File

@@ -17,4 +17,19 @@ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(convert:(NSString *)url)
return [KTVHTTPCache proxyURLWithOriginalURL:[NSURL URLWithString:url]].absoluteString;
}
RCT_EXPORT_METHOD(convertAsync:(NSString *)url
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
if (!KTVHTTPCache.proxyIsRunning) {
NSError *error;
[KTVHTTPCache proxyStart:&error];
if (error) {
reject(@"init.error", @"failed to start proxy server", error);
return;
}
}
resolve([KTVHTTPCache proxyURLWithOriginalURL:[NSURL URLWithString:url]].absoluteString);
}
@end