Skip to content

Commit

Permalink
refactor(function-tree): refactor to ES6
Browse files Browse the repository at this point in the history
  • Loading branch information
christianalfoni committed Oct 17, 2016
1 parent 65770c7 commit 6479bb4
Show file tree
Hide file tree
Showing 20 changed files with 710 additions and 730 deletions.
4 changes: 1 addition & 3 deletions packages/function-tree/package.json
Expand Up @@ -7,7 +7,7 @@
"demo:redux": "node demos/server --demo redux",
"demo:mobx": "node demos/server --demo mobx",
"demo:node": "node demos/node/main.js",
"test": "nodeunit tests",
"test": "../../node_modules/.bin/mocha --compilers js:../../node_modules/babel-register 'tests/**/*.js'",
"build": "../../node_modules/.bin/babel src/ --out-dir=lib/ -s",
"coverage": "../../node_modules/.bin/nyc --reporter=lcov --reporter=json npm run test",
"prepublish": "npm run build"
Expand All @@ -25,8 +25,6 @@
"html-webpack-plugin": "^2.22.0",
"mobx": "^2.5.1",
"mobx-react": "^3.5.5",
"nodeunit": "^0.10.2",
"picture-tube": "^1.0.0",
"react": "^15.3.1",
"react-dom": "^15.3.1",
"react-redux": "^4.4.5",
Expand Down
4 changes: 1 addition & 3 deletions packages/function-tree/src/Abort.js
@@ -1,3 +1 @@
function Abort () {}

module.exports = Abort
export default class Abort {}
21 changes: 11 additions & 10 deletions packages/function-tree/src/Path.js
@@ -1,13 +1,14 @@
function Path (path, payload) {
this.path = path
this.payload = payload
}

Path.prototype.toJS = function () {
return {
path: this.path,
payload: this.payload
class Path {
constructor (path, payload) {
this.path = path
this.payload = payload
}
toJS () {
return {
path: this.path,
payload: this.payload
}
}
}

module.exports = Path
export default Path
20 changes: 11 additions & 9 deletions packages/function-tree/src/executeTree.js
@@ -1,19 +1,20 @@
'use strict'

const assign = require('object-assign')

module.exports = function executeTree (tree, resolveFunctionResult, initialPayload, end) {
/*
Runs through the tree providing a "next" callback to process next step
of execution
*/
export default function executeTree (tree, resolveFunctionResult, initialPayload, end) {
function runBranch (branch, index, payload, nextBranch) {
function runNextItem (result) {
runBranch(branch, index + 1, result, nextBranch)
}

function processFunctionOutput (funcDetails, outputResult) {
return function (result) {
let newPayload = assign({}, payload, result ? result.payload : {})
const newPayload = Object.assign({}, payload, result ? result.payload : {})

if (result && funcDetails.outputs) {
let outputs = Object.keys(funcDetails.outputs)
const outputs = Object.keys(funcDetails.outputs)

if (~outputs.indexOf(result.path)) {
runBranch(funcDetails.outputs[result.path], 0, newPayload, outputResult)
} else {
Expand All @@ -25,15 +26,16 @@ module.exports = function executeTree (tree, resolveFunctionResult, initialPaylo
}
}

let currentItem = branch[index]
const currentItem = branch[index]

if (!currentItem) {
nextBranch ? nextBranch(payload) : end(payload)
} else if (Array.isArray(currentItem)) {
const itemLength = currentItem.length
currentItem.reduce((payloads, action) => {
resolveFunctionResult(action, payload, processFunctionOutput(action, (payload) => {
payloads.push(payload)
if (payloads.length === itemLength) runNextItem(assign.apply(null, [{}].concat(payloads)))
if (payloads.length === itemLength) runNextItem(Object.assign.apply(Object, [{}].concat(payloads)))
}))
return payloads
}, [])
Expand Down
14 changes: 6 additions & 8 deletions packages/function-tree/src/factories/debounce.js
@@ -1,15 +1,15 @@
function createDebounce (time) {
var timer
var currentResolver
let timer
let currentResolver

function debounce (context) {
function debounce ({path}) {
return new Promise(function (resolve) {
if (timer) {
currentResolver(context.path.discarded())
currentResolver(path.discarded())
currentResolver = resolve
} else {
timer = setTimeout(function () {
currentResolver(context.path.accepted())
currentResolver(path.accepted())
timer = null
currentResolver = null
}, time)
Expand All @@ -22,13 +22,11 @@ function createDebounce (time) {
return debounce
}

function debounceFactory (time, continueBranch) {
export default function debounceFactory (time, continueBranch) {
return [
createDebounce(time), {
accepted: continueBranch,
discarded: []
}
]
}

module.exports = debounceFactory

0 comments on commit 6479bb4

Please sign in to comment.