Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Commit

Permalink
Initial work on faster worklog fetching
Browse files Browse the repository at this point in the history
Move auto update listeners to index.js to stop duplicate listeners
  • Loading branch information
alexcroox committed Feb 7, 2018
1 parent 10e046a commit 01e5ea3
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 61 deletions.
5 changes: 4 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ ipcMain.on('fetchWorklogs', (event, args) => {
fetchingWorklogs = false
let executionSeconds = Math.round((Date.now() - executionStart) / 1000)
console.log('Fetched worklogs', worklogs.length, `Took: ${executionSeconds} seconds`)
event.sender.send('worklogs', JSON.stringify(worklogs))
event.sender.send('worklogs', JSON.stringify({
fullWeek,
worklogs
}))
})
.catch(error => {
console.log('Failed to fetch worklogs', error)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.5",
"description": "Jira Timer",
"productName": "Jira Timer",
"main": "dist/main.js",
"main": "index.js",
"scripts": {
"postinstall": "electron-builder install-app-deps",
"start": "NODE_ENV=development electron .",
Expand Down
49 changes: 2 additions & 47 deletions src/containers/update/update-container.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { remote, ipcRenderer } from 'electron'
import { ipcRenderer } from 'electron'
import React, { Component, Fragment } from 'react'
import { connect } from 'react-redux'
import { setVersion, setUpdateInfo, setDownloaded } from '../../modules/updater'
import styled from 'styled-components'
import parse from 'date-fns/parse'
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now'
import FontAwesomeIcon from '@fortawesome/react-fontawesome'
import faSpinner from '@fortawesome/fontawesome-free-solid/faSpinner'
import Button from '../../components/button'

class UpdateContainer extends Component {
Expand All @@ -29,42 +26,6 @@ class UpdateContainer extends Component {
ipcRenderer.send('installUpdate')
}

componentDidMount () {

console.log('App version', remote.app.getVersion())

this.props.setVersion(remote.app.getVersion())

ipcRenderer.send('updateStatus')

ipcRenderer.on('updateStatus', (event, info) => {
var updateInfo = JSON.parse(info)
console.log('updateStatus', updateInfo)
this.props.setUpdateInfo(updateInfo)
})

ipcRenderer.on('updateChecking', () => {
console.log('updateChecking')
})

ipcRenderer.on('updateNotAvailable', () => {
console.log('updateNotAvailable')
})

ipcRenderer.on('updateReady', () => {
console.log('updateReady')
this.props.setDownloaded()
})

ipcRenderer.on('updateDownloading', (event, info) => {
console.log('updateDownloading', JSON.stringify(info))
})

ipcRenderer.on('updateError', () => {
console.log('updateError')
})
}

render () {
if (this.props.updateInfo)
return (
Expand Down Expand Up @@ -125,19 +86,13 @@ const UpdateNotes = styled.div`
line-height: 23px;
`

const mapDispatchToProps = {
setVersion,
setUpdateInfo,
setDownloaded
}

const mapStateToProps = state => ({
version: state.updater.version,
updateInfo: state.updater.updateInfo,
downloaded: state.updater.downloaded,
})

export default connect(mapStateToProps, mapDispatchToProps)(UpdateContainer)
export default connect(mapStateToProps)(UpdateContainer)



2 changes: 1 addition & 1 deletion src/containers/worklog/worklog-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class WorklogContainer extends Component {
}

componentWillMount () {
this.props.fetchWorklogs()
this.props.fetchWorklogs(false)
}

onOpenOptions (worklog) {
Expand Down
31 changes: 26 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ window.onerror = (err) => {
}


import { ipcRenderer } from 'electron'
import { remote, ipcRenderer } from 'electron'
import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import { MemoryRouter } from 'react-router-dom'
import store from './lib/create-store'
import { storeState } from './lib/storage'
import { addWorklogs, setUpdating, fetchWorklogs } from './modules/worklog'
import { setVersion, setUpdateInfo, setDownloaded } from './modules/updater'
import AppContainer from 'containers/app/app-container'

render(
Expand All @@ -35,15 +36,35 @@ setInterval(() => {
storeState(store.getState())
}, 60000)

console.log('App version', remote.app.getVersion())

// We need to keep the window position synced under the icon
ipcRenderer.send('refreshPosition')

store.dispatch(fetchWorklogs(true))

// Globally listen out for worklogs coming from main process
ipcRenderer.on('worklogs', (event, worklogJson) => {
let worklogs = JSON.parse(worklogJson)
console.log('Got worklogs from main process', worklogs.length)
store.dispatch(addWorklogs(worklogs))
ipcRenderer.on('worklogs', (event, worklogPayload) => {
let payload = JSON.parse(worklogPayload)
let worklogs = payload.worklogs
let fullWeek = payload.fullWeek

console.log('Got worklogs from main process', payload)
store.dispatch(addWorklogs(worklogs, fullWeek))
store.dispatch(setUpdating(false))
});

store.dispatch(setVersion(remote.app.getVersion()))

ipcRenderer.send('updateStatus')

ipcRenderer.on('updateStatus', (event, info) => {
var updateInfo = JSON.parse(info)
console.log('updateStatus', updateInfo)
store.dispatch(setUpdateInfo(updateInfo))
})

ipcRenderer.on('updateReady', () => {
console.log('updateReady')
store.dispatch(setDownloaded())
})
11 changes: 8 additions & 3 deletions src/modules/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,20 @@ export default function reducer (state = initialState, action = {}) {

if (timerIndex > -1) {
let timer = list[timerIndex]
timer.paused = action.pause

if (action.pause) {
timer.endTime = Date.now()
timer.previouslyElapsed = (Date.now() - timer.startTime) + timer.previouslyElapsed
// We need to make sure the timer isn't already paused. Otherwise we will
// be adding time since it was last paused!
if (!timer.paused) {
timer.endTime = Date.now()
timer.previouslyElapsed = (Date.now() - timer.startTime) + timer.previouslyElapsed
}
} else {
timer.startTime = Date.now()
}

timer.paused = action.pause

return state.set('list', Immutable(list))
} else {
return state
Expand Down
39 changes: 36 additions & 3 deletions src/modules/worklog.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,39 @@ export default function reducer (state = initialState, action = {}) {
if (typeof previouslyDeleted === "undefined")
previouslyDeleted = []

action.worklogs.forEach(worklog => {
let currentWorklogs = Immutable.asMutable(state.list, {deep: true})
let workLogs = action.worklogs

// If we are sending less than the full week lets add to the array if new
// or update existing time if duplicate
if (!action.fullRefresh) {
action.worklogs.forEach(worklog => {
let existingWorklog = find(currentWorklogs, ['id', worklog.id])

if (existingWorklog) {
existingWorklog.timeSpentSeconds = worklog.timeSpentSeconds
} else {
currentWorklogs.push(worklog)
}
})

// Now loop through all existing worklogs, delete any that aren't present in passed
// worklogs that are from the same day
currentWorklogs.forEach((worklog, index) => {
let created = parse(worklog.created)

if (isToday(created)) {
let matchingWorklog = find(action.worklogs, ['id', worklog.id])

if (!matchingWorklog)
currentWorklogs.splice(index, 1)
}
})

workLogs = currentWorklogs
}

workLogs.forEach(worklog => {

if (previouslyDeleted.indexOf(worklog.id) > -1)
return
Expand Down Expand Up @@ -114,9 +146,10 @@ export const setUpdating = updating => ({
})

// Pass array of worklogs
export const addWorklogs = worklogs => ({
export const addWorklogs = (worklogs, fullRefresh) => ({
type: ADD_WORKLOGS,
worklogs
worklogs,
fullRefresh
})

// Full week isn't supported yet (need to work on merging states)
Expand Down

0 comments on commit 01e5ea3

Please sign in to comment.