Permalink
Browse files

Got selection relation using and propagating sort keys. Refactored re…

…lation code to call onOperandInsert, onOperandUpdate etc. Everything is broken.
  • Loading branch information...
1 parent c5c12e4 commit e4caad2e48879eace3b4476f7fa25a824b4bf80b @nathansobo committed Jan 3, 2011
@@ -1,6 +1,8 @@
(function(Monarch) {
_.constructor("Monarch.Model.Relations.Difference", Monarch.Model.Relations.Relation, {
+ numOperands: 2,
+
initialize: function(leftOperand, rightOperand) {
this.leftOperand = leftOperand;
this.rightOperand = rightOperand;
@@ -1,6 +1,7 @@
(function(Monarch) {
_.constructor("Monarch.Model.Relations.InnerJoin", Monarch.Model.Relations.Relation, {
+ numOperands: 2,
initialize: function(leftOperand, rightOperand, predicate) {
this.leftOperand = leftOperand;
@@ -1,7 +1,7 @@
(function(Monarch) {
_.constructor("Monarch.Model.Relations.Relation", {
- hasOperands: true,
+ numOperands: 1,
_relation_: true,
initializeEventsSystem: function() {
@@ -13,7 +13,7 @@ _.constructor("Monarch.Model.Relations.Relation", {
this.onCleanNode = new Monarch.SubscriptionNode();
this.onInvalidNode = new Monarch.SubscriptionNode();
this.onValidNode = new Monarch.SubscriptionNode();
- if (this.hasOperands) {
+ if (this.numOperands > 0) {
this.operandsSubscriptionBundle = new Monarch.SubscriptionBundle();
this.unsubscribeFromOperandsWhenThisNoLongerHasSubscribers();
}
@@ -204,22 +204,6 @@ _.constructor("Monarch.Model.Relations.Relation", {
return this.onValidNode.subscribe(callback, context);
},
- recordMadeDirty: function(record) {
- this.onDirtyNode.publish(record);
- },
-
- recordMadeClean: function(record) {
- this.onCleanNode.publish(record);
- },
-
- recordMadeInvalid: function(record) {
- this.onInvalidNode.publish(record)
- },
-
- recordMadeValid: function(record) {
- this.onValidNode.publish(record);
- },
-
hasSubscribers: function() {
return !(this.onInsertNode.empty() && this.onRemoveNode.empty()
&& this.onUpdateNode.empty() && this.onDirtyNode.empty() && this.onCleanNode.empty());
@@ -300,13 +284,33 @@ _.constructor("Monarch.Model.Relations.Relation", {
this.onUpdateNode.publish(tuple, changeset, newIndex, oldIndex, newKey, oldKey);
},
- tupleRemovedRemotely: function(tuple, changeset, newKey, oldKey) {
+ tupleRemovedRemotely: function(tuple, newKey, oldKey) {
if (!newKey) newKey = oldKey = this.buildSortKey(tuple);
var index = this.storedTuples.remove(oldKey);
this.onRemoveNode.publish(tuple, index, newKey, oldKey);
},
+ recordMadeDirty: function(record, newKey, oldKey) {
+ if (!newKey) newKey = oldKey = this.buildSortKey(record);
+ this.onDirtyNode.publish(record, this.indexByKey(oldKey), newKey, oldKey);
+ },
+
+ recordMadeClean: function(record, newKey, oldKey) {
+ if (!newKey) newKey = oldKey = this.buildSortKey(record);
+ this.onCleanNode.publish(record, this.indexByKey(oldKey), newKey, oldKey);
+ },
+
+ recordMadeInvalid: function(record, newKey, oldKey) {
+ if (!newKey) newKey = oldKey = this.buildSortKey(record);
+ this.onInvalidNode.publish(record, this.indexByKey(oldKey), newKey, oldKey)
+ },
+
+ recordMadeValid: function(record, newKey, oldKey) {
+ if (!newKey) newKey = oldKey = this.buildSortKey(record);
+ this.onValidNode.publish(record, this.indexByKey(oldKey), newKey, oldKey);
+ },
+
tupleUpdatedLocally: function(tuple, updateData) {
this.onLocalUpdateNode.publish(tuple, updateData);
},
@@ -319,13 +323,68 @@ _.constructor("Monarch.Model.Relations.Relation", {
}
},
+ findByKey: function(sortKey) {
+ return this.storedTuples.find(sortKey);
+ },
+
+ indexByKey: function(sortKey) {
+ return this.storedTuples.indexOf(sortKey);
+ },
+
+ remoteSubscribe: function() {
+ var subscribeFuture = new Monarch.Http.AjaxFuture();
+ Server.subscribe([this]).onSuccess(function(remoteSubscriptions) {
+ subscribeFuture.triggerSuccess(remoteSubscriptions[0]);
+ });
+ return subscribeFuture;
+ },
+
+ // private
+
subscribeToOperandsIfNeeded: function() {
- if (this.hasOperands && !this.hasSubscribers()) {
+ if (this.numOperands > 0 && !this.hasSubscribers()) {
this.subscribeToOperands();
this.memoizeTuples();
}
},
+ subscribeToOperands: function() {
+ if (this.numOperands === 1) {
+ this.operandsSubscriptionBundle.add(this.operand.onInsert(this.hitch('onOperandInsert')));
+ this.operandsSubscriptionBundle.add(this.operand.onUpdate(this.hitch('onOperandUpdate')));
+ this.operandsSubscriptionBundle.add(this.operand.onRemove(this.hitch('onOperandRemove')));
+ this.operandsSubscriptionBundle.add(this.operand.onDirty(this.hitch('onOperandDirty')));
+ this.operandsSubscriptionBundle.add(this.operand.onClean(this.hitch('onOperandClean')));
+ this.operandsSubscriptionBundle.add(this.operand.onInvalid(this.hitch('onOperandInvalid')));
+ this.operandsSubscriptionBundle.add(this.operand.onValid(this.hitch('onOperandValid')));
+ } else {
+ throw new Error("Not implemented")
+ }
+ },
+
+ onOperandInsert: function() {},
+ onOperandUpdate: function() {},
+
+ onOperandRemove: function(tuple, index, newKey, oldKey) {
+ if (this.findByKey(oldKey)) this.tupleRemovedRemotely(tuple, newKey, oldKey);
+ },
+
+ onOperandDirty: function(tuple, index, newKey, oldKey) {
+ if (this.findByKey(oldKey)) this.recordMadeDirty(tuple, newKey, oldKey);
+ },
+
+ onOperandClean: function(tuple, index, newKey, oldKey) {
+ if (this.findByKey(oldKey)) this.recordMadeClean(tuple, newKey, oldKey);
+ },
+
+ onOperandInvalid: function(tuple, index, newKey, oldKey) {
+ if (this.findByKey(oldKey)) this.recordMadeInvalid(tuple, newKey, oldKey);
+ },
+
+ onOperandValid: function(tuple, index, newKey, oldKey) {
+ if (this.findByKey(oldKey)) this.recordMadeValid(tuple, newKey, oldKey);
+ },
+
unsubscribeFromOperandsWhenThisNoLongerHasSubscribers: function() {
var unsubscribeCallback = _.bind(function() {
if (!this.hasSubscribers()) this.unsubscribeFromOperands();
@@ -343,16 +402,6 @@ _.constructor("Monarch.Model.Relations.Relation", {
this.storedTuples = null;
},
- remoteSubscribe: function() {
- var subscribeFuture = new Monarch.Http.AjaxFuture();
- Server.subscribe([this]).onSuccess(function(remoteSubscriptions) {
- subscribeFuture.triggerSuccess(remoteSubscriptions[0]);
- });
- return subscribeFuture;
- },
-
- // private
-
predicateFromHash: function(hash) {
var predicates = [];
_.each(hash, function(value, key) {
@@ -39,6 +39,9 @@ _.constructor("Monarch.Model.Relations.Selection", Monarch.Model.Relations.Relat
},
evaluateInRepository: function(repository) {
+
+ if (!this.operand.evaluateInRepository(repository)) debugger;
+
return new Monarch.Model.Relations.Selection(this.operand.evaluateInRepository(repository), this.predicate);
},
@@ -61,42 +64,21 @@ _.constructor("Monarch.Model.Relations.Selection", Monarch.Model.Relations.Relat
// private
- subscribeToOperands: function() {
- this.operandsSubscriptionBundle.add(this.operand.onInsert(function(record) {
- if (this.predicate.evaluate(record)) this.tupleInsertedRemotely(record);
- }, this));
-
- this.operandsSubscriptionBundle.add(this.operand.onRemove(function(record) {
- if (this.predicate.evaluate(record)) this.tupleRemovedRemotely(record);
- }, this));
-
- this.operandsSubscriptionBundle.add(this.operand.onUpdate(function(record, changeset) {
- if (this.contains(record, changeset)) {
- if (this.predicate.evaluate(record)) {
- this.tupleUpdatedRemotely(record, changeset);
- } else {
- this.tupleRemovedRemotely(record);
- }
- } else {
- if (this.predicate.evaluate(record)) this.tupleInsertedRemotely(record);
- }
- }, this));
-
- this.operandsSubscriptionBundle.add(this.operand.onDirty(function(record) {
- if (this.contains(record)) this.recordMadeDirty(record);
- }, this));
- this.operandsSubscriptionBundle.add(this.operand.onClean(function(record) {
- if (this.contains(record)) this.recordMadeClean(record);
- }, this));
-
- this.operandsSubscriptionBundle.add(this.operand.onInvalid(function(record) {
- if (this.contains(record)) this.recordMadeInvalid(record);
- }, this));
+ onOperandInsert: function(tuple, index, newKey, oldKey) {
+ if (this.predicate.evaluate(tuple)) this.tupleInsertedRemotely(tuple, newKey, oldKey);
+ },
- this.operandsSubscriptionBundle.add(this.operand.onValid(function(record) {
- if (this.contains(record)) this.recordMadeValid(record);
- }, this));
+ onOperandUpdate: function(tuple, changeset, newIndex, oldIndex, newKey, oldKey) {
+ if (this.findByKey(oldKey)) {
+ if (this.predicate.evaluate(tuple)) {
+ this.tupleUpdatedRemotely(tuple, changeset, newKey, oldKey);
+ } else {
+ this.tupleRemovedRemotely(tuple, newKey, oldKey);
+ }
+ } else {
+ if (this.predicate.evaluate(tuple)) this.tupleInsertedRemotely(tuple, newKey, oldKey);
+ }
}
});
@@ -1,7 +1,7 @@
(function(Monarch) {
_.constructor("Monarch.Model.Relations.Table", Monarch.Model.Relations.Relation, {
- hasOperands: false,
+ numOperands: 0,
initialize: function(globalName, recordConstructor) {
this.globalName = globalName;
@@ -12,8 +12,6 @@ _.constructor("Monarch.Model.Relations.TableProjection", Monarch.Model.Relations
this.initializeEventsSystem();
},
- hasOperands: true,
-
tuples: function() {
if (this.storedTuples) return this.storedTuples.values();
@@ -1,6 +1,8 @@
(function(Monarch) {
_.constructor("Monarch.Model.Relations.Union", Monarch.Model.Relations.Relation, {
+ numOperands: 2,
+
initialize: function(leftOperand, rightOperand) {
this.leftOperand = leftOperand;
this.rightOperand = rightOperand;
Oops, something went wrong.

0 comments on commit e4caad2

Please sign in to comment.