bind method

Future<void> bind(
  1. String uid
)

Implementation

Future<void> bind(String uid) async {
  if (_bound) {
    warn("Already bound to user service");
    return;
  }

  _bound = true;

  verbose("Binding user service for $uid");
  try {
    try {
      await FirebaseAnalytics.instance.setUserId(
          id: uid, callOptions: AnalyticsCallOptions(global: true));
      verbose("Bound Analytics");
    } catch (e, es) {
      warn("Analytics is not supported on this platform");
    }

    PrecisionStopwatch p = PrecisionStopwatch.start();
    await [
      _ensureUser(uid).catchError((e, es) {
        warn("Failed to ensure user");
        warn(e);
        warn(es);
      }),
      _ensureUserPrivate(uid).catchError((e, es) {
        warn("Failed to ensure user private");
        warn(e);
        warn(es);
      }),
    ].wait();
    verbose("Got all init data in ${p.getMilliseconds()}");
    List<StreamSubscription> subs = [
      Arcane.app.users.userRef(uid).snapshots().listen((event) {
        lastUser = event.data() ?? {};
        Arcane.app.users.onUserUpdate?.call(lastUser);
      }),
      Arcane.app.users.userCapabilitiesRef(uid).snapshots().listen((event) {
        lastUserCapabilities = event.data() ?? {};
        Arcane.app.users.onUserCapabilitiesUpdate?.call(lastUserCapabilities);
      }),
      Arcane.app.users.userPrivateRef(uid).snapshots().listen((event) {
        lastUserPrivate = event.data() ?? {};
        Arcane.app.users.onUserPrivateUpdate?.call(lastUserPrivate);
      })
    ];
    closable.addAll(subs.map((e) => () => e.cancel()));
  } catch (e, es) {
    Arcane.logger.handle(e, es, "Failed to bind User service!");
  }

  success("Bound User Service for $uid");
}