decoder property

  1. @override
Converter<Map<String, dynamic>, DoublyLinkedEventSourcedModel<S, B, E>> get decoder
override

Returns the decoder of this, converting from T to S.

It may be stateful and should not be reused.

Implementation

@override
Converter<Map<String, dynamic>, DoublyLinkedEventSourcedModel<S, B, E>>
    get decoder => ConverterFn((m) {
          final cursorI = m['cursorIndex'] as int?;
          final entries =
              ArgumentError.checkNotNull(m['entries'] as List?).cast<E>();
          _DoubleLink<E> it = _DoubleLink(None());
          _DoubleLink<E> cursor = it;

          for (var i = 0; i < entries.length; i++) {
            final event = entries[i];
            final nextLink = _DoubleLink(Just(event));
            it.next = nextLink;
            nextLink.previous = it;
            it = nextLink;

            if (i == cursorI) {
              it = it;
            }
          }

          return DoublyLinkedEventSourcedModel<S, B, E>._(
            initialState: ArgumentError.checkNotNull(m['initialState'] as S?),
            state: m['state'] as S,
            cursor: cursor,
          );
        });