美文网首页
Weex源码分析之weex初始化

Weex源码分析之weex初始化

作者: 依然fantac | 来源:发表于2018-02-08 11:17 被阅读151次

从weex集成文档看出,要在application onCreate中调用 WXSDKEngine.initialize

    WXSDKEngine.initialize(this,
                           new InitConfig.Builder()
                               //.setImgAdapter(new FrescoImageAdapter())// use fresco adapter
                               .setImgAdapter(new ImageAdapter())
                               .setWebSocketAdapterFactory(new DefaultWebSocketAdapterFactory())
                               .setJSExceptionAdapter(new JSExceptionAdapter())
                               .setHttpAdapter(new InterceptWXHttpAdapter())
                               .build()
                          );

weex sdk初始化入口WXSDKEngine.initialize

public static void initialize(Application application,InitConfig config){
  ...
  doInitInternal(application,config);
}

initialize方法实际上调用的是 doInitInternal

private static void doInitInternal(final Application application,final InitConfig config){
    // 加载v8引擎
    WXSDKManager sm = WXSDKManager.getInstance();
        sm.onSDKEngineInitialize();
    WXSoInstallMgrSdk.init(application,
                              sm.getIWXSoLoaderAdapter(),
                              sm.getWXStatisticsListener());
    boolean isSoInitSuccess = WXSoInstallMgrSdk.initSo(V8_SO_NAME, 1, config!=null?config.getUtAdapter():null);

    // 初始化jsFramework
    sm.initScriptsFramework(config!=null?config.getFramework():null);

    // 注册Compontent,Module
    register();
}

doInitInternal主要做了三件事:

  • 加载v8引擎
  • 初始化jsFramework
  • 注册Compontent,Module

接下来看看register的具体内容

