acquire method

T acquire()

Acquires a component from the pool. If the pool is empty, a new component is created using the factory function. Otherwise, the last available component is removed from the pool and returned.

A listener is automatically attached that will return the component to the pool when it is removed from its parent. The listener waits for the component to be Component.mounted first, then listens for Component.removed. This ensures recycled components (whose removed future is already completed) don't get immediately returned to the pool before they have a chance to mount.

Just call Component.removeFromParent when done with the component.

Implementation

T acquire() {
  final component = _available.isEmpty ? _factory() : _available.removeLast();
  component.mounted.then((_) {
    component.removed.then((_) => _release(component));
  });
  return component;
}