Skip to content
/ pp.js Public

Pseudo-Parallel, Passing-Procedure, Pretty-Promise. Asynchronous Collection & Procedure Control Flow

License

Notifications You must be signed in to change notification settings

VoQn/pp.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

95 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pp.js - pianissimo -

Build Status

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

Faster, Fewer Cost, Parallel multi process

Benchmark pp.js vs async.js

  • 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.

License

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 # Guide ## CPS (Continuation Passing Style) __pp.js__ is designed by [CPS](http://en.wikipedia.org/wiki/Continuation-passing_style), _Continuation Passing Style_, for effective Asynchronous processing.
# 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 ###]
## Trampolining, "Pseudo-Parallel" __pp.js__ doesn't provide _true_ parallel processing. Parallel processing is a strictly pseudo. This pseudo-parallel processing on [Trampoling](http://en.wikipedia.org/wiki/Trampoline\_\(computing\)).

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

Invocation type of callback, "fill" and "order"

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

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

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

Difference?

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.

Type

One of difference between pp.js with async.js is consisted argument format.

TimeSlice: number (integer milli-second [0 < t])

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)

Callback: function(?Error, [somethings...])

pp.js defined Callback type that is function(Error, [somethings...]).

first argument, received Error, is accepted as nullable.


Iterable: (!Array | !Object as {string: any})

pp.js defined Iterable type that is not null Array or Object.

primitive values ... undefined, null, string, boolean and number aren't accepted.


Iterator: function(callback, [somethings...])

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.


Predicator: function(callback, value, [key, iterable])

pp.js defined Predicator type that is function(callback, value, [key, iterable])

Specially, predicator passing boolean result to callback.

Example

cpsIsEven = (next, value) ->
  next null, value % 2 is 0

Accumulator: function(callback, memo, value, [key, iterable])

pp.js defined Folding type that is function(callback, memo, value, [key, iterable]).

for accumulate array list.

Example

cpsAdd = (next, memo, value) ->
  next null, memo + value

About

Pseudo-Parallel, Passing-Procedure, Pretty-Promise. Asynchronous Collection & Procedure Control Flow

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published