How to define capsules declared in another package? #85
-
This is the scenario: I have a package library and a project. In the package library I define an abstract (or mixin) Can you give me some solutions to this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I did this solution, but maybe there is a better/simpler solution: AppController appControllerCapsule(CapsuleHandle use) {
final (getInitialized, setInitialized) = use.rawValueWrapper(() => false);
final (getApp, setApp) = use.rawValueWrapper<WebApp>();
return (
get: getApp,
set: (app) {
if (getInitialized()) {
throw ('appControllerCapsule -- Yet initialized');
}
setApp(app);
setInitialized(true);
},
);
}
WebApp get app => cc.read(appControllerCapsule).get();
abstract class WebApp {
WebApp() {
// Saving instance
cc.read(appControllerCapsule).set(this);
}
} |
Beta Was this translation helpful? Give feedback.
-
That goes against ReArch quite a bit (using inheritance). You should instead expose capsules in your library package that declare a certain API to be defined elsewhere so that you can then use those common capsules in your library package. There are two ways I can think of to do this off the top of my head.
// In your library package:
(Capsule<int>, void Function(Capsule<int>)) _countCapsuleManager(CapsuleHandle use) =>
use.state((CapsuleHandle use) => throw UnsupportedError('Must set the countCapsule via countCapsuleSetterAction!'));
void Function(Capsule<int>) countCapsuleSetterAction(CapsuleHandle use) => use(_countCapsuleManager).$2;
int countCapsule(CapsuleHandle use) => use(use(_countCapsuleManager).$1); // Your proxy
// Somewhere shortly after you make your container in your application's package:
container.read(countCapsuleSetterAction)(theActualCountCapsuleImpl); |
Beta Was this translation helpful? Give feedback.
That goes against ReArch quite a bit (using inheritance). You should instead expose capsules in your library package that declare a certain API to be defined elsewhere so that you can then use those common capsules in your library package.
There are two ways I can think of to do this off the top of my head.
// In your library package: