strong method

T? strong({
  1. bool keepWeakRef = true,
})

Promotes this reference to strong.

If the object still exists:

  • It becomes strongly reachable again
  • Timestamp is refreshed
  • Manager schedules future weakening

If already collected:

  • Weak reference is cleared
  • Returns null

keepWeakRef keeps the weak handle alongside the strong one.

Implementation

T? strong({bool keepWeakRef = true}) {
  var ref = _strongRef;
  if (ref != null) {
    // Already a strong reference:
    assert(_queued);
    final manager = _manager;
    if (manager != null) {
      manager._handleAccessStrongRef(this);
      assert(_queued);
    }
    return ref;
  }

  _strongRef = ref = _weakRef?.target;

  if (ref == null) {
    // Lost reference, not reachable either strongly or weakly:
    _weakRef = null;
  } else {
    if (!keepWeakRef) {
      // Dispose weak reference, but still reachable through `_strongRef`:
      _weakRef = null;
    }

    // Weak ref state, not queued:
    assert(!_queued);

    // New strong reference, handle it:
    final manager = _manager;
    if (manager != null) {
      manager._handleNewStrongRef(this);
      assert(_queued);
    }
  }

  return ref;
}