mapWithModel<ParentModel, ChildModel, ChildMsg, ParentMsg> static method

Sub<ParentModel, ParentMsg> mapWithModel<ParentModel, ChildModel, ChildMsg, ParentMsg>(
  1. String idPrefix,
  2. Sub<ChildModel, ChildMsg> sub,
  3. ParentMsg msgMapper(
    1. ChildMsg
    ),
  4. ChildModel modelMapper(
    1. ParentModel
    ),
)

Map a subscription from a child model/message pair to a parent model/message pair.

Use this when composing nested MVU components where both the model and message types differ between parent and child.

msgMapper converts a child message to a parent message. modelMapper extracts the child model from the current parent model, so the child subscription always receives a fresh, correctly-scoped model.

Implementation

static Sub<ParentModel, ParentMsg>
    mapWithModel<ParentModel, ChildModel, ChildMsg, ParentMsg>(
            String idPrefix,
            Sub<ChildModel, ChildMsg> sub,
            ParentMsg Function(ChildMsg) msgMapper,
            ChildModel Function(ParentModel) modelMapper) =>
        Sub(sub._subs.map((inner) {
          final (subId, subscribe) = inner;
          return (
            [idPrefix, ...subId],
            (SubHandler<ParentModel, ParentMsg> handler) => subscribe(
                SubHandler<ChildModel, ChildMsg>(
                    (ChildMsg innerMsg) =>
                        handler.dispatch(msgMapper(innerMsg)),
                    () => modelMapper(handler.model)))
          );
        }).toList());