# centralperk

> Manage Complex Work Flow

Latest version **0.4.0** (published 2016-03-28) · ISC license · 0 weekly downloads

## Install

```sh
npm install centralperk
pnpm add centralperk
yarn add centralperk
bun add centralperk
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2016-03-28 |
| First published | 2016-03-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Meng.G |
| Maintainers | gaomeng1900 |

## Links

- npm: https://www.npmjs.com/package/centralperk
- npm.io page: https://npm.io/package/centralperk

## Recent versions

- 0.4.0 (latest) — 2016-03-28
- 0.1.0 — 2016-03-18

## README

# centralperk

Manage Complex Work Flow

Code Async JS 

Elegantly :)

----

**author** Meng G. <gaomeng1900@gmail.com>

**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
<pre>
0 → 1 → 2 → 3
  ↘ 
    4 → 5 → 7
     ↘ 
       6
</pre>
       
What will you do...
(ﾟдﾟ)

Before you crash your ⌨

#### 0. sign the functions
```coffeescript
# 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
# 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
# coffeescript

PERK.link 'task0' 
    .to 'task1', ['task6', 'task5']
    .to 'task2'
    .to 'task3'

# 0 → 1 → 2 → 3
#     ↓ ↘ 
#     ↘  5 
#       6
  
```
```javascript
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}

<blockquote>
- The task function must has 2 arguments.

- 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);
</blockquote>
<blockquote>
- The second one is `done`(or what ever U call it). 

- Call this function means the end of current function. 

- You can pass this `done` to any other function and call it anywhere you want. 
</blockquote>
<blockquote>
- Use `done(obj)` to pass the new arguments to the next task.
</blockquote>



#### sream
- 	@param flow {Object/Map}

<blockquote>
Define a work flow in Object/Map style.
</blockquote>

```
# coffeescript

PERK.stream {
    'task0':
        'task1':
            'task2':
                'task3':{}
        'task4':
            'task5':{}
            'task6':{}
    'task4':
    	'task7':{}
}
# 0 → 1 → 2 → 3
#   ↘ 
#     4 → 5 → 7
#      ↘ 
#        6
#
# 4 → 7
```
** important tip **
<blockquote>
After `0` called `done(someArgs)`, `1` and `4` will be called and `someArgs` will be passed to both of them.
</blockquote>
<blockquote>
If you call `4` like this `PERK.task('task4', someArgs)`, dont worry, only `7` will follow. `5` belong to another flow and it must start with `0`.
</blockquote>



#### 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]

---
_Source: https://npm.io/package/centralperk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
