enqueue method
Implementation
Future<String> enqueue(
String collectionId,
Map<String, dynamic> submission,
) async {
final key = submission['idempotencyKey'];
if (collectionId.isEmpty || key is! String || key.isEmpty)
throw ArgumentError('collection and idempotency key required');
final bytes = Uint8List.fromList(utf8.encode(_canonical(submission)));
if (bytes.length > 65536) throw RangeError('record exceeds 64 KiB');
final (good, bad) = await _read();
for (final prior in good.where((r) => r.idempotencyKey == key)) {
if (prior.collectionId != collectionId ||
!_equal(prior.submission, bytes))
throw StateError('idempotency conflict');
return prior.id;
}
if (good.length >= limits.maxRecords ||
good.fold(0, (n, r) => n + r.byteSize) + bytes.length > limits.maxBytes)
throw StateError('capacity exceeded');
final value = _Envelope(
adapter.createRecordId(),
collectionId,
bytes,
key,
nowMillis(),
bytes.length,
);
await _write([...good, value], bad);
return value.id;
}