From e6db813cbc14f68b3c0611afd51d3376cadf936e Mon Sep 17 00:00:00 2001 From: bin-y Date: Sat, 18 Jul 2020 00:04:55 +0800 Subject: [PATCH 1/2] support synchronous call --- .gitignore | 1 + .npmignore | 5 +++++ index.js | 8 ++++++++ test.js | 28 ++++++++++++++++++++++++++++ tulind.cpp | 20 +++++++++++++++----- 5 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 .npmignore diff --git a/.gitignore b/.gitignore index 5ebc49b..416ecc6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules build lib +*.tgz diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..a2843c7 --- /dev/null +++ b/.npmignore @@ -0,0 +1,5 @@ +build +.* +*.yaml +*.tgz +lib diff --git a/index.js b/index.js index c54de1e..2743bd3 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,13 @@ var make_call = function(ind) { }; }; +var make_sync_call = function(ind) { + var index = ind.index; + return function(inputs, options) { + return tulind.callbyindex(index, inputs, options); + }; +}; + var make_start = function(ind) { var index = ind.index; return function(options) { @@ -34,6 +41,7 @@ for (var key in indicators) { if (typeof ind.indicator == 'function') break; ind.indicator = make_call(ind); + ind.indicator_sync = make_sync_call(ind); ind.start = make_start(ind); delete ind.index; } diff --git a/test.js b/test.js index 0a474d6..b7e5808 100644 --- a/test.js +++ b/test.js @@ -30,6 +30,8 @@ l.run("array dema", function() { tulind.indicators.dema.indicator([a], [5], function(err, results) { l.ok(equal(results[0], b)); }); + const results = tulind.indicators.dema.indicator_sync([a], [5]); + l.ok(equal(results[0], b)); }); l.run("array sma", function() { @@ -63,12 +65,29 @@ l.run("array sma", function() { l.ok(equal(results[0], [])); }); + var results = tulind.indicators.sma.indicator_sync([a], [2]); + l.ok(equal(results[0], [1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5])); + + results = tulind.indicators.sma.indicator_sync([a], [3]); + l.ok(equal(results[0], [2, 3, 4, 5, 6, 7, 8, 9])); + + results = tulind.indicators.sma.indicator_sync([a], [4]); + l.ok(equal(results[0], [2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5])); + + results = tulind.indicators.sma.indicator_sync([a], [10]); + l.ok(equal(results[0], [5.5])); + + results = tulind.indicators.sma.indicator_sync([a], [11]); + l.ok(equal(results[0], [])); //atoz page 207 var c = [25,24.875,24.781,24.594,24.5,24.625,25.219,27.25]; var b = [24.75,24.675,24.744,25.238]; tulind.indicators.sma.indicator([c], [5], function(err, results) { l.ok(equal(results[0], b)); }); + + results = tulind.indicators.sma.indicator_sync([c], [5]); + l.ok(equal(results[0], b)); }); @@ -79,6 +98,9 @@ l.run("array ema", function() { tulind.indicators.ema.indicator([a], [5], function(err, results) { l.ok(equal(results[0], b)); }); + + const results = tulind.indicators.ema.indicator_sync([a], [5]); + l.ok(equal(results[0], b)); }); @@ -89,6 +111,9 @@ l.run("array tema", function() { tulind.indicators.tema.indicator([a], [5], function(err, results) { l.ok(equal(results[0], b)); }); + + const results = tulind.indicators.tema.indicator_sync([a], [5]); + l.ok(equal(results[0], b)); }); l.run("array wilders", function() { @@ -98,6 +123,9 @@ l.run("array wilders", function() { tulind.indicators.wilders.indicator([a], [5], function(err, results) { l.ok(equal(results[0], b)); }); + + const results = tulind.indicators.wilders.indicator_sync([a], [5]); + l.ok(equal(results[0], b)); }); diff --git a/tulind.cpp b/tulind.cpp index 74db289..139d485 100644 --- a/tulind.cpp +++ b/tulind.cpp @@ -89,9 +89,9 @@ NAN_METHOD(startbyindex) { NAN_METHOD(callbyindex) { - - if (info.Length() != 4) { - Nan::ThrowTypeError("Wrong number of arguments. Expecting: index, inputs, options, and callback."); + bool synchronous_call = info.Length() == 3; + if (!synchronous_call && info.Length() != 4) { + Nan::ThrowTypeError("Wrong number of arguments. Expecting: index, inputs, options, and [callback]."); return; } @@ -110,8 +110,8 @@ NAN_METHOD(callbyindex) { return; } - if (!info[3]->IsFunction()) { - Nan::ThrowTypeError("Expecting last argument to be callback function."); + if (!synchronous_call && !info[3]->IsFunction()) { + Nan::ThrowTypeError("Expecting fourth argument to be callback function."); return; } @@ -233,6 +233,16 @@ NAN_METHOD(callbyindex) { free(output_arr[i]); } + if (synchronous_call) { + if (cb_argv[0]->IsNull()) { + info.GetReturnValue().Set(cb_argv[1]); + return; + } + else { + Nan::ThrowError(cb_argv[0]); + return; + } + } v8::Local callbackHandle = info[3].As(); Nan::AsyncResource cb("tulind-callback"); From 4faf6e591fa4e5f5a33981f3a6a888f66b762459 Mon Sep 17 00:00:00 2001 From: bin-y Date: Mon, 20 Jul 2020 02:01:11 +0800 Subject: [PATCH 2/2] Add synchronous call examples --- Readme.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Readme.md b/Readme.md index 3597de6..e212002 100644 --- a/Readme.md +++ b/Readme.md @@ -82,6 +82,27 @@ tulind.indicators.stoch.indicator([high, low, close], [5, 3, 3], function(err, r ``` +Examples of avoiding callbacks: + +``` js +// Using indicator_sync +const results = tulind.indicators.stoch.indicator_sync([high, low, close], [5, 3, 3]); +console.log("Result of stochastic oscillator is:"); +console.log(results[0]); +console.log(results[1]); +``` + +``` js +// Using await +(async ()=> { + const results = await tulind.indicators.stoch.indicator([high, low, close], [5, 3, 3]); + console.log("Result of stochastic oscillator is:"); + console.log(results[0]); + console.log(results[1]); +})(); +``` + + It's also easy to discover argument types at run-time: ``` js