From 08ac4a5e21eabbd9ab658795631776664f23e4bd Mon Sep 17 00:00:00 2001 From: duguyihou Date: Tue, 21 Nov 2023 14:40:06 +1100 Subject: [PATCH] 48 prefetch and clean cache methods in promise ways (#49) * feat(iOS): wip * feat(iOS): add prefetch and clean cache methods * refactor(iOS): prefetch and clean cache methods in promise way * build: upgrade yarn and packages * build: yarn version --- .../project.pbxproj | 15 ++++++++++ example/package.json | 14 ++++----- ios/TurboImage/TurboImageViewManager.m | 13 +++++--- ios/TurboImage/TurboImageViewManager.swift | 22 +++++++++----- package.json | 30 +++++++++---------- src/TurboImage.ios.tsx | 16 +++++++--- 6 files changed, 72 insertions(+), 38 deletions(-) diff --git a/example/ios/TurboImageExample.xcodeproj/project.pbxproj b/example/ios/TurboImageExample.xcodeproj/project.pbxproj index 94d97f4..12c9eca 100644 --- a/example/ios/TurboImageExample.xcodeproj/project.pbxproj +++ b/example/ios/TurboImageExample.xcodeproj/project.pbxproj @@ -582,6 +582,7 @@ GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", "$(inherited)", + _LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION, ); GCC_SYMBOLS_PRIVATE_EXTERN = NO; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; @@ -609,6 +610,11 @@ "-DFOLLY_MOBILE=1", "-DFOLLY_USE_LIBCPP=1", ); + OTHER_LDFLAGS = ( + "$(inherited)", + "-Wl", + "-ld_classic", + ); REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native"; SDKROOT = iphoneos; }; @@ -650,6 +656,10 @@ "EXCLUDED_ARCHS[sdk=iphonesimulator*]" = i386; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_NO_COMMON_BLOCKS = YES; + GCC_PREPROCESSOR_DEFINITIONS = ( + "$(inherited)", + _LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION, + ); GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; @@ -674,6 +684,11 @@ "-DFOLLY_MOBILE=1", "-DFOLLY_USE_LIBCPP=1", ); + OTHER_LDFLAGS = ( + "$(inherited)", + "-Wl", + "-ld_classic", + ); REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native"; SDKROOT = iphoneos; VALIDATE_PRODUCT = YES; diff --git a/example/package.json b/example/package.json index ae20f10..0b16eb0 100644 --- a/example/package.json +++ b/example/package.json @@ -10,18 +10,18 @@ }, "dependencies": { "react": "18.2.0", - "react-native": "0.72.3" + "react-native": "0.72.7" }, "devDependencies": { "@babel/core": "^7.20.0", "@babel/preset-env": "^7.20.0", "@babel/runtime": "^7.20.0", - "@react-native/eslint-config": "^0.72.2", - "@react-native/metro-config": "^0.72.9", - "metro-react-native-babel-preset": "0.76.7", - "babel-plugin-module-resolver": "^5.0.0" + "@react-native/eslint-config": "^0.74.0", + "@react-native/metro-config": "^0.73.2", + "babel-plugin-module-resolver": "^5.0.0", + "metro-react-native-babel-preset": "0.77.0" }, "engines": { - "node": ">=16" + "node": ">=18" } -} \ No newline at end of file +} diff --git a/ios/TurboImage/TurboImageViewManager.m b/ios/TurboImage/TurboImageViewManager.m index 699e8ba..1e0cacd 100644 --- a/ios/TurboImage/TurboImageViewManager.m +++ b/ios/TurboImage/TurboImageViewManager.m @@ -26,13 +26,18 @@ @interface RCT_EXTERN_MODULE(TurboImageViewManager, RCTViewManager) #pragma mark - methods -RCT_EXTERN_METHOD(prefetch:(NSArray *)urlArray) +RCT_EXTERN_METHOD(prefetch:(NSArray *)urlArray + resolve:(RCTPromiseResolveBlock)resolve + reject:(RCTPromiseRejectBlock)reject) -RCT_EXTERN_METHOD(clearAllCache) +RCT_EXTERN_METHOD(clearAllCache:(RCTPromiseResolveBlock)resolve + reject:(RCTPromiseRejectBlock)reject) -RCT_EXTERN_METHOD(clearMemoryCache) +RCT_EXTERN_METHOD(clearMemoryCache:(RCTPromiseResolveBlock)resolve + reject:(RCTPromiseRejectBlock)reject) -RCT_EXTERN_METHOD(clearDiskCache) +RCT_EXTERN_METHOD(clearDiskCache:(RCTPromiseResolveBlock)resolve + reject:(RCTPromiseRejectBlock)reject) @end diff --git a/ios/TurboImage/TurboImageViewManager.swift b/ios/TurboImage/TurboImageViewManager.swift index 00633cb..08077d9 100644 --- a/ios/TurboImage/TurboImageViewManager.swift +++ b/ios/TurboImage/TurboImageViewManager.swift @@ -13,25 +13,31 @@ class TurboImageViewManager: RCTViewManager { } extension TurboImageViewManager { + @objc - func prefetch(_ urlArray: [String]) { + func prefetch(_ urlArray: [String], + resolve: @escaping RCTPromiseResolveBlock, + reject: @escaping RCTPromiseRejectBlock) { let urls = urlArray.map { url in URL(string: url )}.compactMap{ $0 } let prefetcher = ImagePrefetcher(urls: urls) - prefetcher.start() + resolve(prefetcher.start()) } @objc - func clearAllCache() { - ImageCache.default.clearCache() + func clearAllCache(_ resolve: @escaping RCTPromiseResolveBlock, + reject: @escaping RCTPromiseRejectBlock) { + resolve(ImageCache.default.clearCache()) } @objc - func clearMemoryCache() { - ImageCache.default.clearMemoryCache() + func clearMemoryCache(_ resolve: @escaping RCTPromiseResolveBlock, + reject: @escaping RCTPromiseRejectBlock) { + resolve(ImageCache.default.clearMemoryCache()) } @objc - func clearDiskCache() { - ImageCache.default.clearDiskCache() + func clearDiskCache(_ resolve: @escaping RCTPromiseResolveBlock, + reject: @escaping RCTPromiseRejectBlock) { + resolve(ImageCache.default.clearDiskCache()) } } diff --git a/package.json b/package.json index 71af87d..3b6178f 100644 --- a/package.json +++ b/package.json @@ -55,25 +55,25 @@ "registry": "https://registry.npmjs.org/" }, "devDependencies": { - "@commitlint/config-conventional": "^17.0.2", + "@commitlint/config-conventional": "^18.4.2", "@evilmartians/lefthook": "^1.2.2", "@react-native-community/eslint-config": "^3.0.2", - "@release-it/conventional-changelog": "^5.0.0", - "@types/jest": "^28.1.2", - "@types/react": "~17.0.21", - "@types/react-native": "0.70.0", - "commitlint": "^17.0.2", + "@release-it/conventional-changelog": "^8.0.1", + "@types/jest": "^29.5.8", + "@types/react": "18.2.37", + "@types/react-native": "0.72.6", + "commitlint": "^18.4.2", "del-cli": "^5.0.0", "eslint": "^8.4.1", - "eslint-config-prettier": "^8.5.0", - "eslint-plugin-prettier": "^4.0.0", - "jest": "^28.1.1", + "eslint-config-prettier": "^9.0.0", + "eslint-plugin-prettier": "^5.0.1", + "jest": "^29.7.0", "pod-install": "^0.1.0", - "prettier": "^2.0.5", + "prettier": "^3.1.0", "react": "18.2.0", - "react-native": "0.72.3", - "react-native-builder-bob": "^0.20.0", - "release-it": "^15.0.0", + "react-native": "0.72.7", + "react-native-builder-bob": "^0.23.1", + "release-it": "^17.0.0", "turbo": "^1.10.7", "typescript": "^5.0.2" }, @@ -85,9 +85,9 @@ "react-native": "*" }, "engines": { - "node": ">= 16.0.0" + "node": ">= 18.0.0" }, - "packageManager": "^yarn@1.22.15", + "packageManager": "yarn@1.22.21", "jest": { "preset": "react-native", "modulePathIgnorePatterns": [ diff --git a/src/TurboImage.ios.tsx b/src/TurboImage.ios.tsx index 5ff92a9..780e403 100644 --- a/src/TurboImage.ios.tsx +++ b/src/TurboImage.ios.tsx @@ -12,9 +12,17 @@ const TurboImage = (props: TurboImageProps) => { return ; }; -TurboImage.prefetch = (urls: string[]) => TurboImageViewManager.prefetch(urls); -TurboImage.clearAllCache = () => TurboImageViewManager.clearAllCache(); -TurboImage.clearMemoryCache = () => TurboImageViewManager.clearMemoryCache(); -TurboImage.clearDiskCache = () => TurboImageViewManager.clearDiskCache(); +TurboImage.prefetch = async (urls: string[]) => { + return await TurboImageViewManager.prefetch(urls); +}; +TurboImage.clearAllCache = async () => { + return await TurboImageViewManager.clearAllCache(); +}; +TurboImage.clearMemoryCache = async () => { + return await TurboImageViewManager.clearMemoryCache(); +}; +TurboImage.clearDiskCache = async () => { + return await TurboImageViewManager.clearDiskCache(); +}; export default TurboImage;