private static void register() {
    BatchOperationHelper batchHelper = new BatchOperationHelper(WXBridgeManager.getInstance());
    try {
      registerComponent(
        new SimpleComponentHolder(
          WXText.class,
          new WXText.Creator()
        ),
        false,
        WXBasicComponentType.TEXT
      );
      registerComponent(
        new SimpleComponentHolder(
          WXDiv.class,
          new WXDiv.Ceator()
        ),
        false,
        WXBasicComponentType.CONTAINER,
        WXBasicComponentType.DIV,
        WXBasicComponentType.HEADER,
        WXBasicComponentType.FOOTER
      );
      registerComponent(
        new SimpleComponentHolder(
          WXImage.class,
          new WXImage.Ceator()
        ),
        false,
        WXBasicComponentType.IMAGE,
        WXBasicComponentType.IMG
      );
      registerComponent(
        new SimpleComponentHolder(
          WXScroller.class,
          new WXScroller.Creator()
        ),
        false,
        WXBasicComponentType.SCROLLER
      );
      registerComponent(
        new SimpleComponentHolder(
          WXSlider.class,
          new WXSlider.Creator()
        ),
        true,
        WXBasicComponentType.SLIDER,
        WXBasicComponentType.CYCLE_SLIDER
      );
      registerComponent(
        new SimpleComponentHolder(
                WXSliderNeighbor.class,
          new WXSliderNeighbor.Creator()
        ),
        true,
        WXBasicComponentType.SLIDER_NEIGHBOR
      );
      String simpleList = "simplelist";
      registerComponent(SimpleListComponent.class,false,simpleList);
      registerComponent(WXListComponent.class, false,WXBasicComponentType.LIST,WXBasicComponentType.VLIST,WXBasicComponentType.RECYCLER,WXBasicComponentType.WATERFALL);
      registerComponent(WXRecyclerTemplateList.class, false,WXBasicComponentType.RECYCLE_LIST);
      registerComponent(HorizontalListComponent.class,false,WXBasicComponentType.HLIST);
      registerComponent(WXBasicComponentType.CELL, WXCell.class, true);
      registerComponent(WXBasicComponentType.CELL_SLOT, WXCell.class, true);
      registerComponent(WXBasicComponentType.INDICATOR, WXIndicator.class, true);
      registerComponent(WXBasicComponentType.VIDEO, WXVideo.class, false);
      registerComponent(WXBasicComponentType.INPUT, WXInput.class, false);
      registerComponent(WXBasicComponentType.TEXTAREA, Textarea.class,false);
      registerComponent(WXBasicComponentType.SWITCH, WXSwitch.class, false);
      registerComponent(WXBasicComponentType.A, WXA.class, false);
      registerComponent(WXBasicComponentType.EMBED, WXEmbed.class, true);
      registerComponent(WXBasicComponentType.WEB, WXWeb.class);
      registerComponent(WXBasicComponentType.REFRESH, WXRefresh.class);
      registerComponent(WXBasicComponentType.LOADING, WXLoading.class);
      registerComponent(WXBasicComponentType.LOADING_INDICATOR, WXLoadingIndicator.class);
      registerComponent(WXBasicComponentType.HEADER, WXHeader.class);

      registerModule("modal", WXModalUIModule.class, false);
      registerModule("instanceWrap", WXInstanceWrap.class, true);
      registerModule("animation", WXAnimationModule.class, true);
      registerModule("webview", WXWebViewModule.class, true);
      registerModule("navigator", WXNavigatorModule.class);
      registerModule("stream", WXStreamModule.class);
      registerModule("timer", WXTimerModule.class, false);
      registerModule("storage", WXStorageModule.class, true);
      registerModule("clipboard", WXClipboardModule.class, true);
      registerModule("globalEvent",WXGlobalEventModule.class);
      registerModule("picker", WXPickersModule.class);
      registerModule("meta", WXMetaModule.class,true);
      registerModule("webSocket", WebSocketModule.class);
      registerModule("local", WXLocalModule.class);


      registerDomObject(simpleList, WXListDomObject.class);
      registerDomObject(WXBasicComponentType.INDICATOR, WXIndicator.IndicatorDomNode.class);
      registerDomObject(WXBasicComponentType.TEXT, WXTextDomObject.class);
      registerDomObject(WXBasicComponentType.HEADER, WXCellDomObject.class);
      registerDomObject(WXBasicComponentType.CELL, WXCellDomObject.class);
      registerDomObject(WXBasicComponentType.CELL_SLOT, WXCellDomObject.class);
      registerDomObject(WXBasicComponentType.INPUT, BasicEditTextDomObject.class);
      registerDomObject(WXBasicComponentType.TEXTAREA, TextAreaEditTextDomObject.class);
      registerDomObject(WXBasicComponentType.SWITCH, WXSwitchDomObject.class);
      registerDomObject(WXBasicComponentType.LIST, WXListDomObject.class);
      registerDomObject(WXBasicComponentType.RECYCLE_LIST, WXRecyclerDomObject.class);
      registerDomObject(WXBasicComponentType.VLIST, WXListDomObject.class);
      registerDomObject(WXBasicComponentType.HLIST, WXListDomObject.class);
      registerDomObject(WXBasicComponentType.SCROLLER, WXScrollerDomObject.class);
      registerDomObject(WXBasicComponentType.RECYCLER, WXRecyclerDomObject.class);
      registerDomObject(WXBasicComponentType.WATERFALL, WXRecyclerDomObject.class);
    } catch (WXException e) {
      WXLogUtils.e("[WXSDKEngine] register:", e);
    }
    batchHelper.flush();
  }

register中包含各种registerComponent,registerModule,registerDomObject。思考为啥registerComponent和registerDomObject不是一个东西,这里涉及到weex加载dom的一个优化,把dom的构建和渲染分离了,后面再细聊。

public static boolean registerComponent(IFComponentHolder holder, boolean appendTree, String ... names) throws WXException {
  WXComponentRegistry.registerComponent(name, holder, componentInfo);
}

  public static boolean registerDomObject(String type, Class<? extends WXDomObject> clazz) throws WXException {
    return WXDomRegistry.registerDomObject(type, clazz);
  }

  private static <T extends WXModule> boolean registerModule(String moduleName, ModuleFactory factory, boolean global) throws WXException {
    return WXModuleManager.registerModule(moduleName, factory,global);
  }

由此也确定了三个重要的类WXComponentRegistry,WXDomRegistry,WXModuleManager,用来管理weex到native的映射。

总结下初始化工作:

  • 加载v8引擎
  • 初始化jsFramework
  • 注册Compontent,Dom,Module到WXComponentRegistry,WXDomRegistry,WXModuleManager

ps: 初始化jsFramework工作在WXBridgeManager initFramework方法,感兴趣的可以自行查阅

private void initFramework(String framework) {
    if (TextUtils.isEmpty(framework)) {
        // if (WXEnvironment.isApkDebugable()) {
        WXLogUtils.d("weex JS framework from assets");
        // }
        framework = WXFileUtils.loadAsset("main.js", WXEnvironment.getApplication());
      }

    registerDomModule();
    ...
}

相关文章

网友评论

      本文标题:Weex源码分析之weex初始化

      本文链接:https://www.haomeiwen.com/subject/onsozxtx.html