Skip to content

How to load a module #7

@ryuever

Description

@ryuever

Halation在进行module register的时候是依靠load.strategy来实现对于module是否记载的处理

export default function PluginComponent() {
  return {
    name: 'plugin-a',

    loader: {
      strategy: [{
        type: 'ban',
        resolver: () => {}
      },{
        type: 'flags',
        resolver: () => {}
      }, {
        type: 'event',
        resolver: () => {}
      }, {
        type: 'runtime',
        resolver: () => {}
      }],
    }
  }
}

类型的话,主要是分为下面的四种

enum StrategyType {
  // 你可以认为这个属于偏静态的约束方式,比如当进入商详的时候,goodsId其实已经知道了;比如可以通过
  // ban 进行一些黑名单的约束
  ban,

  // 函数会接受flags
  flags,

  // 应用自定义的render phase;
  event,

  // 可能会依赖model在runtime时的值;这个其实就是与业务耦合,需要通过值的判断进行是否加载模块的判断
  // 其实这个halation中更经常碰到的问题
  runtime,
}

对于loader的使用方式,他们最后值的合并类似&&的关系

  1. 如果第一个function返回false,那么后面的function就不需要继续处理
  2. 只有当所有的function都返回true的时候,loader才返回true,也就认为当前的module可以进行加载了

上面的方式只是解决了普遍情况的问题,但是还有一种情况;

  1. module B的自组件包含A,A-
  2. A, A-是通过flag值来判断到底渲染哪个;
  3. 但是还有一个约束,B还要通过runtime的值来判断究竟是A还是A-(比如一个值为空就渲染A-)

目前Halation提供的方式是对component进行registerModuleMap属性的注入,直接通过registerModuleMap拿到对应的module然后调用load的方式进行加载

const B = props =>  {
  const { registerModuleMap } = props

  const load = registerModuleMap.get('A')

  load()
}

Maybe

  1. when to trigger next phrase; useRender to collect which
  2. when to notify to the end of collection
    1. useEffect is not a good choice...
    2. The followed ComponentHelper may works...
  3. Every exported module is required to has useRender function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions