Bläddra i källkod

inline a lot of unnecessary dispatch actions

Adam Shaw 7 år sedan
förälder
incheckning
8732269009
2 ändrade filer med 59 tillägg och 88 borttagningar
  1. 59 22
      src/Calendar.ts
  2. 0 66
      src/reducers/main.ts

+ 59 - 22
src/Calendar.ts

@@ -309,11 +309,7 @@ export default class Calendar {
       this.dispatch({ type: 'ADD_EVENT_SOURCE', rawSource })
     }
 
-    this.dispatch({
-      type: 'SET_VIEW_TYPE',
-      viewType: this.opt('defaultView'),
-      dateMarker: this.getInitialDate()
-    })
+    this.setViewType(this.opt('defaultView'), this.getInitialDate())
   }
 
 
@@ -359,9 +355,7 @@ export default class Calendar {
         this.publiclyTrigger('loading', [ false, this.view ])
       }
 
-      if (oldState !== newState) {
-        this.requestRerender()
-      }
+      this.requestRerender()
 
       // TODO: what about pausing new renders while rendering?
     }
@@ -601,7 +595,7 @@ export default class Calendar {
       }
     }
 
-    this.dispatch({ type: 'SET_VIEW_TYPE', viewType, dateMarker })
+    this.setViewType(viewType, dateMarker)
   }
 
 
@@ -616,9 +610,24 @@ export default class Calendar {
       this.viewSpecManager.getUnitViewSpec(viewType, this)
 
     if (spec) {
-      this.dispatch({ type: 'SET_VIEW_TYPE', viewType: spec.type, dateMarker })
+      this.setViewType(spec.type, dateMarker)
     } else {
-      this.dispatch({ type: 'NAVIGATE_DATE', dateMarker })
+      this.setCurrentDateMarker(dateMarker)
+    }
+  }
+
+
+  setViewType(viewType: string, dateMarker?: DateMarker) { // internal use only
+    if (!this.view || this.view.type !== viewType) {
+      let view = this.getViewByType(viewType)
+      this.view = view
+
+      this.dispatch({ // luckily, will cause a rerender
+        type: 'SET_DATE_PROFILE',
+        dateProfile: view.computeDateProfile(
+          dateMarker || this.state.dateProfile.currentDate
+        )
+      })
     }
   }
 
