stated-bean
stated-bean copied to clipboard
[RFC] bean metadata extension capability
The stated meta-information extension capability of the stated-bean can be used to implement more functions. For example, the routing parameters and the stated field are automatically synchronized.
@StatedBean()
class A {
@Stated()
@RouteParam("name")
a: string;
}
- When accessing the route
/?name=test
, the initial value ofa
will be set totest
- When the value of
a
changes, the route will be synchronized./?name=test2
@RouteParam
const RouteParam = (name: string) => (target: Object, fieldKey: string) => {
getMetadataStorage().collectExtension(Symbol.for('stated-bean-route-param'), ...);
}
StatedBeanMetaStorage
class StatedBeanMetaStorage {
collectExtension(extensionName: string | symbol, extension: any) {
...
}
}
StatedRouteInterceptor
class StatedRouteInterceptor implements StatedInterceptor {
async stateInit(context: EffectContext, next: NextCaller) {
// if with @RouteParam
// get name value from route
// context.setValue('test');
await next();
}
async stateChange(context: EffectContext, next: NextCaller) {
// if with @RouteParam
const value = context.value;
await next();
// sync to router
}
}