myLink method

Future<ReferralLink> myLink({
  1. required String programId,
})

The current user's code and share links, returned at once from the phone after the first time (no network, no waiting) and refreshed in the background at most once a minute. Use it for share buttons; myStats has the live counts. Errors as myStats (referrer_not_found until the user has a link).

Implementation

Future<ReferralLink> myLink({required String programId}) async {
  _requireId(programId, 'program_id');
  final saved = await _savedLinks.saved(programId);
  final link = saved.link;
  if (link != null &&
      await _client.core.isFeatureEnabled(ClikgateFeatures.referrals)) {
    if (_client.claimRefresh('$programId|${saved.token}')) {
      // Best effort: the answer updates the saved link, errors are dropped
      unawaited(_stats(programId, saved.token).then((_) {}, onError: (_) {}));
    }
    return ReferralLink.fromJson(link);
  }
  return ReferralLink.fromJson(await _stats(programId, saved.token));
}