stated-bean icon indicating copy to clipboard operation
stated-bean copied to clipboard

[RFC] bean metadata extension capability

Open foreleven opened this issue 5 years ago • 1 comments

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;
}
  1. When accessing the route /?name=test, the initial value of a will be set to test
  2. 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
  }
}

foreleven avatar Aug 15 '19 12:08 foreleven