emitProxyState method

void emitProxyState(
  1. ProxyState state
)

供 HelperHttpClient 调用,取代直接访问 controller。

连接成功(started)时顺带后台刷新代理模式——clash mode 的数据来源与推送 都内聚在本类,HelperHttpClient 只管 helper service 控制与状态转发。

Implementation

void emitProxyState(ProxyState state) {
  _lastProxyState = state;
  _controller.add(state);
  if (state == ProxyState.started) {
    unawaited(_refreshClashMode());
    _startGroupRefresh();
    _startLogSubscribe();
    _startHelperWatch();
    // 系统代理模式:sing-box 已起,设系统代理
    if (CsSettingsStorage().proxyMode == ProxyMode.systemProxy) {
      unawaited(SystemProxyService().enable());
    }
  } else {
    // 任何离开 started 的状态(启动中/停止中/已停止)都终止存活轮询
    _cancelHelperWatch();
    // is ProxyStopped 覆盖带 errMessage 的异常停止(启动失败、内核崩溃、
    // 存活轮询发现的 helper 崩溃)——这些场景同样需要停副作用流、清系统代理
    if (state is ProxyStopped) {
      _cancelGroupRefresh();
      _cancelLogSubscribe();
      // 只要标志位说系统代理还开着就清(覆盖断开/切换重启/崩溃后停止)
      if (CsSettingsStorage().systemProxyActive) {
        unawaited(SystemProxyService().disable());
      }
    }
  }
}