-
Notifications
You must be signed in to change notification settings - Fork 3
/
projection.js
75 lines (63 loc) · 2.6 KB
/
projection.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
68
69
70
71
72
73
74
75
(function(Monarch) {
_.constructor("Monarch.Model.Relations.Projection", Monarch.Model.Relations.Relation, {
initialize: function(operand, projectedColumns) {
this.operand = operand;
this.projectedColumnsByName = {};
_.each(projectedColumns, function(projectedColumn) {
this.projectedColumnsByName[projectedColumn.name()] = projectedColumn;
}, this);
this.tupleConstructor = _.constructor(Monarch.Model.Tuple);
this.tupleConstructor.projectedColumnsByName = this.projectedColumnsByName;
this.tupleConstructor.initializeFieldReaders();
this.initializeEventsSystem();
},
tuples: function() {
if (this._tuples) return this._tuples.values();
this.tuplesByOperandRecordId = {};
return _.map(this.operand.tuples(), function(operandTuple) {
return this.tuplesByOperandRecordId[operandTuple.id()] = new this.tupleConstructor(operandTuple);
}, this);
},
column: function(name) {
return this.projectedColumnsByName[name];
},
surfaceTables: function() {
throw new Error("Projections do not have surface tables");
},
// private
subscribeToOperands: function() {
this.operandsSubscriptionBundle.add(this.operand.onInsert(function(operandRecord) {
var record = new this.tupleConstructor(operandRecord);
this.tuplesByOperandRecordId[operandRecord.id()] = record;
this.tupleInsertedRemotely(record);
}, this));
this.operandsSubscriptionBundle.add(this.operand.onUpdate(function(operandRecord, operandChanges) {
var changes = this.translateUpdateChanges(operandChanges);
if (_.isEmpty(changes)) return;
this.tupleUpdatedRemotely(this.tuplesByOperandRecordId[operandRecord.id()], changes);
}, this));
this.operandsSubscriptionBundle.add(this.operand.onRemove(function(operandRecord) {
this.tupleRemovedRemotely(this.tuplesByOperandRecordId[operandRecord.id()]);
}, this));
},
translateUpdateChanges: function(changes) {
var translatedChanges = {};
_.each(changes, function(operandColumnChanges) {
var projectedColumn = this.projectedColumnFromOperandColumn(operandColumnChanges.column);
if (projectedColumn) {
translatedChanges[projectedColumn.name()] = {
column: projectedColumn,
oldValue: operandColumnChanges.oldValue,
newValue: operandColumnChanges.newValue
}
}
}, this);
return translatedChanges;
},
projectedColumnFromOperandColumn: function(operandColumn) {
return _.detect(this.projectedColumnsByName, function(projectedColumn) {
return projectedColumn.column === operandColumn;
});
}
});
})(Monarch);