cacheRequests method

Future<void> cacheRequests(
  1. List<EventRequest> eventRequests,
  2. int clearAfterInMs,
  3. int maxCacheSizeBytes
)

Implementation

Future<void> cacheRequests(
  List<EventRequest> eventRequests,
  int clearAfterInMs,
  int maxCacheSizeBytes,
) async {
  Debug.print("RequestCacher: Storing ${eventRequests.length} requests");
  final pendingRequests = await retrieveRequests();
  final k = PerceptSDK.clientTs.asString;
  await _lock.synchronized(() async {
    List<EventRequest> requests = (eventRequests + pendingRequests)
      ..sort((a, b) => (b.data[k]?.compareTo(a.data[k] ?? "0") ?? 0));
    Debug.print("RequestCacher: merges ${jsonEncode(requests)} requests");
    final requestStrings =
        _processRequests(requests, clearAfterInMs, maxCacheSizeBytes);
    Debug.print("RequestCacher: Storing ${requestStrings.length} requests.");
    final queue = {cachedEventsKey: jsonEncode(requestStrings)};
    await PerceptFlutterPlatform.instance.setGlobalProperties(queue);
  });
}