pp.js is called pianissimo.js, which means Pseudo-Parallel, Passing-Procedure, or Pretty-Promise. pp.js is a javascript library for Asynchronous Collection & Procedure Control Flow.
this library is inspired by async.js, JsDeferred, $.Deferred, and Promise/A. And aiming provide compatible API.
to read this library specification see Guide, Reference
- pp.js faster than async.js (more than 1.25x)
- pp.js use fewer memory async.js (lower than 1/3)
- pp.js work looks like parallel (see Guide/Trampolining)
- while running pp.js process, It does not block user control as possible as.
Copyright (c) 2012 VoQn
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
## API Reference- Utilities
- Promise/A - Implement Promise/A of CommonJS
- Generator - simple Generator like ES-Harmony
- Control Flow API
- Collection API
# sync procedure
sq = (x) ->
x * x
console.log sq 10 # return 10 * 10 -> 100 -> console.log(100) => IO output
# CPS procedure
cpsSq = (next, x) ->
next x * x
cpsSq console.log, 10 # console.log(10 * 10) -> console.log(100) => IO output
# Async procedure
heavyProcessing = (callback, parameters) ->
# do something (use long time, or network communication)
# ...
# ...
callback error, result # when process done, result apply asynchronouse
heavyProcessing (e, r) -> # callback
if e # receive error
# do something
else # process has been succeeded
# do something
, [### parameters ###]
pp.js API are curried function.
For example, CPS sum of number array is this.
printSum = pp.foldl (next, memo, value) ->
if typeof value isnt 'number'
next new TypeError "\"folding\" require number, but #{typeof value}"
else
next null, memo + value
return
, (error, result) ->
console.log result
, 0 # See it! subject array has not apply!
printSum [10, 11, 12] #=> 33
printSum [1, 2, 3, 4, 5] #=> 15
In designing Asynchronous operetion, maybe occur a problem that dependency with each procedures.
Because solve it, pp.js provide two iteration. fill and order.
fill
process is ASAP (As Soon As Possible)
fireStack = []
pp.fill [
(next) ->
setTimeout ->
fireStack.push '1st'
next null, '1st'
, 100
, (next) ->
setTimeout ->
fireStack.push '2nd'
next null, '2nd'
, 200
, (next) ->
setTimeout ->
fireStack.push '3rd'
next null, '3rd'
, 50
], (error, result) ->
# result --- ['1st', '2nd', '3rd']
# fire_stack --- ['3rd', '1st', '2nd']
order
process is keep invocation order.
fireStack = []
pp.order [
(next) ->
setTimeout ->
fireStack.push '1st'
next null, '1st'
, 100
, (next) ->
setTimeout ->
fireStack.push '2nd'
next null, '2nd'
, 200
, (next) ->
setTimeout ->
fireStack.push '3rd'
next null, '3rd'
, 50
], (error, result) ->
# result --- ['1st', '2nd', '3rd']
# fire_stack --- ['1st', '2nd', '3rd']
pp.fill F, G, H, CALLBACK
# eval F -> eval G -> eval H -> (wait callback...) -> eval CALLBACK
pp.order F, G, H, CALLBACK
# eval F -> (wait F callback...) -> eval G -> (wait G callback...) -> ...
Why pp.fill
's name is parallel but fill? Because it run all procedures and wait until all callback is filling.
pp.order
is keeping its ordering. When it began run procedure, wait that callback, run next procedure. Until last.
One of difference between pp.js with async.js is consisted argument format.
pp.TIME_SLICE
provide consts for frame rate.
- FPS_240 - 4ms
- FPS_120 - 8ms
- FPS_60 - 16ms
- FPS_30 - 33ms
- FPS_15 - 66ms
- FPS_1 - 1s (1000ms)
pp.js defined Callback type that is function(Error, [somethings...])
.
first argument, received Error, is accepted as nullable.
pp.js defined Iterable type that is not null Array or Object.
primitive values ... undefined
, null
, string
, boolean
and number
aren't accepted.
pp.js defined Iterator type that is function(callback, [somethings...])
For example, available iterator for Array
function(function:next, any:value, number:index, array:iterable)
function(function:next, any:value, number:index)
function(function:next, any:value)
function(function:next)
for Object,
function(function:next, any:value, string:key, object:iterable)
function(function:next, any:value, string:key)
function(function:next, any:value)
function(function:next)
iterator type need continuation function for 1st argument.
pp.js defined Predicator type that is function(callback, value, [key, iterable])
Specially, predicator passing boolean
result to callback.
cpsIsEven = (next, value) ->
next null, value % 2 is 0
pp.js defined Folding type that is function(callback, memo, value, [key, iterable])
.
for accumulate array list.
cpsAdd = (next, memo, value) ->
next null, memo + value