Selaa lähdekoodia

kill memoizer equality checkers. user memoizeOutput instead

Adam Shaw 7 vuotta sitten
vanhempi
sitoutus
f22df8249b
4 muutettua tiedostoa jossa 37 lisäystä ja 47 poistoa
  1. 29 29
      src/Calendar.ts
  2. 3 4
      src/component/memoized-rendering.ts
  3. 2 11
      src/util/array.ts
  4. 3 3
      src/util/reselector.ts

+ 29 - 29
src/Calendar.ts

@@ -14,11 +14,11 @@ import { createFormatter } from './datelib/formatting'
 import { Duration, createDuration } from './datelib/duration'
 import reduce from './reducers/main'
 import { parseDateSpan, DateSpanInput, DateSpan, buildDateSpanApi, DateSpanApi, buildDatePointApi, DatePointApi } from './structs/date-span'
-import reselector from './util/reselector'
+import reselector, { memoizeOutput } from './util/reselector'
 import { mapHash, assignTo, isPropsEqual } from './util/object'
 import { DateRangeInput } from './datelib/date-range'
 import DateProfileGenerator from './DateProfileGenerator'
-import { EventSourceInput, parseEventSource } from './structs/event-source'
+import { EventSourceInput, parseEventSource, EventSourceHash } from './structs/event-source'
 import { EventInput, parseEvent, EventDefHash } from './structs/event'
 import { CalendarState, Action } from './reducers/types'
 import EventSourceApi from './api/EventSourceApi'
@@ -30,7 +30,7 @@ import EventDragging from './interactions/EventDragging'
 import { buildViewSpecs, ViewSpecHash, ViewSpec } from './structs/view-spec'
 import { PluginSystem, PluginDef } from './plugin-system'
 import CalendarComponent from './CalendarComponent'
-import { compileEventUis } from './component/event-rendering';
+import { compileEventUis } from './component/event-rendering'
 
 
 export interface DateClickApi extends DatePointApi {
@@ -65,6 +65,9 @@ export default class Calendar {
 
   private buildDateEnv = reselector(buildDateEnv)
   private buildTheme = reselector(buildTheme)
+  private buildEventUiSingleBase = reselector(processScopedUiProps)
+  private buildEventUiBySource = memoizeOutput(buildEventUiBySource, isPropsEqual)
+  private buildEventUiBases = reselector(buildEventUiBases)
 
   // strictly for constraint system
   eventUiBases: EventUiHash
@@ -405,7 +408,7 @@ export default class Calendar {
         state.eventStore
 
     let eventUiSingleBase = this.buildEventUiSingleBase(viewSpec.options, this)
-    let eventUiBySource: EventUiHash = mapHash(state.eventSources, (eventSource) => eventSource.ui)
+    let eventUiBySource = this.buildEventUiBySource(state.eventSources)
     let eventUiBases = this.eventUiBases = this.buildEventUiBases(renderableEventStore.defs, eventUiSingleBase, eventUiBySource)
 
     this.renderableEventUis = compileEventUis(
@@ -474,31 +477,6 @@ export default class Calendar {
   }
 
 
-  private buildEventUiSingleBase = reselector(processScopedUiProps)
-
-  private buildEventUiBases = reselector(function(
-    eventDefs: EventDefHash,
-    eventUiSingleBase: EventUi,
-    eventUiBySource: EventUiHash
-  ) {
-    let eventUiBases: EventUiHash = { '': eventUiSingleBase }
-
-    for (let defId in eventDefs) {
-      let def = eventDefs[defId]
-
-      if (def.sourceId && eventUiBySource[def.sourceId]) {
-        eventUiBases[defId] = eventUiBySource[def.sourceId]
-      }
-    }
-
-    return eventUiBases
-  }, [
-    // equality funcs. KEEP UP TO DATE with method signature!
-    // TODO: reverse-memoization on eventUiBySource will solve this!!!
-    null, null, isPropsEqual
-  ])
-
-
   // Options
   // -----------------------------------------------------------------------------------------------------------------
 
@@ -1235,3 +1213,25 @@ function buildDelayedRerender(this: Calendar, wait) {
 
   return func
 }
+
+
+function buildEventUiBySource(eventSources: EventSourceHash): EventUiHash {
+  return mapHash(eventSources, function(eventSource) {
+    return eventSource.ui
+  })
+}
+
+
+function buildEventUiBases(eventDefs: EventDefHash, eventUiSingleBase: EventUi, eventUiBySource: EventUiHash) {
+  let eventUiBases: EventUiHash = { '': eventUiSingleBase }
+
+  for (let defId in eventDefs) {
+    let def = eventDefs[defId]
+
+    if (def.sourceId && eventUiBySource[def.sourceId]) {
+      eventUiBases[defId] = eventUiBySource[def.sourceId]
+    }
+  }
+
+  return eventUiBases
+}

+ 3 - 4
src/component/memoized-rendering.ts

@@ -1,4 +1,4 @@
-import { isArraysEqual, EqualityFuncs } from '../util/array'
+import { isArraysEqual } from '../util/array'
 
 export interface MemoizedRendering<ArgsType extends any[]> {
   (...args: ArgsType): void
@@ -9,8 +9,7 @@ export interface MemoizedRendering<ArgsType extends any[]> {
 export function memoizeRendering<ArgsType extends any[]>(
   renderFunc: (...args: ArgsType) => void,
   unrenderFunc?: (...args: ArgsType) => void,
-  dependencies: MemoizedRendering<any>[] = [],
-  equalityFuncs?: EqualityFuncs
+  dependencies: MemoizedRendering<any>[] = []
 ): MemoizedRendering<ArgsType> {
 
   let dependents: MemoizedRendering<any>[] = []
@@ -33,7 +32,7 @@ export function memoizeRendering<ArgsType extends any[]>(
   }
 
   function res() {
-    if (!prevArgs || !isArraysEqual(prevArgs, arguments, equalityFuncs)) {
+    if (!prevArgs || !isArraysEqual(prevArgs, arguments)) {
       unrender()
       thisContext = this
       prevArgs = arguments

+ 2 - 11
src/util/array.ts

@@ -32,10 +32,7 @@ export function removeExact(array, exactVal) {
   return removeCnt
 }
 
-
-export type EqualityFuncs = (null | ((obj0, obj1) => boolean))[]
-
-export function isArraysEqual(a0, a1, equalities: EqualityFuncs = []) {
+export function isArraysEqual(a0, a1) {
   let len = a0.length
   let i
 
@@ -44,13 +41,7 @@ export function isArraysEqual(a0, a1, equalities: EqualityFuncs = []) {
   }
 
   for (i = 0; i < len; i++) {
-    if (
-      a0[i] !== a1[i] &&
-      !(
-        equalities[i] &&
-        equalities[i](a0[i], a1[i])
-      )
-    ) {
+    if (a0[i] !== a1[i]) {
       return false
     }
   }

+ 3 - 3
src/util/reselector.ts

@@ -1,12 +1,12 @@
-import { isArraysEqual, EqualityFuncs } from './array'
+import { isArraysEqual } from './array'
 
 // TODO: eliminate use of equalityFuncs
-export default function<T>(workerFunc: T, equalityFuncs?: EqualityFuncs): T {
+export default function<T>(workerFunc: T): T {
   let args
   let res
 
   return function() {
-    if (!args || !isArraysEqual(args, arguments, equalityFuncs)) {
+    if (!args || !isArraysEqual(args, arguments)) {
       args = arguments
       res = (workerFunc as any).apply(this, arguments)
     }