Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ReactUtils.m 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright @ 2019-present 8x8, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <React/RCTAssert.h>
  17. #import "ReactUtils.h"
  18. #pragma mark - Utility functions
  19. /**
  20. * Merges 2 sets of props into a single one.
  21. */
  22. NSMutableDictionary* mergeProps(NSDictionary *a, NSDictionary *b) {
  23. if (a == nil) {
  24. return [NSMutableDictionary dictionaryWithDictionary:b == nil ? @{} : b];
  25. }
  26. if (b == nil) {
  27. return [NSMutableDictionary dictionaryWithDictionary:a];
  28. }
  29. // Both have values, let's merge them, the strategy is to take the value from a first,
  30. // then override it with the one from b. If the value is a dictionary, merge them
  31. // recursively. Same goes for arrays.
  32. NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:a];
  33. for (NSString *key in b) {
  34. id value = b[key];
  35. id aValue = result[key];
  36. if (aValue == nil) {
  37. result[key] = value;
  38. continue;
  39. }
  40. if ([value isKindOfClass:NSArray.class]) {
  41. result[key] = [aValue arrayByAddingObjectsFromArray:value];
  42. } else if ([value isKindOfClass:NSDictionary.class]) {
  43. result[key] = mergeProps(aValue, value);
  44. } else {
  45. result[key] = value;
  46. }
  47. }
  48. return result;
  49. }
  50. /**
  51. * A `RCTFatalHandler` implementation which swallows JavaScript errors. In the
  52. * Release configuration, React Native will (intentionally) raise an unhandled
  53. * `NSException` for an unhandled JavaScript error. This will effectively kill
  54. * the application. `_RCTFatal` is suitable to be in accord with the Web i.e.
  55. * not kill the application.
  56. */
  57. RCTFatalHandler _RCTFatal = ^(NSError *error) {
  58. id jsStackTrace = error.userInfo[RCTJSStackTraceKey];
  59. @try {
  60. NSString *name
  61. = [NSString stringWithFormat:@"%@: %@",
  62. RCTFatalExceptionName,
  63. error.localizedDescription];
  64. NSString *message
  65. = RCTFormatError(error.localizedDescription, jsStackTrace, 75);
  66. [NSException raise:name format:@"%@", message];
  67. } @catch (NSException *e) {
  68. if (!jsStackTrace) {
  69. @throw;
  70. }
  71. }
  72. };
  73. /**
  74. * Helper function to register a fatal error handler for React. Our handler
  75. * won't kill the process, it will swallow JS errors and print stack traces
  76. * instead.
  77. */
  78. void registerReactFatalErrorHandler() {
  79. #if !DEBUG
  80. // In the Release configuration, React Native will (intentionally) raise an
  81. // unhandled `NSException` for an unhandled JavaScript error. This will
  82. // effectively kill the application. In accord with the Web, do not kill the
  83. // application.
  84. if (!RCTGetFatalHandler()) {
  85. RCTSetFatalHandler(_RCTFatal);
  86. }
  87. #endif
  88. }