delta-store
Version:
An API for a store with change records
172 lines • 8.14 kB
JavaScript
/**
* Created by Papa on 1/7/2016.
*/
"use strict";
var Subject_1 = require("rxjs/Subject");
var DocumentHandle_1 = require("./DocumentHandle");
var Operation;
(function (Operation) {
Operation[Operation["CHANGES_ADDED_BY_OTHERS"] = 0] = "CHANGES_ADDED_BY_OTHERS";
Operation[Operation["CLEAUP_BY_OWNER"] = 1] = "CLEAUP_BY_OWNER";
Operation[Operation["GET_NEXT_CHANGE"] = 2] = "GET_NEXT_CHANGE";
})(Operation = exports.Operation || (exports.Operation = {}));
var GoogleRealtimeAdaptorException = (function () {
function GoogleRealtimeAdaptorException(message, operation, event, exception) {
this.message = message;
this.operation = operation;
this.event = event;
this.exception = exception;
}
return GoogleRealtimeAdaptorException;
}());
exports.GoogleRealtimeAdaptorException = GoogleRealtimeAdaptorException;
var GoogleChangeRecordIterator = (function () {
function GoogleChangeRecordIterator(changeList, event, nextIndex) {
if (nextIndex === void 0) { nextIndex = 0; }
this.changeList = changeList;
this.event = event;
this.nextIndex = nextIndex;
}
GoogleChangeRecordIterator.prototype.next = function () {
if (this.hasNext()) {
var nextValue = this.nextValue;
this.nextValue = null;
this.nextIndex++;
return nextValue;
}
var message = 'Error accessing the changelist at index: ' + this.nextIndex;
throw generateException(message, Operation.GET_NEXT_CHANGE, this.event);
};
GoogleChangeRecordIterator.prototype.hasNext = function () {
if (this.nextValue) {
return true;
}
try {
this.nextValue = this.changeList.get(this.nextIndex);
return !!this.nextValue;
}
catch (exception) {
return false;
}
};
return GoogleChangeRecordIterator;
}());
exports.GoogleChangeRecordIterator = GoogleChangeRecordIterator;
var generateException = function (message, operation, event, exception) {
message = message + '. User ID: ' + event.userId + ', Session ID: ' + event.sessionId;
return new GoogleRealtimeAdaptorException(message, Operation.CLEAUP_BY_OWNER, event, exception);
};
var GoogleRealtimeAdaptor = (function () {
function GoogleRealtimeAdaptor(googleRealtime) {
this.googleRealtime = googleRealtime;
}
GoogleRealtimeAdaptor.prototype.startTest = function () {
var document = this.googleRealtime.createInMemoryDocument();
var eventHub = this.createDocumentHandle(document);
return eventHub;
};
GoogleRealtimeAdaptor.prototype.startNewShare = function (fileId) {
var _this = this;
return this.googleRealtime.initializeFile(fileId).then(function (document) {
var eventHub = _this.createDocumentHandle(document);
return eventHub;
});
};
GoogleRealtimeAdaptor.prototype.createDocumentHandle = function (document) {
var changeList = this.googleRealtime.getChangeList(document);
var valuesAddedSubject = this.subscribeToChangesAddedByOthers(document);
var valuesArchivedSubject = this.subscribeToCleanupByOwner(document, false);
var otherChangesSubject = this.subscribeToUnexpectedModifications(changeList, document);
return new DocumentHandle_1.DocumentHandle(document, changeList, valuesAddedSubject, valuesArchivedSubject, otherChangesSubject);
};
GoogleRealtimeAdaptor.prototype.subscribeToChangesAddedByOthers = function (document) {
var valuesAddedSubject = new Subject_1.Subject();
var changeList = this.googleRealtime.getChangeList(document);
this.googleRealtime.subscribeToValuesAdded(changeList, valuesAddedSubject);
var changesAddedSubject = new Subject_1.Subject();
valuesAddedSubject.subscribe(function (event) {
console.log('Changes by others. BaseModelEvent Type: ' + event.type);
if (event.isLocal) {
return;
}
if (event.target !== changeList) {
throw generateException('List cleaned up on an unexpected target', Operation.CHANGES_ADDED_BY_OTHERS, event);
}
if (event.movedFromIndex === 0 || event.movedFromIndex || event.movedFromList) {
throw generateException('List cleaned up is a move operation', Operation.CHANGES_ADDED_BY_OTHERS, event);
}
event.stopPropagation();
var iterator = new GoogleChangeRecordIterator(changeList, event, event.index);
changesAddedSubject.next(iterator);
});
return changesAddedSubject;
};
GoogleRealtimeAdaptor.prototype.subscribeToCleanupByOwner = function (document, iAmTheOwner) {
var valuesRemovedSubject = new Subject_1.Subject();
var changeList = this.googleRealtime.getChangeList(document);
this.googleRealtime.subscribeToValuesRemoved(changeList, valuesRemovedSubject);
var changesRemovedSubject = new Subject_1.Subject();
valuesRemovedSubject.subscribe(function (event) {
console.log('Clean-up by owner. BaseModelEvent Type: ' + event.type);
if (event.isLocal) {
if (iAmTheOwner) {
return;
}
throw generateException('List cleaned up by non-owner', Operation.CLEAUP_BY_OWNER, event);
}
if (event.target !== changeList) {
throw generateException('List cleaned up on an unexpected target', Operation.CLEAUP_BY_OWNER, event);
}
if (event.movedToIndex === 0 || event.movedToIndex || event.movedToList) {
throw generateException('List cleaned up is a move operation', Operation.CLEAUP_BY_OWNER, event);
}
if (event.index !== 0) {
throw generateException('List cleaned up from invalid index: ' + event.index, Operation.CLEAUP_BY_OWNER, event);
}
event.stopPropagation();
var iterator = new GoogleChangeRecordIterator(changeList, event);
changesRemovedSubject.next(iterator);
});
return changesRemovedSubject;
};
GoogleRealtimeAdaptor.prototype.subscribeToUnexpectedModifications = function (changeList, document) {
var valuesRemovedSubject = new Subject_1.Subject();
this.googleRealtime.subscribeToAnyObjectChanged(document, valuesRemovedSubject);
var changesRemovedSubject = new Subject_1.Subject();
valuesRemovedSubject.subscribe(function (event) {
var message = 'Unexpected change - ';
if (!event.events) {
message += ' target events not found';
}
else if (event.events.length !== 1) {
message += ' unexpected number of target events: ' + event.events.length;
}
else {
var causingEvent = event.events[0];
if (causingEvent.target !== changeList) {
message += ' unexpected target';
}
switch (causingEvent.type) {
case gapi.drive.realtime.EventType.VALUES_ADDED:
case gapi.drive.realtime.EventType.VALUES_REMOVED:
return;
default:
message += 'unexpected event type: ' + causingEvent.type;
}
}
var exception = new GoogleRealtimeAdaptorException(message, null, event);
changesRemovedSubject.next(exception);
});
return changesRemovedSubject;
};
GoogleRealtimeAdaptor.prototype.openShare = function (fileId) {
var _this = this;
return this.googleRealtime.loadFile(fileId).then(function (document) {
var documentHandle = _this.createDocumentHandle(document);
return documentHandle;
});
};
return GoogleRealtimeAdaptor;
}());
exports.GoogleRealtimeAdaptor = GoogleRealtimeAdaptor;
//# sourceMappingURL=GoogleRealtimeAdaptor.js.map