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

Commit

Permalink
Merge pull request #79 from brave/split_app_and_window
Browse files Browse the repository at this point in the history
create appStore for global state
  • Loading branch information
bridiver committed Dec 22, 2015
2 parents 0063bde + e540b39 commit f9c256e
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 39 deletions.
32 changes: 21 additions & 11 deletions docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ Child components should not modify top level state directly, instead they should

# Hierarchy

AppStore

```javascript
{
sites: [{
location: string,
title: string,
tags: [string], // empty, 'bookmark', 'pinned', or 'reader'
lastAccessed: datetime,
}],
visits: [{
location: string,
startTime: datetime
endTime: datetime
}],
updateAvailable: false,
}
```

WindowStore

```javascript
{
activeFrameKey: number,
Expand Down Expand Up @@ -52,17 +73,6 @@ Child components should not modify top level state directly, instead they should
autocompleteURL: string, // ditto re: {searchTerms}
}
}],
sites: [{
location: string,
title: string,
tags: [string], // empty, 'bookmark', 'pinned', or 'reader'
lastAccessed: datetime,
}],
visits: [{
location: string,
startTime: datetime
endTime: datetime
}],
closedFrames: [], // holds the same type of frame objects as above
ui: {
tabs: {
Expand Down
4 changes: 2 additions & 2 deletions js/components/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Main extends ImmutableComponent {
<NavigationBar
navbar={activeFrame && activeFrame.get('navbar')}
frames={this.props.browser.get('frames')}
sites={this.props.browser.get('sites')}
sites={this.props.app.get('sites')}
activeFrame={activeFrame}
searchSuggestions={activeFrame && activeFrame.getIn(['navbar', 'searchSuggestions'])}
searchDetail={activeFrame && activeFrame.get('searchDetail')}
Expand All @@ -119,7 +119,7 @@ class Main extends ImmutableComponent {
key='tab-bar'
activeFrame={activeFrame}
/>
{this.props.browser.get('updateAvailable') ? <UpdateBar/> : null}
{this.props.app.get('updateAvailable') ? <UpdateBar/> : null}
</div>
<div className='mainContainer'
onFocus={this.onMainFocus.bind(this)}>
Expand Down
15 changes: 12 additions & 3 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')

class Window extends React.Component {
constructor () {
super()
this.state = {
immutableData: WindowStore.getState()
immutableData: {
windowState: WindowStore.getState(),
appState: AppStore.getState()
}
}
WindowStore.addChangeListener(this.onChange.bind(this))
AppStore.addChangeListener(this.onChange.bind(this))
}

render () {
return <div id='windowContainer'>
<Main browser={this.state.immutableData}/>
<Main browser={this.state.immutableData.windowState} app={this.state.immutableData.appState} />
</div>
}

Expand All @@ -34,9 +39,13 @@ class Window extends React.Component {

onChange () {
this.setState({
immutableData: WindowStore.getState()
immutableData: {
windowState: WindowStore.getState(),
appState: AppStore.getState()
}
})
}

}

module.exports = Window
13 changes: 13 additions & 0 deletions js/constants/appConstants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */

const mapValuesByKeys = require('../lib/functional').mapValuesByKeys

const _ = null
const AppConstants = {
APP_ADD_SITE: _,
APP_REMOVE_SITE: _
}

module.exports = mapValuesByKeys(AppConstants)
2 changes: 0 additions & 2 deletions js/constants/windowConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ const windowConstants = {
APP_SET_ACTIVE_FRAME_SHORTCUT: _,
APP_SET_URL_BAR_AUTOSELECTED: _,
APP_SET_SEARCH_DETAIL: _,
APP_ADD_SITE: _,
APP_REMOVE_SITE: _,
APP_SET_AUDIO_MUTED: _,
APP_SET_AUDIO_PLAYBACK_ACTIVE: _
}
Expand Down
8 changes: 8 additions & 0 deletions js/dispatcher/appDispatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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')

const appDispatcher = new Dispatcher()
module.exports = appDispatcher
67 changes: 67 additions & 0 deletions js/stores/appStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */

const AppDispatcher = require('../dispatcher/appDispatcher')
const EventEmitter = require('events').EventEmitter
const AppConstants = require('../constants/appConstants')
const Immutable = require('immutable')
const SiteUtil = require('../state/siteUtil')
const ipc = global.require('electron').ipcRenderer
const messages = require('../constants/messages')

// For this simple example, store immutable data object for a simple counter.
// This is of course very silly, but this is just for an app template with top
// level immutable data.
let appState = Immutable.fromJS({
sites: [],
visits: [],
updateAvailable: false
})

var CHANGE_EVENT = 'change'

class AppStore extends EventEmitter {
getState () {
return appState
}

emitChange () {
this.emit(CHANGE_EVENT)
}

addChangeListener (callback) {
this.on(CHANGE_EVENT, callback)
}

removeChangeListener (callback) {
this.removeListener(CHANGE_EVENT, callback)
}
}

const appStore = new AppStore()

// Register callback to handle all updates
AppDispatcher.register((action) => {
switch (action.actionType) {
case AppConstants.APP_ADD_SITE:
appState = appState.set('sites', SiteUtil.addSite(appState.get('sites'), action.frameProps, action.tag))
appStore.emitChange()
break
case AppConstants.APP_REMOVE_SITE:
appState = appState.set('sites', SiteUtil.removeSite(appState.get('sites'), action.frameProps, action.tag))
appStore.emitChange()
break
default:
}
})

ipc.on(messages.UPDATE_AVAILABLE, () => {
console.log('appStore update-available')
appState = appState.merge({
updateAvailable: true
})
appStore.emitChange()
})

module.exports = appStore
23 changes: 2 additions & 21 deletions js/stores/windowStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const EventEmitter = require('events').EventEmitter
const WindowConstants = require('../constants/windowConstants')
const Immutable = require('immutable')
const FrameStateUtil = require('../state/frameStateUtil')
const SiteUtil = require('../state/siteUtil')
const ipc = global.require('electron').ipcRenderer
const messages = require('../constants/messages')

Expand All @@ -18,14 +17,12 @@ let windowState = Immutable.fromJS({
activeFrameKey: null,
frames: [],
closedFrames: [],
sites: [],
searchDetail: null,
updateAvailable: false,
ui: {
tabs: {
activeDraggedTab: null
}
}
},
searchDetail: null
})

var CHANGE_EVENT = 'change'
Expand Down Expand Up @@ -275,14 +272,6 @@ WindowDispatcher.register((action) => {
searchDetail: action.searchDetail
})
break
case WindowConstants.APP_ADD_SITE:
windowState = windowState.set('sites', SiteUtil.addSite(windowState.get('sites'), action.frameProps, action.tag))
windowStore.emitChange()
break
case WindowConstants.APP_REMOVE_SITE:
windowState = windowState.set('sites', SiteUtil.removeSite(windowState.get('sites'), action.frameProps, action.tag))
windowStore.emitChange()
break
case WindowConstants.APP_SET_AUDIO_MUTED:
windowState = windowState.setIn(['frames', FrameStateUtil.getFramePropsIndex(windowState.get('frames'), action.frameProps), 'audioMuted'], action.muted)
windowStore.emitChange()
Expand All @@ -295,14 +284,6 @@ WindowDispatcher.register((action) => {
}
})

ipc.on(messages.UPDATE_AVAILABLE, () => {
console.log('windowStore update-available')
windowState = windowState.merge({
updateAvailable: true
})
windowStore.emitChange()
})

ipc.on(messages.SHORTCUT_NEXT_TAB, () => {
windowState = FrameStateUtil.makeNextFrameActive(windowState)
updateTabPageIndex(FrameStateUtil.getActiveFrame(windowState))
Expand Down

0 comments on commit f9c256e

Please sign in to comment.