setTypeMeta method

void setTypeMeta({
  1. UiFactory<UiProps>? subtypeOfFactory,
  2. Object? subtypeOfRaw,
  3. bool isWrapper = false,
})

Sets metadata for the underlying component class/type associated with this factory, functionally equivalent to the annotations.Component2 annotation.

However, unlike the annotation, this can be used for function components.

subtypeOf arguments: subtypeOfFactory/subtypeOfRaw

The raw JS component type that is this component's "parent type".

Used to enable inheritance in component type-checking in isComponentOfType.

subtypeOfRaw accepts the raw underlying type, and subtypeOfFactory accepts a factory, for convenience.

For example, the following are equivalent:

subtypeOfFactory: Foo,
subtypeOfRaw: Foo().componentFactory.type,
subtypeOfRaw: $FooComponentFactory.type,

E.g., if component Bar is a subtype of component Foo:

// Class component example:
class FooComponent ... {...}

@Component2(subtypeOf: FooComponent)
class BarComponent ... {...}
// Function component example:
UiFactory<FooProps> Foo = uiFunction(...);

UiFactory<FooProps> Bar = uiFunction(...)
  ..setComponentMeta(subtypeOfFactory: Foo);

then:

isComponentOfType(Bar()(), Bar); // true (due to normal type-checking)
isComponentOfType(Bar()(), Foo); // true (due to parent type-checking)

isWrapper

Whether the component clones or passes through its children and needs to be treated as if it were the wrapped component when passed in to isComponentOfType.

// Class component
@Component2(isWrapper: true)
class FooComponent extends UiComponent2<FooProps> { ... }

// Function component
UiFactory<FooProps> Foo = uiFunction(...)
  ..setComponentMeta(isWrapper: true);

Implementation

void setTypeMeta({
  // These are separate arguments because it's very difficult to tell
  // the difference between a UiFactory and a ReactClass at runtime.
  UiFactory? subtypeOfFactory,
  Object? /*ReactClass|JS component function|string*/ subtypeOfRaw,
  bool isWrapper = false,
}) {
  if (subtypeOfFactory != null && subtypeOfRaw != null) {
    throw ArgumentError('subtypeOfFactory and subtypeOfRawJsType cannot both be specified.');
  }
  final parentType = subtypeOfFactory != null
      // Get the type directly as opposed to using getComponentTypeFromAlias
      // because the alias might not have been registered yet
      // due to lazy-initialization of generated component factories.
      ? subtypeOfFactory().componentFactory!.type
      : subtypeOfRaw;

  final type = this().componentFactory!.type as Object;
  setComponentTypeMeta(
    type,
    parentType: parentType,
    isWrapper: isWrapper,
    // Fetch the old meta and preserve the value of isHoc for now.
    // isHoc's implementation/usage seems incomplete and may be removed,
    // so we won't expose it as an argument, at least for now.
    isHoc: getComponentTypeMeta(type).isHoc,
  );
}