[Persistence] Begin integrating persistence queue

Begin integrating persistence queue, tweaking for issues
detected through minimal use. WTD-1033.
This commit is contained in:
Victor Woeltjen 2015-03-20 14:06:11 -07:00
parent 0362d3479c
commit acf058849f
6 changed files with 23 additions and 12 deletions

View File

@ -13,6 +13,7 @@
"platform/features/scrolling",
"platform/forms",
"platform/persistence/cache",
"platform/persistence/queue",
"platform/persistence/elastic",
"example/generator"

View File

@ -38,8 +38,8 @@ define(
data: value
}).then(function (response) {
return response.data;
}, function () {
return undefined;
}, function (response) {
return (response || {}).data;
});
}
@ -91,7 +91,7 @@ define(
function checkResponse(response, key) {
var error;
if (response && !response.error) {
revs[ID] = response[REV];
revs[key] = response[REV];
return response;
} else {
return handleError(response, key);

View File

@ -39,6 +39,7 @@ define(
) {
// Wire up injected dependencies
return new PersistenceQueueImpl(
$q,
$timeout,
new PersistenceQueueHandler(
$q,

View File

@ -21,12 +21,13 @@ define(
* @param {number} [DELAY] optional; delay in milliseconds between
* attempts to flush the queue
*/
function PersistenceQueueImpl($timeout, handler, DELAY) {
function PersistenceQueueImpl($q, $timeout, handler, DELAY) {
var queue = {},
objects = {},
lastObservedSize = 0,
pendingTimeout,
flushPromise;
flushPromise,
activeDefer = $q.defer();
// Check if the queue's size has stopped increasing)
function quiescent() {
@ -39,8 +40,9 @@ define(
flushPromise = handler.persist(queue, objects);
// When persisted, clear the active promise
flushPromise.then(function () {
flushPromise.then(function (value) {
flushPromise = undefined;
activeDefer.resolve(value);
});
// Reset queue, etc.
@ -48,14 +50,19 @@ define(
objects = {};
lastObservedSize = 0;
pendingTimeout = undefined;
activeDefer = $q.defer();
}
// Schedule a flushing of the queue (that is, plan to flush
// all objects in the queue)
function scheduleFlush() {
function maybeFlush() {
// Timeout fired, so clear it
pendingTimeout = undefined;
// Only flush when we've stopped receiving updates
(quiescent() ? flush : scheduleFlush)();
// Update lastObservedSize to detect quiescence
lastObservedSize = Object.keys(queue).length;
}
// If we are already flushing the queue...
@ -68,6 +75,8 @@ define(
pendingTimeout = pendingTimeout ||
$timeout(maybeFlush, DELAY, false);
}
return activeDefer.promise;
}
// If no delay is provided, use a default
@ -84,7 +93,7 @@ define(
var id = domainObject.getId();
queue[id] = persistence;
objects[id] = domainObject;
scheduleFlush();
return scheduleFlush();
}
};
}

View File

@ -15,12 +15,11 @@ define(
* the capability
*/
function QueuingPersistenceCapability(queue, persistence, domainObject) {
var queuingPersistence = Object.create(persistence),
id = domainObject.getId();
var queuingPersistence = Object.create(persistence);
// Override persist calls to queue them instead
queuingPersistence.persist = function () {
queue.put(id, persistence);
return queue.put(domainObject, persistence);
};
return queuingPersistence;

View File

@ -43,8 +43,9 @@ define(
}
function getCapabilities(model) {
return capabilityService.getCapabilities(model)
.then(decoratePersistence);
return decoratePersistence(
capabilityService.getCapabilities(model)
);
}
return {