RCTJavaScriptLoader.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <UIKit/UIKit.h>
  8. #import <React/RCTDefines.h>
  9. extern NSString *const RCTJavaScriptLoaderErrorDomain;
  10. NS_ENUM(NSInteger){
  11. RCTJavaScriptLoaderErrorNoScriptURL = 1,
  12. RCTJavaScriptLoaderErrorFailedOpeningFile = 2,
  13. RCTJavaScriptLoaderErrorFailedReadingFile = 3,
  14. RCTJavaScriptLoaderErrorFailedStatingFile = 3,
  15. RCTJavaScriptLoaderErrorURLLoadFailed = 3,
  16. RCTJavaScriptLoaderErrorBCVersion = 4,
  17. RCTJavaScriptLoaderErrorBCNotSupported = 4,
  18. RCTJavaScriptLoaderErrorCannotBeLoadedSynchronously = 1000,
  19. };
  20. NS_ENUM(NSInteger){
  21. RCTSourceFilesChangedCountNotBuiltByBundler = -2,
  22. RCTSourceFilesChangedCountRebuiltFromScratch = -1,
  23. };
  24. @interface RCTLoadingProgress : NSObject
  25. @property (nonatomic, copy) NSString *status;
  26. @property (strong, nonatomic) NSNumber *done;
  27. @property (strong, nonatomic) NSNumber *total;
  28. @end
  29. @interface RCTSource : NSObject
  30. /**
  31. * URL of the source object.
  32. */
  33. @property (strong, nonatomic, readonly) NSURL *url;
  34. /**
  35. * JS source (or simply the binary header in the case of a RAM bundle).
  36. */
  37. @property (strong, nonatomic, readonly) NSData *data;
  38. /**
  39. * Length of the entire JS bundle. Note that self.length != self.data.length in the case of certain bundle formats. For
  40. * instance, when using RAM bundles:
  41. *
  42. * - self.data will point to the bundle header
  43. * - self.data.length is the length of the bundle header, i.e. sizeof(facebook::react::BundleHeader)
  44. * - self.length is the length of the entire bundle file (header + contents)
  45. */
  46. @property (nonatomic, readonly) NSUInteger length;
  47. /**
  48. * Returns number of files changed when building this bundle:
  49. *
  50. * - RCTSourceFilesChangedCountNotBuiltByBundler if the source wasn't built by the bundler (e.g. read from disk)
  51. * - RCTSourceFilesChangedCountRebuiltFromScratch if the source was rebuilt from scratch by the bundler
  52. * - Otherwise, the number of files changed when incrementally rebuilding the source
  53. */
  54. @property (nonatomic, readonly) NSInteger filesChangedCount;
  55. @end
  56. typedef void (^RCTSourceLoadProgressBlock)(RCTLoadingProgress *progressData);
  57. typedef void (^RCTSourceLoadBlock)(NSError *error, RCTSource *source);
  58. @interface RCTJavaScriptLoader : NSObject
  59. + (void)loadBundleAtURL:(NSURL *)scriptURL
  60. onProgress:(RCTSourceLoadProgressBlock)onProgress
  61. onComplete:(RCTSourceLoadBlock)onComplete;
  62. /**
  63. * @experimental
  64. * Attempts to synchronously load the script at the given URL. The following two conditions must be met:
  65. * 1. It must be a file URL.
  66. * 2. It must not point to a text/javascript file.
  67. * If the URL does not meet those conditions, this method will return nil and supply an error with the domain
  68. * RCTJavaScriptLoaderErrorDomain and the code RCTJavaScriptLoaderErrorCannotBeLoadedSynchronously.
  69. */
  70. + (NSData *)attemptSynchronousLoadOfBundleAtURL:(NSURL *)scriptURL
  71. runtimeBCVersion:(int32_t)runtimeBCVersion
  72. sourceLength:(int64_t *)sourceLength
  73. error:(NSError **)error;
  74. @end