initBridge function

int initBridge()

Init bridge

Implementation

int initBridge() {
  if (kProfileMode) {
    PerformanceTiming.instance().mark(PERF_BRIDGE_REGISTER_DART_METHOD_START);
  }

  // Register methods first to share ptrs for bridge polyfill.
  registerDartMethodsToCpp();

  // Setup binding bridge.
  BindingBridge.setup();

  if (kProfileMode) {
    PerformanceTiming.instance().mark(PERF_BRIDGE_REGISTER_DART_METHOD_END);
  }

  int contextId = -1;

  // We should schedule addPersistentFrameCallback() to the next frame because of initBridge()
  // will be called from persistent frame callbacks and cause infinity loops.
  if (_firstView) {
    Future.microtask(() {
      // Port flutter's frame callback into bridge.
      SchedulerBinding.instance!.addPersistentFrameCallback((_) {
        flushUICommand();
        flushUICommandCallback();
      });
    });
  }

  if (_firstView) {
    initJSPagePool(kKrakenJSPagePoolSize);
    _firstView = false;
    contextId = 0;
  } else {
    contextId = allocateNewPage();
    if (contextId == -1) {
      throw Exception('Can\' allocate new kraken bridge: bridge count had reach the maximum size.');
    }
  }

  return contextId;
}