encoder property

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

Returns the encoder from S to T.

It may be stateful and should not be reused.

Implementation

@override
Converter<DoublyLinkedEventSourcedModel<S, B, E>, Map<String, dynamic>>
    get encoder => ConverterFn((t) {
          final entries = <E>[];
          int? cursorI;
          // skip the sentry (_DoubleLink(None()))
          for (var it = t._eventCursor.start.next, i = 0; it != null; i++) {
            entries.add(it.value.visit(
              just: (e) => e,
              none: () => throw StateError(
                "There should have been an event at the link",
              ),
            ));
            if (it == t._eventCursor) {
              cursorI = i;
            }
          }
          return <String, dynamic>{
            'initialState': t.initialState,
            'state': t._snapshot,
            'cursorIndex': cursorI,
            'entries': entries,
          };
        });