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

Already on GitHub? Sign in to your account

Better Dependency Searching #438

Closed
wants to merge 7 commits into
from
@@ -76,9 +76,15 @@ ko.utils = new (function () {
return (inputType == "checkbox") || (inputType == "radio");
}
+ var idCounter = 0;
+
return {
fieldsIncludedWithJsonPost: ['authenticity_token', /^__RequestVerificationToken(_.*)?$/],
+ generateUniqueId: function() {
+ return idCounter++;
+ },
+
arrayForEach: function (array, action) {
for (var i = 0, j = array.length; i < j; i++)
action(array[i]);
@@ -895,7 +901,7 @@ ko.dependencyDetection = (function () {
return {
begin: function (callback) {
- _frames.push({ callback: callback, distinctDependencies:[] });
+ _frames.push({ callback: callback, distinctDependencies:{} });
},
end: function () {
@@ -907,9 +913,9 @@ ko.dependencyDetection = (function () {
throw new Error("Only subscribable things can act as dependencies");
if (_frames.length > 0) {
var topFrame = _frames[_frames.length - 1];
- if (ko.utils.arrayIndexOf(topFrame.distinctDependencies, subscribable) >= 0)
+ if (topFrame.distinctDependencies[subscribable.id()])
return;
- topFrame.distinctDependencies.push(subscribable);
+ topFrame.distinctDependencies[subscribable.id()] = subscribable;
topFrame.callback(subscribable);
}
}
@@ -955,6 +961,15 @@ ko.observable['fn'] = {
"equalityComparer": function valuesArePrimitiveAndEqual(a, b) {
var oldValueIsPrimitive = (a === null) || (typeof(a) in primitiveTypes);
return oldValueIsPrimitive ? (a === b) : false;
+ },
+
+ "cachedId": undefined,
+
+ "id": function() {
+ if (!this.cachedId)
+ this.cachedId = ko.utils.generateUniqueId();
+
+ return this.cachedId;
}
};
@@ -1075,6 +1090,15 @@ ko.observableArray['fn'] = {
this()[index] = newItem;
this.valueHasMutated();
}
+ },
+
+ 'cachedId': undefined,
+
+ 'id': function() {
+ if (!this.cachedId)
+ this.cachedId = ko.utils.generateUniqueId();
+
+ return this.cachedId;
}
}
@@ -1258,7 +1282,16 @@ ko.isComputed = function(instance) {
var protoProp = ko.observable.protoProperty; // == "__ko_proto__"
ko.dependentObservable[protoProp] = ko.observable;
-ko.dependentObservable['fn'] = {};
+ko.dependentObservable['fn'] = {
+ 'cachedId': undefined,
+
+ 'id': function() {
+ if (!this.cachedId)
+ this.cachedId = ko.utils.generateUniqueId();
+
+ return this.cachedId;
+ }
+};
ko.dependentObservable['fn'][protoProp] = ko.dependentObservable;
ko.exportSymbol('dependentObservable', ko.dependentObservable);

Large diffs are not rendered by default.

Oops, something went wrong.
View
No changes.
@@ -4,7 +4,7 @@ ko.dependencyDetection = (function () {
return {
begin: function (callback) {
- _frames.push({ callback: callback, distinctDependencies:[] });
+ _frames.push({ callback: callback, distinctDependencies:{} });
},
end: function () {
@@ -16,9 +16,9 @@ ko.dependencyDetection = (function () {
throw new Error("Only subscribable things can act as dependencies");
if (_frames.length > 0) {
var topFrame = _frames[_frames.length - 1];
- if (ko.utils.arrayIndexOf(topFrame.distinctDependencies, subscribable) >= 0)
+ if (topFrame.distinctDependencies[subscribable.id()])
return;
- topFrame.distinctDependencies.push(subscribable);
+ topFrame.distinctDependencies[subscribable.id()] = subscribable;
topFrame.callback(subscribable);
}
}
@@ -158,7 +158,16 @@ ko.isComputed = function(instance) {
var protoProp = ko.observable.protoProperty; // == "__ko_proto__"
ko.dependentObservable[protoProp] = ko.observable;
-ko.dependentObservable['fn'] = {};
+ko.dependentObservable['fn'] = {
+ 'cachedId': undefined,
+
+ 'id': function() {
+ if (!this.cachedId)
+ this.cachedId = ko.utils.generateUniqueId();
+
+ return this.cachedId;
+ }
+};
ko.dependentObservable['fn'][protoProp] = ko.dependentObservable;
ko.exportSymbol('dependentObservable', ko.dependentObservable);
@@ -38,6 +38,15 @@ ko.observable['fn'] = {
"equalityComparer": function valuesArePrimitiveAndEqual(a, b) {
var oldValueIsPrimitive = (a === null) || (typeof(a) in primitiveTypes);
return oldValueIsPrimitive ? (a === b) : false;
+ },
+
+ "cachedId": undefined,
+
+ "id": function() {
+ if (!this.cachedId)
+ this.cachedId = ko.utils.generateUniqueId();
+
+ return this.cachedId;
}
};
@@ -88,6 +88,15 @@ ko.observableArray['fn'] = {
this()[index] = newItem;
this.valueHasMutated();
}
+ },
+
+ 'cachedId': undefined,
+
+ 'id': function() {
+ if (!this.cachedId)
+ this.cachedId = ko.utils.generateUniqueId();
+
+ return this.cachedId;
}
}
View
@@ -35,9 +35,15 @@ ko.utils = new (function () {
return (inputType == "checkbox") || (inputType == "radio");
}
+ var idCounter = 0;
+
return {
fieldsIncludedWithJsonPost: ['authenticity_token', /^__RequestVerificationToken(_.*)?$/],
+ generateUniqueId: function() {
+ return idCounter++;
+ },
+
arrayForEach: function (array, action) {
for (var i = 0, j = array.length; i < j; i++)
action(array[i]);