-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
164 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["es2015"] | ||
} |
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,33 @@ | ||
{ | ||
"rules": { | ||
"indent": [ | ||
2, | ||
4 | ||
], | ||
"quotes": [ | ||
2, | ||
"double" | ||
], | ||
"linebreak-style": [ | ||
2, | ||
"unix" | ||
], | ||
"semi": [ | ||
2, | ||
"always" | ||
] | ||
}, | ||
"env": { | ||
"es6": true, | ||
"node": true, | ||
"browser": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"ecmaFeatures": { | ||
"jsx": true, | ||
"experimentalObjectRestSpread": true | ||
}, | ||
"plugins": [ | ||
"react" | ||
] | ||
} |
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 @@ | ||
src/ |
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,27 @@ | ||
{ | ||
"name": "react-chromex-redux", | ||
"version": "0.0.1", | ||
"description": "A set of utilities for building React+Redux applications in Google Chrome Extensions.", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"prepublish": "./node_modules/.bin/babel src --out-dir lib", | ||
"pretest": "./node_modules/.bin/babel src --out-dir lib", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/tshaddix/react-chromex-redux.git" | ||
}, | ||
"author": "Tyler Shaddix", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/tshaddix/react-chromex-redux/issues" | ||
}, | ||
"homepage": "https://github.com/tshaddix/react-chromex-redux#readme", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"babel": "^6.3.26", | ||
"babel-cli": "^6.4.5", | ||
"babel-preset-es2015": "^6.3.13" | ||
} | ||
} |
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,9 @@ | ||
export default aliases => store => next => action => { | ||
const alias = aliases[action.type]; | ||
|
||
if (alias) { | ||
return next(alias(action)); | ||
} else { | ||
return next(action); | ||
} | ||
} |
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,2 @@ | ||
export const DISPATCH_TYPE = 'chromex.dispatch'; | ||
export const STATE_TYPE = 'chromex.state'; |
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,5 @@ | ||
import Store from './store/Store'; | ||
import wrapStore from './wrap-store/wrapStore'; | ||
import alias from './alias/alias'; | ||
|
||
export {Store, wrapStore, alias}; |
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,47 @@ | ||
import { | ||
DISPATCH_TYPE, | ||
STATE_TYPE | ||
} from '../constants'; | ||
|
||
class Store { | ||
constructor({portName, state = {}}) { | ||
this.port = chrome.runtime.connect({name: portName}); | ||
this.listeners = []; | ||
this.state = state; | ||
|
||
this.port.onMessage.addListener((message) => { | ||
if (message.type === STATE_TYPE) { | ||
this.replaceState(message.payload); | ||
} | ||
}); | ||
|
||
this.dispatch = this.dispatch.bind(this); | ||
} | ||
|
||
subscribe(listener) { | ||
this.listeners.push(listener); | ||
|
||
return () => { | ||
this.listeners = this.listeners.filter((l) => l !== listener); | ||
}; | ||
} | ||
|
||
replaceState(state) { | ||
this.state = state; | ||
|
||
this.listeners.forEach((l) => l()); | ||
} | ||
|
||
getState() { | ||
return this.state; | ||
} | ||
|
||
dispatch(data) { | ||
return this.port.postMessage({ | ||
type: DISPATCH_TYPE, | ||
payload: data | ||
}); | ||
} | ||
} | ||
|
||
export default Store; |
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,32 @@ | ||
import { | ||
DISPATCH_TYPE, | ||
STATE_TYPE | ||
} from '../constants'; | ||
|
||
export default (store, {portName}) => { | ||
chrome.runtime.onConnect.addListener((port) => { | ||
if (port.name !== portName) { | ||
return; | ||
} | ||
|
||
const sendState = () => { | ||
port.postMessage({ | ||
type: STATE_TYPE, | ||
payload: store.getState() | ||
}); | ||
}; | ||
|
||
port.onMessage.addListener((msg) => { | ||
if (msg.type === DISPATCH_TYPE) { | ||
store.dispatch(msg.payload); | ||
} | ||
}); | ||
|
||
const unsubscribe = store.subscribe(sendState); | ||
|
||
port.onDisconnect.addListener(unsubscribe); | ||
|
||
// send initial state | ||
sendState(); | ||
}); | ||
}; |