@@ -640,42 +649,62 @@ export default class Calendar {
 
 
   prev() {
-    this.dispatch({ type: 'NAVIGATE_PREV' })
+    this.dispatch({
+      type: 'SET_DATE_PROFILE',
+      dateProfile: this.view.dateProfileGenerator.buildPrev(this.state.dateProfile)
+    })
   }
 
 
   next() {
-    this.dispatch({ type: 'NAVIGATE_NEXT' })
+    this.dispatch({
+      type: 'SET_DATE_PROFILE',
+      dateProfile: this.view.dateProfileGenerator.buildNext(this.state.dateProfile)
+    })
   }
 
 
   prevYear() {
-    this.dispatch({ type: 'NAVIGATE_PREV_YEAR' })
+    this.dispatch({
+      type: 'SET_DATE_PROFILE',
+      dateProfile: this.view.computeDateProfile(
+        this.dateEnv.addYears(this.state.dateProfile.currentDate, -1)
+      )
+    })
   }
 
 
   nextYear() {
-    this.dispatch({ type: 'NAVIGATE_NEXT_YEAR' })
+    this.dispatch({
+      type: 'SET_DATE_PROFILE',
+      dateProfile: this.view.computeDateProfile(
+        this.dateEnv.addYears(this.state.dateProfile.currentDate, 1)
+      )
+    })
   }
 
 
   today() {
-    this.dispatch({ type: 'NAVIGATE_TODAY' })
+    this.dispatch({
+      type: 'SET_DATE_PROFILE',
+      dateProfile: this.view.computeDateProfile(this.getNow())
+    })
   }
 
 
   gotoDate(zonedDateInput) {
-    this.dispatch({
-      type: 'NAVIGATE_DATE',
-      dateMarker: this.dateEnv.createMarker(zonedDateInput)
-    })
+    this.setCurrentDateMarker(
+      this.dateEnv.createMarker(zonedDateInput)
+    )
   }
 
 
   incrementDate(delta) { // is public facing
     this.dispatch({
-      type: 'NAVIGATE_DELTA',
-      delta: createDuration(delta)
+      type: 'SET_DATE_PROFILE',
+      dateProfile: this.view.computeDateProfile(
+        this.dateEnv.add(this.state.dateProfile.currentDate, delta)
+      )
     })
   }
 
@@ -686,6 +715,14 @@ export default class Calendar {
   }
 
 
+  setCurrentDateMarker(date: DateMarker) { // internal use only
+    this.dispatch({
+      type: 'SET_DATE_PROFILE',
+      dateProfile: this.view.computeDateProfile(date)
+    })
+  }
+
+
   // Date Formatting Utils
   // -----------------------------------------------------------------------------------------------------------------
 

+ 0 - 66
src/reducers/main.ts

@@ -27,17 +27,6 @@ export function reduce(state: CalendarState, action: any, calendar: Calendar): C
 
   switch(action.type) {
 
-    case 'SET_VIEW_TYPE':
-      if (!calendar.view || calendar.view.type !== action.viewType) {
-        let view = calendar.getViewByType(action.viewType)
-        calendar.view = view
-        calendar.dispatch({
-          type: 'SET_DATE_PROFILE',
-          dateProfile: view.computeDateProfile(action.dateMarker)
-        })
-      }
-      break
-
     case 'SET_DATE_PROFILE':
       if (action.dateProfile.isValid) {
         newState.dateProfile = action.dateProfile
@@ -45,61 +34,6 @@ export function reduce(state: CalendarState, action: any, calendar: Calendar): C
       }
       break
 
-    case 'NAVIGATE_PREV':
-      calendar.dispatch({
-        type: 'SET_DATE_PROFILE',
-        dateProfile: calendar.view.dateProfileGenerator.buildPrev(newState.dateProfile)
-      })
-      break
-
-    case 'NAVIGATE_NEXT':
-      calendar.dispatch({
-        type: 'SET_DATE_PROFILE',
-        dateProfile: calendar.view.dateProfileGenerator.buildNext(newState.dateProfile)
-      })
-      break
-
-    case 'NAVIGATE_TODAY':
-      calendar.dispatch({
-        type: 'SET_DATE_PROFILE',
-        dateProfile: calendar.view.computeDateProfile(calendar.getNow())
-      })
-      break
-
-    case 'NAVIGATE_PREV_YEAR':
-      calendar.dispatch({
-        type: 'SET_DATE_PROFILE',
-        dateProfile: calendar.view.computeDateProfile(
-          calendar.dateEnv.addYears(newState.dateProfile.currentDate, -1)
-        )
-      })
-      break
-
-    case 'NAVIGATE_NEXT_YEAR':
-      calendar.dispatch({
-        type: 'SET_DATE_PROFILE',
-        dateProfile: calendar.view.computeDateProfile(
-          calendar.dateEnv.addYears(newState.dateProfile.currentDate, 1)
-        )
-      })
-      break
-
-    case 'NAVIGATE_DATE':
-      calendar.dispatch({
-        type: 'SET_DATE_PROFILE',
-        dateProfile: calendar.view.computeDateProfile(action.dateMarker)
-      })
-      break
-
-    case 'NAVIGATE_DELTA':
-      calendar.dispatch({
-        type: 'SET_DATE_PROFILE',
-        dateProfile: calendar.view.computeDateProfile(
-          calendar.dateEnv.add(newState.dateProfile.currentDate, action.delta)
-        )
-      })
-      break
-
     case 'SELECT':
       return assignTo({}, state, {
         selection: action.selection