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

move appStore to main #87

Merged
merged 1 commit into from
Dec 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 4 additions & 41 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@

'use strict'
const electron = require('electron')
const BrowserWindow = electron.BrowserWindow
const ipcMain = electron.ipcMain
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const Menu = require('./menu')
const LocalShortcuts = require('./localShortcuts')
const Updater = require('./updater')
const messages = require('../js/constants/messages')
const AppActions = require('../js/actions/appActions')
require('../js/stores/appStore')

// Report crashes
electron.crashReporter.start()

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let windows = []

app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
Expand All @@ -27,36 +24,8 @@ app.on('window-all-closed', function () {
}
})

const spawnWindow = () => {
let mainWindow = new BrowserWindow({
width: 1360,
height: 800,
minWidth: 400,
// A frame but no title bar and windows buttons in titlebar.
// This only currently has an effect on 10.10 OSX and up and is
// ignore on other platforms.
'title-bar-style': 'hidden'
})
if (process.env.NODE_ENV === 'development') {
mainWindow.loadURL('file://' + __dirname + '/index-dev.html')
} else {
mainWindow.loadURL('file://' + __dirname + '/index.html')
}
mainWindow.on('closed', function () {
LocalShortcuts.unregister(mainWindow)

var index = windows.indexOf(mainWindow)
if (index > -1) {
windows.splice(index, 1)
}
})

LocalShortcuts.register(mainWindow)
return mainWindow
}

