Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support synchronous call #66

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
node_modules
build
lib
*.tgz
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build
.*
*.yaml
*.tgz
lib
21 changes: 21 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
28 changes: 28 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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));
});


Expand All @@ -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));
});


Expand All @@ -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() {
Expand All @@ -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));
});


Expand Down
20 changes: 15 additions & 5 deletions tulind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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<v8::Function> callbackHandle = info[3].As<v8::Function>();
Nan::AsyncResource cb("tulind-callback");
Expand Down