2021-12-03 18:31:27 +01:00
|
|
|
import 'dart:async';
|
|
|
|
|
2021-11-11 18:03:36 +01:00
|
|
|
extension ObjectExtension<T> on T {
|
2021-12-19 19:28:10 +01:00
|
|
|
/// Run [fn] with this, and return this
|
2021-11-11 18:03:36 +01:00
|
|
|
T apply(void Function(T obj) fn) {
|
|
|
|
fn(this);
|
|
|
|
return this;
|
|
|
|
}
|
2021-12-03 18:31:27 +01:00
|
|
|
|
2021-12-19 19:28:10 +01:00
|
|
|
/// Run [fn] with this, and return this
|
2021-12-03 18:31:27 +01:00
|
|
|
Future<T> applyFuture(FutureOr<void> Function(T obj) fn) async {
|
|
|
|
await fn(this);
|
|
|
|
return this;
|
|
|
|
}
|
2021-12-05 12:03:59 +01:00
|
|
|
|
2021-12-19 19:28:10 +01:00
|
|
|
/// Run [fn] with this, and return the results of [fn]
|
2021-12-05 12:03:59 +01:00
|
|
|
U run<U>(U Function(T obj) fn) => fn(this);
|
2022-01-01 21:34:40 +01:00
|
|
|
|
|
|
|
/// Run [fn] with this, and return the results of [fn]
|
|
|
|
Future<U> runFuture<U>(FutureOr<U> Function(T obj) fn) async {
|
|
|
|
return await fn(this);
|
|
|
|
}
|
2022-06-06 19:37:46 +02:00
|
|
|
|
|
|
|
/// Cast this as U, or null if this is not an object of U
|
|
|
|
U? as<U>() => this is U ? this as U : null;
|
2021-11-11 18:03:36 +01:00
|
|
|
}
|