mirror of
https://github.com/nasa/openmct.git
synced 2024-12-29 17:38:53 +00:00
[Persistence] Intermediary commit
Intermediary commit; continue implementing persistence queue for WTD-1033.
This commit is contained in:
parent
75cd78d3ca
commit
8ec65db93d
@ -0,0 +1,18 @@
|
||||
/*global define*/
|
||||
|
||||
define(
|
||||
[],
|
||||
function () {
|
||||
"use strict";
|
||||
|
||||
function PersistenceFailureHandler(dialogService) {
|
||||
return {
|
||||
handle: function (failures) {
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return PersistenceFailureHandler;
|
||||
}
|
||||
);
|
@ -13,13 +13,16 @@ define(
|
||||
*
|
||||
* @param $q Angular's $q
|
||||
* @param $timeout Angular's $timeout
|
||||
* @param {DialogService} dialogService services to prompt for user
|
||||
* input if persistence fails
|
||||
* @param {number} [DELAY] optional; delay in milliseconds between
|
||||
* attempts to flush the queue
|
||||
*/
|
||||
function PersistenceQueue($q, $timeout, DELAY) {
|
||||
function PersistenceQueue($q, $timeout, dialogService, DELAY) {
|
||||
var queue = {},
|
||||
objects = {},
|
||||
lastObservedSize = 0,
|
||||
handler = new QueuedPersistenceHandler($q),
|
||||
handler = new QueuedPersistenceHandler($q, dialogService),
|
||||
pendingTimeout,
|
||||
flushPromise;
|
||||
|
||||
@ -28,18 +31,10 @@ define(
|
||||
return Object.keys(queue).length === lastObservedSize;
|
||||
}
|
||||
|
||||
// Look up a queued persistence capability
|
||||
function lookup(id) {
|
||||
return queue[id];
|
||||
}
|
||||
|
||||
// Persist all queued objects
|
||||
function flush() {
|
||||
// Convert to array of persistence capabilities
|
||||
var toFlush = Object.keys(queue).map(lookup);
|
||||
|
||||
// Persist them
|
||||
flushPromise = handler.persist(toFlush);
|
||||
// Persist all queued objects
|
||||
flushPromise = handler.persist(queue, objects);
|
||||
|
||||
// When persisted, clear the active promise
|
||||
flushPromise.then(function () {
|
||||
@ -48,6 +43,7 @@ define(
|
||||
|
||||
// Reset queue, etc.
|
||||
queue = {};
|
||||
objects = {};
|
||||
lastObservedSize = 0;
|
||||
pendingTimeout = undefined;
|
||||
}
|
||||
@ -72,19 +68,21 @@ define(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// If no delay is provided, use a default
|
||||
DELAY = DELAY || 0;
|
||||
|
||||
return {
|
||||
/**
|
||||
* Queue persistence of a domain object.
|
||||
* @param {string} id the domain object's identifier
|
||||
* @param {DomainObject} domainObject the domain object
|
||||
* @param {PersistenceCapability} persistence the object's
|
||||
* undecorated persistence capability
|
||||
*/
|
||||
put: function (id, persistence) {
|
||||
put: function (domainObject, persistence) {
|
||||
var id = domainObject.getId();
|
||||
queue[id] = persistence;
|
||||
objects[id] = domainObject;
|
||||
scheduleFlush();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -0,0 +1,59 @@
|
||||
/*global define*/
|
||||
|
||||
define(
|
||||
[],
|
||||
function () {
|
||||
"use strict";
|
||||
|
||||
function QueuedPersistenceHandler($q, failureHandler) {
|
||||
|
||||
function persistGroup(ids, queue, domainObjects) {
|
||||
var failures = [];
|
||||
|
||||
function tryPersist(id) {
|
||||
var persistence = queue[id];
|
||||
|
||||
function succeed(value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
function fail(error) {
|
||||
failures.push({
|
||||
id: id,
|
||||
domainObject: domainObjects[id],
|
||||
error: error
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
return persistence.persist().then(succeed, fail);
|
||||
}
|
||||
|
||||
function handleFailure(value) {
|
||||
return failures.length > 0 ?
|
||||
failureHandler.handle(failures) : value;
|
||||
}
|
||||
|
||||
return $q.all(ids.map(tryPersist)).then(handleFailure);
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
/**
|
||||
* Invoke the persist method on the provided persistence
|
||||
* capabilities.
|
||||
* @param {Object.<string,PersistenceCapability>} queue
|
||||
* capabilities to invoke, in id->capability pairs.
|
||||
* @param {Object.<string,DomainObject>} domainObjects
|
||||
* associated domain objects, in id->object pairs.
|
||||
*/
|
||||
persist: function (queue, domainObjects) {
|
||||
var ids = Object.keys(queue);
|
||||
return persistGroup(ids, queue, domainObjects);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return QueuedPersistenceHandler;
|
||||
}
|
||||
);
|
Loading…
Reference in New Issue
Block a user