Skip to content

Commit

Permalink
support synchronous call
Browse files Browse the repository at this point in the history
  • Loading branch information
bin-y committed Jul 17, 2020
1 parent 1d86b1f commit e6db813
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
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
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

0 comments on commit e6db813

Please sign in to comment.