import 'backend.dart'; /// 내 초대 코드 — 공유 링크 `?ref=` 에 붙는다(정책 원장 B-69 추천). /// /// 세션당 한 번 `fnInviteCodeGet` 을 부른다(없으면 서버가 만든다). 공유는 이 값을 기다리지 /// 않는다 — 아직 없거나 실패했으면 `?ref=` 없이 나간다. 계정이 바뀌면 다시 받는다. class InviteCode { InviteCode._(); /// 받아 둔 코드. 없으면 null. static String? current; static String? _uid; static Future? _fetch; static Future ensure(Backend backend) { final uid = backend.uid; if (uid == null) return Future.value(); if (uid != _uid) { _uid = uid; current = null; _fetch = null; } return _fetch ??= _load(backend, uid); } static Future _load(Backend backend, String uid) async { try { final code = (await backend.call('fnInviteCodeGet'))['code']; if (_uid == uid && code is String && code.isNotEmpty) current = code; } catch (_) { if (_uid == uid) _fetch = null; // 다음 기회에 다시 받는다. } } /// 테스트가 회차마다 처음 상태로 돌린다. static void reset() { current = null; _uid = null; _fetch = null; } }