Преглед изворни кода

make date-profile-based fetching happen in one reduce cycle

Adam Shaw пре 7 година
родитељ
комит
27d6c6cab5
2 измењених фајлова са 17 додато и 21 уклоњено
  1. 6 8
      src/reducers/eventSources.ts
  2. 11 13
      src/reducers/main.ts

+ 6 - 8
src/reducers/eventSources.ts

@@ -16,8 +16,7 @@ export default function(eventSourceHash: EventSourceHash, action: Action, datePr
       return removeSource(eventSourceHash, action.sourceId)
 
     case 'SET_DATE_PROFILE':
-      fetchDirtySources(eventSourceHash, action.dateProfile, calendar)
-      return eventSourceHash
+      return fetchDirtySources(eventSourceHash, action.dateProfile, calendar)
 
     case 'FETCH_EVENT_SOURCES':
       if (dateProfile) {
@@ -48,7 +47,7 @@ function addSources(eventSourceHash: EventSourceHash, sources: EventSource[], da
   }
 
   if (dateProfile) {
-    fetchDirtySources(hash, dateProfile, calendar)
+    hash = fetchDirtySources(hash, dateProfile, calendar)
   }
 
   return assignTo({}, eventSourceHash, hash)
@@ -60,7 +59,7 @@ function removeSource(eventSourceHash: EventSourceHash, sourceId: string): Event
   })
 }
 
-function fetchDirtySources(sourceHash: EventSourceHash, dateProfile: DateProfile, calendar: Calendar) {
+function fetchDirtySources(sourceHash: EventSourceHash, dateProfile: DateProfile, calendar: Calendar): EventSourceHash {
   let activeRange = dateProfile.activeRange
   let dirtySourceIds = []
 
@@ -78,11 +77,10 @@ function fetchDirtySources(sourceHash: EventSourceHash, dateProfile: DateProfile
   }
 
   if (dirtySourceIds.length) {
-    calendar.dispatch({
-      type: 'FETCH_EVENT_SOURCES',
-      sourceIds: dirtySourceIds
-    })
+    return fetchSourcesById(sourceHash, dirtySourceIds, dateProfile, calendar)
   }
+
+  return sourceHash
 }
 
 function fetchSourcesById(

+ 11 - 13
src/reducers/main.ts

@@ -22,8 +22,8 @@ export default function(state: CalendarState, action: Action, calendar: Calendar
     eventSelection: reduceSelectedEvent(state.eventSelection, action),
     eventDrag: reduceEventDrag(state.eventDrag, action),
     eventResize: reduceEventResize(state.eventResize, action),
-    eventSourceLoadingLevel: reduceEventSourceLoadingLevel(state.eventSourceLoadingLevel, action, eventSources),
-    loadingLevel: reduceEventSourceLoadingLevel(state.loadingLevel, action, eventSources) // use same func
+    eventSourceLoadingLevel: computeLoadingLevel(eventSources),
+    loadingLevel: computeLoadingLevel(eventSources)
   }
 }
 
@@ -82,16 +82,14 @@ function reduceEventResize(currentEventResize: EventInteractionState | null, act
   }
 }
 
-function reduceEventSourceLoadingLevel(level: number, action: Action, eventSources: EventSourceHash): number {
-  switch (action.type) {
-    case 'REMOVE_ALL_EVENT_SOURCES':
-      return 0
-    case 'FETCH_EVENT_SOURCES':
-      return level + (action.sourceIds ? action.sourceIds.length : Object.keys(eventSources).length)
-    case 'RECEIVE_EVENTS':
-    case 'RECEIVE_EVENT_ERROR':
-      return level - 1
-    default:
-      return level
+function computeLoadingLevel(eventSources: EventSourceHash): number {
+  let cnt = 0
+
+  for (let sourceId in eventSources) {
+    if (eventSources[sourceId].isFetching) {
+      cnt++
+    }
   }
+
+  return cnt
 }