27 lines
1.1 KiB
Dart
27 lines
1.1 KiB
Dart
import 'package:friendly_map/modules/storage/storage.dart';
|
|
|
|
/// 같은 줄에 두 변경이 부딪칠 때 누구를 따를지 정한다.
|
|
///
|
|
/// - 같은 사람이 다시 쓴 변경은 더 늦은 것이 이긴다 (고친 명함이 이전 것을 대체).
|
|
/// - 서로 다른 사람의 변경이 부딪치면 **먼저 쓴 변경**이 이긴다.
|
|
/// 시각은 1/100초 단위이고, 기록은 [KeyValueRepo] 한 곳에 '쓴 사람|시각' 으로 남긴다.
|
|
class ConflictRule {
|
|
ConflictRule(this._meta);
|
|
|
|
final KeyValueRepo _meta;
|
|
|
|
/// [key] 줄에 [author] 가 [stamp] 에 쓴 변경을 반영해도 되는지. 반영해도 되면 기록까지 남긴다.
|
|
Future<bool> accept(String key, String author, int stamp) async {
|
|
final rec = await _meta.get(key);
|
|
if (rec != null) {
|
|
final cut = rec.lastIndexOf('|');
|
|
final oldAuthor = rec.substring(0, cut);
|
|
final oldStamp = int.parse(rec.substring(cut + 1));
|
|
final win = oldAuthor == author ? stamp >= oldStamp : stamp < oldStamp;
|
|
if (!win) return false;
|
|
}
|
|
await _meta.put(key, '$author|$stamp', stamp);
|
|
return true;
|
|
}
|
|
}
|