Factories #21
-
Hi, I just tried factories and work well. It caches the result: Future<clib.OsUserInfo?> Function(String username) osUserInfoCapsule(
CapsuleHandle use) =>
(username) =>
clib.OsUtils.getUserInfo(username, shell: reff.read(shellProvider)); I just wanted to know how you handle it to cache the result for an arguments combination |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hi! 👋 It depends on where the factory is being used from.
(String, void Function(String)) currOsUserCapsule(CapsuleHandle use) => use.state("");
Future<OsUserInfo?> Function(String username) osUserInfoFactory(CapsuleHandle use) => ...;
Future<OsUserInfo?> currOsUserInfoAsyncCapsule(CapsuleHandle use) => use(osUserInfoFactory)(use(currOsUserCapsule));
AsyncValue<OsUserInfo?> currOsUserInfoCapsule(CapsuleHandle use) => use.future(use(currOsUserInfoAsyncCapsule)); Then you can depend on |
Beta Was this translation helpful? Give feedback.
-
Hi again. I'm having troubles to caching factory capsule values: import 'package:rearch/rearch.dart';
void main(List<String> arguments) {
final capsuleContainer = CapsuleContainer();
print(capsuleContainer.read(_monthAsStringFactory)(11));
print(' ');
print(capsuleContainer.read(_monthAsStringFactory)(10));
print(' ');
print(capsuleContainer.read(_monthAsStringFactory)(11));
print(' ');
}
String _getMonthAsString(int month) {
print('getMonthAsString() -- $month');
return switch (month) {
1 => 'Enero',
2 => 'Febrero',
3 => 'Marzo',
4 => 'Abril',
5 => 'Mayo',
6 => 'Junio',
7 => 'Julio',
8 => 'Agosto',
9 => 'Septiembre',
10 => 'Octubre',
11 => 'Noviembre',
12 => 'Diciembre',
_ => throw ('Bad usage'),
};
}
String Function(int) _monthAsStringFactory(CapsuleHandle use) {
print('_monthAsStringFactory() -- I');
return (int month) {
print('_monthAsStringFactory() -- II -- $month');
return use.memo(() {
print('_monthAsStringFactory() -- III -- $month');
return _getMonthAsString(month);
}, [month]);
};
} This is the output:
I'm expecting to be printed steps II and III only the first time for each month, but I get it printed every time. Also I put https://github.com/busslina/rearch-dart-test/blob/master/lib/main_factory.dart Thanks |
Beta Was this translation helpful? Give feedback.
Hi! 👋
It depends on where the factory is being used from.
Widget
, you canuse.memo()
and/oruse.future()
from within aRearchConsumer
.