Skip to content

Commit

Permalink
Initial commit of utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
tshaddix committed Jan 23, 2016
1 parent a349e22 commit 739d7fe
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
33 changes: 33 additions & 0 deletions .eslintrc
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"
]
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ coverage
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

lib

.idea
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/
27 changes: 27 additions & 0 deletions package.json
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"
}
}
9 changes: 9 additions & 0 deletions src/alias/alias.js
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);
}
}
2 changes: 2 additions & 0 deletions src/constants/index.js
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';
5 changes: 5 additions & 0 deletions src/index.js
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};
47 changes: 47 additions & 0 deletions src/store/Store.js
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;
32 changes: 32 additions & 0 deletions src/wrap-store/wrapStore.js
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();
});
};

0 comments on commit 739d7fe

Please sign in to comment.