-
Notifications
You must be signed in to change notification settings - Fork 224
Description
Some frameworks need a way to run initialization code for all leaf code using the framework. For example, dependency injection frameworks want to build a map of injectable types, and serialization frameworks want to know about all serializable types.
Currently frameworks either use codegen or manual initialization to do this.
I discussed with @eernstg and @sigurdm; the idea we got to was that it might be possible to build this on top of existing const canonicalization support, giving something that's both cheap to implement and sufficient for the use cases we care about.
Leaf code wanting to register itself would declare a const in some new special way:
@register
const unimportantName = 'value';
@register
const otherUnimportantName = 'otherValue';
When the framework is initialized it can ask for all @registered const values and iterate over them doing whatever initialization work it wants to.
var registrations = Platform.registeredConsts(); // <Object>['value', 'otherValue']
This is sufficient for e.g. all injectable types or serializable types to register themselves. Note that the value registered could be any const, e.g. a function, or an instance of a const class. A possible refinement would be to make it possible to ask for all registered consts of a particular type:
var registrations = Platform.registeredConsts<String>(); // <String>['value', 'otherValue']
--arguably this removes the need for the register annotation since you can request a framework-specific type that is only used for registration.