Skip to content
This repository has been archived by the owner on Jul 7, 2023. It is now read-only.

Commit

Permalink
- refactor folder structure; output to lib instead of dist
Browse files Browse the repository at this point in the history
- create REPLACE_PLUGIN action
  • Loading branch information
jedwards1211 committed May 15, 2016
1 parent b17d17e commit 6a1dda5
Show file tree
Hide file tree
Showing 18 changed files with 334 additions and 248 deletions.
24 changes: 24 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"parser": "babel-eslint",
"extends": [
"eslint:recommended"
],
"rules": {
"comma-dangle": 2,
"comma-spacing": 2,
"eol-last": 2,
"indent": ["error", 2],
"keyword-spacing": 2,
"linebreak-style": 2,
"no-console": 2,
"no-unused-vars": [2, {"args": "none", "varsIgnorePattern": "React"}],
"no-extra-semi": 2,
"no-trailing-spaces": 2,
"no-unexpected-multiline": 2,
"no-unreachable": 2,
"semi": ["error", "never"]
},
"env": {
"node": true
}
}
2 changes: 1 addition & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[ignore]
<PROJECT_ROOT>/src/test/.*
<PROJECT_ROOT>/test/.*
.*/node_modules/react/.*
.*/node_modules/fbjs/.*
.*/node_modules/json5/.*
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
lib
testlib
4 changes: 2 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
src/test
dist/test
test
testlib
spec
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "redux-plugins-immutable",
"version": "1.0.6",
"version": "1.1.0",
"description": "a powerful app plugin system for redux using immutable state",
"main": "dist/index.js",
"main": "lib/index.js",
"scripts": {
"lint": "eslint src; exit 0",
"build": "rm -rf dist; NODE_ENV=production babel src --out-dir dist",
"test": "npm run build && jasmine",
"prepublish": "eslint src && flow status && npm run test"
"lint": "eslint src test; exit 0",
"lint:fix": "eslint src test --fix; exit 0",
"build": "rm -rf lib; NODE_ENV=production babel src --out-dir lib",
"test": "rm -rf testlib; NODE_ENV=production babel src --out-dir testlib/src && NODE_ENV=production babel test --out-dir testlib/test && jasmine",
"prepublish": "eslint src test --fix && flow status && npm run test && npm run build"
},
"repository": {
"type": "git",
Expand All @@ -32,10 +33,10 @@
"babel-core": "^6.4.0",
"babel-eslint": "^6.0.4",
"babel-plugin-transform-flow-strip-types": "^6.4.0",
"babel-polyfill": "^6.8.0",
"babel-preset-es2015": "^6.3.13",
"babel-preset-stage-1": "^6.3.13",
"eslint": "^2.9.0",
"eslint-plugin-react": "^5.1.1",
"eslint": "^2.10.1",
"immutable": "^3.8.1",
"jasmine": "^2.4.1",
"redux": "^3.3.1"
Expand Down
2 changes: 1 addition & 1 deletion spec/support/jasmine.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"spec_dir": "dist/test",
"spec_dir": "testlib/test",
"spec_files": [
"**/*.js"
],
Expand Down
13 changes: 2 additions & 11 deletions src/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
{
"parser": "babel-eslint",
"extends": "eslint:recommended",
"ecmaFeatures": {
"modules": true,
},
"rules": {
"comma-dangle": 0,
"no-unused-vars": [1, {"args": "none", "varsIgnorePattern": "React"}],
"strict": 0,
},
"env": {
"browser": true
"browser": true,
"node": false
}
}
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as pluginActions from './lib/pluginActions';
import pluginMiddleware from './lib/pluginMiddleware';
import pluginReducer from './lib/pluginReducer';
import * as pluginTypes from './lib/pluginTypes';
import * as pluginActions from './pluginActions'
import pluginMiddleware from './pluginMiddleware'
import pluginReducer from './pluginReducer'
import * as pluginTypes from './pluginTypes'

export {pluginActions, pluginMiddleware, pluginReducer, pluginTypes};
export {pluginActions, pluginMiddleware, pluginReducer, pluginTypes}
7 changes: 0 additions & 7 deletions src/lib/pluginTypes.js

This file was deleted.

30 changes: 19 additions & 11 deletions src/lib/pluginActions.js → src/pluginActions.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
/* @flow */

import * as Immutable from 'immutable';
import type {LoadStatus} from './pluginTypes';
import type {Action} from './reduxTypes';
import * as Immutable from 'immutable'
import type {LoadStatus} from './pluginTypes'
import type {Action} from './reduxTypes'

export const ADD_PLUGIN = 'ADD_PLUGIN';
export const LOAD_PLUGIN = 'LOAD_PLUGIN';
export const INSTALL_PLUGIN = 'INSTALL_PLUGIN';
export const SET_PLUGIN_STATUS = 'SET_PLUGIN_STATUS';
export const ADD_PLUGIN = 'ADD_PLUGIN'
export const REPLACE_PLUGIN = 'REPLACE_PLUGIN'
export const LOAD_PLUGIN = 'LOAD_PLUGIN'
export const INSTALL_PLUGIN = 'INSTALL_PLUGIN'
export const SET_PLUGIN_STATUS = 'SET_PLUGIN_STATUS'

export function addPlugin(plugin: Immutable.Collection): Action {
return {
type: ADD_PLUGIN,
payload: plugin
};
}
}

