executeWithPool<R> method

FutureOr<R> executeWithPool<R>(
  1. FutureOr<R> f(
    1. C o
    ), {
  2. Duration? timeout,
  3. bool validator(
    1. C o
    )?,
  4. dynamic onError(
    1. Object error,
    2. StackTrace stackTrace
    )?,
})
inherited

Implementation

FutureOr<R> executeWithPool<R>(FutureOr<R> Function(O o) f,
    {Duration? timeout,
    bool Function(O o)? validator,
    Function(Object error, StackTrace stackTrace)? onError}) {
  return catchFromPool(timeout: timeout).then((o) {
    try {
      var ret = f(o);

      return ret.then((val) {
        if (validator == null || validator(o)) {
          releaseIntoPool(o);
        } else {
          disposePoolElement(o);
        }
        return val;
      }, onError: (e, s) {
        disposePoolElement(o);
        if (onError != null) {
          return onError(e, s);
        } else {
          throw e;
        }
      });
    } catch (e, s) {
      disposePoolElement(o);
      if (onError != null) {
        return onError(e, s);
      } else {
        rethrow;
      }
    }
  });
}