From 3e3cef87695e47c63d14c435322cbaf74e687f5a Mon Sep 17 00:00:00 2001
From: Julia Friesel <julia.friesel@gmail.com>
Date: Tue, 11 Sep 2018 18:06:19 +0200
Subject: [PATCH] Only use db functions when components are actually mounted

---
 components/calendar.js         |  6 ++++--
 components/chart/chart.js      |  7 ++++---
 components/chart/day-column.js |  7 +++++--
 components/home.js             |  9 ++++-----
 lib/cycle.js                   |  4 ++--
 lib/sympto-adapter.js          | 12 ++++++------
 6 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/components/calendar.js b/components/calendar.js
index 95e8bf86..74b754bd 100644
--- a/components/calendar.js
+++ b/components/calendar.js
@@ -1,13 +1,15 @@
 import React, { Component } from 'react'
 import { CalendarList } from 'react-native-calendars'
 import {LocalDate} from 'js-joda'
-import { getOrCreateCycleDay, bleedingDaysSortedByDate } from '../db'
+import { getOrCreateCycleDay, getBleedingDaysSortedByDate } from '../db'
 import cycleModule from '../lib/cycle'
 import {shadesOfRed} from '../styles/index'
 import styles from '../styles/index'
 
+
 export default class CalendarView extends Component {
   constructor(props) {
+    const bleedingDaysSortedByDate = getBleedingDaysSortedByDate()
     super(props)
     const predictedMenses = cycleModule().getPredictedMenses()
     this.state = {
@@ -31,7 +33,7 @@ export default class CalendarView extends Component {
   }
 
   componentWillUnmount() {
-    bleedingDaysSortedByDate.removeListener(this.setStateWithCalFormattedDays)
+    getBleedingDaysSortedByDate().removeListener(this.setStateWithCalFormattedDays)
   }
 
   passDateToDayView = (result) => {
diff --git a/components/chart/chart.js b/components/chart/chart.js
index a4cc7d19..f6da5399 100644
--- a/components/chart/chart.js
+++ b/components/chart/chart.js
@@ -5,7 +5,7 @@ import { LocalDate } from 'js-joda'
 import { makeYAxisLabels, normalizeToScale, makeHorizontalGrid } from './y-axis'
 import nfpLines from './nfp-lines'
 import DayColumn from './day-column'
-import { getCycleDay, cycleDaysSortedByDate, getAmountOfCycleDays } from '../../db'
+import { getCycleDay, getCycleDaysSortedByDate, getAmountOfCycleDays } from '../../db'
 import styles from './styles'
 import { scaleObservable } from '../../local-storage'
 import config from '../../config'
@@ -25,6 +25,7 @@ export default class CycleChart extends Component {
         />
       )
     }
+    this.cycleDaysSortedByDate = getCycleDaysSortedByDate()
   }
 
   onLayout = ({ nativeEvent }) => {
@@ -35,12 +36,12 @@ export default class CycleChart extends Component {
       this.setState({ columns: this.makeColumnInfo(nfpLines(height)) })
     }
 
-    cycleDaysSortedByDate.addListener(this.reCalculateChartInfo)
+    this.cycleDaysSortedByDate.addListener(this.reCalculateChartInfo)
     this.removeObvListener = scaleObservable(this.reCalculateChartInfo, false)
   }
 
   componentWillUnmount() {
-    cycleDaysSortedByDate.removeListener(this.reCalculateChartInfo)
+    this.cycleDaysSortedByDate.removeListener(this.reCalculateChartInfo)
     this.removeObvListener()
   }
 
diff --git a/components/chart/day-column.js b/components/chart/day-column.js
index 9b010fda..e541ab00 100644
--- a/components/chart/day-column.js
+++ b/components/chart/day-column.js
@@ -9,10 +9,13 @@ import { getOrCreateCycleDay } from '../../db'
 import cycleModule from '../../lib/cycle'
 import DotAndLine from './dot-and-line'
 
-const getCycleDayNumber = cycleModule().getCycleDayNumber
 const label = styles.column.label
 
 export default class DayColumn extends Component {
+  constructor() {
+    super()
+    this.getCycleDayNumber = cycleModule().getCycleDayNumber
+  }
   passDateToDayView(dateString) {
     const cycleDay = getOrCreateCycleDay(dateString)
     this.props.navigate('CycleDay', { cycleDay })
@@ -68,7 +71,7 @@ export default class DayColumn extends Component {
       )
     }
 
-    const cycleDayNumber = getCycleDayNumber(dateString)
+    const cycleDayNumber = this.getCycleDayNumber(dateString)
     const shortDate = dateString.split('-').slice(1).join('-')
     const cycleDayLabel = (
       <Text style = {label.number}>
diff --git a/components/home.js b/components/home.js
index b4832743..b7156d94 100644
--- a/components/home.js
+++ b/components/home.js
@@ -8,14 +8,13 @@ import {
 import { LocalDate, ChronoUnit } from 'js-joda'
 import styles from '../styles/index'
 import cycleModule from '../lib/cycle'
-import { requestHash, getOrCreateCycleDay, bleedingDaysSortedByDate, fillWithMucusDummyData, fillWithCervixDummyData, deleteAll } from '../db'
+import { requestHash, getOrCreateCycleDay, getBleedingDaysSortedByDate, fillWithMucusDummyData, fillWithCervixDummyData, deleteAll } from '../db'
 import {bleedingPrediction as labels} from './labels'
 
-const getCycleDayNumber = cycleModule().getCycleDayNumber
-
 export default class Home extends Component {
   constructor(props) {
     super(props)
+    const getCycleDayNumber = cycleModule().getCycleDayNumber
     this.todayDateString = LocalDate.now().toString()
     const cycleDayNumber = getCycleDayNumber(this.todayDateString)
 
@@ -34,11 +33,11 @@ export default class Home extends Component {
       }
     })(this)
 
-    bleedingDaysSortedByDate.addListener(this.setStateWithCurrentText)
+    getBleedingDaysSortedByDate().addListener(this.setStateWithCurrentText)
   }
 
   componentWillUnmount() {
-    bleedingDaysSortedByDate.removeListener(this.setStateWithCurrentText)
+    getBleedingDaysSortedByDate().removeListener(this.setStateWithCurrentText)
   }
 
   passTodayToDayView() {
diff --git a/lib/cycle.js b/lib/cycle.js
index da91e2dc..7deb02e0 100644
--- a/lib/cycle.js
+++ b/lib/cycle.js
@@ -13,8 +13,8 @@ export default function config(opts) {
   if (!opts) {
     // we only want to require (and run) the db module
     // when not running the tests
-    bleedingDaysSortedByDate = require('../db').bleedingDaysSortedByDate
-    cycleDaysSortedByDate = require('../db').cycleDaysSortedByDate
+    bleedingDaysSortedByDate = require('../db').getBleedingDaysSortedByDate()
+    cycleDaysSortedByDate = require('../db').getCycleDaysSortedByDate()
     maxBreakInBleeding = 1
     maxCycleLength = 99
     minCyclesForPrediction = 3
diff --git a/lib/sympto-adapter.js b/lib/sympto-adapter.js
index b6ab2171..363538ef 100644
--- a/lib/sympto-adapter.js
+++ b/lib/sympto-adapter.js
@@ -2,12 +2,6 @@ import getFertilityStatus from './sympto'
 import cycleModule from './cycle'
 import { fertilityStatus } from '../components/cycle-day/labels/labels'
 
-const {
-  getCycleForDay,
-  getCyclesBefore,
-  getPreviousCycle
-} = cycleModule()
-
 export function getFertilityStatusStringForDay(dateString) {
   const status = getCycleStatusForDay(dateString)
   if (!status) return fertilityStatus.unknown
@@ -28,6 +22,12 @@ export function getFertilityStatusStringForDay(dateString) {
 }
 
 export function getCycleStatusForDay(dateString) {
+  const {
+    getCycleForDay,
+    getCyclesBefore,
+    getPreviousCycle
+  } = cycleModule()
+
   const cycle = getCycleForDay(dateString)
   if (!cycle) return null
 
-- 
GitLab