From 3c72b43423c4b067f9f7bea84f01176f57bba555 Mon Sep 17 00:00:00 2001 From: Kyle Fang Date: Wed, 7 Oct 2020 10:40:44 +0800 Subject: [PATCH] feat: add back async version --- .../reactnative/videocache/VideoCacheModule.java | 9 +++++++++ example/App.js | 10 ++++++++-- example/yarn.lock | 2 +- index.js | 3 +++ index.web.js | 1 + ios/VideoCache.m | 15 +++++++++++++++ 6 files changed, 37 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/com/reactnative/videocache/VideoCacheModule.java b/android/src/main/java/com/reactnative/videocache/VideoCacheModule.java index 15a59a8..665193d 100644 --- a/android/src/main/java/com/reactnative/videocache/VideoCacheModule.java +++ b/android/src/main/java/com/reactnative/videocache/VideoCacheModule.java @@ -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)); + } + } diff --git a/example/App.js b/example/App.js index bdeac2b..b5f64de 100644 --- a/example/App.js +++ b/example/App.js @@ -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 ( ☆Original URL☆ {url} ☆Proxy URL for Video Component☆ {convertToCache(url)} + ☆Async Proxy URL☆ + {asyncVersion} ); } diff --git a/example/yarn.lock b/example/yarn.lock index 92a432b..36d48e8 100644 --- a/example/yarn.lock +++ b/example/yarn.lock @@ -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" diff --git a/index.js b/index.js index dcced8d..4e92512 100644 --- a/index.js +++ b/index.js @@ -6,3 +6,6 @@ export default (url) => { } return NativeModules.VideoCache.convert(url) }; + +export const convertAsync = VideoCache.convertAsync; + diff --git a/index.web.js b/index.web.js index d195e6a..bfbc90b 100644 --- a/index.web.js +++ b/index.web.js @@ -1 +1,2 @@ export default (url) => url; +export const convertAsync = Promise.resolve; diff --git a/ios/VideoCache.m b/ios/VideoCache.m index c0417b9..ffc7548 100644 --- a/ios/VideoCache.m +++ b/ios/VideoCache.m @@ -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