deliver method

DeliverRep deliver(
  1. ServerSentEventCache event, {
  2. bool isPeek = false,
})

Deliver cache event steps

Here, a chain of responsibility will be created, which includes three steps to prepare for the establishment of the chain of responsibility.

Step 1 : Match those interceptors that have same ServerSentEvent.elementType with the event argument, Or it is content matching, which is based on the configuration in the interceptor.

Step 2 : Sort the interceptors according to their priorities.

Step 3 : Check whether the event has been notified to this interceptor. If it has been notified, this interceptor needs to be excluded.

isPeek more detail see SSEInterceptor.isPeek

Implementation

DeliverRep deliver(ServerSentEventCache event, {bool isPeek = false}) {
  // Step 1
  List<SSEInterceptor> matchInterceptors = _interceptors.where((interceptor) {
    return interceptor.watchEvents.where((watchEvent) {
      bool isMatchType = watchEvent.eventType == event.cache.elementType;
      bool isMatchContent = true;
      if (watchEvent.matchContent?.isNotEmpty ?? false) {
        isMatchContent = watchEvent.matchContent == event.cache.result;
      }
      bool commonRule = isMatchType && isMatchContent;
      bool isMatchFinal =
          isPeek ? commonRule && interceptor.isPeek : commonRule && !interceptor.isPeek;
      if (isMatchFinal) {
        interceptor.curWatchEvent = watchEvent;
      } else {
        interceptor.curWatchEvent = null;
      }
      return isMatchFinal;
    }).isNotEmpty;
  }).toList();
  // Step 2
  matchInterceptors.sort((a, b) {
    assert(a.curWatchEvent != null && b.curWatchEvent != null);
    if (a.curWatchEvent!.priority.value > b.curWatchEvent!.priority.value) {
      return -1;
    } else if (a.curWatchEvent!.priority.value == b.curWatchEvent!.priority.value) {
      return 0;
    } else {
      return 1;
    }
  });
  // Step 3
  List<SSEInterceptor> interceptorsReal =
      matchInterceptors.where((element) => !event.notifiedSSEListener.contains(element)).toList();
  slog.d("Deliver Event $event\n", tag: tag);
  interceptorsReal.print(tag);
  SSEChain curChain = SSEChain(interceptorsReal);
  slog.d("proceed event ${event.cache}", tag: tag);
  SSEResponse response = curChain.proceed(SSEResponse(event: event.cache, reqUrl: event.reqUrl));
  return DeliverRep(response, curChain.notifiedInterceptors);
}