mutateState method

Future<Map<String, dynamic>> mutateState({
  1. required String atSign,
  2. required void mutate(
    1. Map<String, dynamic> state
    ),
})

Implementation

Future<Map<String, dynamic>> mutateState({
  required String atSign,
  required void Function(Map<String, dynamic> state) mutate,
}) {
  return _runExclusive(() async {
    final normalized = normalizeAtSign(atSign);
    final current = await loadState(normalized);
    final next = <String, dynamic>{
      ...current,
      'atSign': normalized,
      'otpHistory': _normalizeOtpHistory(current['otpHistory']),
      'autoApprovalServices': _normalizeAutoApprovalServices(
        current['autoApprovalServices'],
      ),
      'history': _normalizeHistory(current['history']),
    };
    mutate(next);
    next['otpHistory'] = _normalizeOtpHistory(next['otpHistory']);
    next['autoApprovalServices'] = _normalizeAutoApprovalServices(
      next['autoApprovalServices'],
    );
    next['history'] = _normalizeHistory(next['history']);

    final file = _fileForAtSign(normalized);
    await file.writeAsString(
      const JsonEncoder.withIndent('  ').convert(next),
    );
    return next;
  });
}