Prechádzať zdrojové kódy

separate EventInstanceChangeset from EventInstanceRepo

Adam Shaw 8 rokov pred
rodič
commit
e2c540f74a

+ 7 - 1
src/View.js

@@ -751,7 +751,13 @@ View.watch('bindingEvents', [ 'dateProfile' ], function(deps) {
 	this.requestEvents(deps.dateProfile);
 
 	if (this.calendar.eventManager.isFinalized()) {
-		this._handleEventsChanged(this.calendar.eventManager.getFinalizedEvents());
+		this._handleEventsChanged(
+			new EventInstanceChangeset(
+				false, // isClear
+				null, // removals
+				this.calendar.eventManager.getEventInstanceRepo() // additions
+			)
+		);
 	}
 
 	this.bindEventChanges();

+ 12 - 17
src/agenda/AgendaView.js

@@ -284,33 +284,28 @@ var AgendaView = FC.AgendaView = View.extend({
 		var timedChangeset = new EventInstanceChangeset();
 
 		if (eventChangeset.isClear) {
-			allDayChangeset.addClear();
-			timedChangeset.addClear();
+			allDayChangeset.isClear = true;
+			timedChangeset.isClear = true;
 		}
 
 		// reference dateProfile.isAllDay() instead of the eventDef
 		// because eventDef might have updated
 
-		// ugly
-		for (var id in eventChangeset.removalsByDefId) {
-			for (var i = 0; i < eventChangeset.removalsByDefId[id].length; i++) {
-				var instance = eventChangeset.removalsByDefId[id][i];
-
-				if (instance.dateProfile.isAllDay()) {
-					allDayChangeset.removeEventInstance(instance);
-				}
-				else {
-					timedChangeset.removeEventInstance(instance);
-				}
+		eventChangeset.removalsRepo.iterEventInstances(function(instance) {
+			if (instance.dateProfile.isAllDay()) {
+				allDayChangeset.removalsRepo.addEventInstance(instance);
 			}
-		}
+			else {
+				timedChangeset.removalsRepo.addEventInstance(instance);
+			}
+		});
 
-		eventChangeset.iterEventInstances(function(instance) {
+		eventChangeset.additionsRepo.iterEventInstances(function(instance) {
 			if (instance.dateProfile.isAllDay()) {
-				allDayChangeset.addEventInstance(instance);
+				allDayChangeset.additionsRepo.addEventInstance(instance);
 			}
 			else {
-				timedChangeset.addEventInstance(instance);
+				timedChangeset.additionsRepo.addEventInstance(instance);
 			}
 		});
 

+ 2 - 2
src/component/DateComponent.js

@@ -270,10 +270,10 @@ var DateComponent = FC.DateComponent = Component.extend({
 		if (this.hasOwnEventRendering()) {
 
 			if (!this.eventInstanceRepo) {
-				this.eventInstanceRepo = new EventInstanceChangeset();
+				this.eventInstanceRepo = new EventInstanceRepo();
 			}
 
-			this.eventInstanceRepo.addChangeset(changeset);
+			changeset.applyToRepo(this.eventInstanceRepo);
 
 			if (this.has('displayingEvents')) {
 				this.requestEventRender(this.eventInstanceRepo);

+ 88 - 58
src/models/EventInstanceChangeset.js

@@ -1,25 +1,14 @@
 
-var EventInstanceChangeset = Class.extend({
+var EventInstanceRepo = Class.extend({
 
-	isClear: false,
 	byDefId: null,
-	removalsByDefId: null,
-	instanceCnt: 0,
-	removalCnt: 0,
+	cnt: 0,
 
 
-	constructor: function(isClear, removals, adds) {
-		this.isClear = isClear || false;
-		this.removalsByDefId = {};
+	constructor: function(eventInstances) {
 		this.byDefId = {};
 
-		(removals || []).forEach(this.removeEventInstance.bind(this));
-		(adds || []).forEach(this.addEventInstance.bind(this));
-	},
-
-
-	isEmpty: function() {
-		return !this.isClear && !this.removalCnt && !this.instanceCnt;
+		(eventInstances || []).forEach(this.addEventInstance.bind(this));
 	},
 
 
@@ -85,80 +74,121 @@ var EventInstanceChangeset = Class.extend({
 
 
 	addEventInstance: function(eventInstance) {
-		this._addEventInstances(eventInstance.def.id, [ eventInstance ]);
+		this._addEventInstance(eventInstance.def.id, eventInstance);
 	},
 
 
 	removeEventInstance: function(eventInstance) {
-		this._removeEventInstances(eventInstance.def.id, [ eventInstance ]);
+		return this._removeEventInstance(eventInstance.def.id, eventInstance);
 	},
 
 
-	_addEventInstances: function(id, eventInstances) {
-		var bucket = (this.byDefId[id] || (this.byDefId[id] = []));
-
-		bucket.push.apply(bucket, eventInstances);
+	_addEventInstance: function(id, eventInstance) {
+		(this.byDefId[id] || (this.byDefId[id] = []))
+			.push(eventInstance);
 
-		this.instanceCnt += eventInstances.length;
+		this.cnt++;
 	},
 
 
-	_removeEventInstances: function(id, eventInstances) {
+	_removeEventInstance: function(id, eventInstance) {
 		var bucket = this.byDefId[id];
+
+		if (bucket && removeExact(bucket, eventInstance)) {
+
+			if (!bucket.length) {
+				delete this.byDefId[id];
+			}
+
+			this.cnt--;
+
+			return true;
+		}
+
+		return false;
+	},
+
+
+	clear: function() {
+		this.byDefId = {};
+		this.cnt = 0;
+	}
+
+
+});
+
+
+var EventInstanceChangeset = Class.extend({
+
+	isClear: false,
+	removalsRepo: null,
+	additionsRepo: null,
+
+
+	constructor: function(isClear, removalsRepo, additionsRepo) {
+		this.isClear = isClear || false;
+		this.removalsRepo = removalsRepo || new EventInstanceRepo();
+		this.additionsRepo = additionsRepo || new EventInstanceRepo();
+	},
+
+
+	applyToRepo: function(repo) {
+		var removalsHash = this.removalsRepo.byDefId;
+		var additionsHash = this.additionsRepo.byDefId;
+		var id, instances;
 		var i;
 
-		for (i = 0; i < eventInstances.length; i++) {
-			if (bucket && removeExact(bucket, eventInstances[i])) {
+		if (this.isClear) {
+			repo.clear();
+		}
 
-				if (!bucket.length) {
-					delete this.byDefId[id];
-				}
+		for (id in removalsHash) {
+			instances = removalsHash[id];
 
-				this.instanceCnt--;
+			for (i = 0; i < instances.length; i++) {
+				repo._removeEventInstance(id, instances[i]);
 			}
-			else {
+		}
 
-				(this.removalsByDefId[id] || (this.removalsByDefId[id] = []))
-					.push(eventInstances[i]);
+		for (id in additionsHash) {
+			instances = additionsHash[id];
 
-				this.removalCnt++;
+			for (i = 0; i < instances.length; i++) {
+				repo._addEventInstance(id, instances[i]);
 			}
 		}
 	},
 
 
-	// returns true/false if resulted in changes
-	addChangeset: function(changeset) {
-		var theirRemoveHash = changeset.removalsByDefId;
-		var theirAddHash = changeset.byDefId;
-		var anyChanges = false;
-		var id;
+	applyToChangeset: function(changeset) {
+		var removalsHash = this.removalsRepo.byDefId;
+		var additionsHash = this.additionsRepo.byDefId;
+		var id, instances;
+		var i;
 
-		if (changeset.isClear) {
-			this.addClear();
-			anyChanges = true;
+		if (this.isClear) {
+			changeset.isClear = true;
+			changeset.removalsRepo.clear();
+			changeset.additionsRepo.clear();
 		}
 
-		for (id in theirRemoveHash) {
-			this._removeEventInstances(id, theirRemoveHash[id]);
-			anyChanges = true;
-		}
+		for (id in removalsHash) {
+			instances = removalsHash[id];
 
-		for (id in theirAddHash) {
-			this._addEventInstances(id, theirAddHash[id]);
-			anyChanges = true;
+			for (i = 0; i < instances.length; i++) {
+				if (!changeset.additionsRepo._removeEventInstance(id, instances[i])) {
+					changeset.removalsRepo._addEventInstance(id, instances[i]);
+				}
+			}
 		}
 
-		return anyChanges;
-	},
-
+		for (id in additionsHash) {
+			instances = additionsHash[id];
 
-	addClear: function() {
-		this.isClear = true;
-		this.removalsByDefId = {};
-		this.byDefId = {};
-		this.removalCnt = 0;
-		this.instanceCnt = 0;
+			for (i = 0; i < instances.length; i++) {
+				changeset.additionsRepo._addEventInstance(id, instances[i]);
+			}
+		}
 	}
 
 });

+ 1 - 1
src/models/EventManager.js

@@ -35,7 +35,7 @@ var EventManager = Class.extend(EmitterMixin, ListenerMixin, {
 	},
 
 
-	getFinalizedEvents: function() {
+	getEventInstanceRepo: function() {
 		if (this.isFinalized()) {
 			return this.currentPeriod.instanceRepo;
 		}

+ 23 - 26
src/models/EventPeriod.js

@@ -31,7 +31,7 @@ var EventPeriod = Class.extend(EmitterMixin, {
 		this.requestsByUid = {};
 		this.eventDefsByUid = {};
 		this.eventDefsById = {};
-		this.instanceRepo = new EventInstanceChangeset();
+		this.instanceRepo = new EventInstanceRepo();
 	},
 
 
@@ -130,15 +130,11 @@ var EventPeriod = Class.extend(EmitterMixin, {
 
 	tryReset: function() {
 		if (this.isFinalized()) {
-			this.freeze();
-
-			// ugly
-			var changeset = new EventInstanceChangeset(true); // isClear=true
-			changeset.byDefId = this.instanceRepo.byDefId;
-			changeset.instanceCnt = this.instanceRepo.instanceCnt;
-
-			this.addChangeset(changeset);
-			this.thaw();
+			this.trigger('receive', new EventInstanceChangeset(
+				true, // isClear
+				null, // removals
+				this.instanceRepo // additions
+			));
 		}
 	},
 
@@ -181,7 +177,11 @@ var EventPeriod = Class.extend(EmitterMixin, {
 		this.eventDefsByUid[eventDef.uid] = eventDef;
 
 		this.addChangeset(
-			new EventInstanceChangeset(false, null, eventInstances)
+			new EventInstanceChangeset(
+				false, // isClear
+				null, // removals
+				new EventInstanceRepo(eventInstances) // additions
+			)
 		);
 	},
 
@@ -198,7 +198,10 @@ var EventPeriod = Class.extend(EmitterMixin, {
 	removeAllEventDefs: function() {
 		this.eventDefsByUid = {};
 		this.eventDefsById = {};
-		this.addClear();
+
+		this.addChangeset(
+			new EventInstanceChangeset(true) // isClear=true
+		);
 	},
 
 
@@ -218,7 +221,9 @@ var EventPeriod = Class.extend(EmitterMixin, {
 			this.addChangeset(
 				new EventInstanceChangeset(
 					false, // isClear
-					this.instanceRepo.getEventInstancesForDef(eventDef) // removals
+					new EventInstanceRepo( // removals
+						this.instanceRepo.getEventInstancesForDef(eventDef)
+					)
 				)
 			);
 		}
@@ -230,25 +235,17 @@ var EventPeriod = Class.extend(EmitterMixin, {
 
 
 	addChangeset: function(changeset) {
-		this.instanceRepo.addChangeset(changeset); // internally record immediately
-		this.ensureOutboundChangeset().addChangeset(changeset);
-		this.trySendOutbound();
-	},
+		if (!this.outboundChangeset) {
+			this.outboundChangeset = new EventInstanceChangeset();
+		}
 
+		changeset.applyToRepo(this.instanceRepo); // internally record immediately
+		changeset.applyToChangeset(this.outboundChangeset);
 
-	addClear: function() {
-		this.instanceRepo.addClear(); // internally record immediately
-		this.ensureOutboundChangeset().addClear();
 		this.trySendOutbound();
 	},
 
 
-	ensureOutboundChangeset: function() {
-		return this.outboundChangeset ||
-			(this.outboundChangeset = new EventInstanceChangeset());
-	},
-
-
 	freeze: function() {
 		this.freezeDepth++;
 	},