2021-12-04 01:31:27 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
2021-11-12 01:03:36 +08:00
|
|
|
extension ObjectExtension<T> on T {
|
2021-12-20 02:28:10 +08:00
|
|
|
/// Run [fn] with this, and return this
|
2021-11-12 01:03:36 +08:00
|
|
|
T apply(void Function(T obj) fn) {
|
|
|
|
fn(this);
|
|
|
|
return this;
|
|
|
|
}
|
2021-12-04 01:31:27 +08:00
|
|
|
|
2021-12-20 02:28:10 +08:00
|
|
|
/// Run [fn] with this, and return this
|
2021-12-04 01:31:27 +08:00
|
|
|
Future<T> applyFuture(FutureOr<void> Function(T obj) fn) async {
|
|
|
|
await fn(this);
|
|
|
|
return this;
|
|
|
|
}
|
2021-12-05 19:03:59 +08:00
|
|
|
|
2021-12-20 02:28:10 +08:00
|
|
|
/// Run [fn] with this, and return the results of [fn]
|
2021-12-05 19:03:59 +08:00
|
|
|
U run<U>(U Function(T obj) fn) => fn(this);
|
2022-01-02 04:34:40 +08: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);
|
|
|
|
}
|
2021-11-12 01:03:36 +08:00
|
|
|
}
|