コード例 #1
0
ファイル: StoreManager.js プロジェクト: wysapp/parsedemo
export function getStore(name) {
  let storeData = stores[name];
  if (!storeData) {
    throw new Error(
      'Unknown store! Attempted to retrieve store with the name ' + name
    );
  }


  let stateGetter = (storeData.isGlobal ?
    StateManager.getGlobalState :
    StateManager.getAppState
  );
  let stateSetter = (storeData.isGlobal ?
    StateManager.setGlobalState :
    StateManager.setAppState
  );

  return {
    getData: stateGetter.bind(null, name),
    isGlobal: storeData.isGlobal,
    dispatch(type, params, app) {
      let action = {...params, type, app};
     
      let newState = storeData.store(stateGetter(name, app), action);
      
      if (newState instanceof Parse.Promise) {
        return newState.then((result) => {
          if (storeData.isGlobal) {
            StateManager.setGlobalState(name, result);
          } else {
            
            StateManager.setAppState(name, app, result);
          }
          for (let id in storeData.subscribers) {
            storeData.subscribers[id](result);
          }
        });
      } else {
        if (storeData.isGlobal) {
          StateManager.setGlobalState(name, newState);
        } else {
          StateManager.setAppState(name, app, newState);
        }
        for (let id in storeData.subscribers) {
          storeData.subscribers[id](newState);
        }
      }
    },
    subscribe(cb) {
      let id = 'sub' + subCount++;
      storeData.subscribers[id] = cb;
      return id;
    },
    unsubscribe(id) {
      delete storeData.subscribers[id];
    }
  }
}
コード例 #2
0
 return newState.then((result) => {
   if (storeData.isGlobal) {
     StateManager.setGlobalState(name, result);
   } else {
     StateManager.setAppState(name, app, result);
   }
   for (let id in storeData.subscribers) {
     storeData.subscribers[id](result);
   }
 });