"Fossies" - the Fresh Open Source Software Archive

Member "jitsi-meet-7312/ios/sdk/src/ReactUtils.m" (1 Jun 2023, 5090 Bytes) of package /linux/misc/jitsi-meet-7312.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Matlab source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    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 
   17 #import <React/RCTAssert.h>
   18 #import <React/RCTLog.h>
   19 
   20 #import "LogUtils.h"
   21 #import "ReactUtils.h"
   22 
   23 #pragma mark - Utility functions
   24 
   25 /**
   26  * Merges 2 sets of props into a single one.
   27  */
   28 NSMutableDictionary* mergeProps(NSDictionary *a, NSDictionary *b) {
   29     if (a == nil) {
   30         return [NSMutableDictionary dictionaryWithDictionary:b == nil ? @{} : b];
   31     }
   32 
   33     if (b == nil) {
   34         return [NSMutableDictionary dictionaryWithDictionary:a];
   35     }
   36 
   37     // Both have values, let's merge them, the strategy is to take the value from a first,
   38     // then override it with the one from b. If the value is a dictionary, merge them
   39     // recursively. Same goes for arrays.
   40     NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:a];
   41 
   42     for (NSString *key in b) {
   43         id value = b[key];
   44         id aValue = result[key];
   45 
   46         if (aValue == nil) {
   47             result[key] = value;
   48             continue;
   49         }
   50 
   51         if ([value isKindOfClass:NSArray.class]) {
   52             result[key] = [aValue arrayByAddingObjectsFromArray:value];
   53         } else if ([value isKindOfClass:NSDictionary.class]) {
   54             result[key] = mergeProps(aValue, value);
   55         } else {
   56             result[key] = value;
   57         }
   58     }
   59 
   60     return result;
   61 }
   62 
   63 /**
   64  * A `RCTFatalHandler` implementation which swallows JavaScript errors. In the
   65  * Release configuration, React Native will (intentionally) raise an unhandled
   66  * `NSException` for an unhandled JavaScript error. This will effectively kill
   67  * the application. `_RCTFatal` is suitable to be in accord with the Web i.e.
   68  * not kill the application.
   69  */
   70 RCTFatalHandler _RCTFatal = ^(NSError *error) {
   71     id jsStackTrace = error.userInfo[RCTJSStackTraceKey];
   72     NSString *name
   73         = [NSString stringWithFormat:@"%@: %@", RCTFatalExceptionName, error.localizedDescription];
   74     NSString *message
   75         = RCTFormatError(error.localizedDescription, jsStackTrace, -1);
   76     DDLogError(@"FATAL ERROR: %@\n%@", name, message);
   77 };
   78 
   79 /**
   80  * Helper function to register a fatal error handler for React. Our handler
   81  * won't kill the process, it will swallow JS errors and print stack traces
   82  * instead.
   83  */
   84 void registerReactFatalErrorHandler() {
   85 #if !DEBUG
   86     // In the Release configuration, React Native will (intentionally) raise an
   87     // unhandled `NSException` for an unhandled JavaScript error. This will
   88     // effectively kill the application. In accord with the Web, do not kill the
   89     // application.
   90     if (!RCTGetFatalHandler()) {
   91         RCTSetFatalHandler(_RCTFatal);
   92     }
   93 #endif
   94 }
   95 
   96 /**
   97  * A `RTCLogFunction` implementation which uses CocoaLumberjack.
   98  */
   99 RCTLogFunction _RCTLog
  100     = ^(RCTLogLevel level, __unused RCTLogSource source, NSString *fileName, NSNumber *lineNumber, NSString *message)
  101 {
  102     // Convert RN log levels into Lumberjack's log flags.
  103     //
  104     DDLogFlag logFlag;
  105     switch (level) {
  106         case RCTLogLevelTrace:
  107             logFlag = DDLogFlagDebug;
  108             break;
  109         case RCTLogLevelInfo:
  110             logFlag = DDLogFlagInfo;
  111             break;
  112         case RCTLogLevelWarning:
  113             logFlag = DDLogFlagWarning;
  114             break;
  115         case RCTLogLevelError:
  116             logFlag = DDLogFlagError;
  117             break;
  118         case RCTLogLevelFatal:
  119             logFlag = DDLogFlagError;
  120             break;
  121         default:
  122             // Just in case more are added in the future.
  123             logFlag = DDLogFlagInfo;
  124             break;
  125     }
  126 
  127     // Build the message object we want to log.
  128     //
  129     DDLogMessage *logMessage
  130         = [[DDLogMessage alloc] initWithMessage:message
  131                                           level:LOG_LEVEL_DEF
  132                                            flag:logFlag
  133                                         context:0
  134                                            file:fileName
  135                                        function:nil
  136                                            line:[lineNumber integerValue]
  137                                             tag:nil
  138                                         options:0
  139                                       timestamp:nil];
  140 
  141     // Log the message. Errors are logged synchronously, and other async, as the Lumberjack defaults.
  142     //
  143     [DDLog log:logFlag != DDLogFlagError
  144        message:logMessage];
  145 };
  146 
  147 /**
  148  * Helper function which registers a React Native log handler.
  149  */
  150 void registerReactLogHandler() {
  151     RCTSetLogFunction(_RCTLog);
  152     RCTSetLogThreshold(RCTLogLevelInfo);
  153 }