registerView method

GraphViewRegistration registerView(
  1. GraphViewDefinition definition, {
  2. Iterable<GraphEntityIdentity> membership = const [],
})

Register one mounted framework view and its current entity membership.

Implementation

GraphViewRegistration registerView(
  GraphViewDefinition definition, {
  Iterable<GraphEntityIdentity> membership = const [],
}) {
  final token = Object();
  final now = DateTime.now().toUtc();
  final entry = _views.putIfAbsent(
    definition.viewId,
    () => _GraphViewEntry(definition: definition, registeredAt: now),
  );
  entry.registrations[token] = _normalizedMembership(membership);
  entry
    ..lastRenderedAt = now
    ..renderCount += 1;
  _publishViewLifecycle(
    entry.registrations.length == 1
        ? GraphViewLifecycleState.registered
        : GraphViewLifecycleState.membershipChanged,
    entry,
  );

  return GraphViewRegistration._(
    update: (nextMembership) {
      final current = _views[definition.viewId];
      if (current == null || !current.registrations.containsKey(token)) {
        return;
      }
      final previous = _combinedMembership(current);
      current.registrations[token] = _normalizedMembership(nextMembership);
      current
        ..lastRenderedAt = DateTime.now().toUtc()
        ..renderCount += 1;
      final next = _combinedMembership(current);
      if (!_sameMembership(previous, next)) {
        _publishViewLifecycle(
          GraphViewLifecycleState.membershipChanged,
          current,
        );
      }
    },
    dispose: () {
      final current = _views[definition.viewId];
      if (current == null || current.registrations.remove(token) == null) {
        return;
      }
      if (current.registrations.isEmpty) {
        final record = _viewRecord(current);
        _views.remove(definition.viewId);
        _publishViewRecord(
          GraphViewLifecycleState.unregistered,
          GraphViewRecord(
            definition: record.definition,
            registeredAt: record.registeredAt,
            lastRenderedAt: record.lastRenderedAt,
            renderCount: record.renderCount,
            subscriberCount: 0,
            membership: const [],
          ),
        );
      } else {
        _publishViewLifecycle(
          GraphViewLifecycleState.membershipChanged,
          current,
        );
      }
    },
  );
}