centralperk
v0.4.0
Published
Manage Complex Work Flow
Downloads
1
Readme
centralperk
Manage Complex Work Flow
Code Async JS
Elegantly :)
author Meng G. [email protected]
date 2016-03-21
github https://github.com/gaomeng1900/centralperk
A flow controller based on Subscribe/Publish model and stream model
RequestsJS is required (already in this git , bower install requestsjs
to get the updated)
Import in ADM/global
way, both supported
centralperk.js
contains the flow controler part. browser/node
centralperk.ext.js
contains useful http part (get/post/put/delete) packed from requests.js. browser only
对于异步编程,有很多现成的解决方案包括
- ES6 async/Promise
- step.js
- co.js
- then.js
- wind.js
- so on...
但是接口还可以更简洁友好,尤其是处理包含异步编程的复杂任务流程时,step之间应该进一步解耦。
centralperk采用了类似于 publish-subscribe pattern 的方式处理异步回调顺序(事实上,前三个版本都是典型的publish-subscribe pattern),将同步任务和异步任务统一由事件中心管理。支持pipe风格的接口,以及针对十分复杂的逻辑的特殊接口:直接以瀑布流式的字面量定义流程。
实现一个订阅中心进行a->b, a->b&c的管理是容易的(centralperk第一版),但是涉及到多级流程时就要正确处理每次任务触发时所在的flow,并避免多条支线交叉时的引用冲突(第二版),另外还有对性能的优化和对ES6规范的支持(第三版)。
centralperk(v4)的核心在于任务链管理,使用闭包传递一个 stream
变量来标示当前调用/构造所在的flow 以及flow中的位置,来做到flow之间的完全隔离,同时并入重构后的requests.js, 获得更简洁更强大的restful调用接口,并增加了一些新的特性。
实例在test.coffee中。
You may find examples in test.coffee
PS. this manual is long and boring
a example
栗子
Suppose that there a 7 jobs(sync ones and async ones), and you want them excecute in flow like this
What will you do... (゚д゚)
Before you crash your ⌨
0. sign the functions
# coffeescript
PERK.sign 'task0', (args, done)->
# do something
done(args)
PERK.sign 'task1', (args, done)->
# maybe some XHR
PERK.get {url:'http://httpbin.org/ip'}, args, done
PERK.sign 'task2', (args, done)->
# maybe your own async task
some_async_fun(args, done)
# other 4 ones...
前方高~~萌~~能
1. set the stream-like flow
# coffeescript
PERK.stream {
'task0':
'task1':
'task2':
'task3':{}
'task4':
'task5':{}
'task6':{}
}
2. Here we go!
PERK.task('task0', args)
You can set the flow in gulp.task.pipe way
# coffeescript
PERK.link 'task0'
.to 'task1', ['task6', 'task5']
.to 'task2'
.to 'task3'
# 0 → 1 → 2 → 3
# ↓ ↘
# ↘ 5
# 6
PERK.stream( { 'task0':{ 'task1':{ 'task2':{} } } } );
// equal to
PERK.link('task0').to('task1').to('task2');
// 0 → 1 → 2
API
sign
- @param taskID {String} : name your task
- @param fun {Function}: the task function
- @return PERK {Object}
The first one is
args
(or what ever U call it).Read more about Currying [https://en.wikipedia.org/wiki/Currying].
It will be passed from the previous fun in the flow or from PERK.task('taskName', args);
Call this function means the end of current function.
You can pass this
done
to any other function and call it anywhere you want.
sream
- @param flow {Object/Map}
# coffeescript
PERK.stream {
'task0':
'task1':
'task2':
'task3':{}
'task4':
'task5':{}
'task6':{}
'task4':
'task7':{}
}
# 0 → 1 → 2 → 3
# ↘
# 4 → 5 → 7
# ↘
# 6
#
# 4 → 7
** important tip **
task
@param taskName {String}
@param args {Object}
Do some task. it will start a flow when this task is at the root of it.
link
- @param taskName {String}
to
- @param taskName {String}
get/post/put/delete
- pack of XHR, based on Requests.js
- see examples and doc of Requests.js [https://github.com/gaomeng1900/requests]