synchronize functions and objects using fibers
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)
- mixed-args (default 'mixed')
npm install make-sync
require 'make-sync'=res = a+bdone nullres# making synchronousf = MakeSync f# sync callSync ->res = f 12consolelog "sync ->"resobj =:res = a+bdone nullres# making synchronousMakeSync obj# sync callSync ->res = objf 12consolelog "obj sync ->"res
f = MakeSync f# orf = 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)
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)
f = MakeSync fmode:'mixed'# orf = 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.
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 + _gdone nullres= -> 10# synchronizing (not using a fixed number of arg)f1 = MakeSync fmode:'mixed''args'Sync ->try f1 12g catch errorconsolelog "f1 throws" # thinks that g is the callbackres = f1 12g # ok when passing at the endconsolelog "f1 sync ->"res# passing a fixed number of argsf2 = MakeSync fmode:'mixed''args'num_of_args: 3Sync ->res = f2 12g # it worksconsolelog "f2 sync ->"res
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.
This can be configured using the sync-return option.
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
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 foptionsSync ->consolelog syncF
If not specified, this options is defaulted to:
This means that MakeSync expect the function to pass an error, and one result to the callback. It will ignore everything else.
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-functionobj =: done null1: done null2: done 3: done null4 + _g: done 5 + _g: done null2options =mode: 'mixed''args'exclude: 'f1'/^_/num_of_args:f4:1'sync-return':'*': 'err,res'_f: 'res'f4: 'res': res + 1MakeSync objoptionsSync ->try objf1 catch error then consolelog "f1 throws" # f1 was excludedconsolelog 'f2 returns'objf2 # OK, not in the exclude listtry obj_f catch error then consolelog "_f throws" # _f was excludedtry objf3 g catch errorconsolelog "f3 throws" # num_of_args not setconsolelog 'f4 returns'objf4 g # OK, num_of_args was setconsolelog '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 2options =mode: 'sync'exclude: '*'include: 'f1''f3''sync-return': 'res' # err is never pass to the done callbackMakeSync objoptionsSync ->consolelog '\nf1 returns'objf1 # OK, f1 was includedtry objf2 catch error then consolelog "f2 throws" # f2 was excludedconsolelog 'f3 returns'objf3 g # OK, f3 was included# exclude can also accept a simple string or a regexoptions1 = exclude: 'f1'options2 = exclude: /^_/