This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 973
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from brave/split_app_and_window
create appStore for global state
- Loading branch information
Showing
8 changed files
with
125 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters