cerebral-modules
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -6,2 +6,3 @@ 'use strict'; | ||
}); | ||
var activeModulePath = ['modules', 'activeModule']; | ||
@@ -11,4 +12,4 @@ exports['default'] = function (name) { | ||
return function setActiveModule(input, state) { | ||
if (state.get(['activeModule']) !== name) { | ||
state.set(['activeModule'], name); | ||
if (state.get(activeModulePath) !== name) { | ||
state.set(activeModulePath, name); | ||
} | ||
@@ -15,0 +16,0 @@ }; |
@@ -6,64 +6,57 @@ 'use strict'; | ||
}); | ||
exports['default'] = setupModules; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
exports['default'] = load; | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | ||
var _actionsSetActiveModule = require('./actions/setActiveModule'); | ||
var modulesPath = ['modules']; | ||
var _actionsSetActiveModule2 = _interopRequireDefault(_actionsSetActiveModule); | ||
function setInitialState(initialState) { | ||
return function setInitialStateAction(input, state) { | ||
state.set(modulesPath, initialState); | ||
}; | ||
} | ||
function setupModules(controller, modules) { | ||
var appChains = {}; | ||
function load(controller) { | ||
var modules = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
if (!Array.isArray(modules) || !modules.length) { | ||
console.error('No modules loaded'); | ||
} else { | ||
(function () { | ||
var initChain = []; | ||
var initialState = {}; | ||
var initChain = []; | ||
modules.forEach(function (mod) { | ||
if (!mod.name) { | ||
console.error('Cannot load unnamed module', mod); | ||
} else { | ||
(function () { | ||
Object.keys('modules').forEach(function (moduleName) { | ||
var module = modules[moduleName]; | ||
// load the module | ||
var chains = typeof mod.init === 'function' ? mod.init(controller) || {} : {}; | ||
// init the module passing controller and registered module name | ||
// add the module init chain to the modules init chain | ||
if (Array.isArray(chains.init)) { | ||
initChain.push.apply(initChain, _toConsumableArray(chains.init)); | ||
} | ||
var _ref = typeof module.init === 'function' ? module.init(controller, moduleName, modules) || {} : {}; | ||
// add the module's external chains | ||
if (mod.isService) { | ||
appChains[mod.name] = chains; | ||
} else { | ||
appChains[mod.name] = {}; | ||
// all non-service modules need an opened chain | ||
if (!Array.isArray(chains.opened)) { | ||
chains.opened = []; | ||
} | ||
// non-service modules should be set as active when external chains are run | ||
Object.keys(chains).forEach(function (chain) { | ||
return appChains[mod.name][chain] = [(0, _actionsSetActiveModule2['default'])(mod.name)].concat(_toConsumableArray(chains[chain])); | ||
}); | ||
} | ||
})(); | ||
} | ||
}); | ||
var init = _ref.init; | ||
var state = _ref.state; | ||
var extend = _ref.extend; | ||
// if one or more modules had an init chain, execute the module.init signal | ||
if (initChain.length) { | ||
controller.signal('modules.init', initChain); | ||
controller.signals.modules.init(); | ||
} | ||
})(); | ||
// add the module init chain to the modules init chain | ||
if (Array.isArray(init)) { | ||
initChain.push.apply(initChain, _toConsumableArray(init)); | ||
} | ||
if (state) { | ||
initialState[moduleName] = state; | ||
} | ||
if (extend) { | ||
modules[moduleName] = _extends({}, module, extend); | ||
} | ||
}); | ||
// if one or more modules had an init chain, execute the module.init signal | ||
if (initChain.length) { | ||
controller.signal('modules.init', [setInitialState(initialState)].concat(initChain)); | ||
controller.signals.modules.init(); | ||
} | ||
return appChains; | ||
return modules; | ||
} | ||
module.exports = exports['default']; |
{ | ||
"name": "cerebral-modules", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "A library for making modular cerebral apps", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"lint": "eslint src test", | ||
"dev": "parallelshell 'babel src --watch --out-dir lib' 'watch --wait=1 \"eslint src test && mocha --compilers js:babel/register --recursive --reporter dot\" src test'", | ||
"lint": "semistandard", | ||
"dev": "parallelshell 'babel src --watch --out-dir lib' 'watch --wait=1 \"semistandard && mocha --compilers js:babel/register --recursive --reporter dot\" src test'", | ||
"prebuild": "npm run lint", | ||
@@ -24,2 +24,5 @@ "build": "babel src --out-dir lib", | ||
"author": "Garth Williams", | ||
"contributors": [ | ||
"Aleksey Guryanov <gurianov@gmail.com>" | ||
], | ||
"license": "MIT", | ||
@@ -34,9 +37,12 @@ "bugs": { | ||
"chai": "^3.4.1", | ||
"eslint": "^1.10.3", | ||
"mocha": "^2.3.4", | ||
"parallelshell": "^2.0.0", | ||
"semistandard": "^7.0.4", | ||
"watch": "^0.17.1" | ||
}, | ||
"dependencies": {}, | ||
"peerDependencies": {} | ||
"peerDependencies": {}, | ||
"semistandard": { | ||
"parser": "babel-eslint" | ||
} | ||
} |
# cerebral-modules | ||
Create cerebral modules that can be reused, shared, imported or just simply keep your app tidy. | ||
## Install | ||
``` | ||
npm install cerebral-modules | ||
``` | ||
## Creating a module | ||
A module needs to export the following: | ||
```js | ||
export default { | ||
name: 'uniqueModuleName', | ||
isService: false, //service modules do not affect the activeModule state | ||
init(controller) { | ||
// initialise the module here | ||
// return signal chains | ||
return { | ||
init: [] // init chain will be execute on app start | ||
// other chains that can be used to setup the routing for the app | ||
}; | ||
}, | ||
// optional root component | ||
Component: ModuleIndexComponent | ||
} | ||
``` | ||
## Loading modules | ||
From your main.js | ||
```js | ||
// import your cerebral controller | ||
import controller from './controller'; | ||
// import you app modules | ||
import external form 'external-node-module'; | ||
import home from './modules/home'; | ||
import notFound from './modules/notFound'; | ||
// init the modules | ||
import setupModules from 'cerebral-modules'; | ||
const chains = setupModules(controller, [ external, home, notFound ]); | ||
// use the chains to setup your routing here | ||
// chains.moduleName.chainName | ||
``` | ||
## Contribute | ||
Fork repo | ||
* `npm install` | ||
* `npm run dev` runs dev mode which watches for changes and auto lints, tests and builds | ||
* `npm test` runs the tests | ||
* `npm run lint` lints the code | ||
* `npm run build` compiles es6 to es5 | ||
Modules are currenly being added to `cerebral` and `cerebral-react` directly, making this experimental library redundant. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5122
4