Skip to content

Commit 4fb0549

Browse files
fix concurrency bug in ~>
1 parent 688ee4c commit 4fb0549

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/jsonata.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,13 +1158,11 @@ var jsonata = (function() {
11581158
var result;
11591159

11601160

1161+
var lhs = yield * evaluate(expr.lhs, input, environment);
11611162
if(expr.rhs.type === 'function') {
11621163
// this is a function _invocation_; invoke it with lhs expression as the first argument
1163-
expr.rhs.arguments.unshift(expr.lhs);
1164-
result = yield * evaluateFunction(expr.rhs, input, environment);
1165-
expr.rhs.arguments.shift();
1164+
result = yield * evaluateFunction(expr.rhs, input, environment, { context: lhs });
11661165
} else {
1167-
var lhs = yield * evaluate(expr.lhs, input, environment);
11681166
var func = yield * evaluate(expr.rhs, input, environment);
11691167

11701168
if(!isFunction(func)) {
@@ -1197,7 +1195,7 @@ var jsonata = (function() {
11971195
* @param {Object} environment - Environment
11981196
* @returns {*} Evaluated input data
11991197
*/
1200-
function* evaluateFunction(expr, input, environment) {
1198+
function* evaluateFunction(expr, input, environment, applyto) {
12011199
var result;
12021200

12031201
// create the procedure
@@ -1217,6 +1215,9 @@ var jsonata = (function() {
12171215
}
12181216

12191217
var evaluatedArgs = [];
1218+
if(typeof applyto !== 'undefined') {
1219+
evaluatedArgs.push(applyto.context);
1220+
}
12201221
// eager evaluation - evaluate the arguments
12211222
for (var jj = 0; jj < expr.arguments.length; jj++) {
12221223
const arg = yield* evaluate(expr.arguments[jj], input, environment);

test/async-function.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ describe('Invoke JSONata with callback - return values', function() {
9191
});
9292
});
9393

94+
describe('Evaluate concurrent expressions with callbacks', function() {
95+
it('should process two expressions concurrently', function(done) {
96+
const expr = jsonata("{'1':'goat','2': 'cheese'} ~> $lookup($string(payload))");
97+
98+
var count = 0;
99+
var partDone = function() {
100+
count++;
101+
if(count >= 2) {
102+
done();
103+
}
104+
};
105+
106+
expr.evaluate({"payload":1}, {}, function(err,result) {
107+
expect(result).to.equal('goat');
108+
partDone();
109+
});
110+
expr.evaluate({"payload":2}, {}, function(err,result) {
111+
expect(result).to.equal('cheese');
112+
partDone();
113+
});
114+
});
115+
});
116+
94117
describe('Handle chained functions that end in promises', function() {
95118

96119
var counter = function(count) {

0 commit comments

Comments
 (0)