Skip to content

Commit

Permalink
query/ExtenderHelper: Changed if-else-constructs to switch statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joscha Rohmann committed Oct 17, 2016
1 parent 9fe340d commit 2f2c57f
Showing 1 changed file with 93 additions and 76 deletions.
169 changes: 93 additions & 76 deletions src/query/ExtenderHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ define([
'../modules/Events',
'./Observer'
], function (blocks, Events, Observer) {

var Action = {
NOOP: 0,
ADD: 1,
REMOVE: 2,
EXISTS: 3
};

var ExtenderHelper = {
waiting: {},
operations: {
Expand Down Expand Up @@ -106,10 +114,7 @@ define([
},

executeOperationsChunk: function (observable, operations) {
var ADD = 'add';
var REMOVE = 'remove';
var EXISTS = 'exists';
var action = EXISTS;
var action = Action.EXISTS;

var collection = observable.__value__;
var view = observable.view;
Expand All @@ -121,40 +126,44 @@ define([
var take = collection.length;
view.update = blocks.noop;

blocks.each(operations, function (operation) {
if (operation.type == ExtenderHelper.operations.SKIP) {
skip = operation.skip;
if (blocks.isFunction(skip)) {
skip = skip.call(observable.__context__);
}
skip = blocks.unwrap(skip);
} else if (operation.type == ExtenderHelper.operations.TAKE) {
take = operation.take;
if (blocks.isFunction(take)) {
take = take.call(observable.__context__);
}
take = blocks.unwrap(take);
} else if (operation.type == ExtenderHelper.operations.SORT) {
if (blocks.isString(operation.sort)) {
collection = blocks.clone(collection).sort(function (valueA, valueB) {
valueA = blocks.unwrap(valueA[operation.sort]);
valueB = blocks.unwrap(valueB[operation.sort]);
if (valueA > valueB) {
return 1;
}
if (valueA < valueB) {
return -1;
}
return 0;
});
} else if (blocks.isFunction(operation.sort)) {
collection = blocks.clone(collection).sort(operation.sort.bind(observable.__context__));
} else {
collection = blocks.clone(collection).sort();
}
if (operations.length == 1) {
operations.push({ type: ExtenderHelper.operations.FILTER, filter: function () { return true; }});
}
blocks.each(operations, function prepareOperations(operation) {
switch (operation.type) {
case ExtenderHelper.operations.SKIP:
skip = operation.skip;
if (blocks.isFunction(skip)) {
skip = skip.call(observable.__context__);
}
skip = blocks.unwrap(skip);
break;
case ExtenderHelper.operations.TAKE:
take = operation.take;
if (blocks.isFunction(take)) {
take = take.call(observable.__context__);
}
take = blocks.unwrap(take);
break;
case ExtenderHelper.operations.SORT:
if (blocks.isString(operation.sort)) {
collection = blocks.clone(collection).sort(function (valueA, valueB) {
valueA = blocks.unwrap(valueA[operation.sort]);
valueB = blocks.unwrap(valueB[operation.sort]);
if (valueA > valueB) {
return 1;
}
if (valueA < valueB) {
return -1;
}
return 0;
});
} else if (blocks.isFunction(operation.sort)) {
collection = blocks.clone(collection).sort(operation.sort.bind(observable.__context__));
} else {
collection = blocks.clone(collection).sort();
}
if (operations.length == 1) {
operations.push({ type: ExtenderHelper.operations.FILTER, filter: function () { return true; }});
}
break;
}
});

Expand All @@ -165,59 +174,67 @@ define([
}
return false;
}

blocks.each(operations, function executeExtender(operation) {
var filterCallback = operation.filter;

action = undefined;

if (filterCallback) {
if (filterCallback.call(observable.__context__, value, index, collection)) {
action = EXISTS;

if (connections[index] === undefined) {
action = ADD;
operation.type = operation.type || (filterCallback && ExtenderHelper.operations.FILTER);

action = Action.NOOP;

switch (operation.type) {
case ExtenderHelper.operations.FILTER:
if (filterCallback.call(observable.__context__, value, index, collection)) {
action = Action.EXISTS;

if (connections[index] === undefined) {
action = Action.ADD;
}
} else {
action = Action.NOOP;
if (connections[index] !== undefined) {
action = Action.REMOVE;
}
return false;
}
} else {
action = undefined;
if (connections[index] !== undefined) {
action = REMOVE;
break;

case ExtenderHelper.operations.SKIP:
action = Action.EXISTS;
skip -= 1;
if (skip >= 0) {
action = Action.REMOVE;
return false;
} else if (skip < 0 && connections[index] === undefined) {
action = Action.ADD;
}
return false;
}
} else if (operation.type == ExtenderHelper.operations.SKIP) {
action = EXISTS;
skip -= 1;
if (skip >= 0) {
action = REMOVE;
return false;
} else if (skip < 0 && connections[index] === undefined) {
action = ADD;
}
} else if (operation.type == ExtenderHelper.operations.TAKE) {
if (take <= 0) {
action = REMOVE;
return false;
} else {
take -= 1;
action = EXISTS;

if (connections[index] === undefined) {
action = ADD;
break;

case ExtenderHelper.operations.TAKE:
if (take <= 0) {
action = Action.REMOVE;
return false;
} else {
take -= 1;
action = Action.EXISTS;

if (connections[index] === undefined) {
action = Action.ADD;
}
}
}
break;
}
});

switch (action) {
case ADD:
case Action.ADD:
newConnections[index] = viewIndex;
view.splice(viewIndex, 0, value);
viewIndex++;
break;
case REMOVE:
case Action.REMOVE:
view.removeAt(viewIndex);
break;
case EXISTS:
case Action.EXISTS:
newConnections[index] = viewIndex;
if (view.__value__.indexOf(collection[index]) != index) {
view.move(view.__value__.indexOf(collection[index]), index);
Expand Down

0 comments on commit 2f2c57f

Please sign in to comment.