mirror of
https://github.com/nasa/openmct.git
synced 2025-01-01 19:06:40 +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 $q Angular's $q
|
||||||
* @param $timeout Angular's $timeout
|
* @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
|
* @param {number} [DELAY] optional; delay in milliseconds between
|
||||||
* attempts to flush the queue
|
* attempts to flush the queue
|
||||||
*/
|
*/
|
||||||
function PersistenceQueue($q, $timeout, DELAY) {
|
function PersistenceQueue($q, $timeout, dialogService, DELAY) {
|
||||||
var queue = {},
|
var queue = {},
|
||||||
|
objects = {},
|
||||||
lastObservedSize = 0,
|
lastObservedSize = 0,
|
||||||
handler = new QueuedPersistenceHandler($q),
|
handler = new QueuedPersistenceHandler($q, dialogService),
|
||||||
pendingTimeout,
|
pendingTimeout,
|
||||||
flushPromise;
|
flushPromise;
|
||||||
|
|
||||||
@ -28,18 +31,10 @@ define(
|
|||||||
return Object.keys(queue).length === lastObservedSize;
|
return Object.keys(queue).length === lastObservedSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Look up a queued persistence capability
|
|
||||||
function lookup(id) {
|
|
||||||
return queue[id];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Persist all queued objects
|
// Persist all queued objects
|
||||||
function flush() {
|
function flush() {
|
||||||
// Convert to array of persistence capabilities
|
// Persist all queued objects
|
||||||
var toFlush = Object.keys(queue).map(lookup);
|
flushPromise = handler.persist(queue, objects);
|
||||||
|
|
||||||
// Persist them
|
|
||||||
flushPromise = handler.persist(toFlush);
|
|
||||||
|
|
||||||
// When persisted, clear the active promise
|
// When persisted, clear the active promise
|
||||||
flushPromise.then(function () {
|
flushPromise.then(function () {
|
||||||
@ -48,6 +43,7 @@ define(
|
|||||||
|
|
||||||
// Reset queue, etc.
|
// Reset queue, etc.
|
||||||
queue = {};
|
queue = {};
|
||||||
|
objects = {};
|
||||||
lastObservedSize = 0;
|
lastObservedSize = 0;
|
||||||
pendingTimeout = undefined;
|
pendingTimeout = undefined;
|
||||||
}
|
}
|
||||||
@ -72,19 +68,21 @@ define(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// If no delay is provided, use a default
|
// If no delay is provided, use a default
|
||||||
DELAY = DELAY || 0;
|
DELAY = DELAY || 0;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
/**
|
/**
|
||||||
* Queue persistence of a domain object.
|
* 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
|
* @param {PersistenceCapability} persistence the object's
|
||||||
* undecorated persistence capability
|
* undecorated persistence capability
|
||||||
*/
|
*/
|
||||||
put: function (id, persistence) {
|
put: function (domainObject, persistence) {
|
||||||
|
var id = domainObject.getId();
|
||||||
queue[id] = persistence;
|
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