app.on('ready', function () {
windows.push(spawnWindow())
AppActions.newWindow()

ipcMain.on(messages.QUIT_APPLICATION, () => {
app.quit()
Expand All @@ -66,12 +35,6 @@ app.on('ready', function () {
BrowserWindow.getFocusedWindow().webContents.send(messages.CONTEXT_MENU_OPENED, nodeName)
})

ipcMain.on(messages.NEW_WINDOW, () => windows.push(spawnWindow()))
process.on(messages.NEW_WINDOW, () => windows.push(spawnWindow()))

ipcMain.on(messages.CLOSE_WINDOW, () => BrowserWindow.getFocusedWindow().close())
process.on(messages.CLOSE_WINDOW, () => BrowserWindow.getFocusedWindow().close())

Menu.init()

ipcMain.on(messages.UPDATE_REQUESTED, () => {
Expand Down
9 changes: 5 additions & 4 deletions app/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const app = electron.app
const Menu = require('menu')
const messages = require('../js/constants/messages')
const dialog = electron.dialog
const AppActions = require('../js/actions/appActions')

/**
* Sends a message to the web contents of the focused window.
Expand Down Expand Up @@ -40,7 +41,7 @@ const init = () => {
click: function (item, focusedWindow) {
if (!sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_NEW_FRAME])) {
// no active windows
process.emit(messages.NEW_WINDOW)
AppActions.newWindow()
}
}
}, {
Expand All @@ -52,11 +53,11 @@ const init = () => {
}, {
label: 'New Window',
accelerator: 'CmdOrCtrl+N',
click: () => process.emit(messages.NEW_WINDOW)
click: () => AppActions.newWindow()
}, {
label: 'New Private Window',
accelerator: 'CmdOrCtrl+Alt+N',
click: () => process.emit(messages.NEW_WINDOW)
click: () => AppActions.newWindow()
}, {
type: 'separator'
}, {
Expand Down Expand Up @@ -105,7 +106,7 @@ const init = () => {
accelerator: 'CmdOrCtrl+Shift+W',
click: function (item, focusedWindow) {
if (focusedWindow) {
process.emit(messages.CLOSE_WINDOW)
AppActions.closeWindow(focusedWindow.id)
}
}
}, {
Expand Down
16 changes: 12 additions & 4 deletions js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,34 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

'use strict'

const AppDispatcher = require('../dispatcher/appDispatcher')
const AppConstants = require('../constants/appConstants')
const ipc = global.require('electron').ipcRenderer
const messages = require('../constants/messages')

const AppActions = {
/**
* Dispatches an event to the main process to create a new window
*/
newWindow: function () {
ipc.send(messages.NEW_WINDOW)
AppDispatcher.dispatch({
actionType: AppConstants.APP_NEW_WINDOW
})
},

closeWindow: function (appWindowId) {
AppDispatcher.dispatch({
actionType: AppConstants.APP_CLOSE_WINDOW,
appWindowId
})
},

/**
* Dispatches an event to the main process to update the browser
*/
updateRequested: function () {
// TODO - change to dispatcher
console.log('appActions updateRequested')
ipc.send(messages.UPDATE_REQUESTED)
global.require('electron').ipcRenderer.send(messages.UPDATE_REQUESTED)
},

/**
Expand Down
18 changes: 7 additions & 11 deletions js/actions/windowActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ const WindowDispatcher = require('../dispatcher/windowDispatcher')
const WindowConstants = require('../constants/windowConstants')
const Config = require('../constants/config')
const UrlUtil = require('../../node_modules/urlutil.js/dist/node-urlutil.js')
const ipc = global.require('electron').ipcRenderer
const electron = global.require('electron')
const ipc = electron.ipcRenderer
const remote = electron.remote
const messages = require('../constants/messages')
const AppActions = require('./appActions')

const AppActions = {
const WindowActions = {
/**
* Dispatches a message to the store to load a new URL for the active frame.
* Both the frame's src and location properties will be updated accordingly.
Expand Down Expand Up @@ -144,17 +147,10 @@ const AppActions = {
frameProps
})
} else {
this.closeWindow()
AppActions.closeWindow(remote.getCurrentWindow().id)
}
},

/**
* Dispatches an event to the main process to close the current window
*/
closeWindow: function () {
ipc.send(messages.CLOSE_WINDOW)
},

/**
* Dispatches a message to the store to undo a closed frame
* The new frame is expected to appear at the index it was last closed at
Expand Down Expand Up @@ -447,4 +443,4 @@ const AppActions = {
}
}

module.exports = AppActions
module.exports = WindowActions
23 changes: 17 additions & 6 deletions js/components/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@
const React = require('react')
const Immutable = require('immutable')
const WindowStore = require('../stores/windowStore')
const AppStore = require('../stores/appStore')
const Main = require('./main')
const ipc = global.require('electron').ipcRenderer
const messages = require('../constants/messages')

class Window extends React.Component {
constructor () {
super()
constructor (props) {
super(props)

// initialize appState from props
// and then listen for updates
this.appState = this.props.appState
this.state = {
immutableData: {
windowState: WindowStore.getState(),
appState: AppStore.getState()
appState: this.appState
}
}
ipc.on(messages.APP_STATE_CHANGE, this.onAppStateChange.bind(this))
WindowStore.addChangeListener(this.onChange.bind(this))
AppStore.addChangeListener(this.onChange.bind(this))
}

render () {
Expand All @@ -31,6 +36,7 @@ class Window extends React.Component {

componentWillUnmount () {
WindowStore.removeChangeListener(this.onChange.bind(this))
ipc.removeListener(this.onAppStateChange)
}

shouldComponentUpdate (nextProps, nextState) {
Expand All @@ -41,11 +47,16 @@ class Window extends React.Component {
this.setState({
immutableData: {
windowState: WindowStore.getState(),
appState: AppStore.getState()
appState: this.appState
}
})
}

onAppStateChange (appState) {
this.appState = appState
this.onChange()
}
}
Window.propTypes = { appState: React.PropTypes.object }

module.exports = Window
2 changes: 2 additions & 0 deletions js/constants/appConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const mapValuesByKeys = require('../lib/functional').mapValuesByKeys

const _ = null
const AppConstants = {
APP_NEW_WINDOW: _,
APP_CLOSE_WINDOW: _,
APP_ADD_SITE: _,
APP_REMOVE_SITE: _
}
Expand Down
7 changes: 3 additions & 4 deletions js/constants/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ const messages = {
SHORTCUT_FRAME_RELOAD: _, /** @arg {number} key of frame */
SHORTCUT_NEXT_TAB: _,
SHORTCUT_PREV_TAB: _,
// Window management
CLOSE_WINDOW: _,
NEW_WINDOW: _,
QUIT_APPLICATION: _,
// Updates
UPDATE_REQUESTED: _,
Expand All @@ -44,7 +41,9 @@ const messages = {
ZOOM_RESET: _,
PRINT_PAGE: _,
SET_AD_DIV_CANDIDATES: _, /** @arg {Array} adDivCandidates, @arg {string} placeholderUrl */
CONTEXT_MENU_OPENED: _ /** @arg {string} nodeName of node being clicked */
CONTEXT_MENU_OPENED: _, /** @arg {string} nodeName of node being clicked */
APP_STATE_CHANGE: _,
APP_ACTION: _
}

module.exports = mapValuesByKeys(messages)
19 changes: 17 additions & 2 deletions js/dispatcher/appDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

var Dispatcher = require('./dispatcher')
'use strict'
const messages = require('../constants/messages')

const appDispatcher = new Dispatcher()
class AppDispatcher {
/**
* dispatch
* @param {object} payload The data from the action.
*/
dispatch (payload) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice I like how you abstracted here so it can be used from both main and renderer processes.

if (process.type === 'renderer') {
global.require('electron').ipcRenderer.send(messages.APP_ACTION, payload)
} else {
process.emit(messages.APP_ACTION, payload)
}
}
}

const appDispatcher = new AppDispatcher()
module.exports = appDispatcher
7 changes: 6 additions & 1 deletion js/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ require('../less/navigationBar.less')
require('../less/tabs.less')
require('../node_modules/font-awesome/css/font-awesome.css')

const URL = require('url')
const Immutable = require('immutable')
const React = require('react')
const ReactDOM = require('react-dom')
const Window = require('./components/window')

// get appStore from url
var appState = Immutable.fromJS(JSON.parse(URL.parse(window.location.href, true).query.appState))

ReactDOM.render(
<Window/>,
<Window appState={appState}/>,
document.getElementById('windowContainer'))
2 changes: 1 addition & 1 deletion js/lib/siteUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

import Immutable from 'immutable'
const Immutable = require('immutable')

/**
* Obtains the index of the location in sites
Expand Down
21 changes: 13 additions & 8 deletions js/state/siteUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

import Immutable from 'immutable'
'use strict'
const Immutable = require('immutable')

var exports = {}

/**
* Obtains the index of the location in sites
Expand All @@ -11,7 +14,7 @@ import Immutable from 'immutable'
* @param location The frameProps of the page in question
* @return index of the location or -1 if not found.
*/
function getSiteUrlIndex (sites, location) {
exports.getSiteUrlIndex = function (sites, location) {
return sites.findIndex(site => site.get('location') === location)
}

Expand All @@ -23,8 +26,8 @@ function getSiteUrlIndex (sites, location) {
* @param tag The tag of the site to check
* @return true if the location is already bookmarked
*/
export function isSiteInList (sites, location, tag) {
let index = getSiteUrlIndex(sites, location)
exports.isSiteInList = function (sites, location, tag) {
let index = exports.getSiteUrlIndex(sites, location)
if (index === -1) {
return false
}
Expand All @@ -42,8 +45,8 @@ export function isSiteInList (sites, location, tag) {
* Otherwise it's only considered to be a history item
* @return The new sites Immutable object
*/
export function addSite (sites, frameProps, tag) {
let index = getSiteUrlIndex(sites, frameProps.get('location'))
exports.addSite = function (sites, frameProps, tag) {
let index = exports.getSiteUrlIndex(sites, frameProps.get('location'))
let tags = sites.getIn([index, 'tags']) || new Immutable.List()
if (tag) {
tags = tags.toSet().add(tag).toList()
Expand Down Expand Up @@ -76,12 +79,14 @@ export function addSite (sites, frameProps, tag) {
* @param frameProps The frameProps of the page in question
* @return The new sites Immutable object
*/
export function removeSite (sites, frameProps, tag) {
let index = getSiteUrlIndex(sites, frameProps.get('location'))
exports.removeSite = function (sites, frameProps, tag) {
let index = exports.getSiteUrlIndex(sites, frameProps.get('location'))
if (index === -1) {
return sites
}

let tags = sites.getIn([index, 'tags'])
return sites.setIn([index, 'tags'], tags.toSet().remove(tag).toList())
}

module.exports = exports
Loading