/
limit.js
67 lines (56 loc) · 1.95 KB
/
limit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(function(Monarch) {
_.constructor("Monarch.Model.Relations.Limit", Monarch.Model.Relations.Relation, {
initialize: function(operand, count) {
this.operand = operand;
this.count = count;
this.sortSpecifications = operand.sortSpecifications;
this.initializeEventsSystem();
},
wireRepresentation: function() {
return {
type: "limit",
count: this.count,
operand: this.operand.wireRepresentation()
};
},
tuples: function() {
if (this.storedTuples) return this.storedTuples.values();
return this.operand.tuples().slice(0, this.count);
},
isEqual: function(other) {
if (!other || other.constructor !== this.constructor) return false;
return other.count === this.count && this.operand.isEqual(other.operand);
},
onOperandInsert: function(tuple, index, newKey, oldKey) {
if (index < this.count) {
var formerLastTuple = this.at(this.count - 1);
if (formerLastTuple) this.tupleRemovedRemotely(formerLastTuple);
this.tupleInsertedRemotely(tuple, newKey, oldKey);
}
},
onOperandUpdate: function(tuple, changeset, newIndex, oldIndex, newKey, oldKey) {
if (oldIndex < this.count) {
if (newIndex >= this.count) {
this.tupleRemovedRemotely(tuple, newKey, oldKey);
var newLastTuple = this.operand.at(this.count - 1);
if (newLastTuple) this.tupleInsertedRemotely(newLastTuple);
} else {
this.tupleUpdatedRemotely(tuple, changeset, newKey, oldKey);
}
} else {
if (newIndex < this.count) {
this.tupleRemovedRemotely(this.at(this.count - 1));
this.tupleInsertedRemotely(tuple, newKey, oldKey);
} else {
}
}
},
onOperandRemove: function(tuple, index, newKey, oldKey) {
if (index < this.count) {
this.tupleRemovedRemotely(tuple, newKey, oldKey);
var newLastTuple = this.operand.at(this.count - 1);
if (newLastTuple) this.tupleInsertedRemotely(newLastTuple);
}
}
});
})(Monarch);