Skip to content

Commit

Permalink
feat(providers): add devtools specific providers
Browse files Browse the repository at this point in the history
  • Loading branch information
christianalfoni authored and Guria committed Apr 19, 2016
1 parent 50b2d10 commit af6aacc
Show file tree
Hide file tree
Showing 7 changed files with 419 additions and 7 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-env browser*/
var MODULE = 'cerebral-module-devtools'
var SignalStore = require('cerebral-module-signal-store')
var utils = require('./utils')
var requestAnimationFrame = requestAnimationFrame || function (cb) { setTimeout(cb) }
Expand All @@ -9,7 +8,12 @@ module.exports = function Devtools () {
if (typeof window.chrome === 'undefined') { return function () {} }

return function init (module, controller) {
module.alias(MODULE)
if (controller.addContextProvider) {
controller.addContextProvider(require('./providers/actionServicesCallsProvider'))
controller.addContextProvider(require('./providers/actionOutputProvider'))
controller.addContextProvider(require('./providers/actionInputProvider'))
controller.addContextProvider(require('./providers/signalOptionsProvider'))
}

module.addModules({
store: SignalStore()
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"homepage": "https://github.com/cerebral/cerebral-module-devtools#readme",
"devDependencies": {
"cerebral": "^0.33.8",
"cerebral": "^0.33.33",
"commitizen": "^2.5.0",
"conventional-changelog": "0.0.17",
"coveralls": "^2.11.6",
Expand All @@ -35,10 +35,10 @@
"validate-commit-msg": "^1.1.1"
},
"peerDependencies": {
"cerebral": "^0.33.8 || ^0.34.0-0"
"cerebral": "^0.33.8 || ^0.34.0-rc.4"
},
"dependencies": {
"cerebral-module-signal-store": "^0.1.7"
"cerebral-module-signal-store": "^0.1.8"
},
"config": {
"commitizen": {
Expand Down
4 changes: 4 additions & 0 deletions providers/actionInputProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function (context, execution) {
execution.action.input = execution.payload
return context
}
22 changes: 22 additions & 0 deletions providers/actionOutputProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = function (context, execution) {
var originalOutput = context.output
var outputPaths = Object.keys(context.output)
var output = function () {
var path = typeof arguments[0] === 'string' ? arguments[0] : null
var payload = path ? arguments[1] : arguments[0]
execution.action.output = payload
originalOutput.apply(null, arguments)
}

outputPaths.reduce(function (output, key) {
output[key] = function () {
execution.action.output = arguments[0] || {}
originalOutput[key].apply(null, arguments)
}
return output
}, output)

context.output = output

return context
}
44 changes: 44 additions & 0 deletions providers/actionServicesCallsProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var convertServices = function (action, path, modulesPaths, services) {
return Object.keys(services).reduce(function (newservices, key) {
path.push(key)
if (
typeof services[key] === 'function' &&
services[key].constructor.name === 'Function' &&
!Object.keys(services[key]).length &&
(!services[key].prototype || !Object.keys(services[key].prototype).length)
) {
var servicePath = path.slice()
var method = servicePath.pop()
newservices[key] = function () {
action.serviceCalls.push({
name: servicePath.join('.'),
method: method,
args: [].slice.call(arguments)
})
return services[key].apply(this, arguments)
}
} else if (
typeof services[key] === 'object' &&
!Array.isArray(services[key]) &&
services[key] !== null &&
modulesPaths.indexOf(path.join('.')) >= 0
) {
newservices[key] = convertServices(action, path, modulesPaths, services[key])
} else {
newservices[key] = services[key]
}
path.pop(key)
return newservices
}, {})
}

module.exports = function (context, execution, controller) {
var action = execution.action
var modules = controller.getModules()
var services = controller.getServices()
var path = []
action.serviceCalls = action.serviceCalls || []
context.services = convertServices(action, path, Object.keys(modules), services)

return context
}
5 changes: 5 additions & 0 deletions providers/signalOptionsProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function (context, execution) {
execution.signal.isRecorded = execution.options.isRecorded
execution.signal.isRouted = execution.options.isRouted
return context
}
Loading

0 comments on commit af6aacc

Please sign in to comment.