attach static method

EntityGraphDevtoolsBinding attach(
  1. EntityGraph graph, {
  2. bool enabled = true,
  3. String? storeId,
  4. EntityGraphDevtoolsValuePolicy valuePolicy = const EntityGraphDevtoolsValuePolicy.metadataOnly(),
  5. EntityGraphIR? schema,
  6. bool vmServiceEnabled = _vmServiceAvailable,
  7. int? historyLimit,
  8. int? historyBytesLimit,
  9. int? eventBytesLimit,
  10. int? snapshotLimit,
  11. int? snapshotBytesLimit,
})

Attach development tooling to graph.

Repeated enabled attachments to the same graph share one controller and the configuration established by the first active attachment. enabled set to false returns an inert binding and changes no reference count.

Implementation

static EntityGraphDevtoolsBinding attach(
  EntityGraph graph, {
  bool enabled = true,
  String? storeId,
  EntityGraphDevtoolsValuePolicy valuePolicy =
      const EntityGraphDevtoolsValuePolicy.metadataOnly(),
  EntityGraphIR? schema,
  bool vmServiceEnabled = _vmServiceAvailable,
  int? historyLimit,
  int? historyBytesLimit,
  int? eventBytesLimit,
  int? snapshotLimit,
  int? snapshotBytesLimit,
}) {
  if (!enabled) {
    return EntityGraphDevtoolsBinding._(
      enabled: false,
      controller: null,
      detach: EntityGraphDevtoolsController._noop,
    );
  }

  final slot = _slots[graph] ??= _EntityGraphDevtoolsSlot();
  var entry = slot.entry;
  if (entry == null ||
      entry.controller.isDisposed ||
      !entry.controller._owns(graph)) {
    final resolvedStoreId =
        storeId ?? (slot.generatedStoreId ??= 'graph-${_nextStoreNumber++}');
    final existingVmController = _vmControllers[resolvedStoreId];
    if (vmServiceEnabled &&
        existingVmController != null &&
        !existingVmController.isDisposed &&
        !existingVmController._disposing) {
      throw StateError(
        'VM-service storeId $resolvedStoreId is already attached',
      );
    }
    final controller = EntityGraphDevtoolsController._(
      graph: graph,
      storeId: resolvedStoreId,
      controllerId:
          '$resolvedStoreId:$_isolateEpoch:${_nextControllerGeneration++}',
      valuePolicy: valuePolicy,
      schema: schema,
      vmServiceEnabled: vmServiceEnabled,
      historyLimit: historyLimit,
      historyBytesLimit: historyBytesLimit,
      eventBytesLimit: eventBytesLimit,
      snapshotLimit: snapshotLimit,
      snapshotBytesLimit: snapshotBytesLimit,
    );
    controller._startObservingGraph();
    controller._publishLifecycle(EntityGraphDevtoolsLifecycleState.attached);
    entry = _EntityGraphDevtoolsControllerEntry(controller);
    slot.entry = entry;
  }

  final retainedEntry = entry;
  retainedEntry.references += 1;
  var detached = false;
  return EntityGraphDevtoolsBinding._(
    enabled: true,
    controller: retainedEntry.controller,
    detach: () {
      if (detached) return;
      detached = true;
      final current = slot.entry;
      if (current == null ||
          !identical(current.controller, retainedEntry.controller)) {
        return;
      }
      if (current.references > 0) current.references -= 1;
      if (current.references != 0) return;
      if (identical(slot.entry, current)) slot.entry = null;
      current.controller._dispose();
    },
  );
}