2015-03-20 13:06:49 -07:00
|
|
|
/*global define*/
|
|
|
|
|
|
|
|
define(
|
|
|
|
[],
|
|
|
|
function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The PersistenceQueue is used by the QueuingPersistenceCapability
|
|
|
|
* to aggregrate calls for object persistence. These are then issued
|
|
|
|
* in a group, such that if some or all are rejected, this result can
|
|
|
|
* be shown to the user (again, in a group.)
|
|
|
|
*
|
|
|
|
* This implementation is separate out from PersistenceQueue, which
|
|
|
|
* handles the wiring of injected dependencies into an instance of
|
|
|
|
* this class.
|
|
|
|
*
|
|
|
|
* @param $timeout Angular's $timeout
|
|
|
|
* @param {PersistenceQueueHandler} handler handles actual
|
|
|
|
* persistence when the queue is flushed
|
|
|
|
* @param {number} [DELAY] optional; delay in milliseconds between
|
|
|
|
* attempts to flush the queue
|
|
|
|
*/
|
2015-03-20 14:06:11 -07:00
|
|
|
function PersistenceQueueImpl($q, $timeout, handler, DELAY) {
|
2015-03-24 14:09:51 -07:00
|
|
|
var self,
|
|
|
|
persistences = {},
|
2015-03-20 13:06:49 -07:00
|
|
|
objects = {},
|
|
|
|
lastObservedSize = 0,
|
|
|
|
pendingTimeout,
|
2015-03-20 14:06:11 -07:00
|
|
|
flushPromise,
|
|
|
|
activeDefer = $q.defer();
|
2015-03-20 13:06:49 -07:00
|
|
|
|
|
|
|
// Check if the queue's size has stopped increasing)
|
|
|
|
function quiescent() {
|
2015-03-24 14:09:51 -07:00
|
|
|
return Object.keys(persistences).length === lastObservedSize;
|
2015-03-20 13:06:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Persist all queued objects
|
|
|
|
function flush() {
|
2015-03-24 12:19:36 -07:00
|
|
|
// Get a local reference to the active promise;
|
|
|
|
// this will be replaced with a promise for the next round
|
|
|
|
// of persistence calls, so we want to make sure we clear
|
|
|
|
// the correct one when this flush completes.
|
|
|
|
var flushingDefer = activeDefer;
|
|
|
|
|
|
|
|
// Clear the active promise for a queue flush
|
|
|
|
function clearFlushPromise(value) {
|
|
|
|
flushPromise = undefined;
|
|
|
|
flushingDefer.resolve(value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2015-03-20 13:06:49 -07:00
|
|
|
// Persist all queued objects
|
2015-03-24 14:09:51 -07:00
|
|
|
flushPromise = handler.persist(persistences, objects, self)
|
2015-03-20 15:53:40 -07:00
|
|
|
.then(clearFlushPromise, clearFlushPromise);
|
2015-03-20 13:06:49 -07:00
|
|
|
|
|
|
|
// Reset queue, etc.
|
2015-03-24 14:09:51 -07:00
|
|
|
persistences = {};
|
2015-03-20 13:06:49 -07:00
|
|
|
objects = {};
|
|
|
|
lastObservedSize = 0;
|
|
|
|
pendingTimeout = undefined;
|
2015-03-20 14:06:11 -07:00
|
|
|
activeDefer = $q.defer();
|
2015-03-20 13:06:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule a flushing of the queue (that is, plan to flush
|
|
|
|
// all objects in the queue)
|
|
|
|
function scheduleFlush() {
|
|
|
|
function maybeFlush() {
|
2015-03-20 14:06:11 -07:00
|
|
|
// Timeout fired, so clear it
|
|
|
|
pendingTimeout = undefined;
|
2015-03-20 13:06:49 -07:00
|
|
|
// Only flush when we've stopped receiving updates
|
|
|
|
(quiescent() ? flush : scheduleFlush)();
|
2015-03-20 14:06:11 -07:00
|
|
|
// Update lastObservedSize to detect quiescence
|
2015-03-24 14:09:51 -07:00
|
|
|
lastObservedSize = Object.keys(persistences).length;
|
2015-03-20 13:06:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we are already flushing the queue...
|
|
|
|
if (flushPromise) {
|
|
|
|
// Wait until that's over before considering a flush
|
|
|
|
flushPromise.then(maybeFlush);
|
|
|
|
} else {
|
|
|
|
// Otherwise, schedule a flush on a timeout (to give
|
|
|
|
// a window for other updates to get aggregated)
|
|
|
|
pendingTimeout = pendingTimeout ||
|
|
|
|
$timeout(maybeFlush, DELAY, false);
|
|
|
|
}
|
2015-03-20 14:06:11 -07:00
|
|
|
|
|
|
|
return activeDefer.promise;
|
2015-03-20 13:06:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If no delay is provided, use a default
|
|
|
|
DELAY = DELAY || 0;
|
|
|
|
|
2015-03-24 14:09:51 -07:00
|
|
|
self = {
|
2015-03-20 13:06:49 -07:00
|
|
|
/**
|
|
|
|
* Queue persistence of a domain object.
|
|
|
|
* @param {DomainObject} domainObject the domain object
|
|
|
|
* @param {PersistenceCapability} persistence the object's
|
|
|
|
* undecorated persistence capability
|
|
|
|
*/
|
|
|
|
put: function (domainObject, persistence) {
|
|
|
|
var id = domainObject.getId();
|
2015-03-24 14:09:51 -07:00
|
|
|
persistences[id] = persistence;
|
2015-03-20 13:06:49 -07:00
|
|
|
objects[id] = domainObject;
|
2015-03-20 14:06:11 -07:00
|
|
|
return scheduleFlush();
|
2015-03-20 13:06:49 -07:00
|
|
|
}
|
|
|
|
};
|
2015-03-24 14:09:51 -07:00
|
|
|
|
|
|
|
return self;
|
2015-03-20 13:06:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return PersistenceQueueImpl;
|
|
|
|
}
|
|
|
|
);
|