RCTBlobCollector.mm 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "RCTBlobCollector.h"
  8. #import <React/RCTBridge+Private.h>
  9. #import <React/RCTBlobManager.h>
  10. namespace facebook {
  11. namespace react {
  12. RCTBlobCollector::RCTBlobCollector(RCTBlobManager *blobManager, const std::string &blobId)
  13. : blobId_(blobId), blobManager_(blobManager) {}
  14. RCTBlobCollector::~RCTBlobCollector() {
  15. RCTBlobManager *blobManager = blobManager_;
  16. NSString *blobId = [NSString stringWithUTF8String:blobId_.c_str()];
  17. dispatch_async([blobManager_ methodQueue], ^{
  18. [blobManager remove:blobId];
  19. });
  20. }
  21. void RCTBlobCollector::install(RCTBlobManager *blobManager) {
  22. __weak RCTCxxBridge *cxxBridge = (RCTCxxBridge *)blobManager.bridge;
  23. [cxxBridge dispatchBlock:^{
  24. if (!cxxBridge || cxxBridge.runtime == nullptr) {
  25. return;
  26. }
  27. jsi::Runtime &runtime = *(jsi::Runtime *)cxxBridge.runtime;
  28. runtime.global().setProperty(
  29. runtime,
  30. "__blobCollectorProvider",
  31. jsi::Function::createFromHostFunction(
  32. runtime,
  33. jsi::PropNameID::forAscii(runtime, "__blobCollectorProvider"),
  34. 1,
  35. [blobManager](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args, size_t count) {
  36. auto blobId = args[0].asString(rt).utf8(rt);
  37. auto blobCollector = std::make_shared<RCTBlobCollector>(blobManager, blobId);
  38. return jsi::Object::createFromHostObject(rt, blobCollector);
  39. }
  40. )
  41. );
  42. } queue:RCTJSThread];
  43. }
  44. } // namespace react
  45. } // namespace facebook