get method

  1. @override
FutureOr<RxUserImpl?> get(
  1. UserId id
)
override

Returns an User by the provided id.

Implementation

@override
FutureOr<RxUserImpl?> get(UserId id) {
  // Return the stored user instance, if it exists.
  final RxUserImpl? user = users[id];
  if (user != null) {
    return user;
  }

  // If [user] doesn't exist, we should lock the [mutex] to avoid remote
  // double invoking.
  Mutex? mutex = _locks[id];
  if (mutex == null) {
    mutex = Mutex();
    _locks[id] = mutex;
  }

  return mutex.protect(() async {
    RxUserImpl? user = users[id];

    if (user == null) {
      final DtoUser? stored = await _userLocal.read(id);
      if (stored != null) {
        final RxUserImpl rxUser = RxUserImpl(this, _userLocal, stored);
        return users[id] = rxUser;
      } else {
        final response = (await _graphQlProvider.getUser(id)).user;
        if (response != null) {
          final DtoUser dto = response.toDto();
          put(dto);

          final RxUserImpl rxUser = RxUserImpl(this, _userLocal, dto);
          return users[id] = rxUser;
        }
      }
    }

    return user;
  });
}