export function replacePlugin(plugin: Immutable.Collection): Action {
return {
type: REPLACE_PLUGIN,
payload: plugin
}
}

export function loadPlugin(key: string): Action {
return {
type: LOAD_PLUGIN,
payload: key
};
}
}

export function installPlugin(plugin: Immutable.Collection): Action {
return {
type: INSTALL_PLUGIN,
payload: plugin
};
}
}

export function setPluginStatus(key: string, payload: {loadStatus?: LoadStatus, loadError?: Error}): Action {
return {
type: SET_PLUGIN_STATUS,
payload,
meta: {key}
};
}
}
56 changes: 28 additions & 28 deletions src/lib/pluginMiddleware.js → src/pluginMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
import * as Immutable from 'immutable';
import Promise from 'bluebird';
import * as Immutable from 'immutable'
import Promise from 'bluebird'

import {createSelector} from 'reselect';
import {createSelector} from 'reselect'

import {createMiddleware, composeMiddleware} from 'mindfront-redux-utils';
import {createMiddleware, composeMiddleware} from 'mindfront-redux-utils'

import {LOADING, NOT_LOADED, LOADED} from './pluginTypes';
import {LOADING, NOT_LOADED, LOADED} from './pluginTypes'

import type {Middleware} from './reduxTypes';
import type {Middleware} from './reduxTypes'

import {
setPluginStatus,
LOAD_PLUGIN,
installPlugin,
} from './pluginActions';
installPlugin
} from './pluginActions'

const loadPluginHandler: Middleware = store => next => action => {
next(action);
next(action)

let key = action.payload;
let plugin = store.getState().getIn(['plugins', key]);
let key = action.payload
let plugin = store.getState().getIn(['plugins', key])
if (plugin) {
if (plugin.get('loadStatus') === LOADED) {
return Promise.resolve(plugin);
return Promise.resolve(plugin)
}
let load = plugin.get('load');
let load = plugin.get('load')
if (!(load instanceof Function)) {
return Promise.reject(new Error(`plugin must have loaded status or a load method`));
return Promise.reject(new Error(`plugin must have loaded status or a load method`))
}
store.dispatch(setPluginStatus(key, {loadStatus: LOADING}));
store.dispatch(setPluginStatus(key, {loadStatus: LOADING}))

let returnedPromise;
let returnedPromise
let callbackPromise = Promise.fromNode(callback => {
returnedPromise = load(store, callback);
});
returnedPromise = load(store, callback)
})

return (returnedPromise || callbackPromise).catch((error: Error) => {
store.dispatch(setPluginStatus(key, {loadStatus: NOT_LOADED, loadError: error}));
throw error;
store.dispatch(setPluginStatus(key, {loadStatus: NOT_LOADED, loadError: error}))
throw error
})
.then((plugin: Immutable.Collection) => {
if (plugin instanceof Immutable.Collection) {
store.dispatch(installPlugin(plugin.set('key', key)));
return plugin;
store.dispatch(installPlugin(plugin.set('key', key)))
return plugin
}
else {
throw new Error('plugin must be an Immutable.Collection');
throw new Error('plugin must be an Immutable.Collection')
}
});
})
}
else {
return Promise.reject(new Error(`no plugin with key '${key}' exists`));
return Promise.reject(new Error(`no plugin with key '${key}' exists`))
}
};
}

const selectPluginMiddleware: (state: Immutable.Collection) => Middleware = createSelector(
state => state.get('plugins'),
plugins => plugins ?
composeMiddleware(...plugins.map(p => p.get('middleware')).toArray()) :
store => next => action => next(action)
);
)

export default composeMiddleware(
createMiddleware({
[LOAD_PLUGIN]: loadPluginHandler
}),
store => next => action => selectPluginMiddleware(store.getState())(store)(next)(action)
);
)
Loading

0 comments on commit 6a1dda5

Please sign in to comment.