make-sync
This module uses node-fibers to transform asynchronous functions into synchronous ones. This is designed to work with asynchronous functions following the standard ('done' callback as the last arg).
The main commands are:
- MakeSync to synchronize a function or object.
- Sync to start a sync environment (starts a fiber).
When applied to an object, MakeSync patches all the object methods by default. It is also possible to pass some options to be more specific.
The following modes may be used to to make function synchronous (see description further down):
- sync (default)
- async
- mixed-args (default 'mixed')
- mixed-fibers
install
npm install make-sync
usage (coffeescript)
simple example
require 'make-sync' = res = a+b done nullres # making synchronous f = MakeSync f # sync call Sync -> res = f 12 consolelog "sync ->"res obj = : res = a+b done nullres # making synchronous MakeSync obj # sync call Sync -> res = objf 12 consolelog "obj sync ->"res
modes
sync (default)
f = MakeSync f# or f = MakeSync fmode:'sync'
This mode assumes that the function is always called in sync mode within a fiber, so that the 'done' callback is never there. (ie if there is a function at the end it will assume this is a function argument and add it own callback)
async
f = MakeSync fmode:'async'
This mode assumes that the function is always called in asynchronous mode, so doesn't change the function behavior. (probably not useful in most case)
mixed-args (default mixed)
f = MakeSync fmode:'mixed'# or f = MakeSync fmode:'mixed''args'
This mode uses the function arguments to determine wether it needs to be called synchronously or asynchronously. When the last argument is a function, MakeSync assume the last argument is the 'done' callback. There may be some issues when using other function arguments. Please refer to the section below.
fixed numbers of args / function arguments
This only applies when using the mixed-args mode and calling the function synchronously.
There are 2 strategies to resolve the confusion between the 'done' callback and other function argument, when those are passed at the end of the argument list:
- use undefined as the last argument
- pass the number of arguments expected (excluding the callback) to MakeSync.
require 'make-sync' = res = a + b + _g done nullres = -> 10 # synchronizing (not using a fixed number of arg) f1 = MakeSync fmode:'mixed''args' Sync -> try f1 12g catch error consolelog "f1 throws" # thinks that g is the callback res = f1 12gundefined # ok when passing undefined at the end consolelog "f1 sync ->"res # passing a fixed number of args f2 = MakeSync fmode:'mixed''args'num_of_args: 3 Sync -> res = f2 12g # it works consolelog "f2 sync ->"res
mixed-fibers
f = MakeSync fmode:'mixed''fibers'
When using this mode, MakeSync checks wether a fiber is currently available, using 'Fiber.current', and uses the sync or async mode accordingly.
return and error handling in sync mode
This can be configured using the sync-return option.
argument matcher
Argument matchers works like the coffeescript arguments with splats. MakeSync will match the arguments passed to the callback using this pattern. If err is defined it will throw it, otherwise res will be returned. (When using splats on res, res becomes an array)
require 'make-sync' # err + res = done null'A'syncF = MakeSync f'sync-return': 'err,res'Sync -> consolelog syncF # single res = done 'B'syncF = MakeSync f'sync-return': 'res'Sync -> consolelog syncF # return error + res array = done null'C1''C2' syncF = MakeSync f'sync-return': 'err, res...'Sync -> consolelog syncF # res array = done 'D1''D2' syncF = MakeSync f'sync-return': 'res...'Sync -> consolelog syncF # the second result only = done 'E1''E2' 'E3'syncF = MakeSync f'sync-return': 'ignoreFirst, res, ignoreLast...'Sync -> consolelog syncF
function
If matchers are not sufficient, you may use a function instead. This function will receive the same arguments as the callback, and its return will be the final result returned.
require 'make-sync' = done null1020options = 'sync-return': res1 + res2syncF = MakeSync foptions Sync -> consolelog syncF
default
If not specified, this options is defaulted to:
'sync-return': 'err,res'
This means that MakeSync expect the function to pass an error, and one result to the callback. It will ignore everything else.
options when calling on objects
When calling MakeSync on an object, MakeSync is called on all its methods.
It is possible to configure the following:
- inclusion and exclusion lists (per method basis)
- num_of_args (per method basis)
- sync-return (per method basis)
- MakeSync mode (globally)
require 'make-sync' = -> 10 # all the function are included by default, # then some are specifically excluded. # sync return is set per-function obj = : done null1 : done null2 : done 3 : done null4 + _g : done 5 + _g : done null2 options = mode: 'mixed''args' exclude: 'f1'/^_/ num_of_args: f4:1 'sync-return': '*': 'err,res' _f: 'res' f4: 'res' : res + 1 MakeSync objoptions Sync -> try objf1 catch error then consolelog "f1 throws" # f1 was excluded consolelog 'f2 returns'objf2 # OK, not in the exclude list try obj_f catch error then consolelog "_f throws" # _f was excluded try objf3 g catch error consolelog "f3 throws" # num_of_args not set consolelog 'f4 returns'objf4 g # OK, num_of_args was set consolelog 'f5 returns'objf5 # uses sync return function # all the function are excluded # then some are specifically included. obj = : done 1 : done 2 : done 3 : done 4 + _g : done 5 + _g : done 2 options = mode: 'sync' exclude: '*' include: 'f1''f3' 'sync-return': 'res' # err is never pass to the done callback MakeSync objoptions Sync -> consolelog '\nf1 returns'objf1 # OK, f1 was included try objf2 catch error then consolelog "f2 throws" # f2 was excluded consolelog 'f3 returns'objf3 g # OK, f3 was included # exclude can also accept a simple string or a regex options1 = exclude: 'f1'options2 = exclude: /^_/