likes
comments
collection
share

SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

作者站长头像
站长
· 阅读数 39

IsStable:首先 false,然后 trueSAP 电商云 Spartacus UI Cart ID 的 local storage 存储

@NgModule()
export class CartPersistenceModule {
  static forRoot(): ModuleWithProviders<CartPersistenceModule> {
    return {
      ngModule: CartPersistenceModule,
      providers: [
        {
          provide: APP_INITIALIZER,
          useFactory: cartStatePersistenceFactory,
          deps: [MultiCartStatePersistenceService, ConfigInitializerService],
          multi: true,
        },

提供的函数在应用程序启动时注入并在应用程序初始化期间执行。 如果这些函数中的任何一个返回 Promise 或 Observable,则在 Promise 解决或 Observable 完成之前,初始化不会完成。

例如,我们可以创建一个加载语言数据或外部配置的工厂函数,并将该函数提供给 APP_INITIALIZER 令牌。 该函数在应用程序引导过程中执行,所需数据在启动时可用。

应用程序初始化时,调用 cartStatePersistenceFactory

Angular 框架调用所有的 app initializer:SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

这个 init 需要返回一个 promise 对象:SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

toPromise 对象内会调用 subscriber 的 next 方法:

SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

调用 cartStatePersistenceService.initSync()

SAP 电商云 Spartacus UI Cart ID 的 local storage 存储cart 信息存储在 local storage 里:SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

从 local storage 里取出的数据:SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

生成 key:SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

SSR 模式下没有浏览器 storage:SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

取出浏览器 local storage 里存储的当前 active cart id:2007

SAP 电商云 Spartacus UI Cart ID 的 local storage 存储也就是下图高亮的 cart id:SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

不同的 base site,其对应的 cart id 不一致。

得到 cart id 后,调用 onRead 方法:SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

首先 clear Cart 状态:SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

进入 scheduleMessage,即利用 store 进行 Action dispatch,很有可能是一个异步过程。

SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

然而没有进入异步 schedule 的分支:SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

直接调用 scheduler 的 flush 方法:SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

调用 Observer 的 next 方法:

SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

重新计算 state,并且把结果保存:SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

computedStates = recomputeStates(computedStates, minInvalidatedStateIndex, reducer, committedState, actionsById, stagedActionIds, skippedActionIds, errorHandler, isPaused);
        monitorState = monitorReducer(monitorState, liftedAction);

前一个状态:

SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

调用 reducer 计算下一个状态:SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

这就是 Spartacus 自己实现的 reducer 了:SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

export function reducer(
  state = initialState,
  action: CmsActions.LoadCmsPageDataSuccess
): EntityState<Page> {
  switch (action.type) {
    case CmsActions.LOAD_CMS_PAGE_DATA_SUCCESS: {
      const page: Page = action.payload;
      return { ...state, entities: { ...state.entities, [page.pageId]: page } };
    }
  }
  return state;
}

SAP 电商云 Spartacus UI Cart ID 的 local storage 存储

会依次执行很多对应的 reducer:SAP 电商云 Spartacus UI Cart ID 的 local storage 存储