callSync<T> static method

T callSync<T>(
  1. T call(), {
  2. String methodName = '',
})

Calls a bridge function synchronously, with the same logging and slow-call detection as the native runtime.

Implementation

static T callSync<T>(T Function() call, {String methodName = ''}) {
  final cfg = NitroConfig.instance;
  final effective = cfg.effectiveLogLevel;
  final traceTimeline = cfg.timelineTracingEnabled;

  if (effective != NitroLogLevel.verbose && !traceTimeline && cfg.slowCallThresholdUs == 0) {
    if (effective == NitroLogLevel.none) return call();
    try {
      return call();
    } catch (e, st) {
      _log(
        NitroLogLevel.error,
        methodName.isEmpty ? 'callSync' : 'callSync($methodName)',
        'threw: $e',
        e,
        st,
      );
      rethrow;
    }
  }

  final tag = methodName.isEmpty ? 'callSync' : 'callSync($methodName)';
  final sw = (effective == NitroLogLevel.verbose || cfg.slowCallThresholdUs > 0) ? (Stopwatch()..start()) : null;

  _log(NitroLogLevel.verbose, tag, 'calling');

  if (traceTimeline) developer.Timeline.startSync(_timelineLabel(tag));
  try {
    final result = call();
    _logCallTiming(sw, tag);
    return result;
  } catch (e, st) {
    _log(NitroLogLevel.error, tag, 'threw: $e', e, st);
    rethrow;
  } finally {
    if (traceTimeline) developer.Timeline.finishSync();
  }
}