Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Changes per code review, now using a calculated time out to update th…
Browse files Browse the repository at this point in the history
…e ledger heart
  • Loading branch information
ryanml committed Apr 25, 2018
1 parent 90459c2 commit 99ce0c3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
44 changes: 43 additions & 1 deletion app/common/lib/ledgerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {responseHasContent} = require('./httpUtil')
const urlUtil = require('../../../js/lib/urlutil')
const getSetting = require('../../../js/settings').getSetting
const urlParse = require('../urlParse')
const appStore = require('../../../js/stores/appStoreRenderer')

/**
* Is page an actual page being viewed by the user? (not an error page, etc)
Expand Down Expand Up @@ -574,6 +575,45 @@ const getMediaProvider = (url, firstPartyUrl, referrer) => {
return provider
}

const shouldSetTimeout = (publisherKey) => {
const minimumVisits = parseInt(getSetting(settings.PAYMENTS_MINIMUM_VISITS))

if (minimumVisits === 1) {
return true
}

const publisher = ledgerState.getPublisher(appStore.state, publisherKey)
const publisherVisits = publisher.get('visits')

if (publisherVisits === undefined) {
return minimumVisits === 1
}

const visitDifference = minimumVisits - publisherVisits

return (visitDifference === 1)
}

const getTimeoutWait = (publisherKey) => {
const minimumVisitTime = getSetting(settings.PAYMENTS_MINIMUM_VISIT_TIME)
const publisher = ledgerState.getPublisher(appStore.state, publisherKey)

if (!publisher) {
return minimumVisitTime
}

const publisherDuration = publisher.get('duration')

if (
publisherDuration === undefined ||
publisherDuration >= minimumVisitTime
) {
return minimumVisitTime
}

return (minimumVisitTime - publisherDuration)
}

const defaultMonthlyAmounts = Immutable.List([5.0, 7.5, 10.0, 17.5, 25.0, 50.0, 75.0, 100.0])

const milliseconds = {
Expand Down Expand Up @@ -606,7 +646,9 @@ const getMethods = () => {
milliseconds,
defaultMonthlyAmounts,
getDefaultMediaFavicon,
generateMediaCacheData
generateMediaCacheData,
shouldSetTimeout,
getTimeoutWait
}

let privateMethods = {}
Expand Down
34 changes: 14 additions & 20 deletions app/renderer/components/navigation/publisherToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,53 +55,47 @@ class PublisherToggle extends React.Component {
}
}

setUpdateInterval () {
if (!this.mounted) {
setUpdateTimeout () {
const updateWait = ledgerUtil.getTimeoutWait(this.props.publisherKey)
const shouldSetTimeout = ledgerUtil.shouldSetTimeout(this.props.publisherKey)
if (!this.mounted || !shouldSetTimeout) {
return
}
// The dispatch to update top sites occurs ~5 seconds after a
// page visit has started recording. It is important that a
// page data update is not triggered before this has happened.
const topSitesWait = ledgerUtil.milliseconds.second * 6
const updateWait = ledgerUtil.milliseconds.second * 3
setTimeout(() => {
let updateInterval = setInterval(() => {
appActions.onPublisherToggleUpdate()
}, updateWait)
this.setState({updateInterval: updateInterval})
}, topSitesWait)
let updateTimeout = setTimeout(() => {
appActions.onPublisherToggleUpdate()
}, updateWait)
this.setState({updateTimeout: updateTimeout})
}

clearUpdateInterval () {
this.state && clearInterval(this.state.updateInterval)
clearUpdateTimeout () {
this.state && clearTimeout(this.state.updateTimeout)
}

componentDidMount () {
this.mounted = true
if (!this.props.isVisibleInLedger) {
this.setUpdateInterval()
this.setUpdateTimeout()
}
}

componentWillUnmount () {
this.mounted = false
this.clearUpdateInterval()
this.clearUpdateTimeout()
}

componentWillReceiveProps (newProps) {
if (!this.mounted) {
return
}

this.clearUpdateInterval()
if (
!newProps.isVisibleInLedger &&
(
newProps.location !== this.props.location ||
newProps.publisherKey !== this.props.publisherKey
)
) {
this.setUpdateInterval()
this.clearUpdateTimeout()
this.setUpdateTimeout()
}
}

Expand Down

0 comments on commit 99ce0c3

Please sign in to comment.